diff options
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/skyblock')
14 files changed, 180 insertions, 233 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/TeleportOverlay.java b/src/main/java/de/hysky/skyblocker/skyblock/TeleportOverlay.java index e878d108..5ea5513e 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/TeleportOverlay.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/TeleportOverlay.java @@ -2,8 +2,9 @@ package de.hysky.skyblocker.skyblock; import com.mojang.blaze3d.systems.RenderSystem; import de.hysky.skyblocker.config.SkyblockerConfigManager; -import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.skyblock.item.PriceInfoTooltip; +import de.hysky.skyblocker.utils.ItemUtils; +import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.render.RenderHelper; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; @@ -27,7 +28,7 @@ public class TeleportOverlay { if (Utils.isOnSkyblock() && SkyblockerConfigManager.get().general.teleportOverlay.enableTeleportOverlays && client.player != null && client.world != null) { ItemStack heldItem = client.player.getMainHandStack(); String itemId = PriceInfoTooltip.getInternalNameFromNBT(heldItem, true); - NbtCompound nbt = heldItem.getNbt(); + NbtCompound extraAttributes = ItemUtils.getExtraAttributes(heldItem); if (itemId != null) { switch (itemId) { @@ -42,20 +43,20 @@ public class TeleportOverlay { } } case "ASPECT_OF_THE_END", "ASPECT_OF_THE_VOID" -> { - if (SkyblockerConfigManager.get().general.teleportOverlay.enableEtherTransmission && client.options.sneakKey.isPressed() && nbt != null && nbt.getCompound("ExtraAttributes").getInt("ethermerge") == 1) { - render(wrc, nbt, 57); + if (SkyblockerConfigManager.get().general.teleportOverlay.enableEtherTransmission && client.options.sneakKey.isPressed() && extraAttributes != null && extraAttributes.getInt("ethermerge") == 1) { + render(wrc, extraAttributes, 57); } else if (SkyblockerConfigManager.get().general.teleportOverlay.enableInstantTransmission) { - render(wrc, nbt, 8); + render(wrc, extraAttributes, 8); } } case "ETHERWARP_CONDUIT" -> { if (SkyblockerConfigManager.get().general.teleportOverlay.enableEtherTransmission) { - render(wrc, nbt, 57); + render(wrc, extraAttributes, 57); } } case "SINSEEKER_SCYTHE" -> { if (SkyblockerConfigManager.get().general.teleportOverlay.enableSinrecallTransmission) { - render(wrc, nbt, 4); + render(wrc, extraAttributes, 4); } } case "NECRON_BLADE", "ASTRAEA", "HYPERION", "SCYLLA", "VALKYRIE" -> { @@ -71,8 +72,8 @@ public class TeleportOverlay { /** * Renders the teleport overlay with a given base range and the tuned transmission stat. */ - private static void render(WorldRenderContext wrc, NbtCompound nbt, int baseRange) { - render(wrc, nbt != null && nbt.getCompound("ExtraAttributes").contains("tuned_transmission") ? baseRange + nbt.getCompound("ExtraAttributes").getInt("tuned_transmission") : baseRange); + private static void render(WorldRenderContext wrc, NbtCompound extraAttributes, int baseRange) { + render(wrc, extraAttributes != null && extraAttributes.contains("tuned_transmission") ? baseRange + extraAttributes.getInt("tuned_transmission") : baseRange); } /** diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/CompactorDeletorPreview.java b/src/main/java/de/hysky/skyblocker/skyblock/item/CompactorDeletorPreview.java index 2a6551c7..7f5b96b9 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/CompactorDeletorPreview.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/CompactorDeletorPreview.java @@ -1,9 +1,10 @@ package de.hysky.skyblocker.skyblock.item; import de.hysky.skyblocker.mixin.accessor.DrawContextInvoker; +import de.hysky.skyblocker.skyblock.itemlist.ItemRegistry; +import de.hysky.skyblocker.utils.ItemUtils; import it.unimi.dsi.fastutil.ints.IntIntPair; import it.unimi.dsi.fastutil.ints.IntObjectPair; -import de.hysky.skyblocker.skyblock.itemlist.ItemRegistry; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; @@ -39,11 +40,8 @@ public class CompactorDeletorPreview { if (targetIndex == -1) return false; // Get items in compactor or deletor - NbtCompound nbt = stack.getNbt(); - if (nbt == null || !nbt.contains("ExtraAttributes", 10)) { - return false; - } - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); + NbtCompound extraAttributes = ItemUtils.getExtraAttributes(stack); + if (extraAttributes == null) return false; // Get the slots and their items from the nbt, which is in the format personal_compact_<slot_number> or personal_deletor_<slot_number> List<IntObjectPair<ItemStack>> slots = extraAttributes.getKeys().stream().filter(slot -> slot.contains(type.toLowerCase().substring(0, 7))).map(slot -> IntObjectPair.of(Integer.parseInt(slot.substring(17)), ItemRegistry.getItemStack(extraAttributes.getString(slot)))).toList(); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java b/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java index 76042a81..1496c90f 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorDyeColors.java @@ -4,6 +4,7 @@ import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; @@ -12,7 +13,6 @@ import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.command.CommandRegistryAccess; import net.minecraft.item.DyeableItem; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; import net.minecraft.text.Text; public class CustomArmorDyeColors { @@ -32,38 +32,34 @@ public class CustomArmorDyeColors { @SuppressWarnings("SameReturnValue") private static int customizeDyeColor(FabricClientCommandSource source, String hex) { ItemStack heldItem = source.getPlayer().getMainHandStack(); - NbtCompound nbt = (heldItem != null) ? heldItem.getNbt() : null; if (hex != null && !isHexadecimalColor(hex)) { source.sendError(Text.translatable("skyblocker.customDyeColors.invalidHex")); return Command.SINGLE_SUCCESS; } - if (Utils.isOnSkyblock() && heldItem != null) { + if (Utils.isOnSkyblock() && heldItem != null) { if (heldItem.getItem() instanceof DyeableItem) { - if (nbt != null && nbt.contains("ExtraAttributes")) { - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; + String itemUuid = ItemUtils.getItemUuid(heldItem); - if (itemUuid != null) { - Object2IntOpenHashMap<String> customDyeColors = SkyblockerConfigManager.get().general.customDyeColors; + if (!itemUuid.isEmpty()) { + Object2IntOpenHashMap<String> customDyeColors = SkyblockerConfigManager.get().general.customDyeColors; - if (hex == null) { - if (customDyeColors.containsKey(itemUuid)) { - customDyeColors.removeInt(itemUuid); - SkyblockerConfigManager.save(); - source.sendFeedback(Text.translatable("skyblocker.customDyeColors.removed")); - } else { - source.sendFeedback(Text.translatable("skyblocker.customDyeColors.neverHad")); - } - } else { - customDyeColors.put(itemUuid, Integer.decode("0x" + hex.replace("#", "")).intValue()); + if (hex == null) { + if (customDyeColors.containsKey(itemUuid)) { + customDyeColors.removeInt(itemUuid); SkyblockerConfigManager.save(); - source.sendFeedback(Text.translatable("skyblocker.customDyeColors.added")); + source.sendFeedback(Text.translatable("skyblocker.customDyeColors.removed")); + } else { + source.sendFeedback(Text.translatable("skyblocker.customDyeColors.neverHad")); } } else { - source.sendError(Text.translatable("skyblocker.customDyeColors.noItemUuid")); + customDyeColors.put(itemUuid, Integer.decode("0x" + hex.replace("#", "")).intValue()); + SkyblockerConfigManager.save(); + source.sendFeedback(Text.translatable("skyblocker.customDyeColors.added")); } + } else { + source.sendError(Text.translatable("skyblocker.customDyeColors.noItemUuid")); } } else { source.sendError(Text.translatable("skyblocker.customDyeColors.notDyeable")); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorTrims.java b/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorTrims.java index b8fa0797..9242d47b 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorTrims.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/CustomArmorTrims.java @@ -3,9 +3,9 @@ package de.hysky.skyblocker.skyblock.item; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.suggestion.SuggestionProvider; - import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.events.SkyblockEvents; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; import dev.isxander.yacl3.config.v2.api.SerialEntry; import it.unimi.dsi.fastutil.Pair; @@ -93,41 +93,37 @@ public class CustomArmorTrims { @SuppressWarnings("SameReturnValue") private static int customizeTrim(FabricClientCommandSource source, Identifier material, Identifier pattern) { ItemStack heldItem = source.getPlayer().getMainHandStack(); - NbtCompound nbt = (heldItem != null) ? heldItem.getNbt() : null; if (Utils.isOnSkyblock() && heldItem != null) { if (heldItem.getItem() instanceof ArmorItem) { - if (nbt != null && nbt.contains("ExtraAttributes")) { - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; - - if (itemUuid != null) { - Object2ObjectOpenHashMap<String, ArmorTrimId> customArmorTrims = SkyblockerConfigManager.get().general.customArmorTrims; - - if (material == null && pattern == null) { - if (customArmorTrims.containsKey(itemUuid)) { - customArmorTrims.remove(itemUuid); - SkyblockerConfigManager.save(); - source.sendFeedback(Text.translatable("skyblocker.customArmorTrims.removed")); - } else { - source.sendFeedback(Text.translatable("skyblocker.customArmorTrims.neverHad")); - } - } else { - // Ensure that the material & trim are valid - ArmorTrimId trimId = new ArmorTrimId(material, pattern); - if (TRIMS_CACHE.get(trimId) == null) { - source.sendError(Text.translatable("skyblocker.customArmorTrims.invalidMaterialOrPattern")); + String itemUuid = ItemUtils.getItemUuid(heldItem); - return Command.SINGLE_SUCCESS; - } + if (!itemUuid.isEmpty()) { + Object2ObjectOpenHashMap<String, ArmorTrimId> customArmorTrims = SkyblockerConfigManager.get().general.customArmorTrims; - customArmorTrims.put(itemUuid, trimId); + if (material == null && pattern == null) { + if (customArmorTrims.containsKey(itemUuid)) { + customArmorTrims.remove(itemUuid); SkyblockerConfigManager.save(); - source.sendFeedback(Text.translatable("skyblocker.customArmorTrims.added")); + source.sendFeedback(Text.translatable("skyblocker.customArmorTrims.removed")); + } else { + source.sendFeedback(Text.translatable("skyblocker.customArmorTrims.neverHad")); } } else { - source.sendError(Text.translatable("skyblocker.customArmorTrims.noItemUuid")); + // Ensure that the material & trim are valid + ArmorTrimId trimId = new ArmorTrimId(material, pattern); + if (TRIMS_CACHE.get(trimId) == null) { + source.sendError(Text.translatable("skyblocker.customArmorTrims.invalidMaterialOrPattern")); + + return Command.SINGLE_SUCCESS; + } + + customArmorTrims.put(itemUuid, trimId); + SkyblockerConfigManager.save(); + source.sendFeedback(Text.translatable("skyblocker.customArmorTrims.added")); } + } else { + source.sendError(Text.translatable("skyblocker.customArmorTrims.noItemUuid")); } } else { source.sendError(Text.translatable("skyblocker.customArmorTrims.notAnArmorPiece")); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/CustomItemNames.java b/src/main/java/de/hysky/skyblocker/skyblock/item/CustomItemNames.java index 5fbff253..b6213eb6 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/CustomItemNames.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/CustomItemNames.java @@ -3,6 +3,7 @@ package de.hysky.skyblocker.skyblock.item; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; @@ -10,8 +11,6 @@ import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallba import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.command.CommandRegistryAccess; import net.minecraft.command.argument.TextArgumentType; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; import net.minecraft.text.MutableText; import net.minecraft.text.Style; import net.minecraft.text.Text; @@ -32,14 +31,10 @@ public class CustomItemNames { @SuppressWarnings("SameReturnValue") private static int renameItem(FabricClientCommandSource source, Text text) { - ItemStack heldItem = source.getPlayer().getMainHandStack(); - NbtCompound nbt = (heldItem != null) ? heldItem.getNbt() : null; + if (Utils.isOnSkyblock()) { + String itemUuid = ItemUtils.getItemUuid(source.getPlayer().getMainHandStack()); - if (Utils.isOnSkyblock() && nbt != null && nbt.contains("ExtraAttributes")) { - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; - - if (itemUuid != null) { + if (!itemUuid.isEmpty()) { Object2ObjectOpenHashMap<String, Text> customItemNames = SkyblockerConfigManager.get().general.customItemNames; if (text == null) { @@ -56,7 +51,7 @@ public class CustomItemNames { //Set italic to false if it hasn't been changed (or was already false) Style currentStyle = text.getStyle(); - ((MutableText) text).setStyle(currentStyle.withItalic((currentStyle.isItalic() ? true : false))); + ((MutableText) text).setStyle(currentStyle.withItalic(currentStyle.isItalic())); customItemNames.put(itemUuid, text); SkyblockerConfigManager.save(); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java index 9c1fa6ad..f1c9239c 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java @@ -34,7 +34,7 @@ public class ItemCooldowns { if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) return; String usedItemId = ItemUtils.getItemId(player.getMainHandStack()); - if (usedItemId == null) return; + if (usedItemId.isEmpty()) return; if (state.isIn(BlockTags.LOGS)) { if (usedItemId.equals(JUNGLE_AXE_ID)) { @@ -53,7 +53,7 @@ public class ItemCooldowns { if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) return TypedActionResult.pass(ItemStack.EMPTY); String usedItemId = ItemUtils.getItemId(player.getMainHandStack()); - if (usedItemId != null && usedItemId.equals(GRAPPLING_HOOK_ID) && player.fishHook != null) { + if (usedItemId.equals(GRAPPLING_HOOK_ID) && player.fishHook != null) { if (!isOnCooldown(GRAPPLING_HOOK_ID) && !isWearingBatArmor(player)) { ITEM_COOLDOWNS.put(GRAPPLING_HOOK_ID, new CooldownEntry(2000)); } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java index d73e1545..ff88ef8d 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java @@ -2,8 +2,8 @@ package de.hysky.skyblocker.skyblock.item; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; - import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; @@ -11,56 +11,44 @@ import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallba import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.command.CommandRegistryAccess; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; import net.minecraft.text.Text; public class ItemProtection { - + public static void init() { ClientCommandRegistrationCallback.EVENT.register(ItemProtection::registerCommand); } public static boolean isItemProtected(ItemStack stack) { - if (stack == null || stack.isEmpty()) return false; - - NbtCompound nbt = stack.getNbt(); - - if (nbt != null && nbt.contains("ExtraAttributes")) { - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : ""; - - return SkyblockerConfigManager.get().general.protectedItems.contains(itemUuid); - } - - return false; + if (stack == null) return false; + String itemUuid = ItemUtils.getItemUuid(stack); + return SkyblockerConfigManager.get().general.protectedItems.contains(itemUuid); } - + private static void registerCommand(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) { dispatcher.register(ClientCommandManager.literal("skyblocker") .then(ClientCommandManager.literal("protectItem") .executes(context -> protectMyItem(context.getSource())))); } - + private static int protectMyItem(FabricClientCommandSource source) { ItemStack heldItem = source.getPlayer().getMainHandStack(); - NbtCompound nbt = (heldItem != null) ? heldItem.getNbt() : null; - - if (Utils.isOnSkyblock() && nbt != null && nbt.contains("ExtraAttributes")) { - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; - - if (itemUuid != null) { + + if (Utils.isOnSkyblock()) { + String itemUuid = ItemUtils.getItemUuid(heldItem); + + if (!itemUuid.isEmpty()) { ObjectOpenHashSet<String> protectedItems = SkyblockerConfigManager.get().general.protectedItems; - + if (!protectedItems.contains(itemUuid)) { protectedItems.add(itemUuid); SkyblockerConfigManager.save(); - + source.sendFeedback(Text.translatable("skyblocker.itemProtection.added", heldItem.getName())); } else { protectedItems.remove(itemUuid); SkyblockerConfigManager.save(); - + source.sendFeedback(Text.translatable("skyblocker.itemProtection.removed", heldItem.getName())); } } else { @@ -69,7 +57,7 @@ public class ItemProtection { } else { source.sendFeedback(Text.translatable("skyblocker.itemProtection.unableToProtect")); } - + return Command.SINGLE_SUCCESS; } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemRarityBackgrounds.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemRarityBackgrounds.java index 0af64bd9..9e1df2bb 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemRarityBackgrounds.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemRarityBackgrounds.java @@ -1,15 +1,10 @@ package de.hysky.skyblocker.skyblock.item; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.function.Supplier; - import com.google.common.collect.ImmutableMap; import com.mojang.blaze3d.systems.RenderSystem; - import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.scheduler.Scheduler; import it.unimi.dsi.fastutil.ints.Int2ReferenceOpenHashMap; @@ -20,10 +15,14 @@ import net.minecraft.client.item.TooltipContext; import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.client.texture.Sprite; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; import net.minecraft.text.Text; import net.minecraft.util.Identifier; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; + public class ItemRarityBackgrounds { private static final Identifier RARITY_BG_TEX = new Identifier(SkyblockerMod.NAMESPACE, "item_rarity_background"); private static final Supplier<Sprite> SPRITE = () -> MinecraftClient.getInstance().getGuiAtlasManager().getSprite(RARITY_BG_TEX); @@ -67,17 +66,11 @@ public class ItemRarityBackgrounds { private static SkyblockItemRarity getItemRarity(ItemStack stack, ClientPlayerEntity player) { if (stack == null || stack.isEmpty()) return null; - - int hashCode = 0; - NbtCompound nbt = stack.getNbt(); - - if (nbt != null && nbt.contains("ExtraAttributes")) { - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - String itemUuid = extraAttributes.getString("uuid"); - - //If the item has an uuid, then use the hash code of the uuid otherwise use the identity hash code of the stack - hashCode = itemUuid.isEmpty() ? System.identityHashCode(stack) : itemUuid.hashCode(); - } + + String itemUuid = ItemUtils.getItemUuid(stack); + + //If the item has an uuid, then use the hash code of the uuid otherwise use the identity hash code of the stack + int hashCode = itemUuid.isEmpty() ? System.identityHashCode(stack) : itemUuid.hashCode(); if (CACHE.containsKey(hashCode)) return CACHE.get(hashCode); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/PriceInfoTooltip.java b/src/main/java/de/hysky/skyblocker/skyblock/item/PriceInfoTooltip.java index 05767558..5ef12e74 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/PriceInfoTooltip.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/PriceInfoTooltip.java @@ -2,10 +2,10 @@ package de.hysky.skyblocker.skyblock.item; import com.google.gson.Gson; import com.google.gson.JsonObject; - import de.hysky.skyblocker.config.SkyblockerConfig; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.utils.Http; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.scheduler.Scheduler; import net.minecraft.client.MinecraftClient; @@ -21,13 +21,7 @@ import org.slf4j.LoggerFactory; import java.net.http.HttpHeaders; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.concurrent.CompletableFuture; public class PriceInfoTooltip { @@ -55,7 +49,7 @@ public class PriceInfoTooltip { String neuName = name; if (name == null || internalID == null) return; - if(name.startsWith("ISSHINY_")){ + if (name.startsWith("ISSHINY_")) { name = "SHINY_" + internalID; neuName = internalID; } @@ -207,11 +201,6 @@ public class PriceInfoTooltip { } } - public static NbtCompound getItemNBT(ItemStack stack) { - if (stack == null) return null; - return stack.getNbt(); - } - /** * this method converts the "timestamp" variable into the same date format as Hypixel represents it in the museum. * Currently, there are two types of timestamps the legacy which is built like this @@ -227,21 +216,17 @@ public class PriceInfoTooltip { * @return if the item have a "Timestamp" it will be shown formated on the tooltip */ public static String getTimestamp(ItemStack stack) { - NbtCompound tag = getItemNBT(stack); - - if (tag != null && tag.contains("ExtraAttributes", 10)) { - NbtCompound ea = tag.getCompound("ExtraAttributes"); + NbtCompound ea = ItemUtils.getExtraAttributes(stack); - if (ea.contains("timestamp", 8)) { - SimpleDateFormat nbtFormat = new SimpleDateFormat("MM/dd/yy"); + if (ea != null && ea.contains("timestamp", 8)) { + SimpleDateFormat nbtFormat = new SimpleDateFormat("MM/dd/yy"); - try { - Date date = nbtFormat.parse(ea.getString("timestamp")); - SimpleDateFormat skyblockerFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH); - return skyblockerFormat.format(date); - } catch (ParseException e) { - LOGGER.warn("[Skyblocker-tooltip] getTimestamp", e); - } + try { + Date date = nbtFormat.parse(ea.getString("timestamp")); + SimpleDateFormat skyblockerFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH); + return skyblockerFormat.format(date); + } catch (ParseException e) { + LOGGER.warn("[Skyblocker-tooltip] getTimestamp", e); } } @@ -249,23 +234,19 @@ public class PriceInfoTooltip { } public static String getInternalNameFromNBT(ItemStack stack, boolean internalIDOnly) { - NbtCompound tag = getItemNBT(stack); - if (tag == null || !tag.contains("ExtraAttributes", 10)) { - return null; - } - NbtCompound ea = tag.getCompound("ExtraAttributes"); + NbtCompound ea = ItemUtils.getExtraAttributes(stack); - if (!ea.contains("id", 8)) { + if (ea == null || !ea.contains(ItemUtils.ID, 8)) { return null; } - String internalName = ea.getString("id"); + String internalName = ea.getString(ItemUtils.ID); if (internalIDOnly) { return internalName; } // Transformation to API format. - if (ea.contains("is_shiny")){ + if (ea.contains("is_shiny")) { return "ISSHINY_" + internalName; } @@ -364,7 +345,7 @@ public class PriceInfoTooltip { List<CompletableFuture<Void>> futureList = new ArrayList<>(); if (SkyblockerConfigManager.get().general.itemTooltip.enableAvgBIN) { - SkyblockerConfig.Average type = SkyblockerConfigManager.get().general.itemTooltip.avg; + SkyblockerConfig.Average type = SkyblockerConfigManager.get().general.itemTooltip.avg; if (type == SkyblockerConfig.Average.BOTH || oneDayAvgPricesJson == null || threeDayAvgPricesJson == null || minute % 5 == 0) { futureList.add(CompletableFuture.runAsync(() -> { @@ -401,33 +382,33 @@ public class PriceInfoTooltip { private static JsonObject downloadPrices(String type) { try { String url = apiAddresses.get(type); - + if (type.equals("npc") || type.equals("museum") || type.equals("motes")) { HttpHeaders headers = Http.sendHeadRequest(url); long combinedHash = Http.getEtag(headers).hashCode() + Http.getLastModified(headers).hashCode(); - + switch (type) { case "npc": if (npcHash == combinedHash) return npcPricesJson; else npcHash = combinedHash; case "museum": if (museumHash == combinedHash) return isMuseumJson; else museumHash = combinedHash; case "motes": if (motesHash == combinedHash) return motesPricesJson; else motesHash = combinedHash; } } - + String apiResponse = Http.sendGetRequest(url); - + return new Gson().fromJson(apiResponse, JsonObject.class); } catch (Exception e) { LOGGER.warn("[Skyblocker] Failed to download " + type + " prices!", e); return null; } } - + public static JsonObject getBazaarPrices() { - return bazaarPricesJson; + return bazaarPricesJson; } - + public static JsonObject getLBINPrices() { - return lowestPricesJson; + return lowestPricesJson; } static { diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java b/src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java index 3ab478d0..d4e6a0df 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java @@ -1,13 +1,12 @@ package de.hysky.skyblocker.skyblock.item; -import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.skyblock.itemlist.ItemRegistry; +import de.hysky.skyblocker.utils.ItemUtils; +import de.hysky.skyblocker.utils.Utils; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; import net.minecraft.screen.slot.Slot; import net.minecraft.text.Text; import net.minecraft.util.Util; @@ -31,11 +30,7 @@ public class WikiLookup { public static String getSkyblockId(Slot slot) { //Grabbing the skyblock NBT data - ItemStack selectedStack = slot.getStack(); - NbtCompound nbt = selectedStack.getSubNbt("ExtraAttributes"); - if (nbt != null) { - id = nbt.getString("id"); - } + ItemUtils.getItemIdOptional(slot.getStack()).ifPresent(newId -> id = newId); return id; } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRegistry.java b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRegistry.java index edfeccc0..b958a85d 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRegistry.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRegistry.java @@ -2,6 +2,7 @@ package de.hysky.skyblocker.skyblock.itemlist; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.NEURepo; import net.minecraft.client.MinecraftClient; import net.minecraft.item.ItemStack; @@ -65,9 +66,9 @@ public class ItemRegistry { } items.sort((lhs, rhs) -> { - String lhsInternalName = getInternalName(lhs); + String lhsInternalName = ItemUtils.getItemId(lhs); String lhsFamilyName = lhsInternalName.replaceAll(".\\d+$", ""); - String rhsInternalName = getInternalName(rhs); + String rhsInternalName = ItemUtils.getItemId(rhs); String rhsFamilyName = rhsInternalName.replaceAll(".\\d+$", ""); if (lhsFamilyName.equals(rhsFamilyName)) { if (lhsInternalName.length() != rhsInternalName.length()) @@ -100,14 +101,16 @@ public class ItemRegistry { public static List<SkyblockCraftingRecipe> getRecipes(String internalName) { List<SkyblockCraftingRecipe> result = new ArrayList<>(); + for (SkyblockCraftingRecipe recipe : recipes) { + if (ItemUtils.getItemId(recipe.result).equals(internalName)) result.add(recipe); + } for (SkyblockCraftingRecipe recipe : recipes) - if (getInternalName(recipe.result).equals(internalName)) result.add(recipe); - for (SkyblockCraftingRecipe recipe : recipes) - for (ItemStack ingredient : recipe.grid) - if (!ingredient.getItem().equals(Items.AIR) && getInternalName(ingredient).equals(internalName)) { + for (ItemStack ingredient : recipe.grid) { + if (!ingredient.getItem().equals(Items.AIR) && ItemUtils.getItemId(ingredient).equals(internalName)) { result.add(recipe); break; } + } return result; } @@ -119,17 +122,6 @@ public class ItemRegistry { return items.stream(); } - /** - * Get Internal name of an ItemStack - * - * @param itemStack ItemStack to get internal name from - * @return internal name of the given ItemStack - */ - public static String getInternalName(ItemStack itemStack) { - if (itemStack.getNbt() == null) return ""; - return itemStack.getNbt().getCompound("ExtraAttributes").getString("id"); - } - public static ItemStack getItemStack(String internalName) { return itemsMap.get(internalName); } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemStackBuilder.java b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemStackBuilder.java index 24146c64..4a6d3474 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemStackBuilder.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemStackBuilder.java @@ -4,6 +4,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.NEURepo; import net.minecraft.item.FireworkRocketItem; import net.minecraft.item.ItemStack; @@ -45,8 +46,8 @@ public class ItemStackBuilder { root.put("tag", tag); NbtCompound extra = new NbtCompound(); - tag.put("ExtraAttributes", extra); - extra.put("id", NbtString.of(internalName)); + tag.put(ItemUtils.EXTRA_ATTRIBUTES, extra); + extra.put(ItemUtils.ID, NbtString.of(internalName)); NbtCompound display = new NbtCompound(); tag.put("display", display); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/SearchResultsWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/SearchResultsWidget.java index eedf695e..8a266d65 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/SearchResultsWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/SearchResultsWidget.java @@ -1,6 +1,7 @@ package de.hysky.skyblocker.skyblock.itemlist; import com.mojang.blaze3d.systems.RenderSystem; +import de.hysky.skyblocker.utils.ItemUtils; import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; @@ -11,6 +12,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.text.Text; import net.minecraft.util.Formatting; import net.minecraft.util.Identifier; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; @@ -18,8 +20,6 @@ import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.jetbrains.annotations.Nullable; - public class SearchResultsWidget implements Drawable { private static final ButtonTextures PAGE_FORWARD_TEXTURES = new ButtonTextures(new Identifier("recipe_book/page_forward"), new Identifier("recipe_book/page_forward_highlighted")); private static final ButtonTextures PAGE_BACKWARD_TEXTURES = new ButtonTextures(new Identifier("recipe_book/page_backward"), new Identifier("recipe_book/page_backward_highlighted")); @@ -198,10 +198,10 @@ public class SearchResultsWidget implements Drawable { public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { for (ResultButtonWidget button : resultButtons) if (button.mouseClicked(mouseX, mouseY, mouseButton)) { - if (button.itemStack.getNbt() == null) { + String internalName = ItemUtils.getItemId(button.itemStack); + if (internalName.isEmpty()) { continue; } - String internalName = button.itemStack.getNbt().getCompound("ExtraAttributes").getString("id"); List<SkyblockCraftingRecipe> recipes = ItemRegistry.getRecipes(internalName); if (!recipes.isEmpty()) { this.recipeResults = recipes; diff --git a/src/main/java/de/hysky/skyblocker/skyblock/quicknav/QuickNavButton.java b/src/main/java/de/hysky/skyblocker/skyblock/quicknav/QuickNavButton.java index 5e76427a..eb744389 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/quicknav/QuickNavButton.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/quicknav/QuickNavButton.java @@ -12,22 +12,32 @@ import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.gui.widget.ClickableWidget; -import net.minecraft.client.util.math.MatrixStack; import net.minecraft.item.ItemStack; import net.minecraft.text.Text; import net.minecraft.util.Identifier; @Environment(value=EnvType.CLIENT) public class QuickNavButton extends ClickableWidget { - private static final Identifier BUTTON_TEXTURE = new Identifier("textures/gui/container/creative_inventory/tabs.png"); - private final int index; private boolean toggled; - private int u; - private int v; private final String command; private final ItemStack icon; + /** + * Checks if the current tab is a top tab based on its index. + * @return true if the index is less than 6, false otherwise. + */ + private boolean isTopTab() { + return index < 6; + } + + /** + * Constructs a new QuickNavButton with the given parameters. + * @param index the index of the button. + * @param toggled the toggled state of the button. + * @param command the command to execute when the button is clicked. + * @param icon the icon to display on the button. + */ public QuickNavButton(int index, boolean toggled, String command, ItemStack icon) { super(0, 0, 26, 32, Text.empty()); this.index = index; @@ -45,11 +55,15 @@ public class QuickNavButton extends ClickableWidget { if (h > 166) --h; // why is this even a thing this.setX(x + this.index % 6 * 26 + 4); this.setY(this.index < 6 ? y - 26 : y + h - 4); - this.u = 26; - this.v = (index < 6 ? 0 : 64) + (toggled ? 32 : 0); } } + /** + * Handles click events. If the button is not currently toggled, + * it sets the toggled state to true and sends a message with the command after cooldown. + * @param mouseX the x-coordinate of the mouse click + * @param mouseY the y-coordinate of the mouse click + */ @Override public void onClick(double mouseX, double mouseY) { if (!this.toggled) { @@ -59,43 +73,40 @@ public class QuickNavButton extends ClickableWidget { } } + /** + * Renders the button on screen. This includes both its texture and its icon. + * The method first updates the coordinates of the button, + * then calculates appropriate values for rendering based on its current state, + * and finally draws both the background and icon of the button on screen. + * @param context the context in which to render the button + * @param mouseX the x-coordinate of the mouse cursor + * @param mouseY the y-coordinate of the mouse cursor + */ @Override public void renderButton(DrawContext context, int mouseX, int mouseY, float delta) { this.updateCoordinates(); - MatrixStack matrices = context.getMatrices(); RenderSystem.disableDepthTest(); - // render button background - if (!this.toggled) { - if (this.index >= 6) - // this.drawTexture(matrices, this.x, this.y + 4, this.u, this.v + 4, this.width, this.height - 4); - context.drawTexture(BUTTON_TEXTURE, this.getX(), this.getY() + 4, this.u, this.v + 4, this.width, this.height - 4); - else - // this.drawTexture(matrices, this.x, this.y, this.u, this.v, this.width, this.height - 4); - context.drawTexture(BUTTON_TEXTURE, this.getX(), this.getY() - 2, this.u, this.v, this.width, this.height - 4); - // } else this.drawTexture(matrices, this.x, this.y, this.u, this.v, this.width, this.height); - } else { - matrices.push(); - //Move the top buttons 2 pixels up if they're selected - if (this.index < 6) matrices.translate(0f, -2f, 0f); - context.drawTexture(BUTTON_TEXTURE, this.getX(), this.getY(), this.u, this.v, this.width, this.height); - matrices.pop(); - } - // render button icon - if (!this.toggled) { - if (this.index >= 6) - // CLIENT.getItemRenderer().renderInGui(this.icon,this.x + 6, this.y + 6); - context.drawItem(this.icon,this.getX() + 5, this.getY() + 6); - else - // CLIENT.getItemRenderer().renderInGui(this.icon,this.x + 6, this.y + 9); - context.drawItem(this.icon,this.getX() + 5, this.getY() + 7); + + // Construct the texture identifier based on the index and toggled state + String tabType = isTopTab() ? "top" : "bottom"; + Identifier BUTTON_TEXTURES = new Identifier("container/creative_inventory/tab_" + tabType + + (toggled ? "_selected_" : "_unselected_") + 2); + + // Render the button texture + int y = this.getY(); + if (this.toggled) { + if (this.index < 6) y -= 2; } else { - if (this.index >= 6) - // CLIENT.getItemRenderer().renderInGui(this.icon,this.x + 6, this.y + 9); - context.drawItem(this.icon,this.getX() + 5, this.getY() + 9); - else - // CLIENT.getItemRenderer().renderInGui(this.icon,this.x + 6, this.y + 6); - context.drawItem(this.icon,this.getX() + 5, this.getY() + 6); + y += (this.index >= 6) ? 4 : -2; } + int height = this.height - ((this.toggled ) ? 0 : 4); + + context.drawGuiTexture(BUTTON_TEXTURES, this.getX(), y, this.width, height); + + // Render the button icon + int yOffset = !this.toggled && this.index < 6 ? 1 : 0; + context.drawItem(this.icon, this.getX() + 5, this.getY() + 6 + yOffset); + RenderSystem.enableDepthTest(); } |