diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/nbt/TagStringReader.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/nbt/TagStringReader.java index baef70a00..960c937c8 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/nbt/TagStringReader.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/nbt/TagStringReader.java @@ -47,6 +47,8 @@ import java.util.stream.LongStream; // - Use OpenNBT tags // - Small byteArray() optimization // - acceptLegacy = true by default +// - Don't parse value as DoubleTag when possiblyNumeric +// - Fix trailing comma reading in compounds, lists, and arrays final class TagStringReader { private static final int MAX_DEPTH = 512; private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; @@ -54,7 +56,7 @@ final class TagStringReader { private static final long[] EMPTY_LONG_ARRAY = new long[0]; private final CharBuffer buffer; - private boolean acceptLegacy = true; + private boolean acceptLegacy = true; // Via - always true private int depth; TagStringReader(final CharBuffer buffer) { @@ -125,11 +127,11 @@ final class TagStringReader { return EMPTY_BYTE_ARRAY; } - final IntList bytes = new IntArrayList(); + final IntList bytes = new IntArrayList(); // Via - no boxing while (this.buffer.hasMore()) { final CharSequence value = this.buffer.skipWhitespace().takeUntil(Tokens.TYPE_BYTE); try { - bytes.add(Byte.parseByte(value.toString())); + bytes.add(Byte.parseByte(value.toString())); // Via } catch (final NumberFormatException ex) { throw this.buffer.makeError("All elements of a byte array must be bytes!"); } @@ -137,7 +139,7 @@ final class TagStringReader { if (this.separatorOrCompleteWith(Tokens.ARRAY_END)) { final byte[] result = new byte[bytes.size()]; for (int i = 0; i < bytes.size(); ++i) { - result[i] = (byte) bytes.getInt(i); + result[i] = (byte) bytes.getInt(i); // Via } return result; } @@ -303,12 +305,8 @@ final class TagStringReader { if (possiblyNumeric) { try { return new IntTag(Integer.parseInt(built)); - } catch (final NumberFormatException ex) { - try { - return new DoubleTag(Double.parseDouble(built)); - } catch (final NumberFormatException ex2) { - // ignore - } + } catch (final NumberFormatException ignored) { + // Via - don't try to parse as DoubleTag here } } @@ -326,7 +324,7 @@ final class TagStringReader { return true; } this.buffer.expect(Tokens.VALUE_SEPARATOR); - return false; + return this.buffer.takeIf(endCharacter); // Via - trailing commas are allowed } /** diff --git a/common/src/test/java/com/viaversion/viaversion/common/nbt/NBTTagTest.java b/common/src/test/java/com/viaversion/viaversion/common/nbt/NBTTagTest.java new file mode 100644 index 000000000..f2a3a143e --- /dev/null +++ b/common/src/test/java/com/viaversion/viaversion/common/nbt/NBTTagTest.java @@ -0,0 +1,44 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viaversion.common.nbt; + +import com.github.steveice10.opennbt.tag.builtin.StringTag; +import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +public class NBTTagTest { + + @Test + void test() throws IOException { + BinaryTagIO.readString("{id:test,test:1}"); + BinaryTagIO.readString("{id:test,test:1,}"); + + BinaryTagIO.readString("{id:[1,2,3,]}"); + + BinaryTagIO.readString("{id:[I;1,2,3]}"); + BinaryTagIO.readString("{id:[I;1,2,3,]}"); + + Assertions.assertTrue(BinaryTagIO.readString("{id:9000b,num:2147483649}").get("num") instanceof StringTag); + + //TODO fix legacy + // BinaryTagIO.readString("{id:minecraft:stone}"); + } +}