Skip to content

Commit

Permalink
Add cultivating counter
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed May 20, 2024
1 parent f7210c0 commit 3faf4f2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
51 changes: 40 additions & 11 deletions src/main/java/de/hysky/skyblocker/skyblock/garden/FarmingHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -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: (?<count>[\\d,]+) .+");
private static final Pattern FARMING_XP = Pattern.compile("§3\\+(?<xp>\\d+.?\\d*) Farming \\((?<percent>[\\d,]+.?\\d*)%\\)");
private static CounterType counterType;
private static final Deque<IntLongPair> counter = new ArrayDeque<>();
private static final LongPriorityQueue blockBreaks = new LongArrayFIFOQueue();
private static final Queue<FloatLongPair> farmingXp = new ArrayDeque<>();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
Expand All @@ -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: (?<count>[\\d,]+) .+"), "Counter: "),
CULTIVATING(Pattern.compile("Cultivating (?<cultivating>[IVXLCDM]+) (?<count>[\\d,]+)"), "Cultivating Counter: ");

private final Pattern pattern;
private final String text;

CounterType(Pattern pattern, String text) {
this.pattern = pattern;
this.text = text;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down

0 comments on commit 3faf4f2

Please sign in to comment.