use of net.glowstone.util.nbt.IntArrayTag in project Glowstone by GlowstoneMC.
the class Mojangson method parseArray.
/**
* Parses an Array value from a Mojangson string.
*
* @param mojangson The Mojangson string
* @return a ByteArrayTag value if the array contains byte values, an IntArrayTag value if the
* array contains int values or a ListTag with the array's elements.
* @throws MojangsonParseException if the Mojangson string could not be parsed as an
* Array value.
*/
public static Tag parseArray(String mojangson) throws MojangsonParseException {
// Parsing context magic value
final int parseArrayStart = 0;
// Parsing context magic value
final int parseArrayElement = 1;
// The current context of the parser
int context = parseArrayStart;
// Temporary value being parsed, in its raw form
String tmpval = "";
// The scope level of the array, this allows coherent nested arrays and compounds.
int scope = 0;
// The current character is part of a string inclusion
boolean inString = false;
// The element content type.
TagType tagType = null;
List<Tag> values = new ArrayList<>();
for (int index = 0; index < mojangson.length(); index++) {
char character = mojangson.charAt(index);
if (character == STRING_QUOTES.getSymbol()) {
inString = !inString;
}
if (character == WHITE_SPACE.getSymbol()) {
if (!inString) {
continue;
}
}
if ((character == COMPOUND_START.getSymbol() || character == ARRAY_START.getSymbol()) && !inString) {
scope++;
}
if ((character == COMPOUND_END.getSymbol() || character == ARRAY_END.getSymbol()) && !inString) {
scope--;
}
if (context == parseArrayStart) {
if (character != ARRAY_START.getSymbol()) {
throw new MojangsonParseException("Index: " + index + ", symbol: \'" + character + "\'", MojangsonParseException.ParseExceptionReason.UNEXPECTED_SYMBOL);
}
context++;
continue;
}
if (context == parseArrayElement) {
if ((character == ELEMENT_SEPERATOR.getSymbol() || character == ARRAY_END.getSymbol()) && scope <= 1 && !inString) {
if (tmpval.length() == 0) {
continue;
}
Tag val = parseTag(tmpval);
if (tagType == null) {
tagType = val.getType();
} else if (tagType != val.getType()) {
throw new MojangsonParseException("Index: " + index + ", value: \'" + tmpval + "\'", MojangsonParseException.ParseExceptionReason.INCOMPATIBLE_TYPE);
}
values.add(val);
tmpval = "";
continue;
}
tmpval += character;
}
}
if (tagType == TagType.BYTE) {
byte[] bytes = new byte[values.size()];
for (int i = 0; i < values.size(); i++) {
bytes[i] = (byte) values.get(i).getValue();
}
return new ByteArrayTag(bytes);
} else if (tagType == TagType.INT) {
int[] ints = new int[values.size()];
for (int i = 0; i < values.size(); i++) {
ints[i] = (int) values.get(i).getValue();
}
return new IntArrayTag(ints);
} else {
return new ListTag<>(tagType, values);
}
}
Aggregations