diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/UUIDIntModifiersMap.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/UUIDIntModifiersMap.kt index 9895d02c8..601913052 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/UUIDIntModifiersMap.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/UUIDIntModifiersMap.kt @@ -15,7 +15,10 @@ class UUIDIntModifiersMap(private val observer: (Int) -> Unit, private val backi var value: Int = 0 private set + private var ignoreRecompute = false + fun recompute() { + if (ignoreRecompute) return var value = 0 for (mapValue in backingMap.values) { @@ -35,9 +38,15 @@ class UUIDIntModifiersMap(private val observer: (Int) -> Unit, private val backi return false } - this.value += value - (old ?: 0) - observer.invoke(this.value) - return true + ignoreRecompute = true + + try { + this.value += value - (old ?: 0) + observer.invoke(this.value) + return true + } finally { + ignoreRecompute = false + } } operator fun get(key: UUID): Int? { @@ -53,17 +62,29 @@ class UUIDIntModifiersMap(private val observer: (Int) -> Unit, private val backi return false } - val old = backingMap.remove(key) ?: return true - value -= old - return true + ignoreRecompute = true + + try { + val old = backingMap.remove(key) ?: return true + value -= old + return true + } finally { + ignoreRecompute = false + } } fun clear() { - backingMap.clear() - val old = this.value - this.value = 0 - if (old != this.value) { - observer.invoke(this.value) + ignoreRecompute = true + + try { + backingMap.clear() + } finally { + ignoreRecompute = false + + if (this.value != 0) { + this.value = 0 + observer.invoke(0) + } } } @@ -83,19 +104,24 @@ class UUIDIntModifiersMap(private val observer: (Int) -> Unit, private val backi nbt ?: return val old = this.value this.value = 0 + ignoreRecompute = true - for (value in nbt) { - value as CompoundTag + try { + for (value in nbt) { + value as CompoundTag - if (value.contains("key", "value")) { - val int = value.getInt("value") - backingMap.put(value.getUUID("key"), int) - this.value += int + if (value.contains("key", "value")) { + val int = value.getInt("value") + backingMap.put(value.getUUID("key"), int) + this.value += int + } } - } - if (old != this.value) { - observer.invoke(this.value) + if (old != this.value) { + observer.invoke(this.value) + } + } finally { + ignoreRecompute = false } } }