diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlockEntity.kt index 3263831..c5152c8 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/netinterface/InterfaceBlockEntity.kt @@ -3,13 +3,12 @@ package net.shadowfacts.phycon.network.block.netinterface import alexiil.mc.lib.attributes.SearchOptions import alexiil.mc.lib.attributes.item.GroupedItemInv import alexiil.mc.lib.attributes.item.ItemAttributes -import net.minecraft.item.ItemStack import net.minecraft.util.math.Direction import net.shadowfacts.phycon.api.packet.Packet import net.shadowfacts.phycon.init.PhyBlockEntities import net.shadowfacts.phycon.network.DeviceBlockEntity -import net.shadowfacts.phycon.network.packet.ReadAllPacket -import net.shadowfacts.phycon.network.packet.RequestReadAllPacket +import net.shadowfacts.phycon.network.packet.ReadInventoryPacket +import net.shadowfacts.phycon.network.packet.RequestInventoryPacket /** * @author shadowfacts @@ -30,22 +29,17 @@ class InterfaceBlockEntity: DeviceBlockEntity(PhyBlockEntities.INTERFACE) { override fun handlePacket(packet: Packet) { when (packet) { - is RequestReadAllPacket -> handleReadAll(packet) + is RequestInventoryPacket -> handleRequestInventory(packet) } } - fun handleReadAll(packet: RequestReadAllPacket) { - enqueueToSingle(ReadAllPacket(readAll(), macAddress, packet.source)) - } - - fun readAll(): Map { + private fun handleRequestInventory(packet: RequestInventoryPacket) { // if we don't have an inventory, try to get one // this happens when readAll is called before a neighbor state changes, such as immediately after world load if (inventory == null) updateInventory() - - return inventory?.let { - it.storedStacks.associateWith(it::getAmount) - } ?: mapOf() + inventory?.also { + enqueueToSingle(ReadInventoryPacket(it, macAddress, packet.source)) + } } } diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt index 8967ee2..03f3197 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt @@ -1,34 +1,32 @@ package net.shadowfacts.phycon.network.block.terminal +import alexiil.mc.lib.attributes.item.GroupedItemInv import alexiil.mc.lib.attributes.item.ItemStackCollections -import it.unimi.dsi.fastutil.objects.Object2IntMap import net.minecraft.item.ItemStack import net.shadowfacts.phycon.api.packet.Packet import net.shadowfacts.phycon.api.util.MACAddress import net.shadowfacts.phycon.init.PhyBlockEntities import net.shadowfacts.phycon.network.DeviceBlockEntity -import net.shadowfacts.phycon.network.block.netinterface.InterfaceBlockEntity import net.shadowfacts.phycon.network.packet.DeviceRemovedPacket -import net.shadowfacts.phycon.network.packet.ReadAllPacket -import net.shadowfacts.phycon.network.packet.RequestReadAllPacket -import java.util.* +import net.shadowfacts.phycon.network.packet.ReadInventoryPacket +import net.shadowfacts.phycon.network.packet.RequestInventoryPacket /** * @author shadowfacts */ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) { - private val inventoryCache = mutableMapOf>() + private val inventoryCache = mutableMapOf() override fun handlePacket(packet: Packet) { when (packet) { - is ReadAllPacket -> handleReadAll(packet) + is ReadInventoryPacket -> handleReadInventory(packet) is DeviceRemovedPacket -> handleDeviceRemoved(packet) } } - private fun handleReadAll(packet: ReadAllPacket) { - inventoryCache[packet.source] = packet.items + private fun handleReadInventory(packet: ReadInventoryPacket) { + inventoryCache[packet.source] = packet.inventory println("new items: ${computeNetItems()}") } @@ -39,8 +37,9 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) { fun computeNetItems(): Map { val net = ItemStackCollections.intMap() - for (map in inventoryCache.values) { - for ((stack, amount) in map) { + for (inventory in inventoryCache.values) { + for (stack in inventory.storedStacks) { + val amount = inventory.getAmount(stack) net.mergeInt(stack, amount) { a, b -> a + b } } } @@ -50,7 +49,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL) { fun onActivate() { if (!world!!.isClient) { inventoryCache.clear() - enqueueToSingle(RequestReadAllPacket(macAddress)) + enqueueToSingle(RequestInventoryPacket(macAddress)) } } diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/packet/ReadAllPacket.kt b/src/main/kotlin/net/shadowfacts/phycon/network/packet/ReadInventoryPacket.kt similarity index 65% rename from src/main/kotlin/net/shadowfacts/phycon/network/packet/ReadAllPacket.kt rename to src/main/kotlin/net/shadowfacts/phycon/network/packet/ReadInventoryPacket.kt index 9587f98..7423ab6 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/packet/ReadAllPacket.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/packet/ReadInventoryPacket.kt @@ -1,13 +1,13 @@ package net.shadowfacts.phycon.network.packet -import net.minecraft.item.ItemStack +import alexiil.mc.lib.attributes.item.GroupedItemInv import net.shadowfacts.phycon.api.util.MACAddress /** * @author shadowfacts */ -class ReadAllPacket( - val items: Map, +class ReadInventoryPacket( + val inventory: GroupedItemInv, source: MACAddress, destination: MACAddress ): BasePacket(source, destination) diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/packet/RequestInventoryPacket.kt b/src/main/kotlin/net/shadowfacts/phycon/network/packet/RequestInventoryPacket.kt new file mode 100644 index 0000000..654f74e --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/network/packet/RequestInventoryPacket.kt @@ -0,0 +1,8 @@ +package net.shadowfacts.phycon.network.packet + +import net.shadowfacts.phycon.api.util.MACAddress + +/** + * @author shadowfacts + */ +class RequestInventoryPacket(source: MACAddress, destination: MACAddress = MACAddress.BROADCAST): BasePacket(source, destination) diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/packet/RequestReadAllPacket.kt b/src/main/kotlin/net/shadowfacts/phycon/network/packet/RequestReadAllPacket.kt deleted file mode 100644 index a4ac63d..0000000 --- a/src/main/kotlin/net/shadowfacts/phycon/network/packet/RequestReadAllPacket.kt +++ /dev/null @@ -1,8 +0,0 @@ -package net.shadowfacts.phycon.network.packet - -import net.shadowfacts.phycon.api.util.MACAddress - -/** - * @author shadowfacts - */ -class RequestReadAllPacket(source: MACAddress, destination: MACAddress = MACAddress.BROADCAST): BasePacket(source, destination)