#3843: Improve NBT javadocs

This commit is contained in:
Outfluencer 2025-06-08 11:08:46 +10:00 committed by md_5
parent 2b9808cd13
commit 3cd530f007
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
2 changed files with 41 additions and 6 deletions

View File

@ -81,12 +81,14 @@ public interface Tag
void write(DataOutput output) throws IOException; void write(DataOutput output) throws IOException;
/** /**
* Reads the data of the {@link DataInput} and parses it into a {@link Tag}. * Reads a {@link Tag} from the given {@link DataInput},
* based on the specified tag type, with limitations of the
* {@link NBTLimiter}.
* *
* @param id the nbt type * @param id the nbt type
* @param input input to read from * @param input the input to read from
* @param limiter limitation of the read data * @param limiter the limiter for this read operation
* @return the initialized {@link Tag} * @return the deserialized {@link Tag}
* @throws IOException if an exception occurs during io operations * @throws IOException if an exception occurs during io operations
*/ */
static TypedTag readById(byte id, DataInput input, NBTLimiter limiter) throws IOException static TypedTag readById(byte id, DataInput input, NBTLimiter limiter) throws IOException
@ -101,6 +103,15 @@ public interface Tag
return tag; return tag;
} }
/**
* Reads a {@link NamedTag} from the given {@link DataInput},
* with limitations of the {@link NBTLimiter}.
*
* @param input the data input to read from
* @param limiter the limiter for this read operation
* @return the deserialized {@link NamedTag}
* @throws IOException if an exception occurs during io operations
*/
static NamedTag readNamedTag(DataInput input, NBTLimiter limiter) throws IOException static NamedTag readNamedTag(DataInput input, NBTLimiter limiter) throws IOException
{ {
NamedTag namedTag = new NamedTag(); NamedTag namedTag = new NamedTag();
@ -108,6 +119,14 @@ public interface Tag
return namedTag; return namedTag;
} }
/**
* Serializes the given {@link TypedTag} into a byte array.
* This is the inverse operation of {@link #fromByteArray(byte[])}.
*
* @param tag the tag to convert
* @return the serialized byte array
* @throws IOException if an exception occurs during io operations
*/
static byte[] toByteArray(TypedTag tag) throws IOException static byte[] toByteArray(TypedTag tag) throws IOException
{ {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
@ -117,11 +136,28 @@ public interface Tag
return byteArrayOutputStream.toByteArray(); return byteArrayOutputStream.toByteArray();
} }
/**
* Deserializes the given byte array into a {@link TypedTag}.
* This is the inverse operation of {@link #toByteArray(TypedTag)}.
*
* @param data the byte array to read from
* @return the deserialized {@link TypedTag}
* @throws IOException if an exception occurs during io operations
*/
static TypedTag fromByteArray(byte[] data) throws IOException static TypedTag fromByteArray(byte[] data) throws IOException
{ {
return fromByteArray( data, NBTLimiter.unlimitedSize() ); return fromByteArray( data, NBTLimiter.unlimitedSize() );
} }
/**
* Deserializes the given byte array into a {@link TypedTag},
* with limitations of the {@link NBTLimiter}.
*
* @param data the byte array to read from
* @param limiter the limiter for this read operation
* @return the deserialized {@link TypedTag}
* @throws IOException if an exception occurs during io operations
*/
static TypedTag fromByteArray(byte[] data, NBTLimiter limiter) throws IOException static TypedTag fromByteArray(byte[] data, NBTLimiter limiter) throws IOException
{ {
DataInputStream stream = new DataInputStream( new ByteArrayInputStream( data ) ); DataInputStream stream = new DataInputStream( new ByteArrayInputStream( data ) );

View File

@ -22,8 +22,7 @@ public class PlayerDataTest
File file = new File( Objects.requireNonNull( classLoader.getResource( "playerdata.nbt" ) ).toURI() ); File file = new File( Objects.requireNonNull( classLoader.getResource( "playerdata.nbt" ) ).toURI() );
FileInputStream fileInputStream = new FileInputStream( file ); FileInputStream fileInputStream = new FileInputStream( file );
DataInputStream dis = new DataInputStream( fileInputStream ); DataInputStream dis = new DataInputStream( fileInputStream );
NamedTag namedTag = new NamedTag(); NamedTag namedTag = Tag.readNamedTag( dis, NBTLimiter.unlimitedSize() );
namedTag.read( dis, NBTLimiter.unlimitedSize() );
assertInstanceOf( CompoundTag.class, namedTag.getTag() ); assertInstanceOf( CompoundTag.class, namedTag.getTag() );
assertEquals( namedTag.getName(), "" ); assertEquals( namedTag.getName(), "" );
} }