Skip to content

Commit

Permalink
Add BlockPaletteParser, for example to deal with minecraft:grass->min…
Browse files Browse the repository at this point in the history
…ecraft:short_grass id upgrade
  • Loading branch information
Minikloon authored and mworzala committed Jul 2, 2024
1 parent cb689a5 commit d46f28e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
18 changes: 12 additions & 6 deletions src/main/java/net/hollowcube/schem/SchematicReader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.hollowcube.schem;


import net.hollowcube.schem.blockpalette.BlockPaletteParser;
import net.hollowcube.schem.blockpalette.CommandBlockPaletteParser;
import net.kyori.adventure.nbt.BinaryTagIO;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.IntBinaryTag;
Expand All @@ -21,26 +23,30 @@
public final class SchematicReader {
private static final BinaryTagIO.Reader NBT_READER = BinaryTagIO.unlimitedReader();

private SchematicReader() {
private BlockPaletteParser paletteParser = new CommandBlockPaletteParser();

public SchematicReader withBlockPaletteParser(BlockPaletteParser parser) {
this.paletteParser = parser;
return this;
}

public static @NotNull Schematic read(@NotNull InputStream stream) {
public @NotNull Schematic read(@NotNull InputStream stream) {
try {
return read(NBT_READER.readNamed(stream, BinaryTagIO.Compression.GZIP));
} catch (Exception e) {
throw new SchematicReadException("failed to read schematic NBT", e);
}
}

public static @NotNull Schematic read(@NotNull Path path) {
public @NotNull Schematic read(@NotNull Path path) {
try {
return read(NBT_READER.readNamed(path, BinaryTagIO.Compression.GZIP));
} catch (Exception e) {
throw new SchematicReadException("failed to read schematic NBT", e);
}
}

public static @NotNull Schematic read(@NotNull Map.Entry<String, CompoundBinaryTag> namedTag) {
public @NotNull Schematic read(@NotNull Map.Entry<String, CompoundBinaryTag> namedTag) {
try {
// If it has a Schematic tag is sponge v2 or 3
var schematicTag = namedTag.getValue().get("Schematic");
Expand All @@ -55,7 +61,7 @@ private SchematicReader() {
}
}

private static @NotNull Schematic read(@NotNull CompoundBinaryTag tag, int version) {
private @NotNull Schematic read(@NotNull CompoundBinaryTag tag, int version) {
short width = tag.getShort("Width");
short height = tag.getShort("Height");
short length = tag.getShort("Length");
Expand Down Expand Up @@ -97,7 +103,7 @@ private SchematicReader() {
palette.forEach((entry) -> {
try {
int assigned = ((IntBinaryTag) entry.getValue()).value();
Block block = ArgumentBlockState.staticParse(entry.getKey());
Block block = paletteParser.parse(entry.getKey());
paletteBlocks[assigned] = block;
} catch (ArgumentSyntaxException e) {
throw new SchematicReadException("Failed to parse block state: " + entry.getKey(), e);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/hollowcube/schem/SchematicWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import java.nio.file.Path;
import java.util.Map;

public class SchematicWriter {
public final class SchematicWriter {

public static byte @NotNull [] write(@NotNull Schematic schematic) {
public byte @NotNull [] write(@NotNull Schematic schematic) {
CompoundBinaryTag.Builder schematicNBT = CompoundBinaryTag.builder();
schematicNBT.putInt("Version", 2);
schematicNBT.putInt("DataVersion", MinecraftServer.DATA_VERSION);
Expand Down Expand Up @@ -56,7 +56,7 @@ public class SchematicWriter {
return out.toByteArray();
}

public static void write(@NotNull Schematic schematic, @NotNull Path schemPath) throws IOException {
public void write(@NotNull Schematic schematic, @NotNull Path schemPath) throws IOException {
Files.write(schemPath, write(schematic));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.hollowcube.schem.blockpalette;

import net.minestom.server.instance.block.Block;

public interface BlockPaletteParser {
Block parse(String key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.hollowcube.schem.blockpalette;

import net.minestom.server.command.builder.arguments.minecraft.ArgumentBlockState;
import net.minestom.server.instance.block.Block;

public class CommandBlockPaletteParser implements BlockPaletteParser {
@Override
public Block parse(String key) {
return ArgumentBlockState.staticParse(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void testReadFail1_20_1() {
private @NotNull Schematic assertReadSchematic(@NotNull String path) {
try (var is = getClass().getResourceAsStream(path)) {
assertNotNull(is, "Failed to load resource: " + path);
return SchematicReader.read(is);
return new SchematicReader().read(is);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/net/hollowcube/schem/demo/DemoServer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.hollowcube.schem.demo;

import net.hollowcube.schem.Rotation;
import net.hollowcube.schem.SchematicReader;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.Command;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void execute(@NotNull CommandSender sender, @NotNull CommandContext conte
var player = (Player) sender;

try (var is = getClass().getResourceAsStream("/" + String.join(" ", context.<String[]>get("path")) + ".schem")) {
var schem = net.hollowcube.schem.SchematicReader.read(is);
var schem = new SchematicReader().read(is);
schem.build(Rotation.NONE, false).apply(instance, player.getPosition(), () -> {
player.sendMessage("Done!");
});
Expand Down

0 comments on commit d46f28e

Please sign in to comment.