From b6f43966f4a49a94c4ecc0498dc647d66cddfe6f Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 17 Feb 2021 22:31:48 -0500 Subject: [PATCH] Fix ConcurrentModificationExceptions --- .../phycon/network/block/terminal/TerminalBlockEntity.kt | 9 ++++----- .../phycon/network/component/NetworkStackDispatcher.kt | 9 +++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt index c1ff204..29f0ed7 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt @@ -161,12 +161,11 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), private fun finishPendingRequests() { if (world!!.isClient) return + if (pendingRequests.isEmpty()) return - for (request in pendingRequests) { - if (request.isFinishable(counter)) { - stackLocateRequestCompleted(request) - } - } + val finishable = pendingRequests.filter { it.isFinishable(counter) } + finishable.forEach(::stackLocateRequestCompleted) + pendingRequests.removeAll(finishable) } fun addObserver() { diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/component/NetworkStackDispatcher.kt b/src/main/kotlin/net/shadowfacts/phycon/network/component/NetworkStackDispatcher.kt index d193ad7..78a4aa7 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/component/NetworkStackDispatcher.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/component/NetworkStackDispatcher.kt @@ -72,9 +72,10 @@ interface NetworkStackDispatcher> Self.finishTimedOutPendingInsertions() where Self: BlockEntity, Self: NetworkStackDispatcher { if (world!!.isClient) return + if (pendingInsertions.isEmpty()) return - for (insertion in pendingInsertions) { - if (!insertion.isFinishable(this)) continue - finishInsertion(insertion) - } + val finishable = pendingInsertions.filter { it.isFinishable(this) } + finishable.forEach(::finishInsertion) + pendingInsertions.removeAll(finishable) + // todo: if a timed-out insertion can't be finished, we should probably retry after some time (exponential backoff?) }