diff options
author | Kevin <92656833+kevinthegreat1@users.noreply.github.com> | 2024-06-10 11:48:18 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-10 11:48:18 +0800 |
commit | ec1c0104a17d9e3a5741efa38528d628b53d940d (patch) | |
tree | f167fe49f102b1bf8a7af05e48d76f28d44df5a0 /src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java | |
parent | 4bde27dc8c1e89d38f76477c5cd8d30a411c8bb6 (diff) | |
parent | 63b4fc4838516725db4b535b5d6de837fece5a69 (diff) | |
download | Skyblocker-ec1c0104a17d9e3a5741efa38528d628b53d940d.tar.gz Skyblocker-ec1c0104a17d9e3a5741efa38528d628b53d940d.tar.bz2 Skyblocker-ec1c0104a17d9e3a5741efa38528d628b53d940d.zip |
Merge pull request #685 from kevinthegreat1/farming-hud-improvements
Farming hud improvements
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java | 55 |
1 files changed, 41 insertions, 14 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 201e6716..b845e07a 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.client.message.v1.ClientReceiveMessageEvents; 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; @@ -33,8 +35,9 @@ import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.lit 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: (?<count>[\\d,]+) .+"); private static final Pattern FARMING_XP = Pattern.compile("ยง3\\+(?<xp>\\d+.?\\d*) Farming \\((?<percent>[\\d,]+.?\\d*)%\\)"); + private static final MinecraftClient client = MinecraftClient.getInstance(); + private static CounterType counterType = CounterType.NONE; private static final Deque<IntLongPair> counter = new ArrayDeque<>(); private static final LongPriorityQueue blockBreaks = new LongArrayFIFOQueue(); private static final Queue<FloatLongPair> farmingXp = new ArrayDeque<>(); @@ -43,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()) { @@ -53,17 +56,9 @@ public class FarmingHud { farmingXp.poll(); } - 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); - } + ItemStack stack = client.player.getMainHandStack(); + if (stack == null || !tryGetCounter(stack, CounterType.CULTIVATING) && !tryGetCounter(stack, CounterType.COUNTER)) { + counterType = CounterType.NONE; } FarmingHudWidget.INSTANCE.update(); @@ -92,8 +87,26 @@ public class FarmingHud { .executes(Scheduler.queueOpenScreenCommand(() -> new FarmingHudConfigScreen(null))))))); } + 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() { - 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() { + return counterType.text; } public static int counter() { @@ -120,4 +133,18 @@ public class FarmingHud { 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("", "No Counter: "), + COUNTER("mined_crops", "Counter: "), + CULTIVATING("farmed_cultivating", "Cultivating Counter: "); + + private final String nbtKey; + private final String text; + + CounterType(String nbtKey, String text) { + this.nbtKey = nbtKey; + this.text = text; + } + } } |