From 1e9764d1d1209177d4a939a9b812931a7d49a926 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal002@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:27:50 +0200 Subject: Backend: PrimitiveIngredient changes and code cleanup (#2576) Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Co-authored-by: Cal --- .../skyhanni/features/bingo/MinionCraftHelper.kt | 9 ++++----- .../features/garden/visitor/GardenVisitorFeatures.kt | 13 ++++++------- .../features/garden/visitor/GardenVisitorSupercraft.kt | 14 +++++++------- src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt | 5 ++--- src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt | 9 +++++---- .../at/hannibal2/skyhanni/utils/PrimitiveIngredient.kt | 16 +++++++++++++--- .../at/hannibal2/skyhanni/utils/PrimitiveItemStack.kt | 2 ++ 7 files changed, 39 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/main/java/at/hannibal2/skyhanni/features/bingo/MinionCraftHelper.kt b/src/main/java/at/hannibal2/skyhanni/features/bingo/MinionCraftHelper.kt index dbec4ea57..6f7a062f8 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/bingo/MinionCraftHelper.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/bingo/MinionCraftHelper.kt @@ -7,6 +7,7 @@ import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName import at.hannibal2.skyhanni.utils.ItemUtils.hasEnchantments import at.hannibal2.skyhanni.utils.ItemUtils.itemName @@ -18,6 +19,7 @@ import at.hannibal2.skyhanni.utils.NEUItems import at.hannibal2.skyhanni.utils.NEUItems.getCachedIngredients import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNecessary +import at.hannibal2.skyhanni.utils.PrimitiveIngredient.Companion.toPrimitiveItemStacks import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher import at.hannibal2.skyhanni.utils.RenderUtils.renderStrings import at.hannibal2.skyhanni.utils.StringUtils.removeColor @@ -197,12 +199,9 @@ object MinionCraftHelper { val output = recipe.output ?: continue if (!output.internalName.contains("_GENERATOR_")) continue val map = mutableMapOf() - for (input in recipe.ingredients) { - val itemId = input.internalName + for ((itemId, count) in recipe.ingredients.toPrimitiveItemStacks()) { if (minionId != itemId) { - val count = input.count.toInt() - val old = map.getOrDefault(itemId, 0) - map[itemId] = old + count + map.addOrPut(itemId, count) } } var allDone = true diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt index 2e8d78966..70392bb46 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorFeatures.kt @@ -29,6 +29,7 @@ import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.CollectionUtils.addAsSingletonList +import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut import at.hannibal2.skyhanni.utils.ConfigUtils import at.hannibal2.skyhanni.utils.EntityUtils import at.hannibal2.skyhanni.utils.HypixelCommands @@ -51,6 +52,7 @@ import at.hannibal2.skyhanni.utils.NEUItems.getPrice import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import at.hannibal2.skyhanni.utils.NumberUtil.formatInt import at.hannibal2.skyhanni.utils.NumberUtil.shortFormat +import at.hannibal2.skyhanni.utils.PrimitiveIngredient.Companion.toPrimitiveItemStacks import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher import at.hannibal2.skyhanni.utils.RenderUtils.drawString import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems @@ -253,16 +255,13 @@ object GardenVisitorFeatures { ?.ingredients ?: emptySet() if (ingredients.isEmpty()) return - // TODO change key to NEUInternalName - val requiredIngredients = mutableMapOf() - for (ingredient in ingredients) { - val key = ingredient.internalName.asString() - requiredIngredients[key] = - requiredIngredients.getOrDefault(key, 0) + ingredient.count.toInt() + val requiredIngredients = mutableMapOf() + for ((key, count) in ingredients.toPrimitiveItemStacks()) { + requiredIngredients.addOrPut(key, count) } var hasIngredients = true for ((key, value) in requiredIngredients) { - val sackItem = key.asInternalName().getAmountInSacks() + val sackItem = key.getAmountInSacks() if (sackItem < value * (amount - amountInSacks)) { hasIngredients = false break diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorSupercraft.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorSupercraft.kt index f2e4f80fb..9b14483dc 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorSupercraft.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/GardenVisitorSupercraft.kt @@ -7,12 +7,14 @@ import at.hannibal2.skyhanni.events.garden.visitor.VisitorOpenEvent import at.hannibal2.skyhanni.events.render.gui.ReplaceItemEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.test.command.ErrorManager +import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut import at.hannibal2.skyhanni.utils.HypixelCommands import at.hannibal2.skyhanni.utils.ItemUtils import at.hannibal2.skyhanni.utils.NEUInternalName import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName import at.hannibal2.skyhanni.utils.NEUItems import at.hannibal2.skyhanni.utils.NEUItems.getItemStack +import at.hannibal2.skyhanni.utils.PrimitiveIngredient.Companion.toPrimitiveItemStacks import at.hannibal2.skyhanni.utils.SimpleTimeMark import net.minecraft.entity.player.InventoryPlayer import net.minecraftforge.fml.common.eventhandler.EventPriority @@ -73,15 +75,13 @@ object GardenVisitorSupercraft { // TODO describe what this line does .firstOrNull { !it.ingredients.first().internalName.contains("PEST") } ?.ingredients ?: return - // TODO change key to NEUInternalName - val ingredientReqs = mutableMapOf() - for (ingredient in ingredients) { - val key = ingredient.internalName.asString() - ingredientReqs[key] = ingredientReqs.getOrDefault(key, 0) + ingredient.count.toInt() + val requiredIngredients = mutableMapOf() + for ((key, count) in ingredients.toPrimitiveItemStacks()) { + requiredIngredients.addOrPut(key, count) } hasIngredients = true - for ((key, value) in ingredientReqs) { - val sackItem = key.asInternalName().getAmountInSacks() + for ((key, value) in requiredIngredients) { + val sackItem = key.getAmountInSacks() lastSuperCraftMaterial = internalName.asString() if (sackItem < value * amount) { hasIngredients = false diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt index add66cc0a..d39b37c8a 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt @@ -9,6 +9,7 @@ import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut import at.hannibal2.skyhanni.utils.ItemPriceUtils.getPrice import at.hannibal2.skyhanni.utils.NEUItems.getItemStackOrNull import at.hannibal2.skyhanni.utils.NumberUtil.formatInt +import at.hannibal2.skyhanni.utils.PrimitiveIngredient.Companion.toPrimitiveItemStacks import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher import at.hannibal2.skyhanni.utils.RegexUtils.matches import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.cachedData @@ -511,9 +512,7 @@ object ItemUtils { fun neededItems(recipe: PrimitiveRecipe): Map { val neededItems = mutableMapOf() - for (ingredient in recipe.ingredients) { - val material = ingredient.internalName - val amount = ingredient.count.toInt() + for ((material, amount) in recipe.ingredients.toPrimitiveItemStacks()) { neededItems.addOrPut(material, amount) } return neededItems diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt index 198b43a85..2f17ea4d1 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt @@ -9,10 +9,12 @@ import at.hannibal2.skyhanni.events.NeuRepositoryReloadEvent import at.hannibal2.skyhanni.events.RepositoryReloadEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.test.command.ErrorManager +import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut import at.hannibal2.skyhanni.utils.ItemBlink.checkBlinkItem import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName import at.hannibal2.skyhanni.utils.NumberUtil.isInt +import at.hannibal2.skyhanni.utils.PrimitiveIngredient.Companion.toPrimitiveItemStacks import at.hannibal2.skyhanni.utils.PrimitiveItemStack.Companion.makePrimitiveStack import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getItemId import at.hannibal2.skyhanni.utils.json.BaseGsonBuilder @@ -308,8 +310,8 @@ object NEUItems { if (!recipe.isCraftingRecipe()) continue val map = mutableMapOf() - for (ingredient in recipe.getCachedIngredients()) { - val count = ingredient.count.toInt() + for (ingredient in recipe.getCachedIngredients().toPrimitiveItemStacks()) { + val amount = ingredient.amount var internalItemId = ingredient.internalName // ignore cactus green if (internalName == "ENCHANTED_CACTUS_GREEN".asInternalName() && internalItemId == "INK_SACK-2".asInternalName()) { @@ -331,8 +333,7 @@ object NEUItems { continue } - val old = map.getOrDefault(internalItemId, 0) - map[internalItemId] = old + count + map.addOrPut(internalItemId, amount) } if (map.size != 1) continue val current = map.iterator().next().toPair() diff --git a/src/main/java/at/hannibal2/skyhanni/utils/PrimitiveIngredient.kt b/src/main/java/at/hannibal2/skyhanni/utils/PrimitiveIngredient.kt index e8d1edefc..b1692a119 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/PrimitiveIngredient.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/PrimitiveIngredient.kt @@ -2,6 +2,7 @@ package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.SKYBLOCK_COIN import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName +import at.hannibal2.skyhanni.utils.NumberUtil.formatDouble import io.github.moulberry.notenoughupdates.recipes.Ingredient class PrimitiveIngredient(val internalName: NEUInternalName, val count: Double = 1.0) { @@ -10,17 +11,26 @@ class PrimitiveIngredient(val internalName: NEUInternalName, val count: Double = constructor(ingredientIdentifier: String) : this( ingredientIdentifier.substringBefore(':').asInternalName(), - ingredientIdentifier.substringAfter(':').toDoubleOrNull() ?: 1.0, + // if second part is blank, the count is assumed to be 1 + ingredientIdentifier.substringAfter(':', "").let { if (it.isBlank()) 1.0 else it.formatDouble() }, ) companion object { fun coinIngredient(count: Double = 1.0) = PrimitiveIngredient(SKYBLOCK_COIN, count) - fun fromNeuIngredient(neuIngredient: Ingredient) = PrimitiveIngredient(neuIngredient.internalItemId.asInternalName(), neuIngredient.count) + fun fromNeuIngredient(neuIngredient: Ingredient) = + PrimitiveIngredient(neuIngredient.internalItemId.asInternalName(), neuIngredient.count) + + fun Set.toPrimitiveItemStacks(): List = + map { it.toPrimitiveItemStack() } } fun isCoin() = internalName == SKYBLOCK_COIN - override fun toString() = "$internalName x$count" + + fun toPair() = Pair(internalName, count) + + // TODO should maybe throw an error when trying to use with internalName == SKYBLOCK_COIN + fun toPrimitiveItemStack() = PrimitiveItemStack(internalName, count.toInt()) } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/PrimitiveItemStack.kt b/src/main/java/at/hannibal2/skyhanni/utils/PrimitiveItemStack.kt index 52faaba30..f9d2b89f7 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/PrimitiveItemStack.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/PrimitiveItemStack.kt @@ -15,6 +15,8 @@ data class PrimitiveItemStack(val internalName: NEUInternalName, val amount: Int val itemName by lazy { internalName.itemName } + fun toPair() = Pair(internalName, amount) + companion object { fun NEUInternalName.makePrimitiveStack(amount: Int = 1) = PrimitiveItemStack(this, amount) -- cgit