diff options
24 files changed, 310 insertions, 325 deletions
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index eb1c4b81..13f9e756 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -29,12 +29,15 @@ body: attributes: label: Log output description: 'Please upload a client log file by dragging and dropping it into this section. Usually this is found at `.minecraft/logs/latest.log`. If you are not sure how to find `.minecraft`, please see this article: https://minecraft.fandom.com/wiki/.minecraft#Locating_.minecraft. Please do not upload a compressed (`.log.gz`) file, they are very difficult for us to read when viewing issue reports. The log file instantly tells us important information like the versions of any other installed mods or if there are errors so it is very very important that you include it in your report.' - - type: input + - type: dropdown id: minecraft-version attributes: label: Minecraft Version description: What version of Minecraft are you running? If you do not know what version you are using, look in the bottom left corner of the main menu in game. - placeholder: ex. Minecraft 1.20.1 + options: + - 1.20.2 + - 1.20.1 + - 1.20.0 validations: required: true - type: input diff --git a/.github/ISSUE_TEMPLATE/crash_report.yml b/.github/ISSUE_TEMPLATE/crash_report.yml index d3433bd0..31828e76 100644 --- a/.github/ISSUE_TEMPLATE/crash_report.yml +++ b/.github/ISSUE_TEMPLATE/crash_report.yml @@ -10,12 +10,15 @@ body: Before continuing to make the issue, please make sure there are no similar issues on [the issue tracker](https://github.com/SkyblockerMod/Skyblocker/issues). If there are, consider contributing your information in there instead! Also, make sure you are using the latest version of the mod! If not, try updating to see if it resolves your issue. - - type: input + - type: dropdown id: minecraft-version attributes: label: Minecraft Version description: What version of Minecraft are you running? If you do not know what version you are using, look in the bottom left corner of the main menu in game. - placeholder: ex. Minecraft 1.20.1 + options: + - 1.20.2 + - 1.20.1 + - 1.20.0 validations: required: true - type: input diff --git a/src/main/java/de/hysky/skyblocker/compatibility/emi/SkyblockEmiRecipe.java b/src/main/java/de/hysky/skyblocker/compatibility/emi/SkyblockEmiRecipe.java index 5875327d..191da283 100644 --- a/src/main/java/de/hysky/skyblocker/compatibility/emi/SkyblockEmiRecipe.java +++ b/src/main/java/de/hysky/skyblocker/compatibility/emi/SkyblockEmiRecipe.java @@ -1,7 +1,7 @@ package de.hysky.skyblocker.compatibility.emi; -import de.hysky.skyblocker.skyblock.itemlist.ItemRegistry; import de.hysky.skyblocker.skyblock.itemlist.SkyblockCraftingRecipe; +import de.hysky.skyblocker.utils.ItemUtils; import dev.emi.emi.api.recipe.EmiCraftingRecipe; import dev.emi.emi.api.recipe.EmiRecipeCategory; import dev.emi.emi.api.stack.Comparison; @@ -16,7 +16,7 @@ public class SkyblockEmiRecipe extends EmiCraftingRecipe { private final String craftText; public SkyblockEmiRecipe(SkyblockCraftingRecipe recipe) { - super(recipe.getGrid().stream().map(EmiStack::of).map(EmiIngredient.class::cast).toList(), EmiStack.of(recipe.getResult()).comparison(Comparison.compareNbt()), Identifier.of("skyblock", ItemRegistry.getInternalName(recipe.getResult()).toLowerCase().replace(';', '_'))); + super(recipe.getGrid().stream().map(EmiStack::of).map(EmiIngredient.class::cast).toList(), EmiStack.of(recipe.getResult()).comparison(Comparison.compareNbt()), Identifier.of("skyblock", ItemUtils.getItemId(recipe.getResult()).toLowerCase().replace(';', '_'))); this.craftText = recipe.getCraftText(); } diff --git a/src/main/java/de/hysky/skyblocker/compatibility/rei/SkyblockCraftingDisplayGenerator.java b/src/main/java/de/hysky/skyblocker/compatibility/rei/SkyblockCraftingDisplayGenerator.java index 8db617dc..60e39b79 100644 --- a/src/main/java/de/hysky/skyblocker/compatibility/rei/SkyblockCraftingDisplayGenerator.java +++ b/src/main/java/de/hysky/skyblocker/compatibility/rei/SkyblockCraftingDisplayGenerator.java @@ -2,6 +2,7 @@ package de.hysky.skyblocker.compatibility.rei; import de.hysky.skyblocker.skyblock.itemlist.ItemRegistry; import de.hysky.skyblocker.skyblock.itemlist.SkyblockCraftingRecipe; +import de.hysky.skyblocker.utils.ItemUtils; import me.shedaniel.rei.api.client.registry.display.DynamicDisplayGenerator; import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.entry.EntryStack; @@ -19,7 +20,11 @@ public class SkyblockCraftingDisplayGenerator implements DynamicDisplayGenerator if (!(entry.getValue() instanceof ItemStack)) return Optional.empty(); EntryStack<ItemStack> inputItem = EntryStacks.of((ItemStack) entry.getValue()); List<SkyblockCraftingRecipe> filteredRecipes = ItemRegistry.getRecipesStream() - .filter(recipe -> ItemRegistry.getInternalName(recipe.getResult()).equals(ItemRegistry.getInternalName(inputItem.getValue()))) + .filter(recipe -> { + ItemStack itemStack = inputItem.getValue(); + ItemStack itemStack1 = recipe.getResult(); + return ItemUtils.getItemId(itemStack1).equals(ItemUtils.getItemId(itemStack)); + }) .toList(); return Optional.of(generateDisplays(filteredRecipes)); @@ -32,8 +37,10 @@ public class SkyblockCraftingDisplayGenerator implements DynamicDisplayGenerator List<SkyblockCraftingRecipe> filteredRecipes = ItemRegistry.getRecipesStream() .filter(recipe -> { for (ItemStack item : recipe.getGrid()) { - if(!ItemRegistry.getInternalName(item).isEmpty() && ItemRegistry.getInternalName(item).equals(ItemRegistry.getInternalName(inputItem.getValue()))) - return true; + if(!ItemUtils.getItemId(item).isEmpty()) { + ItemStack itemStack = inputItem.getValue(); + if (ItemUtils.getItemId(item).equals(ItemUtils.getItemId(itemStack))) return true; + } } return false; }) diff --git a/src/main/java/de/hysky/skyblocker/mixin/ArmorTrimMixin.java b/src/main/java/de/hysky/skyblocker/mixin/ArmorTrimMixin.java index 02d75409..b9bdb523 100644 --- a/src/main/java/de/hysky/skyblocker/mixin/ArmorTrimMixin.java +++ b/src/main/java/de/hysky/skyblocker/mixin/ArmorTrimMixin.java @@ -2,13 +2,13 @@ package de.hysky.skyblocker.mixin; import com.llamalad7.mixinextras.injector.ModifyReturnValue; import com.llamalad7.mixinextras.sugar.Local; -import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.skyblock.item.CustomArmorTrims; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import net.minecraft.item.ItemStack; import net.minecraft.item.trim.ArmorTrim; -import net.minecraft.nbt.NbtCompound; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -19,12 +19,9 @@ public class ArmorTrimMixin { @ModifyReturnValue(method = "getTrim", at = @At("RETURN")) private static Optional<ArmorTrim> skyblocker$customArmorTrims(@SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<ArmorTrim> original, @Local ItemStack stack) { - NbtCompound nbt = stack.getNbt(); - - if (Utils.isOnSkyblock() && nbt != null && nbt.contains("ExtraAttributes")) { + if (Utils.isOnSkyblock()) { Object2ObjectOpenHashMap<String, CustomArmorTrims.ArmorTrimId> customTrims = SkyblockerConfigManager.get().general.customArmorTrims; - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; + String itemUuid = ItemUtils.getItemUuid(stack); if (customTrims.containsKey(itemUuid)) { CustomArmorTrims.ArmorTrimId trimKey = customTrims.get(itemUuid); diff --git a/src/main/java/de/hysky/skyblocker/mixin/DrawContextMixin.java b/src/main/java/de/hysky/skyblocker/mixin/DrawContextMixin.java index 41b8e985..10666874 100644 --- a/src/main/java/de/hysky/skyblocker/mixin/DrawContextMixin.java +++ b/src/main/java/de/hysky/skyblocker/mixin/DrawContextMixin.java @@ -3,12 +3,13 @@ package de.hysky.skyblocker.mixin; import com.llamalad7.mixinextras.injector.ModifyExpressionValue; import com.llamalad7.mixinextras.sugar.Local; import com.llamalad7.mixinextras.sugar.ref.LocalRef; -import dev.cbyrne.betterinject.annotations.Arg; -import dev.cbyrne.betterinject.annotations.Inject; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.skyblock.item.AttributeShards; import de.hysky.skyblocker.skyblock.item.ItemCooldowns; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; +import dev.cbyrne.betterinject.annotations.Arg; +import dev.cbyrne.betterinject.annotations.Inject; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.util.math.MatrixStack; @@ -32,40 +33,38 @@ public abstract class DrawContextMixin { @Inject(method = "drawItemInSlot(Lnet/minecraft/client/font/TextRenderer;Lnet/minecraft/item/ItemStack;IILjava/lang/String;)V", at = @At("HEAD")) private void skyblocker$renderAttributeShardDisplay(@Arg TextRenderer textRenderer, @Arg ItemStack stack, @Arg(ordinal = 0) int x, @Arg(ordinal = 1) int y, @Local(argsOnly = true) LocalRef<String> countOverride) { - if (!SkyblockerConfigManager.get().general.itemInfoDisplay.attributeShardInfo) return; - - NbtCompound nbt = stack.getNbt(); + if (!SkyblockerConfigManager.get().general.itemInfoDisplay.attributeShardInfo) return; - if (Utils.isOnSkyblock() && nbt != null && nbt.contains("ExtraAttributes")) { - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); + if (Utils.isOnSkyblock()) { + NbtCompound extraAttributes = ItemUtils.getExtraAttributes(stack); - if (extraAttributes.getString("id").equals("ATTRIBUTE_SHARD")) { - NbtCompound attributesTag = extraAttributes.getCompound("attributes"); - String[] attributes = attributesTag.getKeys().toArray(String[]::new); + if (extraAttributes != null && extraAttributes.getString(ItemUtils.ID).equals("ATTRIBUTE_SHARD")) { + NbtCompound attributesTag = extraAttributes.getCompound("attributes"); + String[] attributes = attributesTag.getKeys().toArray(String[]::new); - if (attributes.length != 0) { - String attributeId = attributes[0]; - int attributeLevel = attributesTag.getInt(attributeId); + if (attributes.length != 0) { + String attributeId = attributes[0]; + int attributeLevel = attributesTag.getInt(attributeId); - //Set item count - countOverride.set(Integer.toString(attributeLevel)); + //Set item count + countOverride.set(Integer.toString(attributeLevel)); - //Draw the attribute name - this.matrices.push(); - this.matrices.translate(0f, 0f, 200f); + //Draw the attribute name + this.matrices.push(); + this.matrices.translate(0f, 0f, 200f); - String attributeInitials = AttributeShards.getShortName(attributeId); + String attributeInitials = AttributeShards.getShortName(attributeId); - this.drawText(textRenderer, attributeInitials, x, y, Formatting.AQUA.getColorValue(), true); + this.drawText(textRenderer, attributeInitials, x, y, Formatting.AQUA.getColorValue(), true); - this.matrices.pop(); - } - } - } + this.matrices.pop(); + } + } + } } @ModifyExpressionValue(method = "drawItemInSlot(Lnet/minecraft/client/font/TextRenderer;Lnet/minecraft/item/ItemStack;IILjava/lang/String;)V", - at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/ItemCooldownManager;getCooldownProgress(Lnet/minecraft/item/Item;F)F")) + at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/ItemCooldownManager;getCooldownProgress(Lnet/minecraft/item/Item;F)F")) private float skyblocker$modifyItemCooldown(float cooldownProgress, @Local ItemStack stack) { return Utils.isOnSkyblock() && ItemCooldowns.isOnCooldown(stack) ? ItemCooldowns.getItemCooldownEntry(stack).getRemainingCooldownPercent() : cooldownProgress; } diff --git a/src/main/java/de/hysky/skyblocker/mixin/DyeableItemMixin.java b/src/main/java/de/hysky/skyblocker/mixin/DyeableItemMixin.java index 51ab3852..e5697085 100644 --- a/src/main/java/de/hysky/skyblocker/mixin/DyeableItemMixin.java +++ b/src/main/java/de/hysky/skyblocker/mixin/DyeableItemMixin.java @@ -2,10 +2,10 @@ package de.hysky.skyblocker.mixin; import com.llamalad7.mixinextras.injector.ModifyReturnValue; import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; import net.minecraft.item.DyeableItem; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -13,11 +13,8 @@ import org.spongepowered.asm.mixin.injection.At; public interface DyeableItemMixin { @ModifyReturnValue(method = "getColor", at = @At("RETURN")) private int skyblocker$customDyeColor(int originalColor, ItemStack stack) { - NbtCompound nbt = stack.getNbt(); - - if (Utils.isOnSkyblock() && nbt != null && nbt.contains("ExtraAttributes")) { - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; + if (Utils.isOnSkyblock()) { + String itemUuid = ItemUtils.getItemUuid(stack); return SkyblockerConfigManager.get().general.customDyeColors.getOrDefault(itemUuid, originalColor); } diff --git a/src/main/java/de/hysky/skyblocker/mixin/HandledScreenMixin.java b/src/main/java/de/hysky/skyblocker/mixin/HandledScreenMixin.java index 689974c8..b037d45a 100644 --- a/src/main/java/de/hysky/skyblocker/mixin/HandledScreenMixin.java +++ b/src/main/java/de/hysky/skyblocker/mixin/HandledScreenMixin.java @@ -6,12 +6,8 @@ import de.hysky.skyblocker.skyblock.experiment.ChronomatronSolver; import de.hysky.skyblocker.skyblock.experiment.ExperimentSolver; import de.hysky.skyblocker.skyblock.experiment.SuperpairsSolver; import de.hysky.skyblocker.skyblock.experiment.UltrasequencerSolver; -import de.hysky.skyblocker.skyblock.item.BackpackPreview; -import de.hysky.skyblocker.skyblock.item.CompactorDeletorPreview; -import de.hysky.skyblocker.skyblock.item.ItemProtection; -import de.hysky.skyblocker.skyblock.item.ItemRarityBackgrounds; -import de.hysky.skyblocker.skyblock.item.WikiLookup; -import de.hysky.skyblocker.skyblock.itemlist.ItemRegistry; +import de.hysky.skyblocker.skyblock.item.*; +import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.render.gui.ContainerSolver; import net.minecraft.client.MinecraftClient; @@ -89,7 +85,7 @@ public abstract class HandledScreenMixin<T extends ScreenHandler> extends Screen // Compactor Preview if (SkyblockerConfigManager.get().general.compactorDeletorPreview) { ItemStack stack = focusedSlot.getStack(); - Matcher matcher = CompactorDeletorPreview.NAME.matcher(ItemRegistry.getInternalName(stack)); + Matcher matcher = CompactorDeletorPreview.NAME.matcher(ItemUtils.getItemId(stack)); if (matcher.matches() && CompactorDeletorPreview.drawPreview(context, stack, matcher.group("type"), matcher.group("size"), x, y)) { ci.cancel(); } diff --git a/src/main/java/de/hysky/skyblocker/mixin/ItemStackMixin.java b/src/main/java/de/hysky/skyblocker/mixin/ItemStackMixin.java index 76073a2c..79a37d68 100644 --- a/src/main/java/de/hysky/skyblocker/mixin/ItemStackMixin.java +++ b/src/main/java/de/hysky/skyblocker/mixin/ItemStackMixin.java @@ -1,34 +1,22 @@ package de.hysky.skyblocker.mixin; -import org.jetbrains.annotations.Nullable; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; - import com.llamalad7.mixinextras.injector.ModifyReturnValue; - import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.ItemUtils.Durability; import de.hysky.skyblocker.utils.Utils; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; import net.minecraft.text.Text; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; @Mixin(ItemStack.class) public abstract class ItemStackMixin { - @Shadow - @Nullable - private NbtCompound nbt; - @ModifyReturnValue(method = "getName", at = @At("RETURN")) private Text skyblocker$customItemNames(Text original) { - if (Utils.isOnSkyblock() && nbt != null && nbt.contains("ExtraAttributes")) { - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; - - return SkyblockerConfigManager.get().general.customItemNames.getOrDefault(itemUuid, original); + if (Utils.isOnSkyblock()) { + return SkyblockerConfigManager.get().general.customItemNames.getOrDefault(ItemUtils.getItemUuid((ItemStack) (Object) this), original); } return original; 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(); } diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java index 6ae1b4d0..fa04acf8 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java @@ -9,15 +9,20 @@ import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.StringNbtReader; import net.minecraft.text.Text; import net.minecraft.util.Formatting; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.regex.Pattern; public class ItemUtils { private final static Pattern WHITESPACES = Pattern.compile("^\\s*$"); + public static final String EXTRA_ATTRIBUTES = "ExtraAttributes"; + public static final String ID = "id"; + public static final String UUID = "uuid"; public static List<Text> getTooltip(ItemStack item) { MinecraftClient client = MinecraftClient.getInstance(); @@ -37,19 +42,76 @@ public class ItemUtils { return list; } + /** + * Gets the {@code ExtraAttributes} NBT tag from the item stack. + * + * @param stack the item stack to get the {@code ExtraAttributes} NBT tag from + * @return an optional containing the {@code ExtraAttributes} NBT tag of the item stack + */ + public static Optional<NbtCompound> getExtraAttributesOptional(@NotNull ItemStack stack) { + return Optional.ofNullable(stack.getSubNbt(EXTRA_ATTRIBUTES)); + } + + /** + * Gets the {@code ExtraAttributes} NBT tag from the item stack. + * + * @param stack the item stack to get the {@code ExtraAttributes} NBT tag from + * @return the {@code ExtraAttributes} NBT tag of the item stack, or null if the item stack is null or does not have an {@code ExtraAttributes} NBT tag + */ @Nullable - public static Durability getDurability(ItemStack stack) { - if (!Utils.isOnSkyblock() || !SkyblockerConfigManager.get().locations.dwarvenMines.enableDrillFuel || stack.isEmpty()) { - return null; - } + public static NbtCompound getExtraAttributes(@NotNull ItemStack stack) { + return stack.getSubNbt(EXTRA_ATTRIBUTES); + } + + /** + * Gets the internal name of the item stack from the {@code ExtraAttributes} NBT tag. + * + * @param stack the item stack to get the internal name from + * @return an optional containing the internal name of the item stack + */ + public static Optional<String> getItemIdOptional(@NotNull ItemStack stack) { + return getExtraAttributesOptional(stack).map(extraAttributes -> extraAttributes.getString(ID)); + } + + /** + * Gets the internal name of the item stack from the {@code ExtraAttributes} NBT tag. + * + * @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 + */ + public static String getItemId(@NotNull ItemStack stack) { + NbtCompound extraAttributes = getExtraAttributes(stack); + return extraAttributes != null ? extraAttributes.getString(ID) : ""; + } + + /** + * Gets the UUID of the item stack from the {@code ExtraAttributes} NBT tag. + * + * @param stack the item stack to get the UUID from + * @return an optional containing the UUID of the item stack + */ + public static Optional<String> getItemUuidOptional(@NotNull ItemStack stack) { + return getExtraAttributesOptional(stack).map(extraAttributes -> extraAttributes.getString(UUID)); + } + + /** + * Gets the UUID of the item stack from the {@code ExtraAttributes} NBT tag. + * + * @param stack the item stack to get the UUID from + * @return the UUID of the item stack, or null if the item stack is null or does not have a UUID + */ + public static String getItemUuid(@NotNull ItemStack stack) { + NbtCompound extraAttributes = getExtraAttributes(stack); + return extraAttributes != null ? extraAttributes.getString(UUID) : ""; + } - NbtCompound tag = stack.getNbt(); - if (tag == null || !tag.contains("ExtraAttributes")) { + @Nullable + public static Durability getDurability(@NotNull ItemStack stack) { + if (!Utils.isOnSkyblock() || !SkyblockerConfigManager.get().locations.dwarvenMines.enableDrillFuel || stack.isEmpty()) { return null; } - NbtCompound extraAttributes = tag.getCompound("ExtraAttributes"); - if (!extraAttributes.contains("drill_fuel") && !extraAttributes.getString("id").equals("PICKONIMBUS")) { + if (getExtraAttributesOptional(stack).filter(extraAttributes -> extraAttributes.contains("drill_fuel") || extraAttributes.getString(ItemUtils.ID).equals("PICKONIMBUS")).isEmpty()) { return null; } @@ -92,20 +154,6 @@ public class ItemUtils { } } - public static String getItemId(ItemStack itemStack) { - if (itemStack == null) return null; - - NbtCompound nbt = itemStack.getNbt(); - if (nbt != null && nbt.contains("ExtraAttributes")) { - NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); - if (extraAttributes.contains("id")) { - return extraAttributes.getString("id"); - } - } - - return null; - } - public record Durability(int current, int max) { } } |