From 13a1c03698716178822b81b4510e63908ce8e5dd Mon Sep 17 00:00:00 2001 From: jani270 <69345714+jani270@users.noreply.github.com> Date: Sun, 31 Dec 2023 14:26:12 +0100 Subject: Changed gatherJacobData to a regex and fixed a NFE (#980) * Fixed cleanSpecialChars not working Co-Authored-By: hannibal2 <24389977+hannibal002@users.noreply.github.com> * Changed logic of gatherjacobdata and fixed #808 Co-Authored-By: hannibal2 <24389977+hannibal002@users.noreply.github.com> * Removed unused import * Moved Pattern outside gatherJacobData * Made the regex more explicit. --------- Co-authored-by: hannibal2 <24389977+hannibal002@users.noreply.github.com> --- .../overlays/FarmingSkillOverlay.java | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src/main/java/io') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/overlays/FarmingSkillOverlay.java b/src/main/java/io/github/moulberry/notenoughupdates/overlays/FarmingSkillOverlay.java index 3c8f377d..f071c788 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/overlays/FarmingSkillOverlay.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/overlays/FarmingSkillOverlay.java @@ -29,6 +29,7 @@ import io.github.moulberry.notenoughupdates.util.SidebarUtil; import io.github.moulberry.notenoughupdates.util.Utils; import io.github.moulberry.notenoughupdates.util.XPInformation; import io.github.moulberry.notenoughupdates.util.hypixelapi.HypixelItemAPI; +import lombok.val; import net.minecraft.client.Minecraft; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -40,6 +41,7 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.function.Supplier; +import java.util.regex.Pattern; public class FarmingSkillOverlay extends TextOverlay { private static final NumberFormat format = NumberFormat.getIntegerInstance(); @@ -88,6 +90,9 @@ public class FarmingSkillOverlay extends TextOverlay { private String skillType = "Farming"; + private static final Pattern CONTEST_AMOUNT_PATTERN = Pattern.compile( + " (Collected|(BRONZE|SILVER|GOLD|PLATINUM|DIAMOND) with) (?[\\d,]+)"); + public FarmingSkillOverlay( Position position, Supplier> dummyStrings, @@ -136,14 +141,17 @@ public class FarmingSkillOverlay extends TextOverlay { inJacobContest = false; if (isJacobTime()) { int timeLeftInContest = (20 * 60) - ((int) ((System.currentTimeMillis() % 3600000 - 900000) / 1000)); - int cropsFarmed = -1; for (String line : SidebarUtil.readSidebarLines()) { - if (line.contains("Collected") || line.contains("BRONZE") || line.contains("SILVER") || - line.contains("GOLD") || line.contains("PLATINUM") || line.contains("DIAMOND")) { - inJacobContest = true; - String l = line.replaceAll("[^A-Za-z0-9() ]", ""); - cropsFarmed = Integer.parseInt(l.substring(l.lastIndexOf(" ") + 1).replace(",", "")); + val matcher = CONTEST_AMOUNT_PATTERN.matcher(line); + if (matcher.matches()) { + String amount = matcher.group("amount").replace(",", ""); + try { + inJacobContest = true; + cropsFarmed = Integer.parseInt(amount); + } catch (NumberFormatException e) { + e.printStackTrace(); + } } jacobPrediction = (int) (cropsFarmed + (cropsPerSecond * timeLeftInContest)); } -- cgit