Skip to content

Commit 4072f62

Browse files
committed
normalize NBT string as number if it has number suffix
1 parent d7fadad commit 4072f62

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

nbt/src/main/java/io/github/projectunified/craftitem/nbt/NBTMapNormalizer.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,52 @@ public static Object normalize(Object value, UnaryOperator<String> translator) {
6969
return result;
7070
}
7171

72+
if (value instanceof String) {
73+
Number number = tryParseNumberWithSuffix(((String) value).trim());
74+
if (number != null) {
75+
return number;
76+
}
77+
}
78+
7279
return value;
7380
}
7481

82+
/**
83+
* Attempts to parse a string as a number with a type suffix
84+
*/
85+
private static Number tryParseNumberWithSuffix(String str) {
86+
if (str.length() < 2) {
87+
return null;
88+
}
89+
90+
char lastChar = str.charAt(str.length() - 1);
91+
String numPart = str.substring(0, str.length() - 1);
92+
93+
try {
94+
switch (lastChar) {
95+
case 'b':
96+
case 'B':
97+
return Byte.parseByte(numPart);
98+
case 's':
99+
case 'S':
100+
return Short.parseShort(numPart);
101+
case 'l':
102+
case 'L':
103+
return Long.parseLong(numPart);
104+
case 'f':
105+
case 'F':
106+
return Float.parseFloat(numPart);
107+
case 'd':
108+
case 'D':
109+
return Double.parseDouble(numPart);
110+
}
111+
} catch (NumberFormatException e) {
112+
// Not a valid number with this suffix
113+
}
114+
115+
return null;
116+
}
117+
75118
/**
76119
* Normalizes a forced-value based on specified $type
77120
*/

nbt/src/main/java/io/github/projectunified/craftitem/nbt/SNBTConverter.java

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,6 @@ private static String convertToLongArray(long[] arr) {
184184
private static String convertStringValue(String str) {
185185
str = str.trim();
186186

187-
// Try parsing as number with suffix (byte, short, long, float, double)
188-
String numberWithSuffix = tryParseNumberWithSuffix(str);
189-
if (numberWithSuffix != null) {
190-
return numberWithSuffix;
191-
}
192-
193187
// Try parsing as plain number (int or double)
194188
String plainNumber = tryParseNumber(str);
195189
if (plainNumber != null) {
@@ -200,47 +194,6 @@ private static String convertStringValue(String str) {
200194
return escapeString(str);
201195
}
202196

203-
/**
204-
* Attempts to parse a string as a number with a type suffix
205-
*/
206-
private static String tryParseNumberWithSuffix(String str) {
207-
if (str.length() < 2) {
208-
return null;
209-
}
210-
211-
char lastChar = str.charAt(str.length() - 1);
212-
String numPart = str.substring(0, str.length() - 1);
213-
214-
try {
215-
switch (lastChar) {
216-
case 'b':
217-
case 'B':
218-
Byte.parseByte(numPart);
219-
return str.toLowerCase().replace('B', 'b');
220-
case 's':
221-
case 'S':
222-
Short.parseShort(numPart);
223-
return str.toLowerCase().replace('S', 's');
224-
case 'l':
225-
case 'L':
226-
Long.parseLong(numPart);
227-
return str.toUpperCase().replace('l', 'L');
228-
case 'f':
229-
case 'F':
230-
Float.parseFloat(numPart);
231-
return str.toLowerCase().replace('F', 'f');
232-
case 'd':
233-
case 'D':
234-
Double.parseDouble(numPart);
235-
return numPart;
236-
}
237-
} catch (NumberFormatException e) {
238-
// Not a valid number with this suffix
239-
}
240-
241-
return null;
242-
}
243-
244197
/**
245198
* Attempts to parse a string as a plain number (int or double)
246199
*/

0 commit comments

Comments
 (0)