finish sponge schematic addon

This commit is contained in:
dfsek 2021-10-15 22:21:16 -07:00
parent e971223e4f
commit ab100c85a1
2 changed files with 37 additions and 28 deletions

View File

@ -1,9 +1,18 @@
package com.dfsek.terra.addons.sponge; package com.dfsek.terra.addons.sponge;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.addon.TerraAddon;
import com.dfsek.terra.api.addon.annotations.Addon;
import com.dfsek.terra.api.addon.annotations.Author;
import com.dfsek.terra.api.addon.annotations.Version;
import com.dfsek.terra.api.block.state.BlockState; import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
import com.dfsek.terra.api.inject.annotations.Inject;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.structure.Structure;
import net.querz.nbt.io.NBTDeserializer; import net.querz.nbt.io.NBTDeserializer;
import net.querz.nbt.io.NBTUtil;
import net.querz.nbt.tag.ByteArrayTag; import net.querz.nbt.tag.ByteArrayTag;
import net.querz.nbt.tag.CompoundTag; import net.querz.nbt.tag.CompoundTag;
import net.querz.nbt.tag.IntTag; import net.querz.nbt.tag.IntTag;
@ -16,22 +25,14 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import com.dfsek.terra.api.Platform; @Addon("structure-sponge-loader")
import com.dfsek.terra.api.addon.TerraAddon; @Author("Terra")
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent; @Version("1.0.0")
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
import com.dfsek.terra.api.inject.annotations.Inject;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.structure.Structure;
public class SpongeSchematicAddon extends TerraAddon { public class SpongeSchematicAddon extends TerraAddon {
@Inject @Inject
private Platform platform; private Platform platform;
@Override @Override
public void initialize() { public void initialize() {
platform.getEventManager() platform.getEventManager()
@ -41,7 +42,8 @@ public class SpongeSchematicAddon extends TerraAddon {
CheckedRegistry<Structure> structureRegistry = event.getPack().getOrCreateRegistry(Structure.class); CheckedRegistry<Structure> structureRegistry = event.getPack().getOrCreateRegistry(Structure.class);
event.getPack().getLoader().open("", ".schem").thenEntries(entries -> { event.getPack().getLoader().open("", ".schem").thenEntries(entries -> {
for(Map.Entry<String, InputStream> entry : entries) { for(Map.Entry<String, InputStream> entry : entries) {
String id = entry.getKey().substring(0, entry.getKey().length() - ".schem".length());
structureRegistry.register(id, convert(entry.getValue(), id));
} }
}).close(); }).close();
}) })
@ -49,7 +51,7 @@ public class SpongeSchematicAddon extends TerraAddon {
} }
public String convert(InputStream in) { public SpongeStructure convert(InputStream in, String id) {
try { try {
CompoundTag baseTag = (CompoundTag) new NBTDeserializer(false).fromStream(detectDecompression(in)).getTag(); CompoundTag baseTag = (CompoundTag) new NBTDeserializer(false).fromStream(detectDecompression(in)).getTag();
int wid = baseTag.getShort("Width"); int wid = baseTag.getShort("Width");
@ -65,30 +67,31 @@ public class SpongeSchematicAddon extends TerraAddon {
data.put(((IntTag) entry.getValue()).asInt(), entry.getKey()); data.put(((IntTag) entry.getValue()).asInt(), entry.getKey());
} }
BlockState[][][] states = new BlockState[wid][len][hei];
byte[] arr = blocks.getValue(); byte[] arr = blocks.getValue();
ScriptBuilder builder = new ScriptBuilder(); for(int x = 0; x < wid; x++) {
for(int x = 0; x < hei; x++) {
for(int y = 0; y < wid; y++) {
for(int z = 0; z < len; z++) { for(int z = 0; z < len; z++) {
String block = data.get((int) arr[x+z*wid+y*wid*len]); for(int y = 0; y < hei; y++) {
String block = data.get((int) arr[x + z * wid + y * wid * len]);
if(block.startsWith("minecraft:structure_void")) continue; if(block.startsWith("minecraft:structure_void")) continue;
builder.block(x, y, z, block); states[x][z][y] = platform.getWorldHandle().createBlockData(block);
} }
} }
} }
return builder.build(); return new SpongeStructure(states, platform, id);
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); throw new IllegalArgumentException("Failed to parse Sponge schematic: ", e);
} }
return null;
} }
private static InputStream detectDecompression(InputStream is) throws IOException { private static InputStream detectDecompression(InputStream is) throws IOException {
PushbackInputStream pbis = new PushbackInputStream(is, 2); PushbackInputStream pbis = new PushbackInputStream(is, 2);
int signature = (pbis.read() & 0xFF) + (pbis.read() << 8); int signature = (pbis.read() & 0xFF) + (pbis.read() << 8);
pbis.unread(signature >> 8); pbis.unread(signature >> 8);
pbis.unread(signature & 0xFF); pbis.unread(signature & 0xFF);
if (signature == GZIPInputStream.GZIP_MAGIC) { if(signature == GZIPInputStream.GZIP_MAGIC) {
return new GZIPInputStream(pbis); return new GZIPInputStream(pbis);
} }
return pbis; return pbis;

View File

@ -44,7 +44,9 @@ public class SpongeStructure implements Structure {
continue; continue;
} }
for(int y = 0; y < blocks[z].length; y++) { for(int y = 0; y < blocks[z].length; y++) {
world.setBlockData(bX+rX, bY+y, bZ+rZ, blocks[x][z][y]); BlockState state = blocks[x][z][y];
if(state == null) continue;
world.setBlockData(bX+rX, bY+y, bZ+rZ, state);
} }
} }
} }
@ -59,7 +61,9 @@ public class SpongeStructure implements Structure {
int rX = r.getBlockX(); int rX = r.getBlockX();
int rZ = r.getBlockZ(); int rZ = r.getBlockZ();
for(int y = 0; y < blocks[z].length; y++) { for(int y = 0; y < blocks[z].length; y++) {
buffer.addItem(new BufferedBlock(blocks[x][z][y], true, platform, false), new Vector3(rX, y, rZ)); BlockState state = blocks[x][z][y];
if(state == null) continue;
buffer.addItem(new BufferedBlock(state, true, platform, false), new Vector3(rX, y, rZ));
} }
} }
} }
@ -77,7 +81,9 @@ public class SpongeStructure implements Structure {
int rX = r.getBlockX(); int rX = r.getBlockX();
int rZ = r.getBlockZ(); int rZ = r.getBlockZ();
for(int y = 0; y < blocks[z].length; y++) { for(int y = 0; y < blocks[z].length; y++) {
world.setBlockData(bX+rX, bY+y, bZ+rZ, blocks[x][z][y]); BlockState state = blocks[x][z][y];
if(state == null) continue;
world.setBlockData(bX+rX, bY+y, bZ+rZ, state);
} }
} }
} }