Search in sources :

Example 1 with ByteArrayTag

use of net.glowstone.util.nbt.ByteArrayTag 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);
    }
}
Also used : IntArrayTag(net.glowstone.util.nbt.IntArrayTag) ArrayList(java.util.ArrayList) MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) TagType(net.glowstone.util.nbt.TagType) CompoundTag(net.glowstone.util.nbt.CompoundTag) ByteArrayTag(net.glowstone.util.nbt.ByteArrayTag) ListTag(net.glowstone.util.nbt.ListTag) FloatTag(net.glowstone.util.nbt.FloatTag) StringTag(net.glowstone.util.nbt.StringTag) DoubleTag(net.glowstone.util.nbt.DoubleTag) IntArrayTag(net.glowstone.util.nbt.IntArrayTag) LongArrayTag(net.glowstone.util.nbt.LongArrayTag) IntTag(net.glowstone.util.nbt.IntTag) LongTag(net.glowstone.util.nbt.LongTag) ShortTag(net.glowstone.util.nbt.ShortTag) ByteTag(net.glowstone.util.nbt.ByteTag) Tag(net.glowstone.util.nbt.Tag) ByteArrayTag(net.glowstone.util.nbt.ByteArrayTag)

Aggregations

ArrayList (java.util.ArrayList)1 MojangsonParseException (net.glowstone.util.mojangson.ex.MojangsonParseException)1 ByteArrayTag (net.glowstone.util.nbt.ByteArrayTag)1 ByteTag (net.glowstone.util.nbt.ByteTag)1 CompoundTag (net.glowstone.util.nbt.CompoundTag)1 DoubleTag (net.glowstone.util.nbt.DoubleTag)1 FloatTag (net.glowstone.util.nbt.FloatTag)1 IntArrayTag (net.glowstone.util.nbt.IntArrayTag)1 IntTag (net.glowstone.util.nbt.IntTag)1 ListTag (net.glowstone.util.nbt.ListTag)1 LongArrayTag (net.glowstone.util.nbt.LongArrayTag)1 LongTag (net.glowstone.util.nbt.LongTag)1 ShortTag (net.glowstone.util.nbt.ShortTag)1 StringTag (net.glowstone.util.nbt.StringTag)1 Tag (net.glowstone.util.nbt.Tag)1 TagType (net.glowstone.util.nbt.TagType)1