Fix terminal not updating when all networked inventories are disconnected

This commit is contained in:
Shadowfacts 2023-03-12 22:26:35 -04:00
parent d527185888
commit 6d97af8bdc
3 changed files with 22 additions and 13 deletions

View File

@ -38,7 +38,8 @@ abstract class AbstractTerminalBlockEntity(type: BlockEntityType<*>, pos: BlockP
// the locate/insertion timeouts are only 1 tick because that's long enough to hear from every device on the network // the locate/insertion timeouts are only 1 tick because that's long enough to hear from every device on the network
// in a degraded state (when there's latency in the network), not handling interface priorities correctly is acceptable // in a degraded state (when there's latency in the network), not handling interface priorities correctly is acceptable
val LOCATE_REQUEST_TIMEOUT: Long = 1 // ticks val LOCATE_REQUEST_TIMEOUT: Long = 1 // ticks
val INSERTION_TIMEOUT: Long = 1 val INSERTION_TIMEOUT: Long = 1 // ticks
val REQUEST_INVENTORY_TIMEOUT: Long = 1 // ticks
} }
protected val inventoryCache = mutableMapOf<IPAddress, GroupedItemInvView>() protected val inventoryCache = mutableMapOf<IPAddress, GroupedItemInvView>()
@ -48,8 +49,8 @@ abstract class AbstractTerminalBlockEntity(type: BlockEntityType<*>, pos: BlockP
override val pendingInsertions = mutableListOf<PendingInsertion>() override val pendingInsertions = mutableListOf<PendingInsertion>()
override val dispatchStackTimeout = INSERTION_TIMEOUT override val dispatchStackTimeout = INSERTION_TIMEOUT
private var observers = 0
val cachedNetItems = ItemStackCollections.intMap() val cachedNetItems = ItemStackCollections.intMap()
private var requestInventoryTimestamp: Long? = null
// todo: multiple players could have the terminal open simultaneously // todo: multiple players could have the terminal open simultaneously
var netItemObserver: WeakReference<NetItemObserver>? = null var netItemObserver: WeakReference<NetItemObserver>? = null
@ -164,14 +165,26 @@ abstract class AbstractTerminalBlockEntity(type: BlockEntityType<*>, pos: BlockP
if (!world!!.isClient) { if (!world!!.isClient) {
finishPendingRequests() finishPendingRequests()
finishTimedOutPendingInsertions() finishTimedOutPendingInsertions()
if (counter % 20 == 0L) {
beginInsertions()
} }
if (counter % 20 == 0L && !world!!.isClient) { if (requestInventoryTimestamp != null && (counter - requestInventoryTimestamp!!) >= REQUEST_INVENTORY_TIMEOUT) {
beginInsertions() updateAndSync()
requestInventoryTimestamp = null
}
} }
} }
open fun onActivate(player: PlayerEntity) { open fun onActivate(player: PlayerEntity) {
if (!world!!.isClient) {
updateAndSync()
inventoryCache.clear()
sendPacket(RequestInventoryPacket(RequestInventoryPacket.Kind.GROUPED, ipAddress))
requestInventoryTimestamp = counter
}
} }
fun requestItem(stack: ItemStack, amount: Int = stack.count) { fun requestItem(stack: ItemStack, amount: Int = stack.count) {

View File

@ -33,11 +33,9 @@ class CraftingTerminalBlockEntity(pos: BlockPos, state: BlockState): AbstractTer
private val completedCraftingStackRequests = LinkedList<CraftingStackLocateRequest>() private val completedCraftingStackRequests = LinkedList<CraftingStackLocateRequest>()
override fun onActivate(player: PlayerEntity) { override fun onActivate(player: PlayerEntity) {
if (!world!!.isClient) { super.onActivate(player)
updateAndSync()
inventoryCache.clear() if (!world!!.isClient) {
sendPacket(RequestInventoryPacket(RequestInventoryPacket.Kind.GROUPED, ipAddress))
val factory = object: ExtendedScreenHandlerFactory { val factory = object: ExtendedScreenHandlerFactory {
override fun createMenu(syncId: Int, playerInv: PlayerInventory, player: PlayerEntity): ScreenHandler? { override fun createMenu(syncId: Int, playerInv: PlayerInventory, player: PlayerEntity): ScreenHandler? {
return CraftingTerminalScreenHandler(syncId, playerInv, this@CraftingTerminalBlockEntity) return CraftingTerminalScreenHandler(syncId, playerInv, this@CraftingTerminalBlockEntity)

View File

@ -18,11 +18,9 @@ import net.shadowfacts.phycon.packet.RequestInventoryPacket
class TerminalBlockEntity(pos: BlockPos, state: BlockState): AbstractTerminalBlockEntity(PhyBlockEntities.TERMINAL, pos, state) { class TerminalBlockEntity(pos: BlockPos, state: BlockState): AbstractTerminalBlockEntity(PhyBlockEntities.TERMINAL, pos, state) {
override fun onActivate(player: PlayerEntity) { override fun onActivate(player: PlayerEntity) {
if (!world!!.isClient) { super.onActivate(player)
updateAndSync()
inventoryCache.clear() if (!world!!.isClient) {
sendPacket(RequestInventoryPacket(RequestInventoryPacket.Kind.GROUPED, ipAddress))
val factory = object: ExtendedScreenHandlerFactory { val factory = object: ExtendedScreenHandlerFactory {
override fun createMenu(syncId: Int, playerInv: PlayerInventory, player: PlayerEntity): ScreenHandler { override fun createMenu(syncId: Int, playerInv: PlayerInventory, player: PlayerEntity): ScreenHandler {
return TerminalScreenHandler(syncId, playerInv, this@TerminalBlockEntity) return TerminalScreenHandler(syncId, playerInv, this@TerminalBlockEntity)