Search in sources :

Example 1 with ListTag

use of net.glowstone.util.nbt.ListTag 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)

Example 2 with ListTag

use of net.glowstone.util.nbt.ListTag in project Glowstone by GlowstoneMC.

the class Mojangson method fromTag.

/**
 * Creates a Mojangson string from the given List Tag.
 *
 * @param tag the List Tag to convert
 * @return the converted Mojangson string
 */
public static String fromTag(ListTag<Tag> tag) {
    StringBuilder builder = new StringBuilder();
    builder.append(ARRAY_START);
    boolean start = true;
    for (Tag value : tag.getValue()) {
        if (start) {
            start = false;
        } else {
            builder.append(ELEMENT_SEPERATOR);
        }
        builder.append(fromGenericTag(value));
    }
    builder.append(ARRAY_END);
    return builder.toString();
}
Also used : 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)

Aggregations

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