From 16d6a47c85be560517505499df1fff6df9582c18 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal002@users.noreply.github.com> Date: Sun, 25 Feb 2024 08:15:04 +0100 Subject: Fixed internal name resolving. #1046 --- .../hannibal2/skyhanni/config/commands/Commands.kt | 1 + .../skyhanni/features/bingo/MinionCraftHelper.kt | 4 +- .../skyhanni/features/misc/CollectionTracker.kt | 2 +- .../skyhanni/test/HighlightMissingRepoItems.kt | 5 +- .../skyhanni/test/SkyHanniDebugsAndTests.kt | 32 ++++++++ .../hannibal2/skyhanni/utils/ItemNameResolver.kt | 84 ++++++++++++++++++++ .../at/hannibal2/skyhanni/utils/NEUInternalName.kt | 7 +- .../java/at/hannibal2/skyhanni/utils/NEUItems.kt | 90 +--------------------- 8 files changed, 132 insertions(+), 93 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/utils/ItemNameResolver.kt (limited to 'src/main/java/at') diff --git a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt index ee89b5720..a2a1e1a93 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt @@ -374,6 +374,7 @@ object Commands { private fun developersCodingHelp() { registerCommand("shrepopatterns", "See where regexes are loaded from") { RepoPatternGui.open() } registerCommand("shtest", "Unused test command.") { SkyHanniDebugsAndTests.testCommand(it) } + registerCommand("shtestitem", "test item internal name resolving") { SkyHanniDebugsAndTests.testItemCommand(it) } registerCommand( "shfindnullconfig", "Find config elements that are null and prints them into the console" 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 7ada19315..c3ef465f2 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/bingo/MinionCraftHelper.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/bingo/MinionCraftHelper.kt @@ -19,9 +19,9 @@ import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNecessary import at.hannibal2.skyhanni.utils.RenderUtils.renderStrings import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern import com.google.gson.JsonArray import com.google.gson.JsonObject -import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe import net.minecraft.client.Minecraft import net.minecraft.item.ItemStack @@ -267,7 +267,7 @@ class MinionCraftHelper { for ((_, b) in event.inventoryItems) { val name = b.name ?: continue if (!name.startsWith("§e")) continue - val internalName = NEUItems.getInternalNameFromItemName("$name I") + val internalName = NEUInternalName.fromItemName("$name I") .replace("MINION", "GENERATOR").replace(";", "_").replace("CAVE_SPIDER", "CAVESPIDER") if (!tierOneMinionsDone.contains(internalName)) { tierOneMinionsDone.add(internalName) diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/CollectionTracker.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/CollectionTracker.kt index 6e8f746f2..630d4fb83 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/CollectionTracker.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/CollectionTracker.kt @@ -57,7 +57,7 @@ class CollectionTracker { return } - val foundInternalName = NEUItems.getInternalNameOrNullIgnoreCase(rawName) + val foundInternalName = NEUItems.getInternalNameOrNull(rawName) if (foundInternalName == null) { ChatUtils.error("Item '$rawName' does not exist!") return diff --git a/src/main/java/at/hannibal2/skyhanni/test/HighlightMissingRepoItems.kt b/src/main/java/at/hannibal2/skyhanni/test/HighlightMissingRepoItems.kt index e70a572df..53a030f88 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/HighlightMissingRepoItems.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/HighlightMissingRepoItems.kt @@ -7,6 +7,7 @@ import at.hannibal2.skyhanni.utils.ItemUtils.getInternalNameOrNull import at.hannibal2.skyhanni.utils.LorenzColor import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.NEUItems +import at.hannibal2.skyhanni.utils.NEUItems.allItemsCache import at.hannibal2.skyhanni.utils.RenderUtils.highlight import net.minecraft.client.Minecraft import net.minecraft.client.gui.inventory.GuiChest @@ -33,10 +34,10 @@ class HighlightMissingRepoItems { } private fun highlightItems(slots: Iterable) { - if (NEUItems.allInternalNames.isEmpty()) return + if (allItemsCache.isEmpty()) return for (slot in slots) { val internalName = slot.stack?.getInternalNameOrNull() ?: continue - if (NEUItems.allInternalNames.contains(internalName)) continue + if (allItemsCache.containsValue(internalName)) continue if (NEUItems.ignoreItemsFilter.match(internalName.asString())) continue slot highlight LorenzColor.RED diff --git a/src/main/java/at/hannibal2/skyhanni/test/SkyHanniDebugsAndTests.kt b/src/main/java/at/hannibal2/skyhanni/test/SkyHanniDebugsAndTests.kt index adc1b234d..7d4bec855 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/SkyHanniDebugsAndTests.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/SkyHanniDebugsAndTests.kt @@ -32,7 +32,9 @@ import at.hannibal2.skyhanni.utils.LorenzLogger import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.LorenzVec 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.getItemStackOrNull import at.hannibal2.skyhanni.utils.NEUItems.getNpcPriceOrNull import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import at.hannibal2.skyhanni.utils.OSUtils @@ -371,6 +373,36 @@ class SkyHanniDebugsAndTests { ChatUtils.chat("§cDisabled global renderer! Run this command again to show SkyHanni rendering again.") } } + + fun testItemCommand(args: Array) { + if (args.isEmpty()) { + ChatUtils.userError("Usage: /shtestitem ") + return + } + + val input = args.joinToString(" ") + val result = buildList { + add("") + add("§bSkyHanni Test Item") + add("§einput: '§f$input§e'") + + NEUInternalName.fromItemNameOrNull(input)?.let { + add("§eitem name -> internalName: '§7${it.asString()}§e'") + add(" §eitemName: '${it.itemName}§e'") + return@buildList + } + + input.asInternalName().getItemStackOrNull()?.let { + val itemName = it.itemName + add("§einternal name: §7${it.getInternalName().asString()}") + add("§einternal name -> item name: '$itemName§e'") + return@buildList + } + + add("§cNothing found!") + } + ChatUtils.chat(result.joinToString("\n"), prefix = false) + } } @SubscribeEvent diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemNameResolver.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemNameResolver.kt new file mode 100644 index 000000000..d17f579ed --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemNameResolver.kt @@ -0,0 +1,84 @@ +package at.hannibal2.skyhanni.utils + +import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName +import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimal +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher +import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import io.github.moulberry.notenoughupdates.util.ItemResolutionQuery + +object ItemNameResolver { + private val itemNameCache = mutableMapOf() // item name -> internal name + + internal fun getInternalNameOrNull(itemName: String): NEUInternalName? { + val lowercase = itemName.lowercase() + itemNameCache[lowercase]?.let { + return it + } + + if (itemName == "§cmissing repo item") { + return itemNameCache.getOrPut(lowercase) { NEUInternalName.MISSING_ITEM } + } + + resolveEnchantmentByName(itemName)?.let { + return itemNameCache.getOrPut(lowercase) { fixEnchantmentName(it) } + } + + val internalName = ItemResolutionQuery.findInternalNameByDisplayName(itemName, true)?.let { + + // This fixes a NEU bug with §9Hay Bale (cosmetic item) + // TODO remove workaround when this is fixed in neu + val rawInternalName = if (it == "HAY_BALE") "HAY_BLOCK" else it + rawInternalName.asInternalName() + } ?: run { + getInternalNameOrNullIgnoreCase(itemName) + } ?: return null + + itemNameCache[lowercase] = internalName + return internalName + } + + // Taken and edited from NEU + private fun resolveEnchantmentByName(enchantmentName: String) = + UtilsPatterns.enchantmentNamePattern.matchMatcher(enchantmentName) { + val name = group("name").trim { it <= ' ' } + val ultimate = group("format").lowercase().contains("§l") + ((if (ultimate && name != "Ultimate Wise") "ULTIMATE_" else "") + + turboCheck(name).replace(" ", "_").replace("-", "_").uppercase() + + ";" + group("level").romanToDecimal()) + } + + private fun turboCheck(text: String): String { + if (text == "Turbo-Cocoa") return "Turbo-Coco" + if (text == "Turbo-Cacti") return "Turbo-Cactus" + return text + } + + // Workaround for duplex + private val duplexPattern = "ULTIMATE_DUPLEX;(?.*)".toPattern() + + private fun fixEnchantmentName(originalName: String): NEUInternalName { + duplexPattern.matchMatcher(originalName) { + val tier = group("tier") + return "ULTIMATE_REITERATE;$tier".asInternalName() + } + // TODO USE SH-REPO + return originalName.asInternalName() + } + + private fun getInternalNameOrNullIgnoreCase(itemName: String): NEUInternalName? { + val lowercase = itemName.removeColor().lowercase() + itemNameCache[lowercase]?.let { + return it + } + + if (NEUItems.allItemsCache.isEmpty()) { + NEUItems.allItemsCache = NEUItems.readAllNeuItems() + } + NEUItems.allItemsCache[lowercase]?.let { + itemNameCache[lowercase] = it + return it + } + + return null + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NEUInternalName.kt b/src/main/java/at/hannibal2/skyhanni/utils/NEUInternalName.kt index 73fc82622..368877079 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NEUInternalName.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NEUInternalName.kt @@ -13,11 +13,14 @@ class NEUInternalName private constructor(private val internalName: String) { val SKYBLOCK_COIN = "SKYBLOCK_COIN".asInternalName() fun String.asInternalName(): NEUInternalName { - val internalName = uppercase() + val internalName = uppercase().replace(" ", "_") return map.getOrPut(internalName) { NEUInternalName(internalName) } } - fun fromItemName(itemName: String) = NEUItems.getInternalNameFromItemName(itemName) + fun fromItemNameOrNull(itemName: String): NEUInternalName? = ItemNameResolver.getInternalNameOrNull(itemName) + + fun fromItemName(itemName: String): NEUInternalName = + fromItemNameOrNull(itemName) ?: throw Error("NEUInternalName is null for item name '$itemName'") } fun asString() = internalName diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt index c96fb36a3..69f1e4fc3 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt @@ -8,8 +8,6 @@ import at.hannibal2.skyhanni.test.command.ErrorManager 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.romanToDecimal -import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import com.google.gson.JsonObject import com.google.gson.JsonPrimitive @@ -37,13 +35,11 @@ import org.lwjgl.opengl.GL11 object NEUItems { val manager: NEUManager get() = NotEnoughUpdates.INSTANCE.manager - private val itemNameCache = mutableMapOf() // item name -> internal name private val multiplierCache = mutableMapOf>() private val recipesCache = mutableMapOf>() private val ingredientsCache = mutableMapOf>() var allItemsCache = mapOf() // item name -> internal name - var allInternalNames = mutableListOf() val ignoreItemsFilter = MultiFilter() private val fallbackItem by lazy { @@ -60,90 +56,21 @@ object NEUItems { ignoreItemsFilter.load(ignoredItems) } - // TODO remove @Deprecated("Use NEUInternalName rather than String", ReplaceWith("getInternalNameFromItemName()")) - fun getRawInternalName(itemName: String): String { - return getInternalNameFromItemName(itemName).asString() - } - - fun getInternalNameFromItemName(itemName: String): NEUInternalName { - return getInternalNameOrNull(itemName) ?: throw Error("Internal name is null for '$itemName'") - } - - fun getInternalNameOrNullIgnoreCase(itemName: String): NEUInternalName? { - val lowercase = itemName.removeColor().lowercase() - if (itemNameCache.containsKey(lowercase)) { - return itemNameCache[lowercase]!! - } - - if (allItemsCache.isEmpty()) { - allItemsCache = readAllNeuItems() - } - allItemsCache[lowercase]?.let { - itemNameCache[lowercase] = it - return it - } - - return null - } + fun getRawInternalName(itemName: String): String = NEUInternalName.fromItemName(itemName).asString() fun readAllNeuItems(): Map { - allInternalNames.clear() val map = mutableMapOf() for (rawInternalName in allNeuRepoItems().keys) { val name = manager.createItem(rawInternalName).displayName.removeColor().lowercase() val internalName = rawInternalName.asInternalName() map[name] = internalName - allInternalNames.add(internalName) } return map } - fun getInternalNameOrNull(itemName: String): NEUInternalName? { - val lowercase = itemName.lowercase() - if (itemNameCache.containsKey(lowercase)) { - return itemNameCache[lowercase]!! - } - - if (itemName == "§cmissing repo item") { - itemNameCache[lowercase] = NEUInternalName.MISSING_ITEM - return NEUInternalName.MISSING_ITEM - } - - resolveEnchantmentByName(itemName)?.let { - val enchantmentName = fixEnchantmentName(it) - itemNameCache[itemName] = enchantmentName - return enchantmentName - } - var rawInternalName = ItemResolutionQuery.findInternalNameByDisplayName(itemName, false) ?: return null - - // This fixes a NEU bug with §9Hay Bale (cosmetic item) - // TODO remove workaround when this is fixed in neu - rawInternalName = if (rawInternalName == "HAY_BALE") "HAY_BLOCK" else rawInternalName - - val internalName = rawInternalName.asInternalName() - - itemNameCache[lowercase] = internalName - return internalName - } - - // Workaround for duplex - private val duplexPattern = "ULTIMATE_DUPLEX;(?.*)".toPattern() - - private fun fixEnchantmentName(originalName: String): NEUInternalName { - duplexPattern.matchMatcher(originalName) { - val tier = group("tier") - return "ULTIMATE_REITERATE;$tier".asInternalName() - } - // TODO USE SH-REPO - return originalName.asInternalName() - } - - private fun turboCheck(text: String): String { - if (text == "Turbo-Cocoa") return "Turbo-Coco" - if (text == "Turbo-Cacti") return "Turbo-Cactus" - return text - } + @Deprecated("moved", ReplaceWith("NEUInternalName.fromItemNameOrNull(itemName)")) + fun getInternalNameOrNull(itemName: String): NEUInternalName? = NEUInternalName.fromItemNameOrNull(itemName) fun getInternalName(itemStack: ItemStack): String? = ItemResolutionQuery(manager) .withCurrentGuiContext() @@ -184,6 +111,7 @@ object NEUItems { return getNpcPriceOrNull() } + @Deprecated("Use NEUInternalName", ReplaceWith("internalName.asInternalName().getPrice(useSellingPrice)")) fun getPrice(internalName: String, useSellingPrice: Boolean = false): Double = internalName.asInternalName().getPrice(useSellingPrice) @@ -355,16 +283,6 @@ object NEUItems { return false } - // Taken and edited from NEU - private fun resolveEnchantmentByName(enchantmentName: String) = - UtilsPatterns.enchantmentNamePattern.matchMatcher(enchantmentName) { - val name = group("name").trim { it <= ' ' } - val ultimate = group("format").lowercase().contains("§l") - ((if (ultimate && name != "Ultimate Wise") "ULTIMATE_" else "") - + turboCheck(name).replace(" ", "_").replace("-", "_").uppercase() - + ";" + group("level").romanToDecimal()) - } - // Uses NEU fun saveNBTData(item: ItemStack, removeLore: Boolean = true): String { val jsonObject = manager.getJsonForItem(item) -- cgit