From 73c447e8fe535e857fc330d1823b6c9aac663b5c Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sun, 5 May 2024 00:20:55 -0400 Subject: [PATCH 1/7] Add coins per hour and refactor item price calculations --- .../hysky/skyblocker/skyblock/ChestValue.java | 62 +++++----------- .../skyblock/dungeon/CroesusProfit.java | 28 ++----- .../skyblock/garden/FarmingHudWidget.java | 73 ++++++++++--------- .../de/hysky/skyblocker/utils/ItemUtils.java | 60 ++++++++++++--- 4 files changed, 115 insertions(+), 108 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/ChestValue.java b/src/main/java/de/hysky/skyblocker/skyblock/ChestValue.java index 7f8b2c71c4..7b36dd7848 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/ChestValue.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/ChestValue.java @@ -7,13 +7,11 @@ import de.hysky.skyblocker.mixins.accessors.HandledScreenAccessor; import de.hysky.skyblocker.mixins.accessors.ScreenAccessor; import de.hysky.skyblocker.skyblock.item.tooltip.ItemTooltip; -import de.hysky.skyblocker.skyblock.item.tooltip.TooltipInfoType; import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; -import it.unimi.dsi.fastutil.longs.LongBooleanPair; +import it.unimi.dsi.fastutil.doubles.DoubleBooleanPair; import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; import net.fabricmc.fabric.api.client.screen.v1.Screens; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.ingame.GenericContainerScreen; import net.minecraft.client.gui.tooltip.Tooltip; import net.minecraft.client.gui.widget.ButtonWidget; @@ -45,7 +43,7 @@ public static void init() { if (DUNGEON_CHESTS.contains(titleString)) { if (SkyblockerConfigManager.get().dungeons.dungeonChestProfit.enableProfitCalculator) { ScreenEvents.afterTick(screen).register(screen_ -> - ((ScreenAccessor) screen).setTitle(getDungeonChestProfit(genericContainerScreen.getScreenHandler(), title, titleString, client)) + ((ScreenAccessor) screen).setTitle(getDungeonChestProfit(genericContainerScreen.getScreenHandler(), title, titleString)) ); } } else if (SkyblockerConfigManager.get().uiAndVisuals.chestValue.enableChestValue && !titleString.equals("SkyBlock Menu")) { @@ -65,9 +63,9 @@ public static void init() { }); } - private static Text getDungeonChestProfit(GenericContainerScreenHandler handler, Text title, String titleString, MinecraftClient client) { + private static Text getDungeonChestProfit(GenericContainerScreenHandler handler, Text title, String titleString) { try { - long profit = 0; + double profit = 0; boolean hasIncompleteData = false, usedKismet = false; List slots = handler.slots.subList(0, handler.getRows() * 9); @@ -85,12 +83,12 @@ private static Text getDungeonChestProfit(GenericContainerScreenHandler handler, //Regular item price if (id != null) { - LongBooleanPair priceData = getItemPrice(id); + DoubleBooleanPair priceData = ItemUtils.getItemPrice(id); if (!priceData.rightBoolean()) hasIncompleteData = true; //Add the item price to the profit - profit += priceData.leftLong() * stack.getCount(); + profit += priceData.leftDouble() * stack.getCount(); continue; } @@ -103,12 +101,12 @@ private static Text getDungeonChestProfit(GenericContainerScreenHandler handler, String type = matcher.group("type"); int amount = Integer.parseInt(matcher.group("amount")); - LongBooleanPair priceData = getItemPrice(("ESSENCE_" + type).toUpperCase()); + DoubleBooleanPair priceData = ItemUtils.getItemPrice(("ESSENCE_" + type).toUpperCase()); if (!priceData.rightBoolean()) hasIncompleteData = true; //Add the price of the essence to the profit - profit += priceData.leftLong() * amount; + profit += priceData.leftDouble() * amount; continue; } @@ -116,7 +114,7 @@ private static Text getDungeonChestProfit(GenericContainerScreenHandler handler, //Determine the cost of the chest if (name.contains("Open Reward Chest")) { - String foundString = searchLoreFor(stack, client, "Coins"); + String foundString = searchLoreFor(stack, "Coins"); //Incase we're searching the free chest if (!StringUtils.isBlank(foundString)) { @@ -128,19 +126,19 @@ private static Text getDungeonChestProfit(GenericContainerScreenHandler handler, //Determine if a kismet was used or not if (name.contains("Reroll Chest")) { - usedKismet = !StringUtils.isBlank(searchLoreFor(stack, client, "You already rerolled a chest!")); + usedKismet = !StringUtils.isBlank(searchLoreFor(stack, "You already rerolled a chest!")); } } if (SkyblockerConfigManager.get().dungeons.dungeonChestProfit.includeKismet && usedKismet) { - LongBooleanPair kismetPriceData = getItemPrice("KISMET_FEATHER"); + DoubleBooleanPair kismetPriceData = ItemUtils.getItemPrice("KISMET_FEATHER"); if (!kismetPriceData.rightBoolean()) hasIncompleteData = true; - profit -= kismetPriceData.leftLong(); + profit -= kismetPriceData.leftDouble(); } - return Text.literal(titleString).append(getProfitText(profit, hasIncompleteData)); + return Text.literal(titleString).append(getProfitText((long) profit, hasIncompleteData)); } catch (Exception e) { LOGGER.error("[Skyblocker Profit Calculator] Failed to calculate dungeon chest profit! ", e); } @@ -150,7 +148,7 @@ private static Text getDungeonChestProfit(GenericContainerScreenHandler handler, private static Text getChestValue(GenericContainerScreenHandler handler, Text title, String titleString) { try { - long value = 0; + double value = 0; boolean hasIncompleteData = false; List slots = handler.slots.subList(0, handler.getRows() * 9); @@ -163,15 +161,15 @@ private static Text getChestValue(GenericContainerScreenHandler handler, Text ti String id = ItemTooltip.getInternalNameFromNBT(stack, false); if (id != null) { - LongBooleanPair priceData = getItemPrice(id); + DoubleBooleanPair priceData = ItemUtils.getItemPrice(id); if (!priceData.rightBoolean()) hasIncompleteData = true; - value += priceData.leftLong() * stack.getCount(); + value += priceData.leftDouble() * stack.getCount(); } } - return Text.literal(titleString).append(getValueText(value, hasIncompleteData)); + return Text.literal(titleString).append(getValueText((long) value, hasIncompleteData)); } catch (Exception e) { LOGGER.error("[Skyblocker Value Calculator] Failed to calculate dungeon chest value! ", e); } @@ -179,34 +177,10 @@ private static Text getChestValue(GenericContainerScreenHandler handler, Text ti return title; } - /** - * @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. - */ - private static LongBooleanPair getItemPrice(String id) { - JsonObject bazaarPrices = TooltipInfoType.BAZAAR.getData(); - JsonObject lbinPrices = TooltipInfoType.LOWEST_BINS.getData(); - - if (bazaarPrices == null || lbinPrices == null) return LongBooleanPair.of(0L, false); - - if (bazaarPrices.has(id)) { - JsonObject item = bazaarPrices.get(id).getAsJsonObject(); - boolean isPriceNull = item.get("sellPrice").isJsonNull(); - - return LongBooleanPair.of(isPriceNull ? 0L : (long) item.get("sellPrice").getAsDouble(), !isPriceNull); - } - - if (lbinPrices.has(id)) { - return LongBooleanPair.of((long) lbinPrices.get(id).getAsDouble(), true); - } - - return LongBooleanPair.of(0L, false); - } - /** * Searches for a specific string of characters in the name and lore of an item */ - private static String searchLoreFor(ItemStack stack, MinecraftClient client, String searchString) { + private static String searchLoreFor(ItemStack stack, String searchString) { return ItemUtils.getLoreLineIf(stack, line -> line.contains(searchString)); } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/CroesusProfit.java b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/CroesusProfit.java index 70e785cbec..0f459c9d9b 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/CroesusProfit.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/CroesusProfit.java @@ -35,13 +35,13 @@ protected boolean isEnabled() { protected List getColors(String[] groups, Int2ObjectMap slots) { List highlights = new ArrayList<>(); ItemStack bestChest = null, secondBestChest = null; - long bestValue = 0, secondBestValue = 0; // If negative value of chest - it is out of the question - long dungeonKeyPriceData = getItemPrice("DUNGEON_CHEST_KEY") * 2; // lesser ones don't worth the hassle + double bestValue = 0, secondBestValue = 0; // If negative value of chest - it is out of the question + double dungeonKeyPriceData = getItemPrice("DUNGEON_CHEST_KEY") * 2; // lesser ones don't worth the hassle for (Int2ObjectMap.Entry entry : slots.int2ObjectEntrySet()) { ItemStack stack = entry.getValue(); if (stack.getName().getString().contains("Chest")) { - long value = valueChest(stack); + double value = valueChest(stack); if (value > bestValue) { secondBestChest = bestChest; secondBestValue = bestValue; @@ -68,8 +68,8 @@ protected List getColors(String[] groups, Int2ObjectMap chestItems = new ArrayList<>(); @@ -107,22 +107,8 @@ private long valueChest(@NotNull ItemStack chest) { } - private long getItemPrice(String itemDisplayName) { - JsonObject bazaarPrices = TooltipInfoType.BAZAAR.getData(); - JsonObject lbinPrices = TooltipInfoType.LOWEST_BINS.getData(); - long itemValue = 0; - String id = dungeonDropsNameToId.get(itemDisplayName); - - if (bazaarPrices == null || lbinPrices == null) return 0; - - if (bazaarPrices.has(id)) { - JsonObject item = bazaarPrices.get(id).getAsJsonObject(); - boolean isPriceNull = item.get("sellPrice").isJsonNull(); - return (isPriceNull ? 0L : item.get("sellPrice").getAsLong()); - } else if (lbinPrices.has(id)) { - return lbinPrices.get(id).getAsLong(); - } - return itemValue; + private double getItemPrice(String itemDisplayName) { + return ItemUtils.getItemPrice(dungeonDropsNameToId.get(itemDisplayName)).leftDouble(); } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java index f12f6fa815..9fba9249c3 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java @@ -6,6 +6,7 @@ import de.hysky.skyblocker.skyblock.tabhud.widget.component.PlainTextComponent; import de.hysky.skyblocker.skyblock.tabhud.widget.component.ProgressComponent; import de.hysky.skyblocker.utils.ItemUtils; +import it.unimi.dsi.fastutil.doubles.DoubleBooleanPair; import net.minecraft.client.MinecraftClient; import net.minecraft.entity.Entity; import net.minecraft.item.ItemStack; @@ -18,31 +19,31 @@ public class FarmingHudWidget extends Widget { private static final MutableText TITLE = Text.literal("Farming").formatted(Formatting.YELLOW, Formatting.BOLD); - public static final Map FARMING_TOOLS = Map.ofEntries( - Map.entry("THEORETICAL_HOE_WHEAT_1", Ico.WHEAT), - Map.entry("THEORETICAL_HOE_WHEAT_2", Ico.WHEAT), - Map.entry("THEORETICAL_HOE_WHEAT_3", Ico.WHEAT), - Map.entry("THEORETICAL_HOE_CARROT_1", Ico.CARROT), - Map.entry("THEORETICAL_HOE_CARROT_2", Ico.CARROT), - Map.entry("THEORETICAL_HOE_CARROT_3", Ico.CARROT), - Map.entry("THEORETICAL_HOE_POTATO_1", Ico.POTATO), - Map.entry("THEORETICAL_HOE_POTATO_2", Ico.POTATO), - Map.entry("THEORETICAL_HOE_POTATO_3", Ico.POTATO), - Map.entry("THEORETICAL_HOE_CANE_1", Ico.SUGAR_CANE), - Map.entry("THEORETICAL_HOE_CANE_2", Ico.SUGAR_CANE), - Map.entry("THEORETICAL_HOE_CANE_3", Ico.SUGAR_CANE), - Map.entry("THEORETICAL_HOE_WARTS_1", Ico.NETHER_WART), - Map.entry("THEORETICAL_HOE_WARTS_2", Ico.NETHER_WART), - Map.entry("THEORETICAL_HOE_WARTS_3", Ico.NETHER_WART), - Map.entry("FUNGI_CUTTER", Ico.MUSHROOM), - Map.entry("CACTUS_KNIFE", Ico.CACTUS), - Map.entry("MELON_DICER", Ico.MELON), - Map.entry("MELON_DICER_2", Ico.MELON), - Map.entry("MELON_DICER_3", Ico.MELON), - Map.entry("PUMPKIN_DICER", Ico.PUMPKIN), - Map.entry("PUMPKIN_DICER_2", Ico.PUMPKIN), - Map.entry("PUMPKIN_DICER_3", Ico.PUMPKIN), - Map.entry("COCO_CHOPPER", Ico.COCOA_BEANS) + public static final Map FARMING_TOOLS = Map.ofEntries( + Map.entry("THEORETICAL_HOE_WHEAT_1", "WHEAT"), + Map.entry("THEORETICAL_HOE_WHEAT_2", "WHEAT"), + Map.entry("THEORETICAL_HOE_WHEAT_3", "WHEAT"), + Map.entry("THEORETICAL_HOE_CARROT_1", "CARROT_ITEM"), + Map.entry("THEORETICAL_HOE_CARROT_2", "CARROT_ITEM"), + Map.entry("THEORETICAL_HOE_CARROT_3", "CARROT_ITEM"), + Map.entry("THEORETICAL_HOE_POTATO_1", "POTATO_ITEM"), + Map.entry("THEORETICAL_HOE_POTATO_2", "POTATO_ITEM"), + Map.entry("THEORETICAL_HOE_POTATO_3", "POTATO_ITEM"), + Map.entry("THEORETICAL_HOE_CANE_1", "SUGAR_CANE"), + Map.entry("THEORETICAL_HOE_CANE_2", "SUGAR_CANE"), + Map.entry("THEORETICAL_HOE_CANE_3", "SUGAR_CANE"), + Map.entry("THEORETICAL_HOE_WARTS_1", "NETHER_STALK"), + Map.entry("THEORETICAL_HOE_WARTS_2", "NETHER_STALK"), + Map.entry("THEORETICAL_HOE_WARTS_3", "NETHER_STALK"), + Map.entry("FUNGI_CUTTER", "RED_MUSHROOM"), + Map.entry("CACTUS_KNIFE", "CACTUS"), + Map.entry("MELON_DICER", "MELON"), + Map.entry("MELON_DICER_2", "MELON"), + Map.entry("MELON_DICER_3", "MELON"), + Map.entry("PUMPKIN_DICER", "PUMPKIN"), + Map.entry("PUMPKIN_DICER_2", "PUMPKIN"), + Map.entry("PUMPKIN_DICER_3", "PUMPKIN"), + Map.entry("COCO_CHOPPER", "INK_SACK.3") ); public static final FarmingHudWidget INSTANCE = new FarmingHudWidget(); private final MinecraftClient client = MinecraftClient.getInstance(); @@ -56,19 +57,25 @@ public FarmingHudWidget() { @Override public void updateContent() { - ItemStack icon = client.player == null ? Ico.HOE : FARMING_TOOLS.getOrDefault(ItemUtils.getItemId(client.player.getMainHandStack()), Ico.HOE); - addSimpleIcoText(icon, "Counter: ", Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format(FarmingHud.counter())); - addSimpleIcoText(icon, "Crops/min: ", Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format((int) FarmingHud.cropsPerMinute() / 100 * 100)); - addSimpleIcoText(icon, "Blocks/s: ", Formatting.YELLOW, Integer.toString(FarmingHud.blockBreaks())); + if (client.player == null) return; + + ItemStack farmingTool = client.player.getMainHandStack(); + addSimpleIcoText(farmingTool, "Counter: ", Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format(FarmingHud.counter())); + float cropsPerMinute = FarmingHud.cropsPerMinute(); + addSimpleIcoText(farmingTool, "Crops/min: ", Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format((int) cropsPerMinute / 10 * 10)); + DoubleBooleanPair itemPrice = ItemUtils.getItemPrice(FARMING_TOOLS.get(ItemUtils.getItemId(farmingTool))); + addSimpleIcoText(Ico.GOLD, "Coins/h: ", Formatting.GOLD, itemPrice.rightBoolean() ? FarmingHud.NUMBER_FORMAT.format((int) (itemPrice.leftDouble() * cropsPerMinute * 0.6) * 100) : "No Data"); // Multiply by 60 to convert to hourly and divide by 100 for rounding is combined into multiplying by 0.6 + + addSimpleIcoText(farmingTool, "Blocks/s: ", Formatting.YELLOW, Integer.toString(FarmingHud.blockBreaks())); //noinspection DataFlowIssue addComponent(new ProgressComponent(Ico.LANTERN, Text.literal("Farming Level: "), FarmingHud.farmingXpPercentProgress(), Formatting.GOLD.getColorValue())); addSimpleIcoText(Ico.LIME_DYE, "Farming XP/h: ", Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format((int) FarmingHud.farmingXpPerHour())); Entity cameraEntity = client.getCameraEntity(); - double yaw = cameraEntity == null ? 0.0d : cameraEntity.getYaw(); - double pitch = cameraEntity == null ? 0.0d : cameraEntity.getPitch(); - addComponent(new PlainTextComponent(Text.literal("Yaw: " + String.format("%.3f", MathHelper.wrapDegrees(yaw))).formatted(Formatting.YELLOW))); - addComponent(new PlainTextComponent(Text.literal("Pitch: " + String.format("%.3f", MathHelper.wrapDegrees(pitch))).formatted(Formatting.YELLOW))); + String yaw = cameraEntity == null ? "No Camera Entity" : String.format("%.3f", MathHelper.wrapDegrees(cameraEntity.getYaw())); + String pitch = cameraEntity == null ? "No Camera Entity" : String.format("%.3f", MathHelper.wrapDegrees(cameraEntity.getPitch())); + addComponent(new PlainTextComponent(Text.literal("Yaw: " + yaw).formatted(Formatting.YELLOW))); + addComponent(new PlainTextComponent(Text.literal("Pitch: " + pitch).formatted(Formatting.YELLOW))); if (LowerSensitivity.isSensitivityLowered()) { addComponent(new PlainTextComponent(Text.translatable("skyblocker.garden.hud.mouseLocked").formatted(Formatting.ITALIC))); } diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java index 1aa7708045..24f30e48b7 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java @@ -1,5 +1,7 @@ package de.hysky.skyblocker.utils; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.PropertyMap; @@ -9,7 +11,10 @@ import com.mojang.serialization.JsonOps; import com.mojang.serialization.codecs.RecordCodecBuilder; import de.hysky.skyblocker.SkyblockerMod; +import de.hysky.skyblocker.skyblock.item.tooltip.TooltipInfoType; +import it.unimi.dsi.fastutil.doubles.DoubleBooleanPair; import it.unimi.dsi.fastutil.ints.IntIntPair; +import it.unimi.dsi.fastutil.longs.LongBooleanPair; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.component.ComponentChanges; import net.minecraft.component.DataComponentTypes; @@ -64,7 +69,7 @@ public static LiteralArgumentBuilder dumpHeldItemComm } @SuppressWarnings("deprecation") - public static NbtCompound getCustomData(@NotNull ItemStack stack) { + public static NbtCompound getCustomData(@NotNull ItemStack stack) { return stack.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT).getNbt(); } @@ -74,7 +79,7 @@ public static NbtCompound getCustomData(@NotNull ItemStack stack) { * @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 Optional getItemIdOptional(@NotNull ItemStack stack) { NbtCompound customData = getCustomData(stack); return customData.contains(ID) ? Optional.of(customData.getString(ID)) : Optional.empty(); } @@ -85,7 +90,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 String getItemId(@NotNull ItemStack stack) { return getCustomData(stack).getString(ID); } @@ -95,7 +100,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 Optional getItemUuidOptional(@NotNull ItemStack stack) { NbtCompound customData = getCustomData(stack); return customData.contains(UUID) ? Optional.of(customData.getString(UUID)) : Optional.empty(); } @@ -106,10 +111,45 @@ 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 ItemStack stack) { + public static String getItemUuid(@NotNull ItemStack stack) { return getCustomData(stack).getString(UUID); } + /** + * Gets the bazaar sell price or the lowest bin based on the id of the item 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) { + return getItemPrice(getItemId(stack)); + } + + /** + * Gets the bazaar sell price or the lowest bin of the item with the specified id. + * + * @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) { + JsonObject bazaarPrices = TooltipInfoType.BAZAAR.getData(); + JsonObject lowestBinPrices = TooltipInfoType.LOWEST_BINS.getData(); + + if (id == null || id.isEmpty() || bazaarPrices == null || lowestBinPrices == null) return DoubleBooleanPair.of(0, false); + + if (bazaarPrices.has(id)) { + JsonElement sellPrice = bazaarPrices.get(id).getAsJsonObject().get("sellPrice"); + boolean isPriceNull = sellPrice.isJsonNull(); + return DoubleBooleanPair.of(isPriceNull ? 0 : sellPrice.getAsDouble(), !isPriceNull); + } + + if (lowestBinPrices.has(id)) { + return DoubleBooleanPair.of(lowestBinPrices.get(id).getAsDouble(), true); + } + + return DoubleBooleanPair.of(0, false); + } + /** * This method converts the "timestamp" variable into the same date format as Hypixel represents it in the museum. * Currently, there are two types of string timestamps the legacy which is built like this @@ -126,7 +166,7 @@ public static String getItemUuid(@NotNull ItemStack stack) { * @param stack the item under the pointer * @return if the item have a "Timestamp" it will be shown formated on the tooltip */ - public static String getTimestamp(ItemStack stack) { + public static String getTimestamp(ItemStack stack) { NbtCompound customData = getCustomData(stack); if (customData != null && customData.contains("timestamp", NbtElement.LONG_TYPE)) { @@ -197,7 +237,7 @@ public static Matcher getLoreLineIfMatch(ItemStack item, Pattern pattern) { public static List getLore(ItemStack item) { return item.getOrDefault(DataComponentTypes.LORE, LoreComponent.DEFAULT).styledLines(); } - + public static PropertyMap propertyMapWithTexture(String textureValue) { return Codecs.GAME_PROFILE_PROPERTY_MAP.parse(JsonOps.INSTANCE, JsonParser.parseString("[{\"name\":\"textures\",\"value\":\"" + textureValue + "\"}]")).getOrThrow(); } @@ -206,12 +246,12 @@ public static String getHeadTexture(ItemStack stack) { if (!stack.isOf(Items.PLAYER_HEAD) || !stack.contains(DataComponentTypes.PROFILE)) return ""; ProfileComponent profile = stack.get(DataComponentTypes.PROFILE); - String texture = profile.properties().get("textures").stream() + if (profile == null) return ""; + + return profile.properties().get("textures").stream() .map(Property::value) .findFirst() .orElse(""); - - return texture; } public static ItemStack getSkyblockerStack() { From f7210c0b8392823995f90936e7c5055d8ed44314 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sun, 5 May 2024 00:38:24 -0400 Subject: [PATCH 2/7] Fix mouse not unlocking when option is turned off --- .../skyblock/garden/FarmingHudWidget.java | 8 ++++---- .../skyblock/garden/LowerSensitivity.java | 17 ++++++----------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java index 9fba9249c3..d9e0902b1c 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java @@ -72,10 +72,10 @@ public void updateContent() { addSimpleIcoText(Ico.LIME_DYE, "Farming XP/h: ", Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format((int) FarmingHud.farmingXpPerHour())); Entity cameraEntity = client.getCameraEntity(); - String yaw = cameraEntity == null ? "No Camera Entity" : String.format("%.3f", MathHelper.wrapDegrees(cameraEntity.getYaw())); - String pitch = cameraEntity == null ? "No Camera Entity" : String.format("%.3f", MathHelper.wrapDegrees(cameraEntity.getPitch())); - addComponent(new PlainTextComponent(Text.literal("Yaw: " + yaw).formatted(Formatting.YELLOW))); - addComponent(new PlainTextComponent(Text.literal("Pitch: " + pitch).formatted(Formatting.YELLOW))); + String yaw = cameraEntity == null ? "No Camera Entity" : String.format("%.2f", MathHelper.wrapDegrees(cameraEntity.getYaw())); + String pitch = cameraEntity == null ? "No Camera Entity" : String.format("%.2f", MathHelper.wrapDegrees(cameraEntity.getPitch())); + addComponent(new PlainTextComponent(Text.literal("Yaw: " + yaw).formatted(Formatting.GOLD))); + addComponent(new PlainTextComponent(Text.literal("Pitch: " + pitch).formatted(Formatting.GOLD))); if (LowerSensitivity.isSensitivityLowered()) { addComponent(new PlainTextComponent(Text.translatable("skyblocker.garden.hud.mouseLocked").formatted(Formatting.ITALIC))); } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/LowerSensitivity.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/LowerSensitivity.java index ee85761860..2bfb7fb254 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/LowerSensitivity.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/LowerSensitivity.java @@ -9,28 +9,23 @@ import net.minecraft.item.ItemStack; public class LowerSensitivity { - private static boolean sensitivityLowered = false; public static void init() { ClientTickEvents.END_WORLD_TICK.register(world -> { - if (!Utils.isOnSkyblock() || Utils.getLocation() != Location.GARDEN || MinecraftClient.getInstance().player == null) { + if (Utils.getLocation() != Location.GARDEN || MinecraftClient.getInstance().player == null || !SkyblockerConfigManager.get().farming.garden.lockMouseTool) { if (sensitivityLowered) lowerSensitivity(false); return; } - if (SkyblockerConfigManager.get().farming.garden.lockMouseTool) { - ItemStack mainHandStack = MinecraftClient.getInstance().player.getMainHandStack(); - String itemId = ItemUtils.getItemId(mainHandStack); - boolean shouldLockMouse = FarmingHudWidget.FARMING_TOOLS.containsKey(itemId) && (!SkyblockerConfigManager.get().farming.garden.lockMouseGroundOnly || MinecraftClient.getInstance().player.isOnGround()); - if (shouldLockMouse && !sensitivityLowered) lowerSensitivity(true); - else if (!shouldLockMouse && sensitivityLowered) lowerSensitivity(false); - - } + ItemStack mainHandStack = MinecraftClient.getInstance().player.getMainHandStack(); + String itemId = ItemUtils.getItemId(mainHandStack); + boolean shouldLockMouse = FarmingHudWidget.FARMING_TOOLS.containsKey(itemId) && (!SkyblockerConfigManager.get().farming.garden.lockMouseGroundOnly || MinecraftClient.getInstance().player.isOnGround()); + if (shouldLockMouse && !sensitivityLowered) lowerSensitivity(true); + else if (!shouldLockMouse && sensitivityLowered) lowerSensitivity(false); }); } public static void lowerSensitivity(boolean lowerSensitivity) { - if (sensitivityLowered == lowerSensitivity) return; sensitivityLowered = lowerSensitivity; } From 3faf4f2e23ee6d7dccaa9bd724b9fd6d2ecbfb0c Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Fri, 17 May 2024 17:11:03 -0400 Subject: [PATCH 3/7] Add cultivating counter --- .../skyblock/garden/FarmingHud.java | 51 +++++++++++++++---- .../skyblock/garden/FarmingHudWidget.java | 2 +- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java index 201e6716ed..9f7f5ac9cf 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java @@ -33,8 +33,8 @@ public class FarmingHud { private static final Logger LOGGER = LoggerFactory.getLogger(FarmingHud.class); public static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance(Locale.US); - private static final Pattern COUNTER = Pattern.compile("Counter: (?[\\d,]+) .+"); private static final Pattern FARMING_XP = Pattern.compile("§3\\+(?\\d+.?\\d*) Farming \\((?[\\d,]+.?\\d*)%\\)"); + private static CounterType counterType; private static final Deque counter = new ArrayDeque<>(); private static final LongPriorityQueue blockBreaks = new LongArrayFIFOQueue(); private static final Queue farmingXp = new ArrayDeque<>(); @@ -54,16 +54,12 @@ public static void init() { } ItemStack stack = MinecraftClient.getInstance().player.getMainHandStack(); - Matcher matcher = ItemUtils.getLoreLineIfMatch(stack, FarmingHud.COUNTER); - if (matcher != null) { - try { - int count = NUMBER_FORMAT.parse(matcher.group("count")).intValue(); - if (counter.isEmpty() || counter.peekLast().leftInt() != count) { - counter.offer(IntLongPair.of(count, System.currentTimeMillis())); - } - } catch (ParseException e) { - LOGGER.error("[Skyblocker Farming HUD] Failed to parse counter", e); - } + if (tryParseCounter(stack, CounterType.CULTIVATING.pattern)) { + counterType = CounterType.CULTIVATING; + } else if (tryParseCounter(stack, CounterType.COUNTER.pattern)) { + counterType = CounterType.COUNTER; + } else { + counterType = CounterType.NONE; } FarmingHudWidget.INSTANCE.update(); @@ -92,10 +88,29 @@ public static void init() { .executes(Scheduler.queueOpenScreenCommand(() -> new FarmingHudConfigScreen(null))))))); } + private static boolean tryParseCounter(ItemStack stack, Pattern counterPattern) { + Matcher matcher = ItemUtils.getLoreLineIfMatch(stack, counterPattern); + if (matcher == null) return false; + try { + int count = NUMBER_FORMAT.parse(matcher.group("count")).intValue(); + if (counter.isEmpty() || counter.peekLast().leftInt() != count) { + counter.offer(IntLongPair.of(count, System.currentTimeMillis())); + } + return true; + } catch (ParseException e) { + LOGGER.error("[Skyblocker Farming HUD] Failed to parse counter", e); + return false; + } + } + private static boolean shouldRender() { return SkyblockerConfigManager.get().farming.garden.farmingHud.enableHud && Utils.getLocation() == Location.GARDEN; } + public static String counterText() { + return counterType.text; + } + public static int counter() { return counter.isEmpty() ? 0 : counter.peekLast().leftInt(); } @@ -120,4 +135,18 @@ public static float farmingXpPercentProgress() { public static double farmingXpPerHour() { return farmingXp.stream().mapToDouble(FloatLongPair::leftFloat).sum() * blockBreaks() * 1800; // Hypixel only sends xp updates around every half a second } + + public enum CounterType { + NONE(Pattern.compile(""), "No Counter: "), + COUNTER(Pattern.compile("Counter: (?[\\d,]+) .+"), "Counter: "), + CULTIVATING(Pattern.compile("Cultivating (?[IVXLCDM]+) (?[\\d,]+)"), "Cultivating Counter: "); + + private final Pattern pattern; + private final String text; + + CounterType(Pattern pattern, String text) { + this.pattern = pattern; + this.text = text; + } + } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java index d9e0902b1c..143567d42a 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java @@ -60,7 +60,7 @@ public void updateContent() { if (client.player == null) return; ItemStack farmingTool = client.player.getMainHandStack(); - addSimpleIcoText(farmingTool, "Counter: ", Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format(FarmingHud.counter())); + addSimpleIcoText(farmingTool, FarmingHud.counterText(), Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format(FarmingHud.counter())); float cropsPerMinute = FarmingHud.cropsPerMinute(); addSimpleIcoText(farmingTool, "Crops/min: ", Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format((int) cropsPerMinute / 10 * 10)); DoubleBooleanPair itemPrice = ItemUtils.getItemPrice(FARMING_TOOLS.get(ItemUtils.getItemId(farmingTool))); From 118c4b707e46af5b697ccab73f5509069890570f Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Fri, 24 May 2024 17:48:42 -0400 Subject: [PATCH 4/7] Fix bugs and reset counter --- .../skyblocker/skyblock/garden/FarmingHud.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java index 9f7f5ac9cf..f3e8695c3b 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java @@ -34,7 +34,7 @@ public class FarmingHud { private static final Logger LOGGER = LoggerFactory.getLogger(FarmingHud.class); public static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance(Locale.US); private static final Pattern FARMING_XP = Pattern.compile("§3\\+(?\\d+.?\\d*) Farming \\((?[\\d,]+.?\\d*)%\\)"); - private static CounterType counterType; + private static CounterType counterType = CounterType.NONE; private static final Deque counter = new ArrayDeque<>(); private static final LongPriorityQueue blockBreaks = new LongArrayFIFOQueue(); private static final Queue farmingXp = new ArrayDeque<>(); @@ -54,11 +54,7 @@ public static void init() { } ItemStack stack = MinecraftClient.getInstance().player.getMainHandStack(); - if (tryParseCounter(stack, CounterType.CULTIVATING.pattern)) { - counterType = CounterType.CULTIVATING; - } else if (tryParseCounter(stack, CounterType.COUNTER.pattern)) { - counterType = CounterType.COUNTER; - } else { + if (!tryParseCounter(stack, CounterType.CULTIVATING) && !tryParseCounter(stack, CounterType.COUNTER)) { counterType = CounterType.NONE; } @@ -88,11 +84,15 @@ public static void init() { .executes(Scheduler.queueOpenScreenCommand(() -> new FarmingHudConfigScreen(null))))))); } - private static boolean tryParseCounter(ItemStack stack, Pattern counterPattern) { - Matcher matcher = ItemUtils.getLoreLineIfMatch(stack, counterPattern); + private static boolean tryParseCounter(ItemStack stack, CounterType counterType) { + Matcher matcher = ItemUtils.getLoreLineIfMatch(stack, counterType.pattern); if (matcher == null) return false; try { int count = NUMBER_FORMAT.parse(matcher.group("count")).intValue(); + if (FarmingHud.counterType != counterType) { + counter.clear(); + FarmingHud.counterType = counterType; + } if (counter.isEmpty() || counter.peekLast().leftInt() != count) { counter.offer(IntLongPair.of(count, System.currentTimeMillis())); } From 02b66f64a530e54f6cfec65021b06fb89f89648a Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Fri, 24 May 2024 17:51:27 -0400 Subject: [PATCH 5/7] Fix cocoa beans --- .../de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java index 143567d42a..9e1c84f4cb 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java @@ -43,7 +43,7 @@ public class FarmingHudWidget extends Widget { Map.entry("PUMPKIN_DICER", "PUMPKIN"), Map.entry("PUMPKIN_DICER_2", "PUMPKIN"), Map.entry("PUMPKIN_DICER_3", "PUMPKIN"), - Map.entry("COCO_CHOPPER", "INK_SACK.3") + Map.entry("COCO_CHOPPER", "INK_SACK:3") ); public static final FarmingHudWidget INSTANCE = new FarmingHudWidget(); private final MinecraftClient client = MinecraftClient.getInstance(); From 37778fdf996b037b7bf75ffa2f2738327aba0f46 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Thu, 30 May 2024 18:05:04 +0800 Subject: [PATCH 6/7] Improve null safety --- .../skyblocker/skyblock/garden/FarmingHud.java | 7 ++++--- .../skyblock/garden/FarmingHudWidget.java | 14 +++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java index f3e8695c3b..fa6b6c19c0 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java @@ -34,6 +34,7 @@ public class FarmingHud { private static final Logger LOGGER = LoggerFactory.getLogger(FarmingHud.class); public static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance(Locale.US); private static final Pattern FARMING_XP = Pattern.compile("§3\\+(?\\d+.?\\d*) Farming \\((?[\\d,]+.?\\d*)%\\)"); + private static final MinecraftClient client = MinecraftClient.getInstance(); private static CounterType counterType = CounterType.NONE; private static final Deque counter = new ArrayDeque<>(); private static final LongPriorityQueue blockBreaks = new LongArrayFIFOQueue(); @@ -53,8 +54,8 @@ public static void init() { farmingXp.poll(); } - ItemStack stack = MinecraftClient.getInstance().player.getMainHandStack(); - if (!tryParseCounter(stack, CounterType.CULTIVATING) && !tryParseCounter(stack, CounterType.COUNTER)) { + ItemStack stack = client.player.getMainHandStack(); + if (stack == null || !tryParseCounter(stack, CounterType.CULTIVATING) && !tryParseCounter(stack, CounterType.COUNTER)) { counterType = CounterType.NONE; } @@ -104,7 +105,7 @@ private static boolean tryParseCounter(ItemStack stack, CounterType counterType) } private static boolean shouldRender() { - return SkyblockerConfigManager.get().farming.garden.farmingHud.enableHud && Utils.getLocation() == Location.GARDEN; + return SkyblockerConfigManager.get().farming.garden.farmingHud.enableHud && client.player != null && Utils.getLocation() == Location.GARDEN; } public static String counterText() { diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java index 9e1c84f4cb..eb4448133b 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java @@ -1,6 +1,7 @@ package de.hysky.skyblocker.skyblock.garden; import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.skyblock.itemlist.ItemRepository; import de.hysky.skyblocker.skyblock.tabhud.util.Ico; import de.hysky.skyblocker.skyblock.tabhud.widget.Widget; import de.hysky.skyblocker.skyblock.tabhud.widget.component.PlainTextComponent; @@ -58,15 +59,18 @@ public FarmingHudWidget() { @Override public void updateContent() { if (client.player == null) return; + ItemStack farmingToolStack = client.player.getMainHandStack(); + if (farmingToolStack == null) return; + String cropItemId = FARMING_TOOLS.get(ItemUtils.getItemId(farmingToolStack)); + ItemStack cropStack = ItemRepository.getItemStack(cropItemId); - ItemStack farmingTool = client.player.getMainHandStack(); - addSimpleIcoText(farmingTool, FarmingHud.counterText(), Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format(FarmingHud.counter())); + addSimpleIcoText(cropStack, FarmingHud.counterText(), Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format(FarmingHud.counter())); float cropsPerMinute = FarmingHud.cropsPerMinute(); - addSimpleIcoText(farmingTool, "Crops/min: ", Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format((int) cropsPerMinute / 10 * 10)); - DoubleBooleanPair itemPrice = ItemUtils.getItemPrice(FARMING_TOOLS.get(ItemUtils.getItemId(farmingTool))); + addSimpleIcoText(cropStack, "Crops/min: ", Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format((int) cropsPerMinute / 10 * 10)); + DoubleBooleanPair itemPrice = ItemUtils.getItemPrice(cropItemId); addSimpleIcoText(Ico.GOLD, "Coins/h: ", Formatting.GOLD, itemPrice.rightBoolean() ? FarmingHud.NUMBER_FORMAT.format((int) (itemPrice.leftDouble() * cropsPerMinute * 0.6) * 100) : "No Data"); // Multiply by 60 to convert to hourly and divide by 100 for rounding is combined into multiplying by 0.6 - addSimpleIcoText(farmingTool, "Blocks/s: ", Formatting.YELLOW, Integer.toString(FarmingHud.blockBreaks())); + addSimpleIcoText(cropStack, "Blocks/s: ", Formatting.YELLOW, Integer.toString(FarmingHud.blockBreaks())); //noinspection DataFlowIssue addComponent(new ProgressComponent(Ico.LANTERN, Text.literal("Farming Level: "), FarmingHud.farmingXpPercentProgress(), Formatting.GOLD.getColorValue())); addSimpleIcoText(Ico.LIME_DYE, "Farming XP/h: ", Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format((int) FarmingHud.farmingXpPerHour())); From 63b4fc4838516725db4b535b5d6de837fece5a69 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Fri, 7 Jun 2024 12:13:15 +0800 Subject: [PATCH 7/7] Switch from tooltip to nbt --- .../skyblock/garden/FarmingHud.java | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java index fa6b6c19c0..b845e07a72 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java @@ -16,6 +16,8 @@ import net.fabricmc.fabric.api.event.client.player.ClientPlayerBlockBreakEvents; import net.minecraft.client.MinecraftClient; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,7 +46,7 @@ public class FarmingHud { public static void init() { HudRenderEvents.AFTER_MAIN_HUD.register((context, tickDelta) -> { if (shouldRender()) { - if (!counter.isEmpty() && counter.peek().rightLong() + 10_000 < System.currentTimeMillis()) { + if (!counter.isEmpty() && counter.peek().rightLong() + 5000 < System.currentTimeMillis()) { counter.poll(); } if (!blockBreaks.isEmpty() && blockBreaks.firstLong() + 1000 < System.currentTimeMillis()) { @@ -55,7 +57,7 @@ public static void init() { } ItemStack stack = client.player.getMainHandStack(); - if (stack == null || !tryParseCounter(stack, CounterType.CULTIVATING) && !tryParseCounter(stack, CounterType.COUNTER)) { + if (stack == null || !tryGetCounter(stack, CounterType.CULTIVATING) && !tryGetCounter(stack, CounterType.COUNTER)) { counterType = CounterType.NONE; } @@ -85,23 +87,18 @@ public static void init() { .executes(Scheduler.queueOpenScreenCommand(() -> new FarmingHudConfigScreen(null))))))); } - private static boolean tryParseCounter(ItemStack stack, CounterType counterType) { - Matcher matcher = ItemUtils.getLoreLineIfMatch(stack, counterType.pattern); - if (matcher == null) return false; - try { - int count = NUMBER_FORMAT.parse(matcher.group("count")).intValue(); - if (FarmingHud.counterType != counterType) { - counter.clear(); - FarmingHud.counterType = counterType; - } - if (counter.isEmpty() || counter.peekLast().leftInt() != count) { - counter.offer(IntLongPair.of(count, System.currentTimeMillis())); - } - return true; - } catch (ParseException e) { - LOGGER.error("[Skyblocker Farming HUD] Failed to parse counter", e); - return false; + private static boolean tryGetCounter(ItemStack stack, CounterType counterType) { + NbtCompound customData = ItemUtils.getCustomData(stack); + if (customData == null || !customData.contains(counterType.nbtKey, NbtElement.NUMBER_TYPE)) return false; + int count = customData.getInt(counterType.nbtKey); + if (FarmingHud.counterType != counterType) { + counter.clear(); + FarmingHud.counterType = counterType; + } + if (counter.isEmpty() || counter.peekLast().leftInt() != count) { + counter.offer(IntLongPair.of(count, System.currentTimeMillis())); } + return true; } private static boolean shouldRender() { @@ -138,15 +135,15 @@ public static double farmingXpPerHour() { } public enum CounterType { - NONE(Pattern.compile(""), "No Counter: "), - COUNTER(Pattern.compile("Counter: (?[\\d,]+) .+"), "Counter: "), - CULTIVATING(Pattern.compile("Cultivating (?[IVXLCDM]+) (?[\\d,]+)"), "Cultivating Counter: "); + NONE("", "No Counter: "), + COUNTER("mined_crops", "Counter: "), + CULTIVATING("farmed_cultivating", "Cultivating Counter: "); - private final Pattern pattern; + private final String nbtKey; private final String text; - CounterType(Pattern pattern, String text) { - this.pattern = pattern; + CounterType(String nbtKey, String text) { + this.nbtKey = nbtKey; this.text = text; } }