Skip to content
This repository was archived by the owner on May 27, 2023. It is now read-only.

Commit 049bcb7

Browse files
TechnerderLooFifteen
authored andcommitted
Fixed Schematic building location issue
1 parent 20502c6 commit 049bcb7

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

src/main/java/net/crystalgames/scaffolding/schematic/impl/SpongeSchematic.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
package net.crystalgames.scaffolding.schematic.impl;
22

3-
import java.io.OutputStream;
4-
import java.util.ArrayList;
5-
import java.util.HashMap;
6-
import java.util.LinkedHashMap;
7-
import java.util.List;
8-
import java.util.Map;
9-
import java.util.Set;
10-
import java.util.concurrent.CompletableFuture;
113
import net.crystalgames.scaffolding.region.Region;
124
import net.crystalgames.scaffolding.schematic.Schematic;
135
import net.minestom.server.coordinate.Pos;
@@ -19,6 +11,15 @@
1911
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
2012
import org.jglrxavpok.hephaistos.nbt.NBTException;
2113

14+
import java.io.OutputStream;
15+
import java.util.ArrayList;
16+
import java.util.HashMap;
17+
import java.util.LinkedHashMap;
18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.Set;
21+
import java.util.concurrent.CompletableFuture;
22+
2223
// https://github.com/EngineHub/WorldEdit/blob/303f5a76b2df70d63480f2126c9ef4b228eb3c59/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SpongeSchematicReader.java#L261-L297
2324
public class SpongeSchematic implements Schematic {
2425

@@ -32,15 +33,34 @@ public class SpongeSchematic implements Schematic {
3233

3334
private boolean read = false;
3435

36+
private int offsetX, offsetY, offsetZ;
37+
3538
@Override
3639
public void read(NBTCompound nbtTag) throws NBTException {
3740
readSizes(nbtTag);
3841
readBlockPalette(nbtTag);
42+
readOffsets(nbtTag);
3943
readBlocks();
40-
4144
read = true;
4245
}
4346

47+
private void readOffsets(@NotNull NBTCompound nbtTag) throws NBTException {
48+
NBTCompound metaData = nbtTag.getCompound("Metadata");
49+
if (metaData == null) throw new NBTException("Invalid Schematic: No Metadata");
50+
51+
Integer weOffsetX = metaData.getInt("WEOffsetX");
52+
if (weOffsetX == null) throw new NBTException("Invalid Schematic: No WEOffsetX In Metadata");
53+
this.offsetX = weOffsetX;
54+
55+
Integer weOffsetY = metaData.getInt("WEOffsetY");
56+
if (weOffsetY == null) throw new NBTException("Invalid Schematic: No WEOffsetY In Metadata");
57+
this.offsetY = weOffsetY;
58+
59+
Integer weOffsetZ = metaData.getInt("WEOffsetZ");
60+
if (weOffsetZ == null) throw new NBTException("Invalid Schematic: No WEOffsetZ In Metadata");
61+
this.offsetZ = weOffsetZ;
62+
}
63+
4464
private void readSizes(@NotNull NBTCompound nbtTag) throws NBTException {
4565
Short width = nbtTag.getShort("Width");
4666
if (width == null) throw new NBTException("Invalid Schematic: No Width");
@@ -63,9 +83,10 @@ private void readBlockPalette(@NotNull NBTCompound nbtTag) throws NBTException {
6383
if (nbtPalette == null) throw new NBTException("Invalid Schematic: No Palette");
6484

6585
Set<String> keys = nbtPalette.getKeys();
66-
if (keys.size() != maxPalette) throw new NBTException("Invalid Schematic: PaletteMax does not match Palette size");
86+
if (keys.size() != maxPalette)
87+
throw new NBTException("Invalid Schematic: PaletteMax does not match Palette size");
6788

68-
for(String key : keys) {
89+
for (String key : keys) {
6990
Integer value = nbtPalette.getInt(key);
7091
if (value == null) throw new NBTException("Invalid Schematic: Palette contains invalid value");
7192

@@ -74,7 +95,7 @@ private void readBlockPalette(@NotNull NBTCompound nbtTag) throws NBTException {
7495

7596
palette = palette.entrySet().stream()
7697
.sorted(Map.Entry.comparingByValue())
77-
.collect(LinkedHashMap::new,(map, entry) -> map.put(entry.getKey(), entry.getValue()), LinkedHashMap::putAll);
98+
.collect(LinkedHashMap::new, (map, entry) -> map.put(entry.getKey(), entry.getValue()), LinkedHashMap::putAll);
7899

79100
ImmutableByteArray blocksData = nbtTag.getByteArray("BlockData");
80101
if (blocksData == null || blocksData.getSize() == 0) throw new NBTException("Invalid Schematic: No BlockData");
@@ -110,7 +131,7 @@ private void readBlocks() throws NBTException {
110131
short stateId = getStateId(block);
111132

112133
// Is adding the height to y needed?
113-
this.regionBlocks.add(new Region.Block(new Pos(x, y + height, z), stateId));
134+
this.regionBlocks.add(new Region.Block(new Pos(x + offsetX, y + offsetY, z + offsetZ), stateId));
114135

115136
index++;
116137
}
@@ -137,8 +158,7 @@ public CompletableFuture<Region> build(Instance instance, Pos position) {
137158
blockBatch.setBlock(blockPosition.add(position), block);
138159
}
139160
}
140-
141-
blockBatch.apply(instance, () -> future.complete(new Region(instance, position, position.add(width, height, length))));
161+
blockBatch.apply(instance, () -> future.complete(new Region(instance, position, position)));
142162
});
143163
return future;
144164
}

0 commit comments

Comments
 (0)