From 07121d8b416744ae4dadacb8415c739fb0d69224 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 14 Jan 2025 18:19:44 +0700 Subject: [PATCH] Fix crash in addLivelyNode --- .../block/entity/cable/EnergyCableGraph.kt | 12 +++++++---- .../dbotthepony/mc/otm/graph/GraphNodeList.kt | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt index 7b90e6472..906d60282 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt @@ -20,10 +20,14 @@ class EnergyCableGraph : GraphNodeList() fun addLivelyNode(node: EnergyCableBlockEntity.Node) { - require(node in nodesSet) { "$node does not belong to $this" } - - if (livelyNodes.add(node)) { - livelyNodesList.add(node) + when (contains(node)) { + ContainsStatus.ABOUT_TO_BE_REMOVED, ContainsStatus.ABOUT_TO_BE_ADDED -> { } // do nothing + ContainsStatus.DOES_NOT_BELONG -> throw IllegalArgumentException("$node does not belong to $this") + ContainsStatus.CONTAINS -> { + if (livelyNodes.add(node)) { + livelyNodesList.add(node) + } + } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/GraphNodeList.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/GraphNodeList.kt index 0da2e7a1d..b252ac164 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/graph/GraphNodeList.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/graph/GraphNodeList.kt @@ -22,6 +22,26 @@ open class GraphNodeList, G : GraphNodeList> : ICondit val nodesSet: Set = Collections.unmodifiableSet(nodesSetInternal) val size get() = nodesInternal.size + enum class ContainsStatus(val belongs: Boolean, val potentiallyBelongs: Boolean) { + ABOUT_TO_BE_REMOVED(true, false), + DOES_NOT_BELONG(false, false), + ABOUT_TO_BE_ADDED(false, true), + CONTAINS(true, true) + } + + fun contains(node: N): ContainsStatus { + if (node in nodesSet) { + if (node in queuedRemove) + return ContainsStatus.ABOUT_TO_BE_REMOVED + + return ContainsStatus.CONTAINS + } else if (node in queuedAdd) { + return ContainsStatus.ABOUT_TO_BE_ADDED + } else { + return ContainsStatus.DOES_NOT_BELONG + } + } + var isValid = true private set