92 lines
2.4 KiB
Haxe
92 lines
2.4 KiB
Haxe
package kernel.peripherals;
|
|
|
|
import haxe.ds.ReadOnlyArray;
|
|
import kernel.peripherals.Modem;
|
|
import kernel.peripherals.Screen;
|
|
|
|
using lua.Table;
|
|
using Lambda;
|
|
using tink.CoreApi;
|
|
|
|
/**
|
|
Class responseable for retrieving peripherals.
|
|
**/
|
|
class Peripheral {
|
|
/**
|
|
Depends on: KernelEvents
|
|
**/
|
|
public static var instance:Peripheral;
|
|
|
|
private var screens: Array<Screen> = [];
|
|
private var modes: Array<Modem> = [];
|
|
private var drives:Array<Drive> = [];
|
|
private var redstone:Array<Redstone> = [];
|
|
|
|
@:allow(kernel.Init)
|
|
private function new() {
|
|
KernelEvents.instance.onPeripheral.handle(this.updatePeripherals);
|
|
KernelEvents.instance.onPeripheralDetach.handle(this.updatePeripherals);
|
|
updatePeripherals();
|
|
|
|
redstone = [ for(side in [Side.Top,Side.Right,Side.Left,Side.Front,Side.Bottom,Side.Back]) new Redstone(side) ];
|
|
}
|
|
|
|
private function updatePeripherals() {
|
|
findScreens();
|
|
findModems();
|
|
findDrives();
|
|
}
|
|
|
|
/**
|
|
Get all connected screens.
|
|
**/
|
|
public function getScreens():ReadOnlyArray<Screen> {
|
|
return this.screens;
|
|
}
|
|
|
|
private function findScreens():Void {
|
|
var allScreens = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "monitor");
|
|
this.screens = allScreens.map(s -> return new Screen((cc.Peripheral.wrap(s) : Dynamic), s));
|
|
}
|
|
|
|
public function getScreen(addr: String): Screen {
|
|
return this.screens.find(item -> item.getAddr() == addr);
|
|
}
|
|
|
|
/**
|
|
Get all connected modems.
|
|
**/
|
|
public function getModems():ReadOnlyArray<Modem> {
|
|
return this.modes;
|
|
}
|
|
|
|
public function getModem(addr: String): Null<Modem> {
|
|
return this.modes.find(item -> item.getAddr() == addr);
|
|
}
|
|
|
|
private function findModems():Void {
|
|
var allModems = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "modem");
|
|
this.modes = allModems.map(s -> return new Modem((cc.Peripheral.wrap(s) : Dynamic), s));
|
|
}
|
|
|
|
/**
|
|
Get all connected drives.
|
|
**/
|
|
public function getDrives(): ReadOnlyArray<Drive> {
|
|
return this.drives;
|
|
}
|
|
|
|
public function getDrive(addr: String): Null<Drive> {
|
|
return this.drives.find(item -> item.getAddr() == addr);
|
|
}
|
|
|
|
private function findDrives() {
|
|
var allDrives = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "drive");
|
|
this.drives = allDrives.map(s -> return new Drive((cc.Peripheral.wrap(s) : Dynamic), s));
|
|
}
|
|
|
|
public function getRedstone(side: String): Redstone {
|
|
return this.redstone.find(item -> item.getAddr() == side);
|
|
}
|
|
}
|