From 17601f5f72ba2b70eb9f264a2e543a32580b81a3 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Tue, 2 Jul 2024 20:14:50 +0300 Subject: [PATCH 1/4] Fix PetLevelAdder working incorrectly in the sea creatures guide --- .../skyblock/item/slottext/adders/PetLevelAdder.java | 9 ++++++--- src/main/java/de/hysky/skyblocker/utils/ItemUtils.java | 6 +++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/PetLevelAdder.java b/src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/PetLevelAdder.java index 88d48fbf5f..87d42e4f3b 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/PetLevelAdder.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/PetLevelAdder.java @@ -2,11 +2,13 @@ import de.hysky.skyblocker.skyblock.item.slottext.SlotText; import de.hysky.skyblocker.skyblock.item.slottext.SlotTextAdder; +import de.hysky.skyblocker.utils.ItemUtils; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; import net.minecraft.screen.slot.Slot; import net.minecraft.text.Text; -import net.minecraft.util.Formatting; import org.apache.commons.lang3.math.NumberUtils; import org.jetbrains.annotations.NotNull; @@ -22,8 +24,9 @@ public PetLevelAdder() { ItemStack itemStack = slot.getStack(); if (!itemStack.isOf(Items.PLAYER_HEAD)) return List.of(); String level = CatacombsLevelAdder.getBracketedLevelFromName(itemStack); - if (!NumberUtils.isDigits(level)) return List.of(); - if ("100".equals(level) || "200".equals(level)) return List.of(); + if (!NumberUtils.isDigits(level) || "100".equals(level) || "200".equals(level)) return List.of(); + NbtCompound nbt = ItemUtils.getCustomData(itemStack); + if (nbt.isEmpty() || !nbt.contains("id", NbtElement.STRING_TYPE) || !nbt.getString("id").equals("PET")) return List.of(); return List.of(SlotText.topLeft(Text.literal(level).withColor(0xFFDDC1))); } } diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java index de7a0f9ee1..7deabf2bc4 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java @@ -62,8 +62,12 @@ public static LiteralArgumentBuilder dumpHeldItemComm }); } + /** + * Gets the nbt in the custom data component of the item stack. + * @return The {@link DataComponentTypes#CUSTOM_DATA custom data} of the itemstack, or an empty {@link NbtCompound} if the itemstack is missing + */ @SuppressWarnings("deprecation") - public static NbtCompound getCustomData(@NotNull ComponentHolder stack) { + public static @NotNull NbtCompound getCustomData(@NotNull ComponentHolder stack) { return stack.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT).getNbt(); } From 90ce041b5c266a4093c9b5929de06369d179534f Mon Sep 17 00:00:00 2001 From: viciscat <51047087+viciscat@users.noreply.github.com> Date: Tue, 2 Jul 2024 19:33:39 +0200 Subject: [PATCH 2/4] Update ItemUtils.java finish the sentence lol --- src/main/java/de/hysky/skyblocker/utils/ItemUtils.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java index 7deabf2bc4..134a7ed00d 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java @@ -65,6 +65,7 @@ public static LiteralArgumentBuilder dumpHeldItemComm /** * Gets the nbt in the custom data component of the item stack. * @return The {@link DataComponentTypes#CUSTOM_DATA custom data} of the itemstack, or an empty {@link NbtCompound} if the itemstack is missing + * a custom data component */ @SuppressWarnings("deprecation") public static @NotNull NbtCompound getCustomData(@NotNull ComponentHolder stack) { From 2dc53a460ca132ef1de46bd48c4e29b5a1b9cd44 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Thu, 4 Jul 2024 23:53:02 +0300 Subject: [PATCH 3/4] Change the pet detection logic to use `ItemUtils#getItemId` instead --- .../skyblock/item/slottext/adders/PetLevelAdder.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/PetLevelAdder.java b/src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/PetLevelAdder.java index 87d42e4f3b..3049cd3f54 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/PetLevelAdder.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/PetLevelAdder.java @@ -5,8 +5,6 @@ import de.hysky.skyblocker.utils.ItemUtils; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.nbt.NbtElement; import net.minecraft.screen.slot.Slot; import net.minecraft.text.Text; import org.apache.commons.lang3.math.NumberUtils; @@ -25,8 +23,7 @@ public PetLevelAdder() { if (!itemStack.isOf(Items.PLAYER_HEAD)) return List.of(); String level = CatacombsLevelAdder.getBracketedLevelFromName(itemStack); if (!NumberUtils.isDigits(level) || "100".equals(level) || "200".equals(level)) return List.of(); - NbtCompound nbt = ItemUtils.getCustomData(itemStack); - if (nbt.isEmpty() || !nbt.contains("id", NbtElement.STRING_TYPE) || !nbt.getString("id").equals("PET")) return List.of(); + if (!ItemUtils.getItemId(itemStack).equals("PET")) return List.of(); return List.of(SlotText.topLeft(Text.literal(level).withColor(0xFFDDC1))); } } From a004952c30727a2547afba6447387b2a24d4b90a Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Fri, 5 Jul 2024 00:04:03 +0300 Subject: [PATCH 4/4] Add nullability annotations to the rest of `ItemUtils` and fix 2 unnecessary null checks I couldn't stop myself. --- .../de/hysky/skyblocker/utils/ItemUtils.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java index 134a7ed00d..e43ce783c5 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java @@ -10,9 +10,9 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.JsonOps; import com.mojang.serialization.codecs.RecordCodecBuilder; -import de.hysky.skyblocker.skyblock.item.tooltip.adders.ObtainedDateTooltip; import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.skyblock.item.tooltip.TooltipInfoType; +import de.hysky.skyblocker.skyblock.item.tooltip.adders.ObtainedDateTooltip; import it.unimi.dsi.fastutil.doubles.DoubleBooleanPair; import it.unimi.dsi.fastutil.ints.IntIntPair; import it.unimi.dsi.fastutil.longs.LongBooleanPair; @@ -64,8 +64,8 @@ public static LiteralArgumentBuilder dumpHeldItemComm /** * Gets the nbt in the custom data component of the item stack. - * @return The {@link DataComponentTypes#CUSTOM_DATA custom data} of the itemstack, or an empty {@link NbtCompound} if the itemstack is missing - * a custom data component + * @return The {@link DataComponentTypes#CUSTOM_DATA custom data} of the itemstack, + * or an empty {@link NbtCompound} if the itemstack is missing a custom data component */ @SuppressWarnings("deprecation") public static @NotNull NbtCompound getCustomData(@NotNull ComponentHolder stack) { @@ -78,7 +78,7 @@ public static LiteralArgumentBuilder dumpHeldItemComm * @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 getItemIdOptional(@NotNull ItemStack stack) { + public static @NotNull Optional getItemIdOptional(@NotNull ItemStack stack) { NbtCompound customData = getCustomData(stack); return customData.contains(ID) ? Optional.of(customData.getString(ID)) : Optional.empty(); } @@ -89,7 +89,7 @@ public static Optional getItemIdOptional(@NotNull ItemStack stack) { * @param stack the item stack to get the internal name from * @return the internal name of the item stack, or an empty string if the item stack is null or does not have an internal name */ - public static String getItemId(@NotNull ItemStack stack) { + public static @NotNull String getItemId(@NotNull ItemStack stack) { return getCustomData(stack).getString(ID); } @@ -99,7 +99,7 @@ public static String getItemId(@NotNull ItemStack stack) { * @param stack the item stack to get the UUID from * @return an optional containing the UUID of the item stack */ - public static Optional getItemUuidOptional(@NotNull ItemStack stack) { + public static @NotNull Optional getItemUuidOptional(@NotNull ItemStack stack) { NbtCompound customData = getCustomData(stack); return customData.contains(UUID) ? Optional.of(customData.getString(UUID)) : Optional.empty(); } @@ -110,7 +110,7 @@ public static Optional getItemUuidOptional(@NotNull ItemStack stack) { * @param stack the item stack to get the UUID from * @return the UUID of the item stack, or an empty string if the item stack is null or does not have a UUID */ - public static String getItemUuid(@NotNull ComponentHolder stack) { + public static @NotNull String getItemUuid(@NotNull ComponentHolder stack) { return getCustomData(stack).getString(UUID); } @@ -120,7 +120,7 @@ public static String getItemUuid(@NotNull ComponentHolder stack) { * @return An {@link LongBooleanPair} with the {@code left long} representing the item's price, * and the {@code right boolean} indicating if the price was based on complete data. */ - public static DoubleBooleanPair getItemPrice(@NotNull ItemStack stack) { + public static @NotNull DoubleBooleanPair getItemPrice(@NotNull ItemStack stack) { return getItemPrice(getItemId(stack)); } @@ -130,7 +130,7 @@ public static DoubleBooleanPair getItemPrice(@NotNull ItemStack stack) { * @return An {@link LongBooleanPair} with the {@code left long} representing the item's price, * and the {@code right boolean} indicating if the price was based on complete data. */ - public static DoubleBooleanPair getItemPrice(@Nullable String id) { + public static @NotNull DoubleBooleanPair getItemPrice(@Nullable String id) { JsonObject bazaarPrices = TooltipInfoType.BAZAAR.getData(); JsonObject lowestBinPrices = TooltipInfoType.LOWEST_BINS.getData(); @@ -173,15 +173,15 @@ public static String getTimestamp(ItemStack stack) { public static boolean hasCustomDurability(@NotNull ItemStack stack) { NbtCompound customData = getCustomData(stack); - return customData != null && (customData.contains("drill_fuel") || customData.getString(ID).equals("PICKONIMBUS")); + return !customData.isEmpty() && (customData.contains("drill_fuel") || customData.getString(ID).equals("PICKONIMBUS")); } @Nullable public static IntIntPair getDurability(@NotNull ItemStack stack) { NbtCompound customData = getCustomData(stack); - if (customData == null) return null; + if (customData.isEmpty()) return null; - // TODO Calculate drill durability based on the drill_fuel flag, fuel_tank flag, and hotm level + // TODO Calculate drill durability based on the drill_fuel flag, fuel_tank flag, and hotm level // TODO Cache the max durability and only update the current durability on inventory tick int pickonimbusDurability = customData.getInt("pickonimbus_durability"); @@ -223,15 +223,15 @@ public static Matcher getLoreLineIfMatch(ItemStack item, Pattern pattern) { return null; } - public static List getLore(ItemStack item) { + public static @NotNull List getLore(ItemStack item) { return item.getOrDefault(DataComponentTypes.LORE, LoreComponent.DEFAULT).styledLines(); } - public static PropertyMap propertyMapWithTexture(String textureValue) { + public static @NotNull PropertyMap propertyMapWithTexture(String textureValue) { return Codecs.GAME_PROFILE_PROPERTY_MAP.parse(JsonOps.INSTANCE, JsonParser.parseString("[{\"name\":\"textures\",\"value\":\"" + textureValue + "\"}]")).getOrThrow(); } - public static String getHeadTexture(ItemStack stack) { + public static @NotNull String getHeadTexture(@NotNull ItemStack stack) { if (!stack.isOf(Items.PLAYER_HEAD) || !stack.contains(DataComponentTypes.PROFILE)) return ""; ProfileComponent profile = stack.get(DataComponentTypes.PROFILE); @@ -243,13 +243,13 @@ public static String getHeadTexture(ItemStack stack) { .orElse(""); } - public static Optional getHeadTextureOptional(ItemStack stack) { + public static @NotNull Optional getHeadTextureOptional(ItemStack stack) { String texture = getHeadTexture(stack); if (texture.isBlank()) return Optional.empty(); return Optional.of(texture); } - public static ItemStack getSkyblockerStack() { + public static @NotNull ItemStack getSkyblockerStack() { try { ItemStack stack = new ItemStack(Items.PLAYER_HEAD); stack.set(DataComponentTypes.PROFILE, new ProfileComponent(Optional.of("SkyblockerStack"), Optional.of(java.util.UUID.randomUUID()), propertyMapWithTexture("e3RleHR1cmVzOntTS0lOOnt1cmw6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDdjYzY2ODc0MjNkMDU3MGQ1NTZhYzUzZTA2NzZjYjU2M2JiZGQ5NzE3Y2Q4MjY5YmRlYmVkNmY2ZDRlN2JmOCJ9fX0=")));