1
1
package net .hollowcube .schem ;
2
2
3
3
4
+ import net .kyori .adventure .nbt .BinaryTagIO ;
5
+ import net .kyori .adventure .nbt .CompoundBinaryTag ;
6
+ import net .kyori .adventure .nbt .IntBinaryTag ;
4
7
import net .minestom .server .command .builder .arguments .minecraft .ArgumentBlockState ;
5
- import net .minestom .server .coordinate . Point ;
8
+ import net .minestom .server .command . builder . exception . ArgumentSyntaxException ;
6
9
import net .minestom .server .coordinate .Vec ;
7
10
import net .minestom .server .instance .block .Block ;
8
11
import net .minestom .server .utils .validate .Check ;
9
12
import org .jetbrains .annotations .NotNull ;
10
- import org .jglrxavpok .hephaistos .collections .ImmutableByteArray ;
11
- import org .jglrxavpok .hephaistos .nbt .CompressedProcesser ;
12
- import org .jglrxavpok .hephaistos .nbt .NBTCompound ;
13
- import org .jglrxavpok .hephaistos .nbt .NBTInt ;
14
- import org .jglrxavpok .hephaistos .nbt .NBTReader ;
15
13
16
14
import java .io .InputStream ;
17
15
import java .nio .file .Path ;
16
+ import java .util .Map ;
18
17
19
18
/**
20
19
* Simple schematic file reader.
21
20
*/
22
21
public final class SchematicReader {
22
+ private static final BinaryTagIO .Reader NBT_READER = BinaryTagIO .unlimitedReader ();
23
23
24
24
private SchematicReader () {
25
25
}
26
26
27
27
public static @ NotNull Schematic read (@ NotNull InputStream stream ) {
28
- try ( var reader = new NBTReader ( stream , CompressedProcesser . GZIP )) {
29
- return read (reader );
28
+ try {
29
+ return read (NBT_READER . readNamed ( stream , BinaryTagIO . Compression . GZIP ) );
30
30
} catch (Exception e ) {
31
31
throw new SchematicReadException ("failed to read schematic NBT" , e );
32
32
}
33
33
}
34
34
35
35
public static @ NotNull Schematic read (@ NotNull Path path ) {
36
- try ( var reader = new NBTReader ( path , CompressedProcesser . GZIP )) {
37
- return read (reader );
36
+ try {
37
+ return read (NBT_READER . readNamed ( path , BinaryTagIO . Compression . GZIP ) );
38
38
} catch (Exception e ) {
39
39
throw new SchematicReadException ("failed to read schematic NBT" , e );
40
40
}
41
41
}
42
42
43
- public static @ NotNull Schematic read (@ NotNull NBTReader reader ) {
43
+ public static @ NotNull Schematic read (@ NotNull Map . Entry < String , CompoundBinaryTag > namedTag ) {
44
44
try {
45
- NBTCompound tag = (NBTCompound ) reader .read ();
46
-
47
45
// If it has a Schematic tag is sponge v2 or 3
48
- var schematicTag = tag .getCompound ("Schematic" );
49
- if (schematicTag != null ) {
50
- Integer version = schematicTag .getInt ("Version" );
51
- Check .notNull (version , "Missing required field 'Schematic.Version'" );
52
- return read (schematicTag , version );
46
+ var schematicTag = namedTag .getValue ().get ("Schematic" );
47
+ if (schematicTag instanceof CompoundBinaryTag schematicCompound ) {
48
+ return read (schematicCompound , schematicCompound .getInt ("Version" ));
53
49
}
54
50
55
51
// Otherwise it is hopefully v1
56
- return read (tag , 1 );
52
+ return read (namedTag . getValue () , 1 );
57
53
} catch (Exception e ) {
58
54
throw new SchematicReadException ("Invalid schematic file" , e );
59
55
}
60
56
}
61
57
62
- private static @ NotNull Schematic read (@ NotNull NBTCompound tag , int version ) {
63
- Short width = tag .getShort ("Width" );
64
- Check .notNull (width , "Missing required field 'Width'" );
65
- Short height = tag .getShort ("Height" );
66
- Check .notNull (height , "Missing required field 'Height'" );
67
- Short length = tag .getShort ("Length" );
68
- Check .notNull (length , "Missing required field 'Length'" );
58
+ private static @ NotNull Schematic read (@ NotNull CompoundBinaryTag tag , int version ) {
59
+ short width = tag .getShort ("Width" );
60
+ short height = tag .getShort ("Height" );
61
+ short length = tag .getShort ("Length" );
69
62
70
- NBTCompound metadata = tag .getCompound ("Metadata" );
63
+ CompoundBinaryTag metadata = tag .getCompound ("Metadata" );
71
64
72
65
var offset = Vec .ZERO ;
73
- if (metadata != null && metadata .containsKey ("WEOffsetX" )) {
74
- Integer offsetX = metadata .getInt ("WEOffsetX" );
75
- Check .notNull (offsetX , "Missing required field 'Metadata.WEOffsetX'" );
76
- Integer offsetY = metadata .getInt ("WEOffsetY" );
77
- Check .notNull (offsetY , "Missing required field 'Metadata.WEOffsetY'" );
78
- Integer offsetZ = metadata .getInt ("WEOffsetZ" );
79
- Check .notNull (offsetZ , "Missing required field 'Metadata.WEOffsetZ'" );
66
+ if (metadata .keySet ().contains ("WEOffsetX" )) {
67
+ int offsetX = metadata .getInt ("WEOffsetX" );
68
+ int offsetY = metadata .getInt ("WEOffsetY" );
69
+ int offsetZ = metadata .getInt ("WEOffsetZ" );
80
70
81
71
offset = new Vec (offsetX , offsetY , offsetZ );
82
72
} //todo handle sponge Offset
83
73
84
- NBTCompound palette ;
85
- ImmutableByteArray blockArray ;
74
+ CompoundBinaryTag palette ;
75
+ byte [] blockArray ;
86
76
Integer paletteSize ;
87
77
if (version == 1 ) {
88
78
palette = tag .getCompound ("Palette" );
@@ -99,22 +89,26 @@ private SchematicReader() {
99
89
Check .notNull (palette , "Missing required field 'Blocks.Palette'" );
100
90
blockArray = blockEntries .getByteArray ("Data" );
101
91
Check .notNull (blockArray , "Missing required field 'Blocks.Data'" );
102
- paletteSize = palette .getSize ();
92
+ paletteSize = palette .size ();
103
93
}
104
94
105
95
Block [] paletteBlocks = new Block [paletteSize ];
106
96
107
- palette .forEach ((key , value ) -> {
108
- int assigned = ((NBTInt ) value ).getValue ();
109
- Block block = ArgumentBlockState .staticParse (key );
110
- paletteBlocks [assigned ] = block ;
97
+ palette .forEach ((entry ) -> {
98
+ try {
99
+ int assigned = ((IntBinaryTag ) entry .getValue ()).value ();
100
+ Block block = ArgumentBlockState .staticParse (entry .getKey ());
101
+ paletteBlocks [assigned ] = block ;
102
+ } catch (ArgumentSyntaxException e ) {
103
+ throw new SchematicReadException ("Failed to parse block state: " + entry .getKey (), e );
104
+ }
111
105
});
112
106
113
107
return new Schematic (
114
108
new Vec (width , height , length ),
115
109
offset ,
116
110
paletteBlocks ,
117
- blockArray . copyArray ()
111
+ blockArray
118
112
);
119
113
}
120
114
0 commit comments