diff options
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/utils/ItemUtils.java | 92 |
1 files changed, 70 insertions, 22 deletions
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) { } } |