Search in sources :

Example 6 with LongTag

use of com.github.steveice10.opennbt.tag.builtin.LongTag in project ViaVersion by ViaVersion.

the class TagStringReader method scalar.

/**
 * A tag that is definitely some sort of scalar.
 *
 * <p>Does not detect quoted strings, so those should have been parsed already.</p>
 *
 * @return a parsed tag
 */
private Tag scalar() {
    final StringBuilder builder = new StringBuilder();
    int noLongerNumericAt = -1;
    while (this.buffer.hasMore()) {
        char current = this.buffer.peek();
        if (current == '\\') {
            // escape -- we are significantly more lenient than original format at the moment
            this.buffer.advance();
            current = this.buffer.take();
        } else if (Tokens.id(current)) {
            this.buffer.advance();
        } else {
            // end of value
            break;
        }
        builder.append(current);
        if (noLongerNumericAt == -1 && !Tokens.numeric(current)) {
            noLongerNumericAt = builder.length();
        }
    }
    final int length = builder.length();
    final String built = builder.toString();
    if (noLongerNumericAt == length) {
        final char last = built.charAt(length - 1);
        try {
            switch(// try to read and return as a number
            Character.toLowerCase(last)) {
                case Tokens.TYPE_BYTE:
                    return new ByteTag(Byte.parseByte(built.substring(0, length - 1)));
                case Tokens.TYPE_SHORT:
                    return new ShortTag(Short.parseShort(built.substring(0, length - 1)));
                case Tokens.TYPE_INT:
                    return new IntTag(Integer.parseInt(built.substring(0, length - 1)));
                case Tokens.TYPE_LONG:
                    return new LongTag(Long.parseLong(built.substring(0, length - 1)));
                case Tokens.TYPE_FLOAT:
                    final float floatValue = Float.parseFloat(built.substring(0, length - 1));
                    if (Float.isFinite(floatValue)) {
                        // don't accept NaN and Infinity
                        return new FloatTag(floatValue);
                    }
                    break;
                case Tokens.TYPE_DOUBLE:
                    final double doubleValue = Double.parseDouble(built.substring(0, length - 1));
                    if (Double.isFinite(doubleValue)) {
                        // don't accept NaN and Infinity
                        return new DoubleTag(doubleValue);
                    }
                    break;
            }
        } catch (final NumberFormatException ignored) {
        }
    } else if (noLongerNumericAt == -1) {
        // if we run out of content without an explicit value separator, then we're either an integer or string tag -- all others have a character at the end
        try {
            return new IntTag(Integer.parseInt(built));
        } catch (final NumberFormatException ex) {
            if (built.indexOf('.') != -1) {
                try {
                    return new DoubleTag(Double.parseDouble(built));
                } catch (final NumberFormatException ex2) {
                // ignore
                }
            }
        }
    }
    if (built.equalsIgnoreCase(Tokens.LITERAL_TRUE)) {
        return new ByteTag((byte) 1);
    } else if (built.equalsIgnoreCase(Tokens.LITERAL_FALSE)) {
        return new ByteTag((byte) 0);
    }
    return new StringTag(built);
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) DoubleTag(com.github.steveice10.opennbt.tag.builtin.DoubleTag) ShortTag(com.github.steveice10.opennbt.tag.builtin.ShortTag) FloatTag(com.github.steveice10.opennbt.tag.builtin.FloatTag) ByteTag(com.github.steveice10.opennbt.tag.builtin.ByteTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) LongTag(com.github.steveice10.opennbt.tag.builtin.LongTag)

Aggregations

CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)5 IntTag (com.github.steveice10.opennbt.tag.builtin.IntTag)5 LongTag (com.github.steveice10.opennbt.tag.builtin.LongTag)5 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)5 ByteTag (com.github.steveice10.opennbt.tag.builtin.ByteTag)3 FloatTag (com.github.steveice10.opennbt.tag.builtin.FloatTag)3 Tag (com.github.steveice10.opennbt.tag.builtin.Tag)3 DoubleTag (com.github.steveice10.opennbt.tag.builtin.DoubleTag)1 IntArrayTag (com.github.steveice10.opennbt.tag.builtin.IntArrayTag)1 ListTag (com.github.steveice10.opennbt.tag.builtin.ListTag)1 NumberTag (com.github.steveice10.opennbt.tag.builtin.NumberTag)1 ShortTag (com.github.steveice10.opennbt.tag.builtin.ShortTag)1 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1 List (java.util.List)1 UUID (java.util.UUID)1 ListTag (org.dragonet.common.data.nbt.tag.ListTag)1