Only recalculate and sync net items when a player has the terminal open
This commit is contained in:
parent
7bc859eaf6
commit
de81412630
|
@ -27,9 +27,10 @@ import net.shadowfacts.phycon.util.toTag
|
|||
class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), InventoryListener, BlockEntityClientSerializable {
|
||||
|
||||
private val inventoryCache = mutableMapOf<MACAddress, GroupedItemInv>()
|
||||
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) {
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue