diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/netinterface/InterfaceBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/block/netinterface/InterfaceBlockEntity.kt index 011aa3b..02d0542 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/netinterface/InterfaceBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/netinterface/InterfaceBlockEntity.kt @@ -19,6 +19,7 @@ import net.shadowfacts.phycon.component.NetworkStackReceiver import net.shadowfacts.phycon.component.handleItemStack import net.shadowfacts.phycon.packet.* import net.shadowfacts.phycon.util.ClientConfigurableDevice +import java.lang.ref.WeakReference import kotlin.math.min /** @@ -37,20 +38,21 @@ class InterfaceBlockEntity(pos: BlockPos, state: BlockState): DeviceBlockEntity( override var receiverPriority = 0 var syncPriorities = true - // todo: should this be a weak ref? - private var inventory: GroupedItemInv? = null + private var inventory: WeakReference? = null fun updateInventory() { val offsetPos = pos.offset(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? { // 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 - if (inventory == null) updateInventory() - return inventory + if (inventory?.get() == null) updateInventory() + return inventory?.get() } override fun handle(packet: Packet) {