11package xyz .webmc .wlib .api .structure ;
22
3+ import xyz .webmc .wlib .api .util .SchemUtil ;
4+
35import java .io .File ;
4- import java .io .FileInputStream ;
5- import java .io .FileOutputStream ;
66import java .io .IOException ;
77import java .util .ArrayList ;
88import java .util .List ;
9- import java .util .concurrent .CompletionException ;
109
1110import com .cryptomorin .xseries .XMaterial ;
1211import dev .zerite .craftlib .commons .world .Block ;
1312import dev .zerite .craftlib .schematic .Schematic ;
14- import dev .zerite .craftlib .schematic .SchematicIO ;
1513import dev .zerite .craftlib .schematic .SchematicMaterials ;
1614import org .bukkit .Location ;
1715
1816public abstract class AbstractBaseStructure {
19-
2017 private final List <BlockRelative > blocks = new ArrayList <>();
2118 private final String name ;
2219
@@ -38,122 +35,80 @@ public final String getName() {
3835 return this .name ;
3936 }
4037
41- public final void loadFromSchematic (final File file ) {
42- try {
43- final Schematic schematic = readSchematic (file );
38+ public final void saveSchematic (final File file ) throws IOException {
39+ final Schematic schematic = createSchematic ();
40+ SchemUtil .writeSchematicFile (schematic , file );
41+ }
4442
45- final int width = schematic .getWidth ();
46- final int height = schematic .getHeight ();
47- final int length = schematic .getLength ();
43+ public final void loadSchematic (final File file ) throws IOException {
44+ final Schematic schematic = SchemUtil .readSchematicFile (file );
4845
49- final int offsetX = schematic .getOffsetX ();
50- final int offsetY = schematic .getOffsetY ();
51- final int offsetZ = schematic .getOffsetZ ();
46+ final int width = schematic .getWidth ();
47+ final int height = schematic .getHeight ();
48+ final int length = schematic .getLength ();
5249
53- for (int y = 0 ; y < height ; y ++) {
54- for (int z = 0 ; z < length ; z ++) {
55- for (int x = 0 ; x < width ; x ++) {
56- final Block block = schematic .get (x , y , z );
57- if (block == null || block == Block .AIR ) continue ;
50+ final int offsetX = schematic .getOffsetX ();
51+ final int offsetY = schematic .getOffsetY ();
52+ final int offsetZ = schematic .getOffsetZ ();
5853
54+ for (int y = 0 ; y < height ; y ++) {
55+ for (int z = 0 ; z < length ; z ++) {
56+ for (int x = 0 ; x < width ; x ++) {
57+ final Block block = schematic .get (x , y , z );
58+ if (block != Block .AIR ) {
5959 final XMaterial mat = XMaterial .matchXMaterial (block .toString ()).get ();
60- if (mat == null ) continue ;
61-
62- this .blocks .add (new BlockRelative (
63- x - offsetX ,
64- y - offsetY ,
65- z - offsetZ ,
66- mat ,
67- (byte ) block .getMetadata ()
68- ));
60+ if (mat != null ) {
61+ this .addBlock (new BlockRelative (x - offsetX , y - offsetY , z - offsetZ , mat , (byte ) block .getMetadata ()));
62+ }
6963 }
7064 }
7165 }
72- } catch (Exception e ) {
73- throw new RuntimeException ("Failed to load schematic: " + file .getName (), e );
7466 }
7567 }
7668
77- private static Schematic readSchematic (final File file ) throws IOException {
78- try (FileInputStream input = new FileInputStream (file )) {
79- try {
80- return SchematicIO .readFuture (input , false ).join ();
81- } catch (CompletionException ex ) {
82- final Throwable cause = ex .getCause ();
83- if (cause instanceof IOException ) {
84- throw (IOException ) cause ;
85- }
86- throw ex ;
69+ private Schematic createSchematic () {
70+ if (!this .blocks .isEmpty ()) {
71+ int minX = Integer .MAX_VALUE ;
72+ int minY = Integer .MAX_VALUE ;
73+ int minZ = Integer .MAX_VALUE ;
74+ int maxX = Integer .MIN_VALUE ;
75+ int maxY = Integer .MIN_VALUE ;
76+ int maxZ = Integer .MIN_VALUE ;
77+
78+ for (final BlockRelative blk : this .blocks ) {
79+ minX = Math .min (minX , blk .getX ());
80+ minY = Math .min (minY , blk .getY ());
81+ minZ = Math .min (minZ , blk .getZ ());
82+ maxX = Math .max (maxX , blk .getX ());
83+ maxY = Math .max (maxY , blk .getY ());
84+ maxZ = Math .max (maxZ , blk .getZ ());
8785 }
88- }
89- }
9086
91- public final void saveToSchematic (final File file ) {
92- try {
93- final Schematic schematic = buildSchematic ();
94- writeSchematic (schematic , file );
95- } catch (Exception e ) {
96- throw new RuntimeException ("Failed to save schematic: " + file .getName (), e );
97- }
98- }
99-
100- private Schematic buildSchematic () {
101- if (this .blocks .isEmpty ()) {
102- return new Schematic ((short ) 0 , (short ) 0 , (short ) 0 , SchematicMaterials .ALPHA );
103- }
87+ final short width = (short ) (maxX - minX + 1 );
88+ final short height = (short ) (maxY - minY + 1 );
89+ final short length = (short ) (maxZ - minZ + 1 );
10490
105- int minX = Integer .MAX_VALUE ;
106- int minY = Integer .MAX_VALUE ;
107- int minZ = Integer .MAX_VALUE ;
108- int maxX = Integer .MIN_VALUE ;
109- int maxY = Integer .MIN_VALUE ;
110- int maxZ = Integer .MIN_VALUE ;
111-
112- for (final BlockRelative blk : this .blocks ) {
113- minX = Math .min (minX , blk .getX ());
114- minY = Math .min (minY , blk .getY ());
115- minZ = Math .min (minZ , blk .getZ ());
116- maxX = Math .max (maxX , blk .getX ());
117- maxY = Math .max (maxY , blk .getY ());
118- maxZ = Math .max (maxZ , blk .getZ ());
119- }
120-
121- final short width = (short ) (maxX - minX + 1 );
122- final short height = (short ) (maxY - minY + 1 );
123- final short length = (short ) (maxZ - minZ + 1 );
124-
125- final Block [] blockArray = new Block [width * height * length ];
126- for (int i = 0 ; i < blockArray .length ; i ++) {
127- blockArray [i ] = Block .AIR ;
128- }
129-
130- for (final BlockRelative blk : this .blocks ) {
131- final int x = blk .getX () - minX ;
132- final int y = blk .getY () - minY ;
133- final int z = blk .getZ () - minZ ;
134- final int index = x + z * width + y * width * length ;
135-
136- final XMaterial xmat = blk .getMaterial ();
137- if (xmat != null && xmat .getId () >= 0 ) {
138- final int blockId = (xmat .getId () << 4 ) | (blk .getDataLegacy () & 0xF );
139- blockArray [index ] = new Block (blockId , x , y , z );
91+ final Block [] blockArray = new Block [width * height * length ];
92+ for (int i = 0 ; i < blockArray .length ; i ++) {
93+ blockArray [i ] = Block .AIR ;
14094 }
141- }
14295
143- return new Schematic (width , height , length , SchematicMaterials .ALPHA , blockArray , new ArrayList <>(), new ArrayList <>(), -minX , -minY , -minZ );
144- }
96+ for (final BlockRelative blk : this .blocks ) {
97+ final int x = blk .getX () - minX ;
98+ final int y = blk .getY () - minY ;
99+ final int z = blk .getZ () - minZ ;
100+ final int index = x + z * width + y * width * length ;
145101
146- private static void writeSchematic (final Schematic schematic , final File file ) throws IOException {
147- try (FileOutputStream output = new FileOutputStream (file )) {
148- try {
149- SchematicIO .writeFuture (schematic , output , false ).join ();
150- } catch (CompletionException ex ) {
151- final Throwable cause = ex .getCause ();
152- if (cause instanceof IOException ) {
153- throw (IOException ) cause ;
102+ final XMaterial xmat = blk .getMaterial ();
103+ if (xmat != null && xmat .getId () >= 0 ) {
104+ final int blockId = (xmat .getId () << 4 ) | (blk .getDataLegacy () & 0xF );
105+ blockArray [index ] = new Block (blockId , x , y , z );
154106 }
155- throw ex ;
156107 }
108+
109+ return new Schematic (width , height , length , SchematicMaterials .ALPHA , blockArray , new ArrayList <>(), new ArrayList <>(), -minX , -minY , -minZ );
110+ } else {
111+ return new Schematic ((short ) 0 , (short ) 0 , (short ) 0 , SchematicMaterials .ALPHA );
157112 }
158113 }
159114}
0 commit comments