Only recalculate and sync net items when a player has the terminal open

This commit is contained in:
Shadowfacts 2019-10-29 17:45:58 -04:00
parent 7bc859eaf6
commit de81412630
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 25 additions and 7 deletions

View File

@ -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) {
if (observers > 0 && (++counter % 20) == 0) {
counter = 0
if (world!!.isClient) {
println(cachedNetItems)
} else {
updateNetItems()
sync()
}
if (world!!.isClient && (++counter % 20) == 0) {
println(cachedNetItems)
}
}
@ -81,6 +92,7 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), Invento
buf.writeBlockPos(pos)
}
}
addObserver()
}
override fun onInvChange(inv: Inventory) {

View File

@ -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()
}
}