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 {
|
class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), InventoryListener, BlockEntityClientSerializable {
|
||||||
|
|
||||||
private val inventoryCache = mutableMapOf<MACAddress, GroupedItemInv>()
|
private val inventoryCache = mutableMapOf<MACAddress, GroupedItemInv>()
|
||||||
val cachedNetItems = ItemStackCollections.intMap()
|
|
||||||
val internalBuffer = BasicInventory(18)
|
val internalBuffer = BasicInventory(18)
|
||||||
|
|
||||||
|
private var observers = 0
|
||||||
|
val cachedNetItems = ItemStackCollections.intMap()
|
||||||
var counter = 0
|
var counter = 0
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
@ -61,15 +62,25 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun addObserver() {
|
||||||
|
observers++
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeObserver() {
|
||||||
|
observers--
|
||||||
|
}
|
||||||
|
|
||||||
override fun tick() {
|
override fun tick() {
|
||||||
super.tick()
|
super.tick()
|
||||||
if (!world!!.isClient && (++counter % 20) == 0) {
|
|
||||||
|
if (observers > 0 && (++counter % 20) == 0) {
|
||||||
|
counter = 0
|
||||||
|
if (world!!.isClient) {
|
||||||
|
println(cachedNetItems)
|
||||||
|
} else {
|
||||||
updateNetItems()
|
updateNetItems()
|
||||||
sync()
|
sync()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (world!!.isClient && (++counter % 20) == 0) {
|
|
||||||
println(cachedNetItems)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,6 +92,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
|
||||||
buf.writeBlockPos(pos)
|
buf.writeBlockPos(pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
addObserver()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onInvChange(inv: Inventory) {
|
override fun onInvChange(inv: Inventory) {
|
||||||
|
|
|
@ -39,4 +39,10 @@ class TerminalContainer(syncId: Int, playerInv: PlayerInventory, val terminal: T
|
||||||
override fun canUse(player: PlayerEntity): Boolean {
|
override fun canUse(player: PlayerEntity): Boolean {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun close(player: PlayerEntity) {
|
||||||
|
super.close(player)
|
||||||
|
|
||||||
|
terminal.removeObserver()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue