Store inventories as weak references in interface

This commit is contained in:
Shadowfacts 2023-03-12 22:28:36 -04:00
parent 6d97af8bdc
commit c18af9794b
1 changed files with 7 additions and 5 deletions

View File

@ -19,6 +19,7 @@ import net.shadowfacts.phycon.component.NetworkStackReceiver
import net.shadowfacts.phycon.component.handleItemStack import net.shadowfacts.phycon.component.handleItemStack
import net.shadowfacts.phycon.packet.* import net.shadowfacts.phycon.packet.*
import net.shadowfacts.phycon.util.ClientConfigurableDevice import net.shadowfacts.phycon.util.ClientConfigurableDevice
import java.lang.ref.WeakReference
import kotlin.math.min import kotlin.math.min
/** /**
@ -37,20 +38,21 @@ class InterfaceBlockEntity(pos: BlockPos, state: BlockState): DeviceBlockEntity(
override var receiverPriority = 0 override var receiverPriority = 0
var syncPriorities = true var syncPriorities = true
// todo: should this be a weak ref? private var inventory: WeakReference<GroupedItemInv>? = null
private var inventory: GroupedItemInv? = null
fun updateInventory() { fun updateInventory() {
val offsetPos = pos.offset(facing) val offsetPos = pos.offset(facing)
val option = SearchOptions.inDirection(facing) val option = SearchOptions.inDirection(facing)
inventory = ItemAttributes.GROUPED_INV.getFirstOrNull(world, offsetPos, option) inventory = ItemAttributes.GROUPED_INV.getFirstOrNull(world, offsetPos, option).let {
WeakReference(it)
}
} }
private fun getInventory(): GroupedItemInv? { private fun getInventory(): GroupedItemInv? {
// if we don't have an inventory, try to get one // if we don't have an inventory, try to get one
// this happens when readAll is called before a neighbor state changes, such as immediately after world load // this happens when readAll is called before a neighbor state changes, such as immediately after world load
if (inventory == null) updateInventory() if (inventory?.get() == null) updateInventory()
return inventory return inventory?.get()
} }
override fun handle(packet: Packet) { override fun handle(packet: Packet) {