Fix crash in addLivelyNode

This commit is contained in:
DBotThePony 2025-01-14 18:19:44 +07:00
parent 43afa73df7
commit 07121d8b41
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 28 additions and 4 deletions

View File

@ -20,10 +20,14 @@ class EnergyCableGraph : GraphNodeList<EnergyCableBlockEntity.Node, EnergyCableG
private val livelyNodesList = ArrayList<EnergyCableBlockEntity.Node>()
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)
}
}
}
}

View File

@ -22,6 +22,26 @@ open class GraphNodeList<N : GraphNode<N, G>, G : GraphNodeList<N, G>> : ICondit
val nodesSet: Set<N> = 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