diff options
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/skyblock/garden')
3 files changed, 91 insertions, 58 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; + } + } } 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 f12f6fa8..eb444813 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHudWidget.java @@ -1,11 +1,13 @@ 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; 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 +20,31 @@ import java.util.Map; public class FarmingHudWidget extends Widget { private static final MutableText TITLE = Text.literal("Farming").formatted(Formatting.YELLOW, Formatting.BOLD); - public static final Map<String, ItemStack> 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<String, String> 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 +58,28 @@ public class FarmingHudWidget extends Widget { @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 farmingToolStack = client.player.getMainHandStack(); + if (farmingToolStack == null) return; + String cropItemId = FARMING_TOOLS.get(ItemUtils.getItemId(farmingToolStack)); + ItemStack cropStack = ItemRepository.getItemStack(cropItemId); + + addSimpleIcoText(cropStack, FarmingHud.counterText(), Formatting.YELLOW, FarmingHud.NUMBER_FORMAT.format(FarmingHud.counter())); + float cropsPerMinute = FarmingHud.cropsPerMinute(); + 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(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())); 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("%.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 ee857618..2bfb7fb2 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.client.MinecraftClient; 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; } |