From a5cad9ed535562b8deeb1511c4ff42630ebb30b7 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sat, 27 Jul 2024 15:44:40 +0800 Subject: Clean up id mess and begin to add tests --- .../de/hysky/skyblocker/mixins/ItemStackMixin.java | 130 +-------------- .../de/hysky/skyblocker/skyblock/ChestValue.java | 6 +- .../skyblock/auction/AuctionBrowserScreen.java | 3 +- .../skyblocker/skyblock/dungeon/CroesusProfit.java | 4 +- .../skyblock/garden/FarmingHudWidget.java | 3 +- .../skyblock/item/tooltip/ItemTooltip.java | 40 +---- .../skyblock/itemlist/ItemRepository.java | 2 +- .../java/de/hysky/skyblocker/utils/ItemUtils.java | 184 +++++++++++++++++++-- 8 files changed, 188 insertions(+), 184 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java b/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java index 1493cf26..2377f6aa 100644 --- a/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java +++ b/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java @@ -1,13 +1,9 @@ package de.hysky.skyblocker.mixins; -import com.google.gson.JsonParser; import com.llamalad7.mixinextras.injector.ModifyReturnValue; -import com.mojang.serialization.JsonOps; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.injected.SkyblockerStack; -import de.hysky.skyblocker.skyblock.PetCache.PetInfo; -import de.hysky.skyblocker.skyblock.item.tooltip.ItemTooltip; import de.hysky.skyblocker.skyblock.profileviewer.ProfileViewerScreen; import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; @@ -17,8 +13,6 @@ import net.minecraft.component.ComponentHolder; import net.minecraft.component.type.ItemEnchantmentsComponent; import net.minecraft.item.ItemStack; import net.minecraft.item.tooltip.TooltipAppender; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.nbt.NbtElement; import net.minecraft.text.Text; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; @@ -29,9 +23,6 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.ModifyVariable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import java.util.Locale; -import java.util.Optional; - @Mixin(ItemStack.class) public abstract class ItemStackMixin implements ComponentHolder, SkyblockerStack { @@ -132,14 +123,14 @@ public abstract class ItemStackMixin implements ComponentHolder, SkyblockerStack @Nullable public String getSkyblockId() { if (skyblockId != null && !skyblockId.isEmpty()) return skyblockId; - return skyblockId = skyblocker$getSkyblockId(true); + return skyblockId = ItemUtils.getSkyblockApiId((ItemStack) (Object) this, true); } @Override @Nullable public String getSkyblockApiId() { if (skyblockApiId != null && !skyblockApiId.isEmpty()) return skyblockApiId; - return skyblockApiId = skyblocker$getSkyblockId(false); + return skyblockApiId = ItemUtils.getSkyblockApiId((ItemStack) (Object) this, false); } @Override @@ -152,121 +143,6 @@ public abstract class ItemStackMixin implements ComponentHolder, SkyblockerStack if (apiId.startsWith("ISSHINY_")) apiId = id; - return neuName = ItemTooltip.getNeuName(id, apiId); - } - - @Unique - private String skyblocker$getSkyblockId(boolean internalIDOnly) { - NbtCompound customData = ItemUtils.getCustomData((ItemStack) (Object) this); - - if (customData == null || !customData.contains(ItemUtils.ID, NbtElement.STRING_TYPE)) { - return null; - } - String customDataString = customData.getString(ItemUtils.ID); - - if (internalIDOnly) { - return customDataString; - } - - // Transformation to API format. - //TODO future - remove this and just handle it directly for the NEU id conversion because this whole system is confusing and hard to follow - if (customData.contains("is_shiny")) { - return "ISSHINY_" + customDataString; - } - - switch (customDataString) { - case "ENCHANTED_BOOK" -> { - if (customData.contains("enchantments")) { - NbtCompound enchants = customData.getCompound("enchantments"); - Optional firstEnchant = enchants.getKeys().stream().findFirst(); - String enchant = firstEnchant.orElse(""); - return "ENCHANTMENT_" + enchant.toUpperCase(Locale.ENGLISH) + "_" + enchants.getInt(enchant); - } - } - - case "PET" -> { - if (customData.contains("petInfo")) { - PetInfo petInfo = PetInfo.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseString(customData.getString("petInfo"))).getOrThrow(); - return "LVL_1_" + petInfo.tier() + "_" + petInfo.type(); - } - } - - case "POTION" -> { - String enhanced = customData.contains("enhanced") ? "_ENHANCED" : ""; - String extended = customData.contains("extended") ? "_EXTENDED" : ""; - String splash = customData.contains("splash") ? "_SPLASH" : ""; - if (customData.contains("potion") && customData.contains("potion_level")) { - return (customData.getString("potion") + "_" + customDataString + "_" + customData.getInt("potion_level") - + enhanced + extended + splash).toUpperCase(Locale.ENGLISH); - } - } - - case "RUNE" -> { - if (customData.contains("runes")) { - NbtCompound runes = customData.getCompound("runes"); - Optional firstRunes = runes.getKeys().stream().findFirst(); - String rune = firstRunes.orElse(""); - return rune.toUpperCase(Locale.ENGLISH) + "_RUNE_" + runes.getInt(rune); - } - } - - case "ATTRIBUTE_SHARD" -> { - if (customData.contains("attributes")) { - NbtCompound shards = customData.getCompound("attributes"); - Optional firstShards = shards.getKeys().stream().findFirst(); - String shard = firstShards.orElse(""); - return customDataString + "-" + shard.toUpperCase(Locale.ENGLISH) + "_" + shards.getInt(shard); - } - } - - case "NEW_YEAR_CAKE" -> { - return customDataString + "_" + customData.getInt("new_years_cake"); - } - - case "PARTY_HAT_CRAB", "PARTY_HAT_CRAB_ANIMATED", "BALLOON_HAT_2024" -> { - return customDataString + "_" + customData.getString("party_hat_color").toUpperCase(Locale.ENGLISH); - } - - case "PARTY_HAT_SLOTH" -> { - return customDataString + "_" + customData.getString("party_hat_emoji").toUpperCase(Locale.ENGLISH); - } - - case "CRIMSON_HELMET", "CRIMSON_CHESTPLATE", "CRIMSON_LEGGINGS", "CRIMSON_BOOTS" -> { - NbtCompound attributes = customData.getCompound("attributes"); - - if (attributes.contains("magic_find") && attributes.contains("veteran")) { - return customDataString + "-MAGIC_FIND-VETERAN"; - } - } - - case "AURORA_HELMET", "AURORA_CHESTPLATE", "AURORA_LEGGINGS", "AURORA_BOOTS" -> { - NbtCompound attributes = customData.getCompound("attributes"); - - if (attributes.contains("mana_pool") && attributes.contains("mana_regeneration")) { - return customDataString + "-MANA_POOL-MANA_REGENERATION"; - } - } - - case "TERROR_HELMET", "TERROR_CHESTPLATE", "TERROR_LEGGINGS", "TERROR_BOOTS" -> { - NbtCompound attributes = customData.getCompound("attributes"); - - if (attributes.contains("lifeline") && attributes.contains("mana_pool")) { - return customDataString + "-LIFELINE-MANA_POOL"; - } - } - - case "MIDAS_SWORD" -> { - if (customData.getInt("winning_bid") >= 50000000) { - return customDataString + "_50M"; - } - } - - case "MIDAS_STAFF" -> { - if (customData.getInt("winning_bid") >= 100000000) { - return customDataString + "_100M"; - } - } - } - return customDataString; + return neuName = ItemUtils.getNeuId(id, apiId); } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/ChestValue.java b/src/main/java/de/hysky/skyblocker/skyblock/ChestValue.java index 908525e1..b352bead 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/ChestValue.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/ChestValue.java @@ -79,11 +79,11 @@ public class ChestValue { } String name = stack.getName().getString(); - String id = stack.getSkyblockApiId(); + String skyblockApiId = stack.getSkyblockApiId(); //Regular item price - if (id != null) { - DoubleBooleanPair priceData = ItemUtils.getItemPrice(id); + if (skyblockApiId != null) { + DoubleBooleanPair priceData = ItemUtils.getItemPrice(skyblockApiId); if (!priceData.rightBoolean()) hasIncompleteData = true; diff --git a/src/main/java/de/hysky/skyblocker/skyblock/auction/AuctionBrowserScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/auction/AuctionBrowserScreen.java index b690baa1..e16a81fc 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/auction/AuctionBrowserScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/auction/AuctionBrowserScreen.java @@ -7,7 +7,6 @@ import de.hysky.skyblocker.skyblock.auction.widgets.AuctionTypeWidget; import de.hysky.skyblocker.skyblock.auction.widgets.CategoryTabWidget; import de.hysky.skyblocker.skyblock.auction.widgets.RarityWidget; import de.hysky.skyblocker.skyblock.auction.widgets.SortWidget; -import de.hysky.skyblocker.skyblock.item.tooltip.ItemTooltip; import de.hysky.skyblocker.skyblock.item.tooltip.TooltipInfoType; import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.render.gui.AbstractCustomHypixelGUI; @@ -302,7 +301,7 @@ public class AuctionBrowserScreen extends AbstractCustomHypixelGUI parsed); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/CroesusProfit.java b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/CroesusProfit.java index 82acc3f2..12954118 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/CroesusProfit.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/CroesusProfit.java @@ -105,12 +105,12 @@ public class CroesusProfit extends SimpleContainerSolver { private double getItemPrice(String itemDisplayName) { - return ItemUtils.getItemPrice(dungeonDropsNameToId.get(itemDisplayName)).leftDouble(); + return ItemUtils.getItemPrice(dungeonDropsNameToApiId.get(itemDisplayName)).leftDouble(); } // I did a thing :( - private final Map dungeonDropsNameToId = Util.make(new HashMap<>(), map -> { + private final Map dungeonDropsNameToApiId = Util.make(new HashMap<>(), map -> { map.put("Enchanted Book (Ultimate Jerry I)", "ENCHANTMENT_ULTIMATE_JERRY_1"); // ultimate books start map.put("Enchanted Book (Ultimate Jerry II)", "ENCHANTMENT_ULTIMATE_JERRY_2"); map.put("Enchanted Book (Ultimate Jerry III)", "ENCHANTMENT_ULTIMATE_JERRY_3"); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java index 84013f0b..6dd0c39b 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java @@ -1,7 +1,6 @@ package de.hysky.skyblocker.skyblock.garden; import de.hysky.skyblocker.config.SkyblockerConfigManager; -import de.hysky.skyblocker.skyblock.item.tooltip.ItemTooltip; import de.hysky.skyblocker.skyblock.item.tooltip.TooltipInfoType; import de.hysky.skyblocker.skyblock.itemlist.ItemRepository; import de.hysky.skyblocker.skyblock.tabhud.util.Ico; @@ -64,7 +63,7 @@ public class FarmingHudWidget extends Widget { ItemStack farmingToolStack = client.player.getMainHandStack(); if (farmingToolStack == null) return; String cropItemId = FARMING_TOOLS.get(ItemUtils.getItemId(farmingToolStack)); - ItemStack cropStack = ItemRepository.getItemStack(ItemTooltip.getNeuName(cropItemId, cropItemId)); // The cropItemId is being used as the api id in the second parameter because the skyblock id and api id are the same for all crops. + ItemStack cropStack = ItemRepository.getItemStack(ItemUtils.getNeuId(cropItemId, cropItemId)); // The cropItemId is being used as the api id in the second parameter because the skyblock id and api id are the same for all crops. String counterText = FarmingHud.counterText(); String counterNumber = FarmingHud.NUMBER_FORMAT.format(FarmingHud.counter()); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/ItemTooltip.java b/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/ItemTooltip.java index 955ebc87..7a8db9f6 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/ItemTooltip.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/ItemTooltip.java @@ -4,12 +4,12 @@ import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.config.configs.GeneralConfig; import de.hysky.skyblocker.skyblock.item.tooltip.adders.CraftPriceTooltip; import de.hysky.skyblocker.utils.Constants; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.scheduler.Scheduler; import net.minecraft.client.MinecraftClient; import net.minecraft.text.Text; import net.minecraft.util.Formatting; -import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,43 +25,11 @@ public class ItemTooltip { private static volatile boolean sentNullWarning = false; /** - * Gets the NEU id from an id and an api id. - * @param id the id of the skyblock item, gotten from {@link de.hysky.skyblocker.utils.ItemUtils#getItemId(net.minecraft.item.ItemStack) ItemUtils#getItemId(ItemStack)} or {@link net.minecraft.item.ItemStack#getSkyblockId() ItemStack#getSkyblockId()} - * @param apiId the api id of the skyblock item, matching the id of the item on the Skyblocker api, gotten from {@link net.minecraft.item.ItemStack#getSkyblockApiId() ItemStack#getSkyblockApiId()} - * @return the NEU id of the skyblock item, matching the id of the item gotten from {@link io.github.moulberry.repo.data.NEUItem#getSkyblockItemId() NEUItem#getSkyblockItemId()} or {@link net.minecraft.item.ItemStack#getNeuName() ItemStack#getNeuName()}, - * or an empty string if either id or apiId is null + * @deprecated Use {@link ItemUtils#getNeuId(String, String)} instead */ - @NotNull + @Deprecated(since = "1.22") public static String getNeuName(String id, String apiId) { - if (id == null || apiId == null) return ""; - switch (id) { - case "PET" -> { - apiId = apiId.replaceAll("LVL_\\d*_", ""); - String[] parts = apiId.split("_"); - String type = parts[0]; - apiId = apiId.replaceAll(type + "_", ""); - apiId = apiId + "-" + type; - apiId = apiId.replace("UNCOMMON", "1") - .replace("COMMON", "0") - .replace("RARE", "2") - .replace("EPIC", "3") - .replace("LEGENDARY", "4") - .replace("MYTHIC", "5") - .replace("-", ";"); - } - case "RUNE" -> apiId = apiId.replaceAll("_(?!.*_)", ";"); - case "POTION" -> apiId = ""; - case "ATTRIBUTE_SHARD" -> - apiId = id + "+" + apiId.replace("SHARD-", "").replaceAll("_(?!.*_)", ";"); - case "NEW_YEAR_CAKE" -> apiId = id + "+" + apiId.replace("NEW_YEAR_CAKE_", ""); - case "PARTY_HAT_CRAB_ANIMATED" -> apiId = "PARTY_HAT_CRAB_" + apiId.replace("PARTY_HAT_CRAB_ANIMATED_", "") + "_ANIMATED"; - case "CRIMSON_HELMET", "CRIMSON_CHESTPLATE", "CRIMSON_LEGGINGS", "CRIMSON_BOOTS", - "AURORA_HELMET", "AURORA_CHESTPLATE", "AURORA_LEGGINGS", "AURORA_BOOTS", - "TERROR_HELMET", "TERROR_CHESTPLATE", "TERROR_LEGGINGS", "TERROR_BOOTS" -> apiId = id; - case "MIDAS_SWORD", "MIDAS_STAFF" -> apiId = id; - default -> apiId = apiId.replace(":", "-"); - } - return apiId; + return ItemUtils.getNeuId(id, apiId); } public static void nullWarning() { diff --git a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRepository.java b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRepository.java index 4dca91d6..5196901c 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRepository.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRepository.java @@ -119,7 +119,7 @@ public class ItemRepository { } /** - * @param neuId the NEU item id gotten through {@link NEUItem#getSkyblockItemId()}, {@link ItemStack#getNeuName()}, or {@link de.hysky.skyblocker.skyblock.item.tooltip.ItemTooltip#getNeuName(String, String) ItemTooltip#getNeuName(String, String)} + * @param neuId the NEU item id gotten through {@link NEUItem#getSkyblockItemId()}, {@link ItemStack#getNeuName()}, or {@link ItemUtils#getNeuId(String, String) ItemTooltip#getNeuName(String, String)} */ @Nullable public static ItemStack getItemStack(String neuId) { diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java index 894ef8ec..3b671c60 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java @@ -11,6 +11,7 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.JsonOps; import com.mojang.serialization.codecs.RecordCodecBuilder; import de.hysky.skyblocker.SkyblockerMod; +import de.hysky.skyblocker.skyblock.PetCache; import de.hysky.skyblocker.skyblock.item.tooltip.TooltipInfoType; import de.hysky.skyblocker.skyblock.item.tooltip.adders.ObtainedDateTooltip; import it.unimi.dsi.fastutil.doubles.DoubleBooleanPair; @@ -27,6 +28,7 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; import net.minecraft.registry.Registries; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.text.Text; @@ -36,6 +38,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; +import java.util.Locale; import java.util.Optional; import java.util.function.Predicate; import java.util.regex.Matcher; @@ -78,7 +81,7 @@ public final class ItemUtils { * Gets the Skyblock item id of the item stack. * * @param stack the item stack to get the internal name from - * @return an optional containing the internal name of the item stack + * @return an optional containing the Skyblock item id of the item stack */ public static @NotNull Optional getItemIdOptional(@NotNull ItemStack stack) { NbtCompound customData = getCustomData(stack); @@ -89,7 +92,7 @@ public final class ItemUtils { * Gets the Skyblock item id of the item stack. * * @param stack the item stack to get the internal name from - * @return the internal name of the item stack, or an empty string if the item stack is null or does not have an internal name + * @return the Skyblock item id of the item stack, or an empty string if the item stack does not have a Skyblock id */ public static @NotNull String getItemId(@NotNull ItemStack stack) { return getCustomData(stack).getString(ID); @@ -110,12 +113,171 @@ public final class ItemUtils { * Gets the UUID of the item stack. * * @param stack the item stack to get the UUID from - * @return the UUID of the item stack, or an empty string if the item stack is null or does not have a UUID + * @return the UUID of the item stack, or an empty string if the item stack does not have a UUID */ public static @NotNull String getItemUuid(@NotNull ComponentHolder stack) { return getCustomData(stack).getString(UUID); } + /** + * Gets the Skyblock api id of the item stack. + * @return the Skyblock api id if of the item stack, or null if the item stack does not have a Skyblock id. + */ + @Nullable + public static String getSkyblockApiId(@NotNull ItemStack itemStack, boolean itemIdOnly) { + NbtCompound customData = getCustomData(itemStack); + + if (customData == null || !customData.contains(ID, NbtElement.STRING_TYPE)) { + return null; + } + String customDataString = customData.getString(ID); + + if (itemIdOnly) { + return customDataString; + } + + // Transformation to API format. + //TODO future - remove this and just handle it directly for the NEU id conversion because this whole system is confusing and hard to follow + if (customData.contains("is_shiny")) { + return "ISSHINY_" + customDataString; + } + + switch (customDataString) { + case "ENCHANTED_BOOK" -> { + if (customData.contains("enchantments")) { + NbtCompound enchants = customData.getCompound("enchantments"); + Optional firstEnchant = enchants.getKeys().stream().findFirst(); + String enchant = firstEnchant.orElse(""); + return "ENCHANTMENT_" + enchant.toUpperCase(Locale.ENGLISH) + "_" + enchants.getInt(enchant); + } + } + + case "PET" -> { + if (customData.contains("petInfo")) { + PetCache.PetInfo petInfo = PetCache.PetInfo.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseString(customData.getString("petInfo"))).getOrThrow(); + return "LVL_1_" + petInfo.tier() + "_" + petInfo.type(); + } + } + + case "POTION" -> { + String enhanced = customData.contains("enhanced") ? "_ENHANCED" : ""; + String extended = customData.contains("extended") ? "_EXTENDED" : ""; + String splash = customData.contains("splash") ? "_SPLASH" : ""; + if (customData.contains("potion") && customData.contains("potion_level")) { + return (customData.getString("potion") + "_" + customDataString + "_" + customData.getInt("potion_level") + + enhanced + extended + splash).toUpperCase(Locale.ENGLISH); + } + } + + case "RUNE" -> { + if (customData.contains("runes")) { + NbtCompound runes = customData.getCompound("runes"); + Optional firstRunes = runes.getKeys().stream().findFirst(); + String rune = firstRunes.orElse(""); + return rune.toUpperCase(Locale.ENGLISH) + "_RUNE_" + runes.getInt(rune); + } + } + + case "ATTRIBUTE_SHARD" -> { + if (customData.contains("attributes")) { + NbtCompound shards = customData.getCompound("attributes"); + Optional firstShards = shards.getKeys().stream().findFirst(); + String shard = firstShards.orElse(""); + return customDataString + "-" + shard.toUpperCase(Locale.ENGLISH) + "_" + shards.getInt(shard); + } + } + + case "NEW_YEAR_CAKE" -> { + return customDataString + "_" + customData.getInt("new_years_cake"); + } + + case "PARTY_HAT_CRAB", "PARTY_HAT_CRAB_ANIMATED", "BALLOON_HAT_2024" -> { + return customDataString + "_" + customData.getString("party_hat_color").toUpperCase(Locale.ENGLISH); + } + + case "PARTY_HAT_SLOTH" -> { + return customDataString + "_" + customData.getString("party_hat_emoji").toUpperCase(Locale.ENGLISH); + } + + case "CRIMSON_HELMET", "CRIMSON_CHESTPLATE", "CRIMSON_LEGGINGS", "CRIMSON_BOOTS" -> { + NbtCompound attributes = customData.getCompound("attributes"); + + if (attributes.contains("magic_find") && attributes.contains("veteran")) { + return customDataString + "-MAGIC_FIND-VETERAN"; + } + } + + case "AURORA_HELMET", "AURORA_CHESTPLATE", "AURORA_LEGGINGS", "AURORA_BOOTS" -> { + NbtCompound attributes = customData.getCompound("attributes"); + + if (attributes.contains("mana_pool") && attributes.contains("mana_regeneration")) { + return customDataString + "-MANA_POOL-MANA_REGENERATION"; + } + } + + case "TERROR_HELMET", "TERROR_CHESTPLATE", "TERROR_LEGGINGS", "TERROR_BOOTS" -> { + NbtCompound attributes = customData.getCompound("attributes"); + + if (attributes.contains("lifeline") && attributes.contains("mana_pool")) { + return customDataString + "-LIFELINE-MANA_POOL"; + } + } + + case "MIDAS_SWORD" -> { + if (customData.getInt("winning_bid") >= 50000000) { + return customDataString + "_50M"; + } + } + + case "MIDAS_STAFF" -> { + if (customData.getInt("winning_bid") >= 100000000) { + return customDataString + "_100M"; + } + } + } + return customDataString; + } + + /** + * Gets the NEU id from an id and an api id. + * @param id the id of the skyblock item, gotten from {@link ItemUtils#getItemId(ItemStack) ItemUtils#getItemId(ItemStack)} or {@link ItemStack#getSkyblockId() ItemStack#getSkyblockId()} + * @param apiId the api id of the skyblock item, matching the id of the item on the Skyblocker api, gotten from {@link ItemStack#getSkyblockApiId() ItemStack#getSkyblockApiId()} + * @return the NEU id of the skyblock item, matching the id of the item gotten from {@link io.github.moulberry.repo.data.NEUItem#getSkyblockItemId() NEUItem#getSkyblockItemId()} or {@link ItemStack#getNeuName() ItemStack#getNeuName()}, + * or an empty string if either id or apiId is null + */ + @NotNull + public static String getNeuId(String id, String apiId) { + if (id == null || apiId == null) return ""; + switch (id) { + case "PET" -> { + apiId = apiId.replaceAll("LVL_\\d*_", ""); + String[] parts = apiId.split("_"); + String type = parts[0]; + apiId = apiId.replaceAll(type + "_", ""); + apiId = apiId + "-" + type; + apiId = apiId.replace("UNCOMMON", "1") + .replace("COMMON", "0") + .replace("RARE", "2") + .replace("EPIC", "3") + .replace("LEGENDARY", "4") + .replace("MYTHIC", "5") + .replace("-", ";"); + } + case "RUNE" -> apiId = apiId.replaceAll("_(?!.*_)", ";"); + case "POTION" -> apiId = ""; + case "ATTRIBUTE_SHARD" -> + apiId = id + "+" + apiId.replace("SHARD-", "").replaceAll("_(?!.*_)", ";"); + case "NEW_YEAR_CAKE" -> apiId = id + "+" + apiId.replace("NEW_YEAR_CAKE_", ""); + case "PARTY_HAT_CRAB_ANIMATED" -> apiId = "PARTY_HAT_CRAB_" + apiId.replace("PARTY_HAT_CRAB_ANIMATED_", "") + "_ANIMATED"; + case "CRIMSON_HELMET", "CRIMSON_CHESTPLATE", "CRIMSON_LEGGINGS", "CRIMSON_BOOTS", + "AURORA_HELMET", "AURORA_CHESTPLATE", "AURORA_LEGGINGS", "AURORA_BOOTS", + "TERROR_HELMET", "TERROR_CHESTPLATE", "TERROR_LEGGINGS", "TERROR_BOOTS" -> apiId = id; + case "MIDAS_SWORD", "MIDAS_STAFF" -> apiId = id; + default -> apiId = apiId.replace(":", "-"); + } + return apiId; + } + /** * Gets the bazaar sell price or the lowest bin based on the id of the item stack. * @@ -123,29 +285,29 @@ public final class ItemUtils { * and the {@code right boolean} indicating if the price was based on complete data. */ public static @NotNull DoubleBooleanPair getItemPrice(@NotNull ItemStack stack) { - return getItemPrice(getItemId(stack)); + return getItemPrice(stack.getSkyblockApiId()); } /** - * Gets the bazaar sell price or the lowest bin of the item with the specified id. + * Gets the bazaar sell price or the lowest bin of the item with the specified skyblock api id. * * @return An {@link LongBooleanPair} with the {@code left long} representing the item's price, * and the {@code right boolean} indicating if the price was based on complete data. */ - public static @NotNull DoubleBooleanPair getItemPrice(@Nullable String id) { + public static @NotNull DoubleBooleanPair getItemPrice(@Nullable String skyblockApiId) { JsonObject bazaarPrices = TooltipInfoType.BAZAAR.getData(); JsonObject lowestBinPrices = TooltipInfoType.LOWEST_BINS.getData(); - if (id == null || id.isEmpty() || bazaarPrices == null || lowestBinPrices == null) return DoubleBooleanPair.of(0, false); + if (skyblockApiId == null || skyblockApiId.isEmpty() || bazaarPrices == null || lowestBinPrices == null) return DoubleBooleanPair.of(0, false); - if (bazaarPrices.has(id)) { - JsonElement sellPrice = bazaarPrices.get(id).getAsJsonObject().get("sellPrice"); + if (bazaarPrices.has(skyblockApiId)) { + JsonElement sellPrice = bazaarPrices.get(skyblockApiId).getAsJsonObject().get("sellPrice"); boolean isPriceNull = sellPrice.isJsonNull(); return DoubleBooleanPair.of(isPriceNull ? 0 : sellPrice.getAsDouble(), !isPriceNull); } - if (lowestBinPrices.has(id)) { - return DoubleBooleanPair.of(lowestBinPrices.get(id).getAsDouble(), true); + if (lowestBinPrices.has(skyblockApiId)) { + return DoubleBooleanPair.of(lowestBinPrices.get(skyblockApiId).getAsDouble(), true); } return DoubleBooleanPair.of(0, false); -- cgit From 0817cc6aee383122c3b528805c546f86f1dd9c25 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sun, 28 Jul 2024 14:07:57 +0800 Subject: Add more tests --- .../de/hysky/skyblocker/mixins/ItemStackMixin.java | 2 +- .../skyblock/auction/AuctionBrowserScreen.java | 2 +- .../skyblock/garden/FarmingHudWidget.java | 2 +- .../skyblock/item/tooltip/ItemTooltip.java | 2 +- .../java/de/hysky/skyblocker/utils/ItemUtils.java | 105 ++++++++++++--------- 5 files changed, 63 insertions(+), 50 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java b/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java index 2377f6aa..f7e433f2 100644 --- a/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java +++ b/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java @@ -143,6 +143,6 @@ public abstract class ItemStackMixin implements ComponentHolder, SkyblockerStack if (apiId.startsWith("ISSHINY_")) apiId = id; - return neuName = ItemUtils.getNeuId(id, apiId); + return neuName = ItemUtils.getNeuId((ItemStack) (Object) this, id, apiId); } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/auction/AuctionBrowserScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/auction/AuctionBrowserScreen.java index e16a81fc..bdb384a4 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/auction/AuctionBrowserScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/auction/AuctionBrowserScreen.java @@ -301,7 +301,7 @@ public class AuctionBrowserScreen extends AbstractCustomHypixelGUI parsed); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java index 6dd0c39b..c1d9fe48 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java @@ -63,7 +63,7 @@ public class FarmingHudWidget extends Widget { ItemStack farmingToolStack = client.player.getMainHandStack(); if (farmingToolStack == null) return; String cropItemId = FARMING_TOOLS.get(ItemUtils.getItemId(farmingToolStack)); - ItemStack cropStack = ItemRepository.getItemStack(ItemUtils.getNeuId(cropItemId, cropItemId)); // The cropItemId is being used as the api id in the second parameter because the skyblock id and api id are the same for all crops. + ItemStack cropStack = ItemRepository.getItemStack(ItemUtils.getNeuId(null, cropItemId, cropItemId)); // The cropItemId is being used as the api id in the second parameter because the skyblock id and api id are the same for all crops. String counterText = FarmingHud.counterText(); String counterNumber = FarmingHud.NUMBER_FORMAT.format(FarmingHud.counter()); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/ItemTooltip.java b/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/ItemTooltip.java index 7a8db9f6..74f2e06e 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/ItemTooltip.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/ItemTooltip.java @@ -29,7 +29,7 @@ public class ItemTooltip { */ @Deprecated(since = "1.22") public static String getNeuName(String id, String apiId) { - return ItemUtils.getNeuId(id, apiId); + return ItemUtils.getNeuId(null, id, apiId); } public static void nullWarning() { diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java index 3b671c60..8f3e3ada 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java @@ -73,7 +73,7 @@ public final class ItemUtils { * or an empty {@link NbtCompound} if the itemstack is missing a custom data component */ @SuppressWarnings("deprecation") - public static @NotNull NbtCompound getCustomData(@NotNull ComponentHolder stack) { + public static @NotNull NbtCompound getCustomData(@NotNull ComponentHolder stack) { return stack.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT).getNbt(); } @@ -115,7 +115,7 @@ public final class ItemUtils { * @param stack the item stack to get the UUID from * @return the UUID of the item stack, or an empty string if the item stack does not have a UUID */ - public static @NotNull String getItemUuid(@NotNull ComponentHolder stack) { + public static @NotNull String getItemUuid(@NotNull ComponentHolder stack) { return getCustomData(stack).getString(UUID); } @@ -139,7 +139,7 @@ public final class ItemUtils { // Transformation to API format. //TODO future - remove this and just handle it directly for the NEU id conversion because this whole system is confusing and hard to follow if (customData.contains("is_shiny")) { - return "ISSHINY_" + customDataString; + return "SHINY_" + customDataString; } switch (customDataString) { @@ -246,9 +246,17 @@ public final class ItemUtils { * or an empty string if either id or apiId is null */ @NotNull - public static String getNeuId(String id, String apiId) { + public static String getNeuId(@Nullable ItemStack stack, String id, String apiId) { if (id == null || apiId == null) return ""; switch (id) { + case "ENCHANTED_BOOK" -> { + if (stack == null) { + return ""; + } + NbtCompound customData = ItemUtils.getCustomData(stack); + String enchant = customData.getCompound("enchantments").getKeys().stream().findFirst().orElse(""); + return enchant.toUpperCase(Locale.ROOT) + ";" + customData.getCompound("enchantments").getInt(enchant); + } case "PET" -> { apiId = apiId.replaceAll("LVL_\\d*_", ""); String[] parts = apiId.split("_"); @@ -264,18 +272,23 @@ public final class ItemUtils { .replace("-", ";"); } case "RUNE" -> apiId = apiId.replaceAll("_(?!.*_)", ";"); - case "POTION" -> apiId = ""; - case "ATTRIBUTE_SHARD" -> - apiId = id + "+" + apiId.replace("SHARD-", "").replaceAll("_(?!.*_)", ";"); + case "POTION" -> { + if (stack == null) { + return ""; + } + NbtCompound customData = ItemUtils.getCustomData(stack); + apiId = "POTION_" + customData.getString("potion").toUpperCase(Locale.ROOT) + ";" + customData.getInt("potion_level"); + } + case "ATTRIBUTE_SHARD" -> apiId = "ATTRIBUTE_SHARD"; case "NEW_YEAR_CAKE" -> apiId = id + "+" + apiId.replace("NEW_YEAR_CAKE_", ""); case "PARTY_HAT_CRAB_ANIMATED" -> apiId = "PARTY_HAT_CRAB_" + apiId.replace("PARTY_HAT_CRAB_ANIMATED_", "") + "_ANIMATED"; case "CRIMSON_HELMET", "CRIMSON_CHESTPLATE", "CRIMSON_LEGGINGS", "CRIMSON_BOOTS", "AURORA_HELMET", "AURORA_CHESTPLATE", "AURORA_LEGGINGS", "AURORA_BOOTS", - "TERROR_HELMET", "TERROR_CHESTPLATE", "TERROR_LEGGINGS", "TERROR_BOOTS" -> apiId = id; - case "MIDAS_SWORD", "MIDAS_STAFF" -> apiId = id; + "TERROR_HELMET", "TERROR_CHESTPLATE", "TERROR_LEGGINGS", "TERROR_BOOTS", + "MIDAS_SWORD", "MIDAS_STAFF" -> apiId = id; default -> apiId = apiId.replace(":", "-"); } - return apiId; + return apiId.replace("SHINY_", ""); } /** @@ -345,7 +358,7 @@ public final class ItemUtils { NbtCompound customData = getCustomData(stack); if (customData.isEmpty()) return null; - // TODO Calculate drill durability based on the drill_fuel flag, fuel_tank flag, and hotm level + // TODO Calculate drill durability based on the drill_fuel flag, fuel_tank flag, and hotm level // TODO Cache the max durability and only update the current durability on inventory tick int pickonimbusDurability = customData.getInt("pickonimbus_durability"); @@ -393,22 +406,22 @@ public final class ItemUtils { return null; } - /** - * Gets the first line of the lore that matches the specified pattern, using {@link Matcher#find()}. - * @param pattern the pattern to search for - * @param stack the stack to search the lore of - * @return A {@link Matcher matcher} that contains match results if the pattern was found in the lore, otherwise {@code null}. - */ - @Nullable - public static Matcher getLoreLineIfContainsMatch(ItemStack stack, Pattern pattern) { - Matcher matcher = pattern.matcher(""); - for (Text line : getLore(stack)) { - if (matcher.reset(line.getString()).find()) { - return matcher; - } - } - return null; - } + /** + * Gets the first line of the lore that matches the specified pattern, using {@link Matcher#find()}. + * @param pattern the pattern to search for + * @param stack the stack to search the lore of + * @return A {@link Matcher matcher} that contains match results if the pattern was found in the lore, otherwise {@code null}. + */ + @Nullable + public static Matcher getLoreLineIfContainsMatch(ItemStack stack, Pattern pattern) { + Matcher matcher = pattern.matcher(""); + for (Text line : getLore(stack)) { + if (matcher.reset(line.getString()).find()) { + return matcher; + } + } + return null; + } public static @NotNull List getLore(ItemStack stack) { return stack.getOrDefault(DataComponentTypes.LORE, LoreComponent.DEFAULT).styledLines(); @@ -446,23 +459,23 @@ public final class ItemUtils { } } - /** - * Utility method. - */ - public static @NotNull String getConcatenatedLore(@NotNull ItemStack item) { - return concatenateLore(getLore(item)); - } - - /** - * Concatenates the lore of an item into one string. - * This is useful in case some pattern we're looking for is split into multiple lines, which would make it harder to regex. - */ - public static @NotNull String concatenateLore(@NotNull List lore) { - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < lore.size(); i++) { - stringBuilder.append(lore.get(i).getString()); - if (i != lore.size() - 1) stringBuilder.append(" "); - } - return stringBuilder.toString(); - } + /** + * Utility method. + */ + public static @NotNull String getConcatenatedLore(@NotNull ItemStack item) { + return concatenateLore(getLore(item)); + } + + /** + * Concatenates the lore of an item into one string. + * This is useful in case some pattern we're looking for is split into multiple lines, which would make it harder to regex. + */ + public static @NotNull String concatenateLore(@NotNull List lore) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < lore.size(); i++) { + stringBuilder.append(lore.get(i).getString()); + if (i != lore.size() - 1) stringBuilder.append(" "); + } + return stringBuilder.toString(); + } } -- cgit From c8d39a4d9db48d4d72439037fb187919a876f400 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sun, 28 Jul 2024 16:17:36 +0800 Subject: Refactor item id processing --- .../hysky/skyblocker/injected/SkyblockerStack.java | 8 +- .../de/hysky/skyblocker/mixins/ItemStackMixin.java | 21 ++-- .../de/hysky/skyblocker/skyblock/PetCache.java | 16 ++- .../skyblock/auction/AuctionBrowserScreen.java | 9 +- .../skyblock/garden/FarmingHudWidget.java | 2 +- .../skyblock/item/tooltip/ItemTooltip.java | 6 +- .../java/de/hysky/skyblocker/utils/ItemUtils.java | 125 ++++++++------------- 7 files changed, 76 insertions(+), 111 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/de/hysky/skyblocker/injected/SkyblockerStack.java b/src/main/java/de/hysky/skyblocker/injected/SkyblockerStack.java index 2f54917b..28282b20 100644 --- a/src/main/java/de/hysky/skyblocker/injected/SkyblockerStack.java +++ b/src/main/java/de/hysky/skyblocker/injected/SkyblockerStack.java @@ -1,19 +1,19 @@ package de.hysky.skyblocker.injected; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.NotNull; public interface SkyblockerStack { - @Nullable + @NotNull default String getSkyblockId() { return ""; } - @Nullable + @NotNull default String getSkyblockApiId() { return ""; } - @Nullable + @NotNull default String getNeuName() { return ""; } diff --git a/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java b/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java index f7e433f2..c059100e 100644 --- a/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java +++ b/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java @@ -1,7 +1,6 @@ package de.hysky.skyblocker.mixins; import com.llamalad7.mixinextras.injector.ModifyReturnValue; - import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.injected.SkyblockerStack; import de.hysky.skyblocker.skyblock.profileviewer.ProfileViewerScreen; @@ -14,7 +13,7 @@ import net.minecraft.component.type.ItemEnchantmentsComponent; import net.minecraft.item.ItemStack; import net.minecraft.item.tooltip.TooltipAppender; import net.minecraft.text.Text; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.NotNull; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -120,29 +119,23 @@ public abstract class ItemStackMixin implements ComponentHolder, SkyblockerStack } @Override - @Nullable + @NotNull public String getSkyblockId() { if (skyblockId != null && !skyblockId.isEmpty()) return skyblockId; - return skyblockId = ItemUtils.getSkyblockApiId((ItemStack) (Object) this, true); + return skyblockId = ItemUtils.getItemId(this); } @Override - @Nullable + @NotNull public String getSkyblockApiId() { if (skyblockApiId != null && !skyblockApiId.isEmpty()) return skyblockApiId; - return skyblockApiId = ItemUtils.getSkyblockApiId((ItemStack) (Object) this, false); + return skyblockApiId = ItemUtils.getSkyblockApiId(this); } @Override - @Nullable + @NotNull public String getNeuName() { if (neuName != null && !neuName.isEmpty()) return neuName; - String apiId = getSkyblockApiId(); - String id = getSkyblockId(); - if (apiId == null || id == null) return null; - - if (apiId.startsWith("ISSHINY_")) apiId = id; - - return neuName = ItemUtils.getNeuId((ItemStack) (Object) this, id, apiId); + return neuName = ItemUtils.getNeuId((ItemStack) (Object) this); } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/PetCache.java b/src/main/java/de/hysky/skyblocker/skyblock/PetCache.java index 8ddcd60e..59cae058 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/PetCache.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/PetCache.java @@ -14,6 +14,7 @@ import net.minecraft.client.gui.screen.ingame.GenericContainerScreen; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.screen.slot.Slot; +import org.apache.commons.lang3.ArrayUtils; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @@ -27,7 +28,7 @@ import java.util.concurrent.CompletableFuture; /** * Doesn't work with auto pet right now because thats complicated. - * + *

* Want support? Ask the Admins for a Mod API event or open your pets menu. */ public class PetCache { @@ -134,16 +135,23 @@ public class PetCache { } public record PetInfo(String type, double exp, String tier, Optional uuid, Optional item, Optional skin) { + // TODO: Combine with SkyblockItemRarity + private static final String[] TIER_INDEX = {"COMMON", "UNCOMMON", "RARE", "EPIC", "LEGENDARY", "MYTHIC"}; + public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( Codec.STRING.fieldOf("type").forGetter(PetInfo::type), Codec.DOUBLE.fieldOf("exp").forGetter(PetInfo::exp), Codec.STRING.fieldOf("tier").forGetter(PetInfo::tier), Codec.STRING.optionalFieldOf("uuid").forGetter(PetInfo::uuid), Codec.STRING.optionalFieldOf("heldItem").forGetter(PetInfo::item), - Codec.STRING.optionalFieldOf("skin").forGetter(PetInfo::skin)) - .apply(instance, PetInfo::new)); + Codec.STRING.optionalFieldOf("skin").forGetter(PetInfo::skin) + ).apply(instance, PetInfo::new)); private static final Codec>> SERIALIZATION_CODEC = Codec.unboundedMap(Codec.STRING, Codec.unboundedMap(Codec.STRING, CODEC).xmap(Object2ObjectOpenHashMap::new, Object2ObjectOpenHashMap::new) - ).xmap(Object2ObjectOpenHashMap::new, Object2ObjectOpenHashMap::new); + ).xmap(Object2ObjectOpenHashMap::new, Object2ObjectOpenHashMap::new); + + public int tierIndex() { + return ArrayUtils.indexOf(TIER_INDEX, tier); + } } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/auction/AuctionBrowserScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/auction/AuctionBrowserScreen.java index bdb384a4..ecffab3e 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/auction/AuctionBrowserScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/auction/AuctionBrowserScreen.java @@ -294,14 +294,7 @@ public class AuctionBrowserScreen extends AbstractCustomHypixelGUI parsed); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java index c1d9fe48..adc25a82 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java @@ -63,7 +63,7 @@ public class FarmingHudWidget extends Widget { ItemStack farmingToolStack = client.player.getMainHandStack(); if (farmingToolStack == null) return; String cropItemId = FARMING_TOOLS.get(ItemUtils.getItemId(farmingToolStack)); - ItemStack cropStack = ItemRepository.getItemStack(ItemUtils.getNeuId(null, cropItemId, cropItemId)); // The cropItemId is being used as the api id in the second parameter because the skyblock id and api id are the same for all crops. + ItemStack cropStack = ItemRepository.getItemStack(cropItemId.replace(":", "-")); // Hacky conversion to neu id since ItemUtils.getNeuId requires an item stack. String counterText = FarmingHud.counterText(); String counterNumber = FarmingHud.NUMBER_FORMAT.format(FarmingHud.counter()); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/ItemTooltip.java b/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/ItemTooltip.java index 74f2e06e..c6cb97ab 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/ItemTooltip.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/tooltip/ItemTooltip.java @@ -8,6 +8,7 @@ import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.scheduler.Scheduler; import net.minecraft.client.MinecraftClient; +import net.minecraft.item.ItemStack; import net.minecraft.text.Text; import net.minecraft.util.Formatting; import org.slf4j.Logger; @@ -25,11 +26,12 @@ public class ItemTooltip { private static volatile boolean sentNullWarning = false; /** - * @deprecated Use {@link ItemUtils#getNeuId(String, String)} instead + * @deprecated Use {@link ItemUtils#getNeuId(ItemStack)} instead */ @Deprecated(since = "1.22") public static String getNeuName(String id, String apiId) { - return ItemUtils.getNeuId(null, id, apiId); + LOGGER.error("[Skyblocker Item Tooltip] ItemTooltip.getNeuName is deprecated and will not work. Use ItemStack#getNeuName instead."); + return ""; } public static void nullWarning() { diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java index 8f3e3ada..84a06b24 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java @@ -28,7 +28,6 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.nbt.NbtCompound; -import net.minecraft.nbt.NbtElement; import net.minecraft.registry.Registries; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.text.Text; @@ -83,7 +82,7 @@ public final class ItemUtils { * @param stack the item stack to get the internal name from * @return an optional containing the Skyblock item id of the item stack */ - public static @NotNull Optional getItemIdOptional(@NotNull ItemStack stack) { + public static @NotNull Optional getItemIdOptional(@NotNull ComponentHolder stack) { NbtCompound customData = getCustomData(stack); return customData.contains(ID) ? Optional.of(customData.getString(ID)) : Optional.empty(); } @@ -94,7 +93,7 @@ public final class ItemUtils { * @param stack the item stack to get the internal name from * @return the Skyblock item id of the item stack, or an empty string if the item stack does not have a Skyblock id */ - public static @NotNull String getItemId(@NotNull ItemStack stack) { + public static @NotNull String getItemId(@NotNull ComponentHolder stack) { return getCustomData(stack).getString(ID); } @@ -104,7 +103,7 @@ public final class ItemUtils { * @param stack the item stack to get the UUID from * @return an optional containing the UUID of the item stack */ - public static @NotNull Optional getItemUuidOptional(@NotNull ItemStack stack) { + public static @NotNull Optional getItemUuidOptional(@NotNull ComponentHolder stack) { NbtCompound customData = getCustomData(stack); return customData.contains(UUID) ? Optional.of(customData.getString(UUID)) : Optional.empty(); } @@ -123,32 +122,23 @@ public final class ItemUtils { * Gets the Skyblock api id of the item stack. * @return the Skyblock api id if of the item stack, or null if the item stack does not have a Skyblock id. */ - @Nullable - public static String getSkyblockApiId(@NotNull ItemStack itemStack, boolean itemIdOnly) { + public static @NotNull String getSkyblockApiId(@NotNull ComponentHolder itemStack) { NbtCompound customData = getCustomData(itemStack); - - if (customData == null || !customData.contains(ID, NbtElement.STRING_TYPE)) { - return null; - } - String customDataString = customData.getString(ID); - - if (itemIdOnly) { - return customDataString; - } + String id = customData.getString(ID); // Transformation to API format. //TODO future - remove this and just handle it directly for the NEU id conversion because this whole system is confusing and hard to follow if (customData.contains("is_shiny")) { - return "SHINY_" + customDataString; + return "SHINY_" + id; } - switch (customDataString) { + switch (id) { case "ENCHANTED_BOOK" -> { if (customData.contains("enchantments")) { NbtCompound enchants = customData.getCompound("enchantments"); Optional firstEnchant = enchants.getKeys().stream().findFirst(); String enchant = firstEnchant.orElse(""); - return "ENCHANTMENT_" + enchant.toUpperCase(Locale.ENGLISH) + "_" + enchants.getInt(enchant); + return "ENCHANTMENT_" + enchant.toUpperCase(Locale.ROOT) + "_" + enchants.getInt(enchant); } } @@ -164,46 +154,44 @@ public final class ItemUtils { String extended = customData.contains("extended") ? "_EXTENDED" : ""; String splash = customData.contains("splash") ? "_SPLASH" : ""; if (customData.contains("potion") && customData.contains("potion_level")) { - return (customData.getString("potion") + "_" + customDataString + "_" + customData.getInt("potion_level") - + enhanced + extended + splash).toUpperCase(Locale.ENGLISH); + return (customData.getString("potion") + "_" + id + "_" + customData.getInt("potion_level") + + enhanced + extended + splash).toUpperCase(Locale.ROOT); } } case "RUNE" -> { if (customData.contains("runes")) { NbtCompound runes = customData.getCompound("runes"); - Optional firstRunes = runes.getKeys().stream().findFirst(); - String rune = firstRunes.orElse(""); - return rune.toUpperCase(Locale.ENGLISH) + "_RUNE_" + runes.getInt(rune); + String rune = runes.getKeys().stream().findFirst().orElse(""); + return rune.toUpperCase(Locale.ROOT) + "_RUNE_" + runes.getInt(rune); } } case "ATTRIBUTE_SHARD" -> { if (customData.contains("attributes")) { NbtCompound shards = customData.getCompound("attributes"); - Optional firstShards = shards.getKeys().stream().findFirst(); - String shard = firstShards.orElse(""); - return customDataString + "-" + shard.toUpperCase(Locale.ENGLISH) + "_" + shards.getInt(shard); + String shard = shards.getKeys().stream().findFirst().orElse(""); + return id + "-" + shard.toUpperCase(Locale.ROOT) + "_" + shards.getInt(shard); } } case "NEW_YEAR_CAKE" -> { - return customDataString + "_" + customData.getInt("new_years_cake"); + return id + "_" + customData.getInt("new_years_cake"); } case "PARTY_HAT_CRAB", "PARTY_HAT_CRAB_ANIMATED", "BALLOON_HAT_2024" -> { - return customDataString + "_" + customData.getString("party_hat_color").toUpperCase(Locale.ENGLISH); + return id + "_" + customData.getString("party_hat_color").toUpperCase(Locale.ROOT); } case "PARTY_HAT_SLOTH" -> { - return customDataString + "_" + customData.getString("party_hat_emoji").toUpperCase(Locale.ENGLISH); + return id + "_" + customData.getString("party_hat_emoji").toUpperCase(Locale.ROOT); } case "CRIMSON_HELMET", "CRIMSON_CHESTPLATE", "CRIMSON_LEGGINGS", "CRIMSON_BOOTS" -> { NbtCompound attributes = customData.getCompound("attributes"); if (attributes.contains("magic_find") && attributes.contains("veteran")) { - return customDataString + "-MAGIC_FIND-VETERAN"; + return id + "-MAGIC_FIND-VETERAN"; } } @@ -211,7 +199,7 @@ public final class ItemUtils { NbtCompound attributes = customData.getCompound("attributes"); if (attributes.contains("mana_pool") && attributes.contains("mana_regeneration")) { - return customDataString + "-MANA_POOL-MANA_REGENERATION"; + return id + "-MANA_POOL-MANA_REGENERATION"; } } @@ -219,76 +207,57 @@ public final class ItemUtils { NbtCompound attributes = customData.getCompound("attributes"); if (attributes.contains("lifeline") && attributes.contains("mana_pool")) { - return customDataString + "-LIFELINE-MANA_POOL"; + return id + "-LIFELINE-MANA_POOL"; } } case "MIDAS_SWORD" -> { if (customData.getInt("winning_bid") >= 50000000) { - return customDataString + "_50M"; + return id + "_50M"; } } case "MIDAS_STAFF" -> { if (customData.getInt("winning_bid") >= 100000000) { - return customDataString + "_100M"; + return id + "_100M"; } } } - return customDataString; + return id; } /** * Gets the NEU id from an id and an api id. - * @param id the id of the skyblock item, gotten from {@link ItemUtils#getItemId(ItemStack) ItemUtils#getItemId(ItemStack)} or {@link ItemStack#getSkyblockId() ItemStack#getSkyblockId()} - * @param apiId the api id of the skyblock item, matching the id of the item on the Skyblocker api, gotten from {@link ItemStack#getSkyblockApiId() ItemStack#getSkyblockApiId()} - * @return the NEU id of the skyblock item, matching the id of the item gotten from {@link io.github.moulberry.repo.data.NEUItem#getSkyblockItemId() NEUItem#getSkyblockItemId()} or {@link ItemStack#getNeuName() ItemStack#getNeuName()}, - * or an empty string if either id or apiId is null + * + * @return the NEU id of the skyblock item, matching the id of the item gotten from {@link io.github.moulberry.repo.data.NEUItem#getSkyblockItemId() NEUItem#getSkyblockItemId()} or {@link ItemStack#getNeuName()}, + * or an empty string if stack is null */ - @NotNull - public static String getNeuId(@Nullable ItemStack stack, String id, String apiId) { - if (id == null || apiId == null) return ""; - switch (id) { + public static @NotNull String getNeuId(ItemStack stack) { + if (stack == null) return ""; + String id = stack.getSkyblockId(); + NbtCompound customData = ItemUtils.getCustomData(stack); + return switch (id) { case "ENCHANTED_BOOK" -> { - if (stack == null) { - return ""; - } - NbtCompound customData = ItemUtils.getCustomData(stack); - String enchant = customData.getCompound("enchantments").getKeys().stream().findFirst().orElse(""); - return enchant.toUpperCase(Locale.ROOT) + ";" + customData.getCompound("enchantments").getInt(enchant); + NbtCompound enchantments = customData.getCompound("enchantments"); + String enchant = enchantments.getKeys().stream().findFirst().orElse(""); + yield enchant.toUpperCase(Locale.ROOT) + ";" + enchantments.getInt(enchant); } case "PET" -> { - apiId = apiId.replaceAll("LVL_\\d*_", ""); - String[] parts = apiId.split("_"); - String type = parts[0]; - apiId = apiId.replaceAll(type + "_", ""); - apiId = apiId + "-" + type; - apiId = apiId.replace("UNCOMMON", "1") - .replace("COMMON", "0") - .replace("RARE", "2") - .replace("EPIC", "3") - .replace("LEGENDARY", "4") - .replace("MYTHIC", "5") - .replace("-", ";"); + PetCache.PetInfo petInfo = PetCache.PetInfo.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseString(customData.getString("petInfo"))).getOrThrow(); + yield petInfo.type() + ';' + petInfo.tierIndex(); } - case "RUNE" -> apiId = apiId.replaceAll("_(?!.*_)", ";"); - case "POTION" -> { - if (stack == null) { - return ""; - } - NbtCompound customData = ItemUtils.getCustomData(stack); - api