From 7bc859eaf67ffd14109f6bbce8b6eeb76fa319d7 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 29 Oct 2019 17:33:33 -0400 Subject: [PATCH] Sync terminal cached item counts to client --- .../block/terminal/TerminalBlockEntity.kt | 49 ++++++++++++++++--- 1 file changed, 42 insertions(+), 7 deletions(-) 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 3307e8e..cff2980 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 @@ -2,6 +2,7 @@ package net.shadowfacts.phycon.network.block.terminal import alexiil.mc.lib.attributes.item.GroupedItemInv import alexiil.mc.lib.attributes.item.ItemStackCollections +import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable import net.fabricmc.fabric.api.container.ContainerProviderRegistry import net.minecraft.entity.player.PlayerEntity import net.minecraft.inventory.BasicInventory @@ -9,6 +10,7 @@ import net.minecraft.inventory.Inventory import net.minecraft.inventory.InventoryListener import net.minecraft.item.ItemStack import net.minecraft.nbt.CompoundTag +import net.minecraft.nbt.ListTag import net.shadowfacts.phycon.api.packet.Packet import net.shadowfacts.phycon.api.util.MACAddress import net.shadowfacts.phycon.init.PhyBlockEntities @@ -22,11 +24,14 @@ import net.shadowfacts.phycon.util.toTag /** * @author shadowfacts */ -class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), InventoryListener { +class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), InventoryListener, BlockEntityClientSerializable { private val inventoryCache = mutableMapOf() + val cachedNetItems = ItemStackCollections.intMap() val internalBuffer = BasicInventory(18) + var counter = 0 + init { internalBuffer.addListener(this) } @@ -40,23 +45,32 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento private fun handleReadInventory(packet: ReadInventoryPacket) { inventoryCache[packet.source] = packet.inventory - - println("new items: ${computeNetItems()}") } private fun handleDeviceRemoved(packet: DeviceRemovedPacket) { inventoryCache.remove(packet.source) } - fun computeNetItems(): Map { - val net = ItemStackCollections.intMap() + private fun updateNetItems() { + cachedNetItems.clear() for (inventory in inventoryCache.values) { for (stack in inventory.storedStacks) { val amount = inventory.getAmount(stack) - net.mergeInt(stack, amount) { a, b -> a + b } + cachedNetItems.mergeInt(stack, amount) { a, b -> a + b } } } - return net + } + + override fun tick() { + super.tick() + if (!world!!.isClient && (++counter % 20) == 0) { + updateNetItems() + sync() + } + + if (world!!.isClient && (++counter % 20) == 0) { + println(cachedNetItems) + } } fun onActivate(player: PlayerEntity) { @@ -85,4 +99,25 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento internalBuffer.fromTag(tag.getList("InternalBuffer", 10)) } + override fun toClientTag(tag: CompoundTag): CompoundTag { + val list = ListTag() + tag.put("CachedNetItems", list) + for ((stack, amount) in cachedNetItems) { + val entryTag = stack.toTag(CompoundTag()) + entryTag.putInt("NetAmount", amount) + list.add(entryTag) + } + return tag + } + + override fun fromClientTag(tag: CompoundTag) { + val list = tag.getList("CachedNetItems", 10) + cachedNetItems.clear() + for (entryTag in list) { + val stack = ItemStack.fromTag(entryTag as CompoundTag) + val netAmount = entryTag.getInt("NetAmount") + cachedNetItems[stack] = netAmount + } + } + }