From 519629cfb9b8f33ca06aae5f7db8ef601cef68f8 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal002@users.noreply.github.com> Date: Sun, 27 Oct 2024 15:52:28 +0100 Subject: Fix: async map clear (#2833) Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../experimentationtable/SuperpairDataDisplay.kt | 52 +++++++++++----------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/experimentationtable/SuperpairDataDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/experimentationtable/SuperpairDataDisplay.kt index 9fa9a7571..0713baf73 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/experimentationtable/SuperpairDataDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/experimentationtable/SuperpairDataDisplay.kt @@ -43,14 +43,14 @@ object SuperpairDataDisplay { private val emptySuperpairItem = SuperpairItem(-1, "", -1) private var display = emptyList() - private var uncoveredItems = mutableMapOf() + private var uncoveredItems = mapOf() private val found = mutableMapOf>() @SubscribeEvent fun onInventoryClose(event: InventoryCloseEvent) { display = emptyList() - uncoveredItems = mutableMapOf() + uncoveredItems = emptyMap() found.clear() } @@ -81,68 +81,70 @@ object SuperpairDataDisplay { val clicksItem = InventoryUtils.getItemAtSlotIndex(4) // TODO add variable name to indicate what is going on here - if (uncoveredItems.none { it.value.index == event.slotId && it.key == uncoveredItems.keys.max() }) { + val items = uncoveredItems.toMutableMap() + if (items.none { it.value.index == event.slotId && it.key == items.keys.max() }) { if (clicksItem != null) { remainingClicksPattern.matchMatcher(clicksItem.displayName.removeColor()) { if (group("clicks").toInt() == 0) return } } - handleItem(event.slotId) + handleItem(items, event.slotId) } + uncoveredItems = items } - private fun handleItem(slot: Int) = DelayedRun.runDelayed(200.milliseconds) { + private fun handleItem(items: MutableMap, slot: Int) = DelayedRun.runDelayed(200.milliseconds) { val itemNow = InventoryUtils.getItemAtSlotIndex(slot) ?: return@runDelayed val itemName = itemNow.displayName.removeColor() val reward = convertToReward(itemNow) val itemData = SuperpairItem(slot, reward, itemNow.itemDamage) - val uncovered = uncoveredItems.keys.maxOrNull() ?: -1 + val uncovered = items.keys.maxOrNull() ?: -1 if (isWaiting(itemName)) return@runDelayed - if (uncoveredItems.none { it.key == uncovered && it.value.index == slot }) uncoveredItems[uncovered + 1] = itemData + if (items.none { it.key == uncovered && it.value.index == slot }) items[uncovered + 1] = itemData when { - isPowerUp(reward) -> handlePowerUp(itemData, uncovered + 1) - isReward(itemName) -> handleReward(itemData, uncovered + 1) + isPowerUp(reward) -> handlePowerUp(items, itemData, uncovered + 1) + isReward(itemName) -> handleReward(items, itemData, uncovered + 1) } - val since = clicksSinceSeparator(uncoveredItems) + val since = clicksSinceSeparator(items) - val lastReward = uncoveredItems.entries.last().value.reward + val lastReward = items.entries.last().value.reward // TODO use repo patterns for "Instant Find" - if ((since >= 2 || (since == -1 && uncoveredItems.size >= 2)) && lastReward != "Instant Find") uncoveredItems[uncovered + 2] = + if ((since >= 2 || (since == -1 && items.size >= 2)) && lastReward != "Instant Find") items[uncovered + 2] = emptySuperpairItem display = drawDisplay() } - private fun handlePowerUp(item: SuperpairItem, uncovered: Int) { + private fun handlePowerUp(items: MutableMap, item: SuperpairItem, uncovered: Int) { // TODO use repo patterns for "Instant Find" - if (item.reward != "Instant Find") uncoveredItems.remove(uncovered) + if (item.reward != "Instant Find") items.remove(uncovered) val itemData = FoundData(item = item) found.getOrPut(FoundType.POWERUP) { mutableListOf(itemData) }.apply { if (!contains(itemData)) add(itemData) } } - private fun handleReward(item: SuperpairItem, uncovered: Int) { - val last = uncoveredItems.getOrDefault(uncovered - 1, item) + private fun handleReward(items: MutableMap, item: SuperpairItem, uncovered: Int) { + val last = items.getOrDefault(uncovered - 1, item) if (isWaiting(last.reward)) return when { // TODO use repo patterns for "Instant Find" - last.reward == "Instant Find" -> handleInstantFind(item, uncovered) + last.reward == "Instant Find" -> handleInstantFind(items, item, uncovered) hasFoundPair(item, last) -> handleFoundPair(item, last) - hasFoundMatch(item) -> handleFoundMatch(item) + hasFoundMatch(items, item) -> handleFoundMatch(items, item) else -> handleNormalReward(item) } println(found) } - private fun handleInstantFind(item: SuperpairItem, uncovered: Int) { - uncoveredItems[uncovered - 1] = item - uncoveredItems[uncovered] = emptySuperpairItem + private fun handleInstantFind(items: MutableMap, item: SuperpairItem, uncovered: Int) { + items[uncovered - 1] = item + items[uncovered] = emptySuperpairItem handleFoundPair(item, emptySuperpairItem) } @@ -164,9 +166,9 @@ object SuperpairDataDisplay { found.getOrPut(FoundType.PAIR) { mutableListOf(pairData) }.apply { if (!contains(pairData)) add(pairData) } } - private fun handleFoundMatch(item: SuperpairItem) { + private fun handleFoundMatch(items: MutableMap, item: SuperpairItem) { // TODO better name - val match = uncoveredItems.values.find { it.index != item.index && it.sameAs(item) } ?: return + val match = items.values.find { it.index != item.index && it.sameAs(item) } ?: return found.entries.forEach { when { @@ -269,8 +271,8 @@ object SuperpairDataDisplay { ) = first.index != second.index && first.sameAs(second) // TODO extract logic greatly - private fun hasFoundMatch(firstItem: SuperpairItem) = - uncoveredItems.any { it.value.index != firstItem.index && it.value.sameAs(firstItem) } && + private fun hasFoundMatch(items: Map, firstItem: SuperpairItem) = + items.any { it.value.index != firstItem.index && it.value.sameAs(firstItem) } && found.entries.none { it.key.isAnyOf(FoundType.PAIR, FoundType.MATCH) && it.value.any { data -> -- cgit