Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/main/java/net/hollowcube/schem/Structure.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package net.hollowcube.schem;

import net.hollowcube.schem.util.CoordinateUtil;
import net.hollowcube.schem.util.BlockConsumer;
import net.hollowcube.schem.util.Rotation;
import net.kyori.adventure.nbt.ByteArrayBinaryTag;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Point;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.network.NetworkBuffer;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

@SuppressWarnings("UnstableApiUsage")
public record Structure(
Expand All @@ -31,7 +35,18 @@ public record BlockInfo(

@Override
public void forEachBlock(Rotation rotation, BlockConsumer consumer) {

final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
for(BlockInfo blockInfo : this.blocks) {
Block block = this.palettes.getFirst()[blockInfo.paletteIndex];
if(blockInfo.blockEntity != null) {
block = block.withHandler(BLOCK_MANAGER.getHandlerOrDummy(blockInfo.blockEntity.id().toLowerCase(Locale.ROOT)))
.withNbt(blockInfo.blockEntity.data());
}
consumer.accept(
CoordinateUtil.rotatePos(blockInfo.pos, rotation),
CoordinateUtil.rotateBlock(block, rotation)
);
}
}

@Override
Expand All @@ -43,7 +58,7 @@ public boolean hasBlockData() {
public List<Block> blockPalette() {
// All of this logic just ensures the palette contains air.
int airIndex = -1;
var palette = palettes.get(0);
var palette = palettes.getFirst();
for (int i = 0; i < palette.length; i++) {
if (palette[i] == Block.AIR) {
airIndex = i;
Expand All @@ -61,7 +76,7 @@ public List<Block> blockPalette() {
public ByteArrayBinaryTag blockData() {
// Figure out the index of the air entry, or add one at the end
int airIndex = -1;
var palette = palettes.get(0);
var palette = palettes.getFirst();
for (int i = 0; i < palette.length; i++) {
if (palette[i] == Block.AIR) {
airIndex = i;
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/net/hollowcube/schem/reader/StructureReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import net.kyori.adventure.nbt.BinaryTagTypes;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.ListBinaryTag;
import net.minestom.server.coordinate.BlockVec;
import net.minestom.server.coordinate.Point;
import net.minestom.server.instance.block.Block;

import java.io.ByteArrayInputStream;
Expand All @@ -30,6 +32,13 @@ public Schematic read(byte[] data) throws SchematicReadException {
}
}

public static Point getRequiredPoint(CompoundBinaryTag tag, String key) {
var rawOffset = getRequired(tag, key, BinaryTagTypes.LIST);
assertTrue(rawOffset.size() == 3, "invalid {0} size {1}", key, rawOffset.size());
assertTrue(rawOffset.elementType() == BinaryTagTypes.INT, "position list must contain ints");
return new BlockVec(rawOffset.getInt(0), rawOffset.getInt(1), rawOffset.getInt(2));
}

public Schematic read(Map.Entry<String, CompoundBinaryTag> rootPair) {
assertTrue("".equals(rootPair.getKey()), "root tag must be empty, was: '{0}'", rootPair.getKey());
var root = rootPair.getValue();
Expand All @@ -44,7 +53,7 @@ public Schematic read(Map.Entry<String, CompoundBinaryTag> rootPair) {
if (singlePalette.size() != 0) {
var palette = new Block[singlePalette.size()];
for (int i = 0; i < singlePalette.size(); i++)
palette[i++] = readBlockState(singlePalette.getCompound(i));
palette[i] = readBlockState(singlePalette.getCompound(i));
palettes.add(palette);
paletteSize = palette.length;
} else {
Expand All @@ -53,7 +62,7 @@ public Schematic read(Map.Entry<String, CompoundBinaryTag> rootPair) {
var innerPalette = (ListBinaryTag) innerPaletteRaw;
var palette = new Block[innerPalette.size()];
for (int i = 0; i < innerPalette.size(); i++)
palette[i++] = readBlockState(innerPalette.getCompound(i));
palette[i] = readBlockState(innerPalette.getCompound(i));
palettes.add(palette);

if (paletteSize == -1) paletteSize = palette.length;
Expand Down