diff --git a/src/kernel/peripherals/Inventory.hx b/src/kernel/peripherals/Inventory.hx new file mode 100644 index 0000000..d2391f5 --- /dev/null +++ b/src/kernel/peripherals/Inventory.hx @@ -0,0 +1,124 @@ +package kernel.peripherals; + +import lib.Item; +import cc.periphs.ItemStorage.ReducedItemInfo; +import lua.Table; + +using Lambda; + +class ItemInfo { + public final count:Int; + public final name:Item; + public final nbt:Null; + + @:allow(kernel.peripherals.Inventory) + private function new(from:ReducedItemInfo) { + this.count = from.count; + this.name = from.name; + this.nbt = from.nbt; + } +} + +class DetailedItemInfo extends ItemInfo { + public final displayName:String; + public final maxCount:Int; + public final tags:Array; + public final durability:Null; + public final damage:Null; + public final maxDamage:Null; + public final enchantments:Array<{displayName:String, level:Int, name:String}>; + + @:allow(kernel.peripherals.Inventory) + private function new(from:cc.periphs.ItemStorage.DetailedItemInfo) { + super(from); + + this.tags = []; + for (k => _ in Table.toMap(from.tags)) { + this.tags.push(k); + } + + this.displayName = from.displayName; + this.maxCount = from.maxCount; + this.durability = from.durability; + this.damage = from.damage; + this.maxDamage = from.maxDamage; + + if (from.enchantments != null) { + this.enchantments = from.enchantments; + } else { + this.enchantments = []; + } + } +} + +class Inventory implements IPeripheral { + public static inline final TYPE_NAME:String = "inventory"; + + private final addr:String; + private final native:cc.periphs.ItemStorage; + + public function new(addr:String) { + this.addr = addr; + this.native = cc.Peripheral.wrap(addr); + } + + public function getAddr():String { + return this.addr; + } + + public function getType():String { + return TYPE_NAME; + } + + /** + Get the size of this inventory. + **/ + public function size():Int { + return this.native.size(); + } + + /** + List all items in this inventory. + **/ + public function list():Map { + var list = Table.toArray(this.native.list()); + var rtn:Map = new Map(); + + for (k => v in list) { + rtn.set(k, new ItemInfo(v)); + } + + return rtn; + } + + /** + Get detailed information about an item. + **/ + public function getItemDetail(slot:Int):DetailedItemInfo { + return new DetailedItemInfo(this.native.getItemDetail(slot)); + } + + /** + Get the maximum number of items which can be stored in this slot. + Typically this will be limited to 64 items. + However, some inventorwies (such as barrels or caches) can store hundreds or thousands of items in one slot. + Keep in mind that this does not depend on that item stored in this slot. + **/ + public function getItemLimit(slot:Int):Int { + return this.native.getItemLimit(slot); + } + + /** + Push items from one inventory to another connected one. + **/ + public function pushItems(toSlot:Int, to:String, ?limit:Int, ?toSlot:Int):Int { + return this.native.pushItems(to, toSlot, limit, toSlot); + } + + /** + Pull items from a connected inventory into this one. + **/ + public function pullItems(from:String, fromSlot:Int, ?limit:Int, ?toSlot:Int):Int { + return this.native.pullItems(from, fromSlot, limit, toSlot); + } +} diff --git a/src/kernel/peripherals/Peripherals.hx b/src/kernel/peripherals/Peripherals.hx index 4afd8ba..65d7643 100644 --- a/src/kernel/peripherals/Peripherals.hx +++ b/src/kernel/peripherals/Peripherals.hx @@ -81,6 +81,8 @@ class Peripheral { return getSpeaker(addr); case Redstone.TYPE_NAME: return getRedstone(addr); + case Inventory.TYPE_NAME: + return getInventory(addr); } return null; @@ -167,4 +169,15 @@ class Peripheral { public static function getAllSpeakers():Array { return [for (addr in findAddrByType(Speaker.TYPE_NAME)) new Speaker(addr)]; } + + public static function getInventory(addr:String):Null { + var addr = safeGetAddr(addr, Inventory.TYPE_NAME); + if (addr == null) + return null; + return new Inventory(addr); + } + + public static function getAllInventorys():Array { + return [for (addr in findAddrByType(Inventory.TYPE_NAME)) new Inventory(addr)]; + } }