added inventory peripheral

This commit is contained in:
Niklas Kapelle 2024-08-12 15:16:32 +02:00
parent 5f5e899f6e
commit fe16799bbb
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
2 changed files with 137 additions and 0 deletions

View File

@ -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<String>;
@: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<String>;
public final durability:Null<Float>;
public final damage:Null<Int>;
public final maxDamage:Null<Int>;
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<Int, ItemInfo> {
var list = Table.toArray(this.native.list());
var rtn:Map<Int, ItemInfo> = 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);
}
}

View File

@ -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<Speaker> {
return [for (addr in findAddrByType(Speaker.TYPE_NAME)) new Speaker(addr)];
}
public static function getInventory(addr:String):Null<Inventory> {
var addr = safeGetAddr(addr, Inventory.TYPE_NAME);
if (addr == null)
return null;
return new Inventory(addr);
}
public static function getAllInventorys():Array<Inventory> {
return [for (addr in findAddrByType(Inventory.TYPE_NAME)) new Inventory(addr)];
}
}