Skip to content

Commit 202ccd7

Browse files
committed
Implemented more methods
1 parent 01882fa commit 202ccd7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

MCStructs-snbt/src/main/java/net/lenni0451/mcstructs/snbt/impl/v1_12/StringReader_v1_12.java

+28
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ public void skipWhitespaces() {
4747
while (this.canRead() && Character.isWhitespace(this.peek())) this.index++;
4848
}
4949

50+
public int readInt() throws SNbtDeserializeException {
51+
int start = this.index;
52+
while (this.canRead() && this.isNumerical(this.peek())) this.index++;
53+
String number = this.s.substring(start, this.index);
54+
if (number.isEmpty()) throw new SNbtDeserializeException("Expected double but got nothing", this.s, this.index);
55+
try {
56+
return Integer.parseInt(number);
57+
} catch (NumberFormatException e) {
58+
throw new SNbtDeserializeException("Expected double but got '" + number + "'", this.s, start);
59+
}
60+
}
61+
62+
public double readDouble() throws SNbtDeserializeException {
63+
int start = this.index;
64+
while (this.canRead() && this.isNumerical(this.peek())) this.index++;
65+
String number = this.s.substring(start, this.index);
66+
if (number.isEmpty()) throw new SNbtDeserializeException("Expected double but got nothing", this.s, this.index);
67+
try {
68+
return Double.parseDouble(number);
69+
} catch (NumberFormatException e) {
70+
throw new SNbtDeserializeException("Expected double but got '" + number + "'", this.s, start);
71+
}
72+
}
73+
5074
public String readString() throws SNbtDeserializeException {
5175
this.skipWhitespaces();
5276
if (!this.canRead()) return null;
@@ -95,6 +119,10 @@ protected boolean isQuote(final char c) {
95119
return c == '"';
96120
}
97121

122+
private boolean isNumerical(final char c) {
123+
return c >= '0' && c <= '9' || c == '.' || c == '-';
124+
}
125+
98126
private boolean isAlphanumeric(final char c) {
99127
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c == '.' || c == '+';
100128
}

0 commit comments

Comments
 (0)