Skip to content

Commit 08c4893

Browse files
committed
chore: fix code style
1 parent b4d98e4 commit 08c4893

3 files changed

Lines changed: 101 additions & 102 deletions

File tree

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@
134134
<pattern>com.cryptomorin.xseries</pattern>
135135
<shadedPattern>${plugin.pckg}.lib.xseries</shadedPattern>
136136
</relocation>
137+
<relocation>
138+
<pattern>dev.zerite.craftlib</pattern>
139+
<shadedPattern>${plugin.pckg}.lib.craftlib</shadedPattern>
140+
</relocation>
137141
</relocations>
138142
</configuration>
139143
</execution>
Lines changed: 57 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
package xyz.webmc.wlib.api.structure;
22

3+
import xyz.webmc.wlib.api.util.SchemUtil;
4+
35
import java.io.File;
4-
import java.io.FileInputStream;
5-
import java.io.FileOutputStream;
66
import java.io.IOException;
77
import java.util.ArrayList;
88
import java.util.List;
9-
import java.util.concurrent.CompletionException;
109

1110
import com.cryptomorin.xseries.XMaterial;
1211
import dev.zerite.craftlib.commons.world.Block;
1312
import dev.zerite.craftlib.schematic.Schematic;
14-
import dev.zerite.craftlib.schematic.SchematicIO;
1513
import dev.zerite.craftlib.schematic.SchematicMaterials;
1614
import org.bukkit.Location;
1715

1816
public 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
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package xyz.webmc.wlib.api.util;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.util.concurrent.CompletionException;
8+
9+
import dev.zerite.craftlib.schematic.Schematic;
10+
import dev.zerite.craftlib.schematic.SchematicIO;
11+
12+
public final class SchemUtil {
13+
public static final Schematic readSchematicFile(final File file) throws IOException {
14+
try (FileInputStream input = new FileInputStream(file)) {
15+
try {
16+
return SchematicIO.readFuture(input, false).join();
17+
} catch (final CompletionException ex) {
18+
if (ex.getCause() instanceof IOException io) {
19+
throw io;
20+
} else {
21+
throw ex;
22+
}
23+
}
24+
}
25+
}
26+
27+
public static final void writeSchematicFile(final Schematic schematic, final File file) throws IOException {
28+
try (FileOutputStream output = new FileOutputStream(file)) {
29+
try {
30+
SchematicIO.writeFuture(schematic, output, false).join();
31+
} catch (final CompletionException ex) {
32+
if (ex.getCause() instanceof IOException io) {
33+
throw io;
34+
} else {
35+
throw ex;
36+
}
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)