fix: apply origin to sponge v3 schematics (#2909)

* fix(#2864)

* Cherry-picked EngineHub/WorldEdit@cfd9eea

Co-authored-by: Pierre Maurice Schwang <mail@pschwang.eu>

* Allow metadata tags to be optional + default value for this.origin

Co-authored-by: Pierre Maurice Schwang <mail@pschwang.eu>

* Correct depth to be default of `0`

Co-authored-by: Pierre Maurice Schwang <mail@pschwang.eu>

---------

Co-authored-by: Pierre Maurice Schwang <mail@pschwang.eu>
This commit is contained in:
Tyler Resch 2024-09-22 16:41:38 -04:00 committed by GitHub
parent 7daafa97f0
commit 8e1922dfe7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 2 deletions

View File

@ -41,6 +41,8 @@ import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.enginehub.linbus.tree.LinCompoundTag; import org.enginehub.linbus.tree.LinCompoundTag;
import org.enginehub.linbus.tree.LinIntArrayTag;
import org.enginehub.linbus.tree.LinTagType;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
@ -88,6 +90,7 @@ public class FastSchematicReaderV3 implements ClipboardReader {
private VersionedDataFixer dataFixer; private VersionedDataFixer dataFixer;
private BlockVector3 offset; private BlockVector3 offset;
private BlockVector3 origin = BlockVector3.ZERO;
private BlockState[] blockPalette; private BlockState[] blockPalette;
private BiomeType[] biomePalette; private BiomeType[] biomePalette;
private int dataVersion = -1; private int dataVersion = -1;
@ -137,6 +140,24 @@ public class FastSchematicReaderV3 implements ClipboardReader {
this.dataVersion = this.dataInputStream.readInt(); this.dataVersion = this.dataInputStream.readInt();
this.dataFixer = ReaderUtil.getVersionedDataFixer(this.dataVersion, platform, platform.getDataVersion()); this.dataFixer = ReaderUtil.getVersionedDataFixer(this.dataVersion, platform, platform.getDataVersion());
} }
case "Metadata" -> {
LinCompoundTag metadataCompoundTag =
(LinCompoundTag) this.nbtInputStream.readTagPayload(NBTConstants.TYPE_COMPOUND, 0).toLinTag();
LinCompoundTag worldEditTag = metadataCompoundTag.findTag("WorldEdit", LinTagType.compoundTag());
if (worldEditTag != null) { // allowed to be optional
LinIntArrayTag originTag = worldEditTag.findTag("Origin", LinTagType.intArrayTag());
if (originTag != null) { // allowed to be optional
int[] parts = originTag.value();
if (parts.length != 3) {
throw new IOException("`Metadata > WorldEdit > Origin` int array length is invalid.");
}
this.origin = BlockVector3.at(parts[0], parts[1], parts[2]);
}
}
}
case "Offset" -> { case "Offset" -> {
this.dataInputStream.skipNBytes(4); // Array Length field (4 byte int) this.dataInputStream.skipNBytes(4); // Array Length field (4 byte int)
this.offset = BlockVector3.at( this.offset = BlockVector3.at(
@ -173,7 +194,7 @@ public class FastSchematicReaderV3 implements ClipboardReader {
clipboard.setOrigin(this.offset.multiply(-1)); clipboard.setOrigin(this.offset.multiply(-1));
if (clipboard instanceof SimpleClipboard simpleClipboard && !this.offset.equals(BlockVector3.ZERO)) { if (clipboard instanceof SimpleClipboard simpleClipboard && !this.offset.equals(BlockVector3.ZERO)) {
clipboard = new BlockArrayClipboard(simpleClipboard, this.offset); clipboard = new BlockArrayClipboard(simpleClipboard, this.offset.add(this.origin));
} }
return clipboard; return clipboard;
} }

View File

@ -135,7 +135,7 @@ public class SpongeSchematicV3Reader implements ClipboardReader {
Map<Integer, BlockState> palette = ReaderUtil.decodePalette(paletteObject, fixer); Map<Integer, BlockState> palette = ReaderUtil.decodePalette(paletteObject, fixer);
byte[] blocks = blockContainer.getTag("Data", LinTagType.byteArrayTag()).value(); byte[] blocks = blockContainer.getTag("Data", LinTagType.byteArrayTag()).value();
LinListTag<LinCompoundTag> blockEntities = blockContainer.getListTag("BlockEntities", LinTagType.compoundTag()); LinListTag<LinCompoundTag> blockEntities = blockContainer.findListTag("BlockEntities", LinTagType.compoundTag());
ReaderUtil.initializeClipboardFromBlocks(clipboard, palette, blocks, blockEntities, fixer, true); ReaderUtil.initializeClipboardFromBlocks(clipboard, palette, blocks, blockEntities, fixer, true);
} }