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 = []; private var modes: Array = []; private var drives:Array = []; private var redstone:Array = []; @: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 { 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 { return this.modes; } public function getModem(addr: String): Null { 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 { return this.drives; } public function getDrive(addr: String): Null { 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); } }