aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorviciscat <51047087+viciscat@users.noreply.github.com>2025-06-05 19:49:19 +0200
committerGitHub <noreply@github.com>2025-06-05 13:49:19 -0400
commitb3634c3326fff2488d27b5306e46c69937d23e85 (patch)
tree76d92ee8e16cd93ebb065be264bc01ccdaa32e68 /src/main/java
parent28802fb1db9e92fe0344b0063f6bd895f0db12e2 (diff)
downloadSkyblocker-b3634c3326fff2488d27b5306e46c69937d23e85.tar.gz
Skyblocker-b3634c3326fff2488d27b5306e46c69937d23e85.tar.bz2
Skyblocker-b3634c3326fff2488d27b5306e46c69937d23e85.zip
optional parse int (#1303)
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/SkillLevelAdder.java5
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/Utils.java15
2 files changed, 18 insertions, 2 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/SkillLevelAdder.java b/src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/SkillLevelAdder.java
index 885b0789..2640fb73 100644
--- a/src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/SkillLevelAdder.java
+++ b/src/main/java/de/hysky/skyblocker/skyblock/item/slottext/adders/SkillLevelAdder.java
@@ -4,6 +4,7 @@ import de.hysky.skyblocker.skyblock.item.slottext.SlotText;
import de.hysky.skyblocker.skyblock.item.slottext.SimpleSlotTextAdder;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.RomanNumerals;
+import de.hysky.skyblocker.utils.Utils;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
@@ -31,9 +32,9 @@ public class SkillLevelAdder extends SimpleSlotTextAdder {
String romanNumeral = name.substring(lastIndex + 1); //+1 because we don't need the space itself
//The "romanNumeral" might be a latin numeral, too. There's a skyblock setting for this, so we have to do it this way V
if (ItemUtils.getLoreLineIf(stack, s -> s.contains("Max Skill level reached!")) != null) {
- return SlotText.bottomLeftList(Text.literal(String.valueOf(RomanNumerals.isValidRomanNumeral(romanNumeral) ? RomanNumerals.romanToDecimal(romanNumeral) : Integer.parseInt(romanNumeral))).withColor(SlotText.GOLD));
+ return SlotText.bottomLeftList(Text.literal(String.valueOf(RomanNumerals.isValidRomanNumeral(romanNumeral) ? RomanNumerals.romanToDecimal(romanNumeral) : Utils.parseInt(romanNumeral).orElse(0))).withColor(SlotText.GOLD));
} else {
- return SlotText.bottomLeftList(Text.literal(String.valueOf(RomanNumerals.isValidRomanNumeral(romanNumeral) ? RomanNumerals.romanToDecimal(romanNumeral) : Integer.parseInt(romanNumeral))).withColor(SlotText.CREAM));
+ return SlotText.bottomLeftList(Text.literal(String.valueOf(RomanNumerals.isValidRomanNumeral(romanNumeral) ? RomanNumerals.romanToDecimal(romanNumeral) : Utils.parseInt(romanNumeral).orElse(0))).withColor(SlotText.CREAM));
}
}
default -> {
diff --git a/src/main/java/de/hysky/skyblocker/utils/Utils.java b/src/main/java/de/hysky/skyblocker/utils/Utils.java
index 5f9d6955..36546cac 100644
--- a/src/main/java/de/hysky/skyblocker/utils/Utils.java
+++ b/src/main/java/de/hysky/skyblocker/utils/Utils.java
@@ -40,6 +40,7 @@ import org.slf4j.LoggerFactory;
import java.time.Instant;
import java.util.Collections;
+import java.util.OptionalInt;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -591,4 +592,18 @@ public class Utils {
// Null check on client for tests
return client != null && client.getNetworkHandler() != null && client.getNetworkHandler().getRegistryManager() != null ? client.getNetworkHandler().getRegistryManager() : LOOKUP;
}
+
+ /**
+ * Parses an int from a string
+ * @param input the string to parse
+ * @return the int parsed or an empty optional if it failed
+ * @implNote Does not log the exception if thrown
+ */
+ public static OptionalInt parseInt(String input) {
+ try {
+ return OptionalInt.of(Integer.parseInt(input));
+ } catch (NumberFormatException e) {
+ return OptionalInt.empty();
+ }
+ }
}