1111import net .kyori .adventure .nbt .ListBinaryTag ;
1212import net .roxymc .slime .CompoundBinaryTagHolder ;
1313import net .roxymc .slime .loader .SlimeLoader ;
14+ import net .roxymc .slime .util .function .IOBiConsumer ;
1415import net .roxymc .slime .util .function .IOBiFunction ;
1516import net .roxymc .slime .util .function .IOConsumer ;
1617import net .roxymc .slime .util .function .IOFunction ;
@@ -32,10 +33,8 @@ public SlimeSerializer(SlimeLoader loader) {
3233 public static SlimeSerializer forVersion (SlimeLoader loader , int version ) {
3334 Preconditions .checkArgument (
3435 version >= 12 ,
35- String .format (
36- "Serializers below version 12 (%s) are not supported. See: https://github.com/roxymc-net/SlimeLoader#legacy-slime-versions" ,
37- version
38- )
36+ "Serializers below version 12 (%s) are not supported. See: https://github.com/roxymc-net/SlimeLoader#legacy-slime-versions" ,
37+ version
3938 );
4039
4140 return switch (version ) {
@@ -51,7 +50,7 @@ public void serialize(World world, ByteArrayDataOutput out) throws IOException {
5150 out .writeInt (world .version ());
5251 }
5352
54- protected void serializeCompressed (byte [] bytes , ByteArrayDataOutput out ) {
53+ protected void writeCompressed (byte [] bytes , ByteArrayDataOutput out ) {
5554 byte [] compressed = Zstd .compress (bytes );
5655
5756 out .writeInt (compressed .length );
@@ -60,32 +59,44 @@ protected void serializeCompressed(byte[] bytes, ByteArrayDataOutput out) {
6059 out .write (compressed );
6160 }
6261
63- protected void serializeCompressed (IOConsumer <ByteArrayDataOutput > consumer , ByteArrayDataOutput out ) throws IOException {
64- ByteArrayDataOutput tempOut = ByteStreams .newDataOutput ();
65- consumer .accept (tempOut );
62+ protected void writeCompressed (IOConsumer <ByteArrayDataOutput > consumer , ByteArrayDataOutput out ) throws IOException {
63+ ByteArrayDataOutput output = ByteStreams .newDataOutput ();
64+ consumer .accept (output );
6665
67- serializeCompressed ( tempOut .toByteArray (), out );
66+ writeCompressed ( output .toByteArray (), out );
6867 }
6968
70- protected void serializeCompound ( CompoundBinaryTag tag , ByteArrayDataOutput out ) throws IOException {
71- serializeCompound ( tag , out , true );
69+ protected < T > void writeCompressed ( T value , IOBiConsumer < T , ByteArrayDataOutput > consumer , ByteArrayDataOutput out ) throws IOException {
70+ writeCompressed ( output -> consumer . accept ( value , output ), out );
7271 }
7372
74- protected void serializeCompound (CompoundBinaryTag tag , ByteArrayDataOutput out , boolean writeLength ) throws IOException {
75- ByteArrayDataOutput tagOut = ByteStreams .newDataOutput ();
76- BinaryTagIO .writer ().write (tag , tagOut );
73+ protected <T > void writeArray (T [] array , ByteArrayDataOutput out , IOBiConsumer <T , ByteArrayDataOutput > serializer ) throws IOException {
74+ out .writeInt (array .length );
7775
78- byte [] bytes = tagOut .toByteArray ();
79-
80- if (writeLength ) {
81- out .writeInt (bytes .length );
76+ for (T element : array ) {
77+ serializer .accept (element , out );
8278 }
79+ }
80+
81+ protected void writeCompound (CompoundBinaryTag tag , ByteArrayDataOutput out ) throws IOException {
82+ ByteArrayDataOutput output = ByteStreams .newDataOutput ();
83+ writeRawCompound (tag , output );
84+
85+ byte [] bytes = output .toByteArray ();
8386
87+ out .writeInt (bytes .length );
8488 out .write (bytes );
8589 }
8690
87- protected <T extends CompoundBinaryTagHolder > void serializeCompoundList (T [] array , ByteArrayDataOutput out , String key ) throws IOException {
91+ protected void writeRawCompound (CompoundBinaryTag tag , ByteArrayDataOutput out ) throws IOException {
92+ if (!tag .isEmpty ()) {
93+ BinaryTagIO .writer ().write (tag , out );
94+ }
95+ }
96+
97+ protected <T extends CompoundBinaryTagHolder > void writeCompoundArray (T [] array , ByteArrayDataOutput out , String key ) throws IOException {
8898 ListBinaryTag .Builder <CompoundBinaryTag > builder = ListBinaryTag .builder (BinaryTagTypes .COMPOUND );
99+
89100 for (T element : array ) {
90101 builder .add (element .tag ());
91102 }
@@ -94,12 +105,12 @@ protected <T extends CompoundBinaryTagHolder> void serializeCompoundList(T[] arr
94105 .put (key , builder .build ())
95106 .build ();
96107
97- serializeCompound (tag , out );
108+ writeCompound (tag , out );
98109 }
99110
100111 public abstract World deserialize (ByteArrayDataInput in ) throws IOException ;
101112
102- protected byte [] deserializeCompressed (ByteArrayDataInput in ) {
113+ protected byte [] readCompressed (ByteArrayDataInput in ) {
103114 int compressedLength = in .readInt ();
104115 int length = in .readInt ();
105116
@@ -109,37 +120,43 @@ protected byte[] deserializeCompressed(ByteArrayDataInput in) {
109120 return Zstd .decompress (compressed , length );
110121 }
111122
112- protected <T > T deserializeCompressed (IOFunction <ByteArrayDataInput , T > function , ByteArrayDataInput in ) throws IOException {
113- return deserializeCompressed ((length , dataIn ) -> function .apply (dataIn ), in );
123+ protected <T > T readCompressed (IOFunction <ByteArrayDataInput , T > function , ByteArrayDataInput in ) throws IOException {
124+ return readCompressed ((length , input ) -> function .apply (input ), in );
114125 }
115126
116- protected <T > T deserializeCompressed (IOBiFunction <Integer , ByteArrayDataInput , T > function , ByteArrayDataInput in ) throws IOException {
117- byte [] bytes = deserializeCompressed (in );
127+ protected <T > T readCompressed (IOBiFunction <Integer , ByteArrayDataInput , T > function , ByteArrayDataInput in ) throws IOException {
128+ byte [] bytes = readCompressed (in );
118129
119130 return function .apply (bytes .length , ByteStreams .newDataInput (bytes ));
120131 }
121132
122- protected CompoundBinaryTag deserializeCompound (ByteArrayDataInput in ) throws IOException {
133+ protected <T > T [] readArray (
134+ IntFunction <T []> arrayBuilder , ByteArrayDataInput in , IOFunction <ByteArrayDataInput , T > deserializer
135+ ) throws IOException {
123136 int length = in .readInt ();
124137
125- return deserializeCompound (length , in );
138+ T [] array = arrayBuilder .apply (length );
139+ for (int i = 0 ; i < length ; i ++) {
140+ array [i ] = deserializer .apply (in );
141+ }
142+
143+ return array ;
126144 }
127145
128- protected CompoundBinaryTag deserializeCompound (int length , ByteArrayDataInput in ) throws IOException {
129- if (length == 0 ) {
130- return CompoundBinaryTag .empty ();
131- }
146+ protected CompoundBinaryTag readCompound (ByteArrayDataInput in ) throws IOException {
147+ int length = in .readInt ();
132148
133- byte [] bytes = new byte [ length ] ;
134- in . readFully ( bytes );
149+ return readRawCompound ( length , in ) ;
150+ }
135151
136- return BinaryTagIO .reader ().read (ByteStreams .newDataInput (bytes ));
152+ protected CompoundBinaryTag readRawCompound (int length , ByteArrayDataInput in ) throws IOException {
153+ return length == 0 ? CompoundBinaryTag .empty () : BinaryTagIO .reader ().read (in );
137154 }
138155
139- protected <T extends CompoundBinaryTagHolder > T [] deserializeCompoundList (
156+ protected <T extends CompoundBinaryTagHolder > T [] readCompoundArray (
140157 IntFunction <T []> arrayBuilder , ByteArrayDataInput in , String key , Function <CompoundBinaryTag , T > deserializer
141158 ) throws IOException {
142- CompoundBinaryTag tag = deserializeCompound (in );
159+ CompoundBinaryTag tag = readCompound (in );
143160 ListBinaryTag list = tag .getList (key , BinaryTagTypes .COMPOUND );
144161
145162 T [] array = arrayBuilder .apply (list .size ());
0 commit comments