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 cff2980..4cae69e 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 @@ -27,9 +27,10 @@ import net.shadowfacts.phycon.util.toTag class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), InventoryListener, BlockEntityClientSerializable { private val inventoryCache = mutableMapOf() - val cachedNetItems = ItemStackCollections.intMap() val internalBuffer = BasicInventory(18) + private var observers = 0 + val cachedNetItems = ItemStackCollections.intMap() var counter = 0 init { @@ -61,15 +62,25 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento } } + fun addObserver() { + observers++ + } + + fun removeObserver() { + observers-- + } + override fun tick() { super.tick() - if (!world!!.isClient && (++counter % 20) == 0) { - updateNetItems() - sync() - } - if (world!!.isClient && (++counter % 20) == 0) { - println(cachedNetItems) + if (observers > 0 && (++counter % 20) == 0) { + counter = 0 + if (world!!.isClient) { + println(cachedNetItems) + } else { + updateNetItems() + sync() + } } } @@ -81,6 +92,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento buf.writeBlockPos(pos) } } + addObserver() } override fun onInvChange(inv: Inventory) { diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalContainer.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalContainer.kt index 9483196..3fdd0c5 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalContainer.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalContainer.kt @@ -39,4 +39,10 @@ class TerminalContainer(syncId: Int, playerInv: PlayerInventory, val terminal: T override fun canUse(player: PlayerEntity): Boolean { return true } + + override fun close(player: PlayerEntity) { + super.close(player) + + terminal.removeObserver() + } }