From 9c83e78af28c2d0ffc5c2059ed27237da1e5cae6 Mon Sep 17 00:00:00 2001 From: booky10 Date: Fri, 1 Sep 2023 18:20:59 +0200 Subject: [PATCH 1/3] feat(nbt): Implement nameless binary serialization --- .../net/kyori/adventure/nbt/BinaryTagIO.java | 140 ++++++++++++++++++ .../adventure/nbt/BinaryTagReaderImpl.java | 27 +++- .../adventure/nbt/BinaryTagWriterImpl.java | 27 +++- .../kyori/adventure/nbt/BinaryTagIOTest.java | 10 ++ 4 files changed, 202 insertions(+), 2 deletions(-) diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagIO.java b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagIO.java index 1da8dec37..51439d6ff 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagIO.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagIO.java @@ -305,6 +305,76 @@ public interface Reader { */ @NotNull CompoundBinaryTag read(final @NotNull DataInput input) throws IOException; + /** + * Reads a binary tag from {@code path}. + * + *

This is the equivalent of passing {@code Compression#NONE} as the second parameter to {@link #read(Path, Compression)}.

+ * + *

Doesn't read a name from the {@link Path}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ * + * @param path the path + * @return a binary tag + * @throws IOException if an exception was encountered while reading the tag + * @since 4.15.0 + */ + default @NotNull CompoundBinaryTag readNameless(final @NotNull Path path) throws IOException { + return this.readNameless(path, Compression.NONE); + } + + /** + * Reads a binary tag from {@code path} with a {@code compression} type. + * + *

Doesn't read a name from the {@link Path}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ * + * @param path the path + * @param compression the compression type + * @return a binary tag + * @throws IOException if an exception was encountered while reading the tag + * @since 4.15.0 + */ + @NotNull CompoundBinaryTag readNameless(final @NotNull Path path, final @NotNull Compression compression) throws IOException; + + /** + * Reads a binary tag from {@code input}. + * + *

This is the equivalent of passing {@code Compression#NONE} as the second parameter to {@link #read(InputStream, Compression)}.

+ * + *

Doesn't read a name from the {@link InputStream}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ * + * @param input the input stream + * @return a binary tag + * @throws IOException if an exception was encountered while reading the tag + * @since 4.15.0 + */ + default @NotNull CompoundBinaryTag readNameless(final @NotNull InputStream input) throws IOException { + return this.readNameless(input, Compression.NONE); + } + + /** + * Reads a binary tag from {@code input} with a {@code compression} type. + * + *

Doesn't read a name from the {@link InputStream}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ * + * @param input the input stream + * @param compression the compression type + * @return a binary tag + * @throws IOException if an exception was encountered while reading the tag + * @since 4.15.0 + */ + @NotNull CompoundBinaryTag readNameless(final @NotNull InputStream input, final @NotNull Compression compression) throws IOException; + + /** + * Reads a binary tag from {@code input}. + * + *

Doesn't read a name from the {@link DataInput}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ * + * @param input the input stream + * @return a binary tag + * @throws IOException if an exception was encountered while reading the tag + * @since 4.15.0 + */ + @NotNull CompoundBinaryTag readNameless(final @NotNull DataInput input) throws IOException; + /** * Reads a binary tag, with a name, from {@code path}. * @@ -432,6 +502,76 @@ default void write(final @NotNull CompoundBinaryTag tag, final @NotNull OutputSt */ void write(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutput output) throws IOException; + /** + * Writes a binary tag to {@code path} with a {@code compression} type. + * + *

This is the equivalent of passing {@code Compression#NONE} as the second parameter to {@link #write(CompoundBinaryTag, Path, Compression)}.

+ * + *

Doesn't write a name to the {@link Path}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ * + * @param tag the tag to write + * @param path the path + * @throws IOException if an exception was encountered while reading the tag + * @since 4.15.0 + */ + default void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull Path path) throws IOException { + this.writeNameless(tag, path, Compression.NONE); + } + + /** + * Writes a binary tag to {@code path} with a {@code compression} type. + * + *

Doesn't write a name to the {@link Path}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ * + * @param tag the tag to write + * @param path the path + * @param compression the compression type + * @throws IOException if an exception was encountered while reading the tag + * @since 4.15.0 + */ + void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull Path path, final @NotNull Compression compression) throws IOException; + + /** + * Writes a binary tag to {@code output}. + * + *

This is the equivalent of passing {@link Compression#NONE} as the second parameter to {@link #write(CompoundBinaryTag, OutputStream, Compression)}.

+ * + *

Doesn't write a name to the {@link OutputStream}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ * + * @param tag the tag to write + * @param output the output stream + * @throws IOException if an exception was encountered while reading the tag + * @since 4.15.0 + */ + default void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull OutputStream output) throws IOException { + this.writeNameless(tag, output, Compression.NONE); + } + + /** + * Writes a binary tag to {@code output} with a {@code compression} type. + * + *

Doesn't write a name to the {@link OutputStream}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ * + * @param tag the tag to write + * @param output the output stream + * @param compression the compression type + * @throws IOException if an exception was encountered while reading the tag + * @since 4.15.0 + */ + void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull OutputStream output, final @NotNull Compression compression) throws IOException; + + /** + * Writes a binary tag to {@code output}. + * + *

Doesn't write a name to the {@link DataOutput}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ * + * @param tag the tag to write + * @param output the output + * @throws IOException if an exception was encountered while reading the tag + * @since 4.15.0 + */ + void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutput output) throws IOException; + /** * Writes a binary tag, with a name, to {@code path}. * diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java index 3e93441dd..68eac901d 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java @@ -62,16 +62,41 @@ final class BinaryTagReaderImpl implements BinaryTagIO.Reader { @Override public @NotNull CompoundBinaryTag read(@NotNull DataInput input) throws IOException { + return this.read(input, false); + } + + private @NotNull CompoundBinaryTag read(@NotNull DataInput input, boolean nameless) throws IOException { if (!(input instanceof TrackingDataInput)) { input = new TrackingDataInput(input, this.maxBytes); } final BinaryTagType type = BinaryTagType.binaryTagType(input.readByte()); requireCompound(type); - input.skipBytes(input.readUnsignedShort()); // read empty name + if (!nameless) { + input.skipBytes(input.readUnsignedShort()); // read empty name + } return BinaryTagTypes.COMPOUND.read(input); } + @Override + public @NotNull CompoundBinaryTag readNameless(final @NotNull Path path, final BinaryTagIO.@NotNull Compression compression) throws IOException { + try (final InputStream is = Files.newInputStream(path)) { + return this.readNameless(is, compression); + } + } + + @Override + public @NotNull CompoundBinaryTag readNameless(final @NotNull InputStream input, final BinaryTagIO.@NotNull Compression compression) throws IOException { + try (final DataInputStream dis = new DataInputStream(new BufferedInputStream(compression.decompress(closeShield(input))))) { + return this.readNameless((DataInput) dis); + } + } + + @Override + public @NotNull CompoundBinaryTag readNameless(@NotNull DataInput input) throws IOException { + return this.read(input, true); + } + @Override public Map.@NotNull Entry readNamed(final @NotNull Path path, final BinaryTagIO.@NotNull Compression compression) throws IOException { try (final InputStream is = Files.newInputStream(path)) { diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java index b01c09ab5..7144665a8 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java @@ -54,11 +54,36 @@ public void write(final @NotNull CompoundBinaryTag tag, final @NotNull OutputStr @Override public void write(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutput output) throws IOException { + this.write(tag, output, false); + } + + private void write(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutput output, boolean nameless) throws IOException { output.writeByte(BinaryTagTypes.COMPOUND.id()); - output.writeUTF(""); // write empty name + if (!nameless) { + output.writeUTF(""); // write empty name + } BinaryTagTypes.COMPOUND.write(tag, output); } + @Override + public void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull Path path, final BinaryTagIO.@NotNull Compression compression) throws IOException { + try (final OutputStream os = Files.newOutputStream(path)) { + this.writeNameless(tag, os, compression); + } + } + + @Override + public void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull OutputStream output, final BinaryTagIO.@NotNull Compression compression) throws IOException { + try (final DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(compression.compress(closeShield(output))))) { + this.writeNameless(tag, (DataOutput) dos); + } + } + + @Override + public void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutput output) throws IOException { + this.write(tag, output, true); + } + @Override public void writeNamed(final Map.@NotNull Entry tag, final @NotNull Path path, final BinaryTagIO.@NotNull Compression compression) throws IOException { try (final OutputStream os = Files.newOutputStream(path)) { diff --git a/nbt/src/test/java/net/kyori/adventure/nbt/BinaryTagIOTest.java b/nbt/src/test/java/net/kyori/adventure/nbt/BinaryTagIOTest.java index 257205fd6..3fb2fe78b 100644 --- a/nbt/src/test/java/net/kyori/adventure/nbt/BinaryTagIOTest.java +++ b/nbt/src/test/java/net/kyori/adventure/nbt/BinaryTagIOTest.java @@ -60,4 +60,14 @@ void testWriteAndReadZLIBCompression() throws IOException { BinaryTagIO.writer().write(tag, output, BinaryTagIO.Compression.ZLIB); assertEquals(tag, BinaryTagIO.reader().read(new ByteArrayInputStream(output.toByteArray()), BinaryTagIO.Compression.ZLIB)); } + + @Test + void testNamelessWriteAndReadNoCompression() throws IOException { + final CompoundBinaryTag tag = CompoundBinaryTag.builder() + .putString("name", "test") + .build(); + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + BinaryTagIO.writer().writeNameless(tag, output); + assertEquals(tag, BinaryTagIO.reader().readNameless(new ByteArrayInputStream(output.toByteArray()))); + } } From 3006be796ac4ab8029889396f44bf2e40b36f46b Mon Sep 17 00:00:00 2001 From: booky10 Date: Sat, 2 Sep 2023 16:11:52 +0200 Subject: [PATCH 2/3] Make checkstyle happy --- .../java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java | 6 +++--- .../java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java index 68eac901d..8c482d923 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java @@ -61,11 +61,11 @@ final class BinaryTagReaderImpl implements BinaryTagIO.Reader { } @Override - public @NotNull CompoundBinaryTag read(@NotNull DataInput input) throws IOException { + public @NotNull CompoundBinaryTag read(final @NotNull DataInput input) throws IOException { return this.read(input, false); } - private @NotNull CompoundBinaryTag read(@NotNull DataInput input, boolean nameless) throws IOException { + private @NotNull CompoundBinaryTag read(@NotNull DataInput input, final boolean nameless) throws IOException { if (!(input instanceof TrackingDataInput)) { input = new TrackingDataInput(input, this.maxBytes); } @@ -93,7 +93,7 @@ final class BinaryTagReaderImpl implements BinaryTagIO.Reader { } @Override - public @NotNull CompoundBinaryTag readNameless(@NotNull DataInput input) throws IOException { + public @NotNull CompoundBinaryTag readNameless(final @NotNull DataInput input) throws IOException { return this.read(input, true); } diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java index 7144665a8..ac717a384 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java @@ -57,7 +57,7 @@ public void write(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutpu this.write(tag, output, false); } - private void write(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutput output, boolean nameless) throws IOException { + private void write(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutput output, final boolean nameless) throws IOException { output.writeByte(BinaryTagTypes.COMPOUND.id()); if (!nameless) { output.writeUTF(""); // write empty name From 3a3a0e2a2c89301b82e17c21c73a52f187970985 Mon Sep 17 00:00:00 2001 From: zml Date: Sun, 17 Dec 2023 21:42:29 -0800 Subject: [PATCH 3/3] chore(nbt): Improve documentation around nameless tags --- .../net/kyori/adventure/nbt/BinaryTagIO.java | 50 +++++++++++++++---- .../adventure/nbt/BinaryTagReaderImpl.java | 8 +-- .../adventure/nbt/BinaryTagWriterImpl.java | 8 +-- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagIO.java b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagIO.java index 51439d6ff..c75260fc3 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagIO.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagIO.java @@ -250,6 +250,8 @@ public interface Reader { * *

This is the equivalent of passing {@code Compression#NONE} as the second parameter to {@link #read(Path, Compression)}.

* + *

The root name field is discarded.

+ * * @param path the path * @return a binary tag * @throws IOException if an exception was encountered while reading the tag @@ -262,6 +264,8 @@ public interface Reader { /** * Reads a binary tag from {@code path} with a {@code compression} type. * + *

The root name field is discarded.

+ * * @param path the path * @param compression the compression type * @return a binary tag @@ -275,6 +279,8 @@ public interface Reader { * *

This is the equivalent of passing {@code Compression#NONE} as the second parameter to {@link #read(InputStream, Compression)}.

* + *

The root name field is discarded.

+ * * @param input the input stream * @return a binary tag * @throws IOException if an exception was encountered while reading the tag @@ -287,6 +293,8 @@ public interface Reader { /** * Reads a binary tag from {@code input} with a {@code compression} type. * + *

The root name field is discarded.

+ * * @param input the input stream * @param compression the compression type * @return a binary tag @@ -298,6 +306,8 @@ public interface Reader { /** * Reads a binary tag from {@code input}. * + *

The root name field is discarded.

+ * * @param input the input stream * @return a binary tag * @throws IOException if an exception was encountered while reading the tag @@ -310,12 +320,13 @@ public interface Reader { * *

This is the equivalent of passing {@code Compression#NONE} as the second parameter to {@link #read(Path, Compression)}.

* - *

Doesn't read a name from the {@link Path}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ *

Doesn't read a root name from the {@link Path} at all, to match the wire protocol in modern game versions.

* * @param path the path * @return a binary tag * @throws IOException if an exception was encountered while reading the tag * @since 4.15.0 + * @sinceMinecraft 1.20.2 */ default @NotNull CompoundBinaryTag readNameless(final @NotNull Path path) throws IOException { return this.readNameless(path, Compression.NONE); @@ -324,13 +335,14 @@ public interface Reader { /** * Reads a binary tag from {@code path} with a {@code compression} type. * - *

Doesn't read a name from the {@link Path}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ *

Doesn't read a root name from the {@link Path} at all, to match the wire protocol in modern game versions.

* * @param path the path * @param compression the compression type * @return a binary tag * @throws IOException if an exception was encountered while reading the tag * @since 4.15.0 + * @sinceMinecraft 1.20.2 */ @NotNull CompoundBinaryTag readNameless(final @NotNull Path path, final @NotNull Compression compression) throws IOException; @@ -339,12 +351,13 @@ public interface Reader { * *

This is the equivalent of passing {@code Compression#NONE} as the second parameter to {@link #read(InputStream, Compression)}.

* - *

Doesn't read a name from the {@link InputStream}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ *

Doesn't read a root name from the {@link InputStream} at all, to match the wire protocol in modern game versions.

* * @param input the input stream * @return a binary tag * @throws IOException if an exception was encountered while reading the tag * @since 4.15.0 + * @sinceMinecraft 1.20.2 */ default @NotNull CompoundBinaryTag readNameless(final @NotNull InputStream input) throws IOException { return this.readNameless(input, Compression.NONE); @@ -353,25 +366,27 @@ public interface Reader { /** * Reads a binary tag from {@code input} with a {@code compression} type. * - *

Doesn't read a name from the {@link InputStream}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ *

Doesn't read a root name from the {@link InputStream} at all, to match the wire protocol in modern game versions.

* * @param input the input stream * @param compression the compression type * @return a binary tag * @throws IOException if an exception was encountered while reading the tag * @since 4.15.0 + * @sinceMinecraft 1.20.2 */ @NotNull CompoundBinaryTag readNameless(final @NotNull InputStream input, final @NotNull Compression compression) throws IOException; /** * Reads a binary tag from {@code input}. * - *

Doesn't read a name from the {@link DataInput}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ *

Doesn't read a root name from the {@link DataInput} at all, to match the wire protocol in modern game versions.

* * @param input the input stream * @return a binary tag * @throws IOException if an exception was encountered while reading the tag * @since 4.15.0 + * @sinceMinecraft 1.20.2 */ @NotNull CompoundBinaryTag readNameless(final @NotNull DataInput input) throws IOException; @@ -447,6 +462,8 @@ public interface Writer { * *

This is the equivalent of passing {@code Compression#NONE} as the second parameter to {@link #write(CompoundBinaryTag, Path, Compression)}.

* + *

An empty root name is written.

+ * * @param tag the tag to write * @param path the path * @throws IOException if an exception was encountered while reading the tag @@ -459,6 +476,8 @@ default void write(final @NotNull CompoundBinaryTag tag, final @NotNull Path pat /** * Writes a binary tag to {@code path} with a {@code compression} type. * + *

An empty root name is written.

+ * * @param tag the tag to write * @param path the path * @param compression the compression type @@ -472,6 +491,8 @@ default void write(final @NotNull CompoundBinaryTag tag, final @NotNull Path pat * *

This is the equivalent of passing {@link Compression#NONE} as the second parameter to {@link #write(CompoundBinaryTag, OutputStream, Compression)}.

* + *

An empty root name is written.

+ * * @param tag the tag to write * @param output the output stream * @throws IOException if an exception was encountered while reading the tag @@ -484,6 +505,8 @@ default void write(final @NotNull CompoundBinaryTag tag, final @NotNull OutputSt /** * Writes a binary tag to {@code output} with a {@code compression} type. * + *

An empty root name is written.

+ * * @param tag the tag to write * @param output the output stream * @param compression the compression type @@ -495,6 +518,8 @@ default void write(final @NotNull CompoundBinaryTag tag, final @NotNull OutputSt /** * Writes a binary tag to {@code output}. * + *

An empty root name is written.

+ * * @param tag the tag to write * @param output the output * @throws IOException if an exception was encountered while reading the tag @@ -507,12 +532,13 @@ default void write(final @NotNull CompoundBinaryTag tag, final @NotNull OutputSt * *

This is the equivalent of passing {@code Compression#NONE} as the second parameter to {@link #write(CompoundBinaryTag, Path, Compression)}.

* - *

Doesn't write a name to the {@link Path}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ *

Doesn't write a root name to the {@link Path} at all, to match the wire protocol in modern game versions.

* * @param tag the tag to write * @param path the path * @throws IOException if an exception was encountered while reading the tag * @since 4.15.0 + * @sinceMinecraft 1.20.2 */ default void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull Path path) throws IOException { this.writeNameless(tag, path, Compression.NONE); @@ -521,13 +547,14 @@ default void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull /** * Writes a binary tag to {@code path} with a {@code compression} type. * - *

Doesn't write a name to the {@link Path}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ *

Doesn't write a root name to the {@link Path} at all, to match the wire protocol in modern game versions.

* * @param tag the tag to write * @param path the path * @param compression the compression type * @throws IOException if an exception was encountered while reading the tag * @since 4.15.0 + * @sinceMinecraft 1.20.2 */ void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull Path path, final @NotNull Compression compression) throws IOException; @@ -536,12 +563,13 @@ default void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull * *

This is the equivalent of passing {@link Compression#NONE} as the second parameter to {@link #write(CompoundBinaryTag, OutputStream, Compression)}.

* - *

Doesn't write a name to the {@link OutputStream}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ *

Doesn't write a root name to the {@link OutputStream} at all, to match the wire protocol in modern game versions.

* * @param tag the tag to write * @param output the output stream * @throws IOException if an exception was encountered while reading the tag * @since 4.15.0 + * @sinceMinecraft 1.20.2 */ default void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull OutputStream output) throws IOException { this.writeNameless(tag, output, Compression.NONE); @@ -550,25 +578,27 @@ default void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull /** * Writes a binary tag to {@code output} with a {@code compression} type. * - *

Doesn't write a name to the {@link OutputStream}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ *

Doesn't write a root name to the {@link OutputStream} at all, to match the wire protocol in modern game versions.

* * @param tag the tag to write * @param output the output stream * @param compression the compression type * @throws IOException if an exception was encountered while reading the tag * @since 4.15.0 + * @sinceMinecraft 1.20.2 */ void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull OutputStream output, final @NotNull Compression compression) throws IOException; /** * Writes a binary tag to {@code output}. * - *

Doesn't write a name to the {@link DataOutput}, as changed by mojang in the network protocol since 23w31a (1.20.2 snapshot).

+ *

Doesn't write a root name to the {@link DataOutput} at all, to match the wire protocol in modern game versions.

* * @param tag the tag to write * @param output the output * @throws IOException if an exception was encountered while reading the tag * @since 4.15.0 + * @sinceMinecraft 1.20.2 */ void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutput output) throws IOException; diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java index 8c482d923..c8b722427 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagReaderImpl.java @@ -62,17 +62,17 @@ final class BinaryTagReaderImpl implements BinaryTagIO.Reader { @Override public @NotNull CompoundBinaryTag read(final @NotNull DataInput input) throws IOException { - return this.read(input, false); + return this.read(input, true); } - private @NotNull CompoundBinaryTag read(@NotNull DataInput input, final boolean nameless) throws IOException { + private @NotNull CompoundBinaryTag read(@NotNull DataInput input, final boolean named) throws IOException { if (!(input instanceof TrackingDataInput)) { input = new TrackingDataInput(input, this.maxBytes); } final BinaryTagType type = BinaryTagType.binaryTagType(input.readByte()); requireCompound(type); - if (!nameless) { + if (named) { input.skipBytes(input.readUnsignedShort()); // read empty name } return BinaryTagTypes.COMPOUND.read(input); @@ -94,7 +94,7 @@ final class BinaryTagReaderImpl implements BinaryTagIO.Reader { @Override public @NotNull CompoundBinaryTag readNameless(final @NotNull DataInput input) throws IOException { - return this.read(input, true); + return this.read(input, false); } @Override diff --git a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java index ac717a384..24055355b 100644 --- a/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java +++ b/nbt/src/main/java/net/kyori/adventure/nbt/BinaryTagWriterImpl.java @@ -54,12 +54,12 @@ public void write(final @NotNull CompoundBinaryTag tag, final @NotNull OutputStr @Override public void write(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutput output) throws IOException { - this.write(tag, output, false); + this.write(tag, output, true); } - private void write(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutput output, final boolean nameless) throws IOException { + private void write(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutput output, final boolean named) throws IOException { output.writeByte(BinaryTagTypes.COMPOUND.id()); - if (!nameless) { + if (named) { output.writeUTF(""); // write empty name } BinaryTagTypes.COMPOUND.write(tag, output); @@ -81,7 +81,7 @@ public void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull O @Override public void writeNameless(final @NotNull CompoundBinaryTag tag, final @NotNull DataOutput output) throws IOException { - this.write(tag, output, true); + this.write(tag, output, false); } @Override