diff options
| author | Kevin <92656833+kevinthegreat1@users.noreply.github.com> | 2023-10-14 17:03:49 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-14 17:03:49 -0400 |
| commit | a41b069db0216c571bce269a0ea9866c1d701b87 (patch) | |
| tree | 390fa5d88f79b9f126c46ce5b803aa310f7be0f5 /src/main/java/de/hysky/skyblocker/utils | |
| parent | ab3f55fc2e81f5594009ecea708a931468ec7ccf (diff) | |
| parent | 4fbcf1b2a4790f3033e97c55cab344abdfaea8d2 (diff) | |
| download | Skyblocker-a41b069db0216c571bce269a0ea9866c1d701b87.tar.gz Skyblocker-a41b069db0216c571bce269a0ea9866c1d701b87.tar.bz2 Skyblocker-a41b069db0216c571bce269a0ea9866c1d701b87.zip | |
Merge pull request #350 from kevinthegreat1/nbt-utils
Refactor NBT Parsing
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) { } } |
