aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java')
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java b/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java
index 5fac9208..7b714cd0 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java
@@ -653,6 +653,41 @@ public class Utils {
return (float) Math.round(value * scale) / scale;
}
+ // Parses Roman numerals, allowing for single character irregular subtractive notation (e.g. IL is 49, IIL is invalid)
+ public static int parseRomanNumeral(String input) {
+ int prevVal = 0;
+ int total = 0;
+ for (int i = input.length()-1; i >= 0; i--) {
+ int val;
+ char ch = input.charAt(i);
+ switch (ch) {
+ case 'I' : val = 1; break;
+ case 'V' : val = 5; break;
+ case 'X' : val = 10; break;
+ case 'L' : val = 50; break;
+ case 'C' : val = 100; break;
+ case 'D' : val = 500; break;
+ case 'M' : val = 1000; break;
+ default: throw new IllegalArgumentException("Invalid Roman Numeral Character: " + ch);
+ }
+ if (val < prevVal) val = -val;
+ total += val;
+ prevVal = val;
+ }
+
+ return total;
+ }
+
+ public static int parseIntOrRomanNumeral(String input) {
+ // 0 through 9, '-', and '+' come before 'A' in ANSI, UTF8, and UTF16 character sets
+ //
+ if (input.charAt(0) < 'A') {
+ return Integer.parseInt(input);
+ }
+
+ return parseRomanNumeral(input);
+ }
+
public static void playPressSound() {
playSound(new ResourceLocation("gui.button.press"), true);
}