mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-13 15:08:35 +00:00
some fixes
Use sponge schematic format instead of structure block Fix VS undo running on main thread Fix missing sections when setting blocks
This commit is contained in:
@ -21,8 +21,10 @@ package com.sk89q.worldedit.extent;
|
||||
|
||||
import com.boydti.fawe.jnbt.anvil.generator.*;
|
||||
import com.boydti.fawe.object.PseudoRandom;
|
||||
import com.boydti.fawe.object.clipboard.WorldCopyClipboard;
|
||||
import com.sk89q.worldedit.*;
|
||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
|
||||
import com.sk89q.worldedit.regions.CuboidRegion;
|
||||
import com.sk89q.worldedit.util.Countable;
|
||||
import com.sk89q.worldedit.world.block.*;
|
||||
@ -359,6 +361,19 @@ public interface Extent extends InputExtent, OutputExtent {
|
||||
return distribution;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazily copy a region
|
||||
*
|
||||
* @param region
|
||||
* @return
|
||||
*/
|
||||
default BlockArrayClipboard lazyCopy(Region region) {
|
||||
WorldCopyClipboard faweClipboard = new WorldCopyClipboard(this, region);
|
||||
BlockArrayClipboard weClipboard = new BlockArrayClipboard(region, faweClipboard);
|
||||
weClipboard.setOrigin(region.getMinimumPoint());
|
||||
return weClipboard;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
default Operation commit() {
|
||||
|
@ -94,6 +94,7 @@ public enum ClipboardFormat {
|
||||
|
||||
@Override
|
||||
public boolean isFormat(File file) {
|
||||
if (!file.getName().toLowerCase().endsWith(".schematic")) return false;
|
||||
DataInputStream str = null;
|
||||
try {
|
||||
str = new DataInputStream(new GZIPInputStream(new FileInputStream(file)));
|
||||
@ -151,6 +152,7 @@ public enum ClipboardFormat {
|
||||
|
||||
@Override
|
||||
public boolean isFormat(File file) {
|
||||
if (!file.getName().toLowerCase().endsWith(".schem")) return false;
|
||||
DataInputStream str = null;
|
||||
try {
|
||||
str = new DataInputStream(new GZIPInputStream(new FileInputStream(file)));
|
||||
@ -496,13 +498,7 @@ public enum ClipboardFormat {
|
||||
|
||||
LocalSession session = WorldEdit.getInstance().getSessionManager().get(player);
|
||||
session.setClipboard(null);
|
||||
if (reader instanceof SchematicReader) {
|
||||
clipboard = ((SchematicReader) reader).read(player.getUniqueId());
|
||||
} else if (reader instanceof StructureFormat) {
|
||||
clipboard = ((StructureFormat) reader).read(player.getUniqueId());
|
||||
} else {
|
||||
clipboard = reader.read();
|
||||
}
|
||||
clipboard = reader.read(player.getUniqueId());
|
||||
URIClipboardHolder holder = new URIClipboardHolder(uri, clipboard);
|
||||
session.setClipboard(holder);
|
||||
return holder;
|
||||
|
@ -24,6 +24,7 @@ import com.sk89q.worldedit.world.registry.Registries;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Reads {@code Clipboard}s.
|
||||
@ -40,4 +41,7 @@ public interface ClipboardReader extends Closeable {
|
||||
*/
|
||||
Clipboard read() throws IOException;
|
||||
|
||||
}
|
||||
default Clipboard read(UUID uuid) throws IOException {
|
||||
return read();
|
||||
}
|
||||
}
|
@ -19,6 +19,18 @@
|
||||
|
||||
package com.sk89q.worldedit.extent.clipboard.io;
|
||||
|
||||
import com.boydti.fawe.Fawe;
|
||||
import com.boydti.fawe.config.Settings;
|
||||
import com.boydti.fawe.jnbt.NBTStreamer;
|
||||
import com.boydti.fawe.object.FaweInputStream;
|
||||
import com.boydti.fawe.object.FaweOutputStream;
|
||||
import com.boydti.fawe.object.clipboard.CPUOptimizedClipboard;
|
||||
import com.boydti.fawe.object.clipboard.DiskOptimizedClipboard;
|
||||
import com.boydti.fawe.object.clipboard.FaweClipboard;
|
||||
import com.boydti.fawe.object.clipboard.MemoryOptimizedClipboard;
|
||||
import com.boydti.fawe.object.io.FastByteArrayOutputStream;
|
||||
import com.boydti.fawe.object.io.FastByteArraysInputStream;
|
||||
import com.boydti.fawe.util.IOUtil;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.sk89q.jnbt.ByteArrayTag;
|
||||
import com.sk89q.jnbt.CompoundTag;
|
||||
@ -28,11 +40,13 @@ import com.sk89q.jnbt.ListTag;
|
||||
import com.sk89q.jnbt.NBTInputStream;
|
||||
import com.sk89q.jnbt.NamedTag;
|
||||
import com.sk89q.jnbt.ShortTag;
|
||||
import com.sk89q.jnbt.StringTag;
|
||||
import com.sk89q.jnbt.Tag;
|
||||
import com.sk89q.worldedit.BlockVector;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||
import com.sk89q.worldedit.entity.BaseEntity;
|
||||
import com.sk89q.worldedit.extension.input.ParserContext;
|
||||
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
|
||||
import com.sk89q.worldedit.extent.clipboard.Clipboard;
|
||||
@ -40,12 +54,21 @@ import com.sk89q.worldedit.extent.clipboard.io.legacycompat.NBTCompatibilityHand
|
||||
import com.sk89q.worldedit.regions.CuboidRegion;
|
||||
import com.sk89q.worldedit.regions.Region;
|
||||
import com.sk89q.worldedit.world.block.BlockState;
|
||||
import com.sk89q.worldedit.world.block.BlockTypes;
|
||||
import com.sk89q.worldedit.world.entity.EntityType;
|
||||
import com.sk89q.worldedit.world.entity.EntityTypes;
|
||||
import net.jpountz.lz4.LZ4BlockInputStream;
|
||||
import net.jpountz.lz4.LZ4BlockOutputStream;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -77,141 +100,151 @@ public class SpongeSchematicReader extends NBTSchematicReader {
|
||||
|
||||
@Override
|
||||
public Clipboard read() throws IOException {
|
||||
NamedTag rootTag = inputStream.readNamedTag();
|
||||
if (!rootTag.getName().equals("Schematic")) {
|
||||
throw new IOException("Tag 'Schematic' does not exist or is not first");
|
||||
}
|
||||
CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
|
||||
return read(UUID.randomUUID());
|
||||
}
|
||||
|
||||
// Check
|
||||
Map<String, Tag> schematic = schematicTag.getValue();
|
||||
int version = requireTag(schematic, "Version", IntTag.class).getValue();
|
||||
switch (version) {
|
||||
case 1:
|
||||
return readVersion1(schematic);
|
||||
default:
|
||||
throw new IOException("This schematic version is currently not supported");
|
||||
@Override
|
||||
public Clipboard read(UUID uuid) throws IOException {
|
||||
return readVersion1(uuid);
|
||||
}
|
||||
|
||||
private int width, height, length;
|
||||
private int offsetX, offsetY, offsetZ;
|
||||
private char[] palette;
|
||||
private Vector min;
|
||||
private FaweClipboard fc;
|
||||
|
||||
private FaweClipboard setupClipboard(int size, UUID uuid) {
|
||||
if (fc != null) {
|
||||
if (fc.getDimensions().getX() == 0) {
|
||||
fc.setDimensions(new Vector(size, 1, 1));
|
||||
}
|
||||
return fc;
|
||||
}
|
||||
if (Settings.IMP.CLIPBOARD.USE_DISK) {
|
||||
return fc = new DiskOptimizedClipboard(size, 1, 1, uuid);
|
||||
} else if (Settings.IMP.CLIPBOARD.COMPRESSION_LEVEL == 0) {
|
||||
return fc = new CPUOptimizedClipboard(size, 1, 1);
|
||||
} else {
|
||||
return fc = new MemoryOptimizedClipboard(size, 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
private Clipboard readVersion1(Map<String, Tag> schematic) throws IOException {
|
||||
Vector origin;
|
||||
Region region;
|
||||
private Clipboard readVersion1(UUID uuid) throws IOException {
|
||||
width = height = length = offsetX = offsetY = offsetZ = Integer.MIN_VALUE;
|
||||
|
||||
Map<String, Tag> metadata = requireTag(schematic, "Metadata", CompoundTag.class).getValue();
|
||||
final BlockArrayClipboard clipboard = new BlockArrayClipboard(new CuboidRegion(new Vector(0, 0, 0), new Vector(0, 0, 0)), fc);
|
||||
FastByteArrayOutputStream blocksOut = new FastByteArrayOutputStream();
|
||||
FastByteArrayOutputStream biomesOut = new FastByteArrayOutputStream();
|
||||
|
||||
int width = requireTag(schematic, "Width", ShortTag.class).getValue();
|
||||
int height = requireTag(schematic, "Height", ShortTag.class).getValue();
|
||||
int length = requireTag(schematic, "Length", ShortTag.class).getValue();
|
||||
|
||||
int[] offsetParts = requireTag(schematic, "Offset", IntArrayTag.class).getValue();
|
||||
if (offsetParts.length != 3) {
|
||||
throw new IOException("Invalid offset specified in schematic.");
|
||||
}
|
||||
|
||||
Vector min = new Vector(offsetParts[0], offsetParts[1], offsetParts[2]);
|
||||
|
||||
if (metadata.containsKey("WEOffsetX")) {
|
||||
// We appear to have WorldEdit Metadata
|
||||
int offsetX = requireTag(metadata, "WEOffsetX", IntTag.class).getValue();
|
||||
int offsetY = requireTag(metadata, "WEOffsetY", IntTag.class).getValue();
|
||||
int offsetZ = requireTag(metadata, "WEOffsetZ", IntTag.class).getValue();
|
||||
Vector offset = new Vector(offsetX, offsetY, offsetZ);
|
||||
origin = min.subtract(offset);
|
||||
NBTStreamer streamer = new NBTStreamer(inputStream);
|
||||
streamer.addReader("Schematic.Width", (BiConsumer<Integer, Short>) (i, v) -> width = v);
|
||||
streamer.addReader("Schematic.Height", (BiConsumer<Integer, Short>) (i, v) -> height = v);
|
||||
streamer.addReader("Schematic.Length", (BiConsumer<Integer, Short>) (i, v) -> length = v);
|
||||
streamer.addReader("Schematic.Offset", (BiConsumer<Integer, int[]>) (i, v) -> min = new BlockVector(v[0], v[1], v[2]));
|
||||
streamer.addReader("Schematic.Metadata.WEOffsetX", (BiConsumer<Integer, Integer>) (i, v) -> offsetX = v);
|
||||
streamer.addReader("Schematic.Metadata.WEOffsetY", (BiConsumer<Integer, Integer>) (i, v) -> offsetY = v);
|
||||
streamer.addReader("Schematic.Metadata.WEOffsetZ", (BiConsumer<Integer, Integer>) (i, v) -> offsetZ = v);
|
||||
streamer.addReader("Schematic.Palette", (BiConsumer<Integer, HashMap<String, Tag>>) (i, v) -> {
|
||||
palette = new char[v.size()];
|
||||
for (Map.Entry<String, Tag> entry : v.entrySet()) {
|
||||
BlockState state = BlockState.get(entry.getKey());
|
||||
int index = ((IntTag) entry.getValue()).getValue();
|
||||
palette[index] = (char) state.getOrdinal();
|
||||
}
|
||||
});
|
||||
streamer.addReader("Schematic.BlockData.#", new NBTStreamer.LazyReader() {
|
||||
@Override
|
||||
public void accept(Integer arrayLen, DataInputStream dis) {
|
||||
try (FaweOutputStream blocks = new FaweOutputStream(new LZ4BlockOutputStream(blocksOut))) {
|
||||
IOUtil.copy(dis, blocks, arrayLen);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
streamer.addReader("Schematic.Biomes.#", new NBTStreamer.LazyReader() {
|
||||
@Override
|
||||
public void accept(Integer arrayLen, DataInputStream dis) {
|
||||
try (FaweOutputStream biomes = new FaweOutputStream(new LZ4BlockOutputStream(biomesOut))) {
|
||||
IOUtil.copy(dis, biomes, arrayLen);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
streamer.addReader("Schematic.TileEntities.#", new BiConsumer<Integer, CompoundTag>() {
|
||||
@Override
|
||||
public void accept(Integer index, CompoundTag value) {
|
||||
if (fc == null) {
|
||||
setupClipboard(0, uuid);
|
||||
}
|
||||
int[] pos = value.getIntArray("Pos");
|
||||
int x = pos[0];
|
||||
int y = pos[1];
|
||||
int z = pos[2];
|
||||
fc.setTile(x, y, z, value);
|
||||
}
|
||||
});
|
||||
streamer.addReader("Schematic.Entities.#", new BiConsumer<Integer, CompoundTag>() {
|
||||
@Override
|
||||
public void accept(Integer index, CompoundTag compound) {
|
||||
if (fc == null) {
|
||||
setupClipboard(0, uuid);
|
||||
}
|
||||
String id = compound.getString("id");
|
||||
if (id.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
ListTag positionTag = compound.getListTag("Pos");
|
||||
ListTag directionTag = compound.getListTag("Rotation");
|
||||
EntityType type = EntityTypes.parse(id);
|
||||
if (type != null) {
|
||||
compound.getValue().put("Id", new StringTag(type.getId()));
|
||||
BaseEntity state = new BaseEntity(type, compound);
|
||||
fc.createEntity(clipboard, positionTag.asDouble(0), positionTag.asDouble(1), positionTag.asDouble(2), (float) directionTag.asDouble(0), (float) directionTag.asDouble(1), state);
|
||||
} else {
|
||||
Fawe.debug("Invalid entity: " + id);
|
||||
}
|
||||
}
|
||||
});
|
||||
streamer.readFully();
|
||||
if (fc == null) setupClipboard(length * width * height, uuid);
|
||||
Vector origin = min;
|
||||
CuboidRegion region;
|
||||
if (offsetX != Integer.MIN_VALUE && offsetY != Integer.MIN_VALUE && offsetZ != Integer.MIN_VALUE) {
|
||||
origin = origin.subtract(new Vector(offsetX, offsetY, offsetZ));
|
||||
region = new CuboidRegion(min, min.add(width, height, length).subtract(Vector.ONE));
|
||||
} else {
|
||||
origin = min;
|
||||
region = new CuboidRegion(origin, origin.add(width, height, length).subtract(Vector.ONE));
|
||||
region = new CuboidRegion(min, min.add(width, height, length).subtract(Vector.ONE));
|
||||
}
|
||||
|
||||
int paletteMax = requireTag(schematic, "PaletteMax", IntTag.class).getValue();
|
||||
Map<String, Tag> paletteObject = requireTag(schematic, "Palette", CompoundTag.class).getValue();
|
||||
if (paletteObject.size() != paletteMax) {
|
||||
throw new IOException("Differing given palette size to actual size");
|
||||
}
|
||||
|
||||
Map<Integer, BlockState> palette = new HashMap<>();
|
||||
|
||||
ParserContext parserContext = new ParserContext();
|
||||
parserContext.setRestricted(false);
|
||||
parserContext.setTryLegacy(false);
|
||||
parserContext.setPreferringWildcard(false);
|
||||
|
||||
for (String palettePart : paletteObject.keySet()) {
|
||||
int id = requireTag(paletteObject, palettePart, IntTag.class).getValue();
|
||||
BlockState state = BlockState.get(palettePart);
|
||||
palette.put(id, state);
|
||||
}
|
||||
|
||||
byte[] blocks = requireTag(schematic, "BlockData", ByteArrayTag.class).getValue();
|
||||
|
||||
Map<BlockVector, Map<String, Tag>> tileEntitiesMap = new HashMap<>();
|
||||
try {
|
||||
List<Map<String, Tag>> tileEntityTags = ((ListTag<CompoundTag>) requireTag(schematic, "TileEntities", ListTag.class)).getValue().stream()
|
||||
.map(CompoundTag::getValue)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (Map<String, Tag> tileEntity : tileEntityTags) {
|
||||
int[] pos = requireTag(tileEntity, "Pos", IntArrayTag.class).getValue();
|
||||
tileEntitiesMap.put(new BlockVector(pos[0], pos[1], pos[2]), tileEntity);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IOException("Failed to load Tile Entities: " + e.getMessage());
|
||||
}
|
||||
|
||||
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
|
||||
clipboard.setOrigin(origin);
|
||||
|
||||
int index = 0;
|
||||
int i = 0;
|
||||
int value = 0;
|
||||
int varintLength = 0;
|
||||
while (i < blocks.length) {
|
||||
value = 0;
|
||||
varintLength = 0;
|
||||
|
||||
while (true) {
|
||||
value |= (blocks[i] & 127) << (varintLength++ * 7);
|
||||
if (varintLength > 5) {
|
||||
throw new RuntimeException("VarInt too big (probably corrupted data)");
|
||||
}
|
||||
if ((blocks[i] & 128) != 128) {
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
// index = (y * length + z) * width + x
|
||||
int y = index / (width * length);
|
||||
int z = (index % (width * length)) / width;
|
||||
int x = (index % (width * length)) % width;
|
||||
BlockState state = palette.get(value);
|
||||
BlockVector pt = new BlockVector(x, y, z);
|
||||
try {
|
||||
if (state.getBlockType().getMaterial().hasContainer() && tileEntitiesMap.containsKey(pt)) {
|
||||
Map<String, Tag> values = Maps.newHashMap(tileEntitiesMap.get(pt));
|
||||
for (NBTCompatibilityHandler handler : COMPATIBILITY_HANDLERS) {
|
||||
if (handler.isAffectedBlock(state)) {
|
||||
handler.updateNBT(state, values);
|
||||
}
|
||||
if (blocksOut.getSize() != 0) {
|
||||
try (FaweInputStream fis = new FaweInputStream(new LZ4BlockInputStream(new FastByteArraysInputStream(blocksOut.toByteArrays())))) {
|
||||
int volume = width * height * length;
|
||||
if (palette.length < 128) {
|
||||
for (int index = 0; index < volume; index++) {
|
||||
BlockState state = BlockTypes.states[palette[fis.read()]];
|
||||
fc.setBlock(index, state);
|
||||
}
|
||||
values.put("x", new IntTag(pt.getBlockX()));
|
||||
values.put("y", new IntTag(pt.getBlockY()));
|
||||
values.put("z", new IntTag(pt.getBlockZ()));
|
||||
values.put("id", values.get("Id"));
|
||||
values.remove("Id");
|
||||
values.remove("Pos");
|
||||
clipboard.setBlock(clipboard.getMinimumPoint().add(pt), new BaseBlock(state, new CompoundTag(values)));
|
||||
} else {
|
||||
clipboard.setBlock(clipboard.getMinimumPoint().add(pt), state);
|
||||
for (int index = 0; index < volume; index++) {
|
||||
BlockState state = BlockTypes.states[palette[fis.readVarInt()]];
|
||||
fc.setBlock(index, state);
|
||||
}
|
||||
}
|
||||
} catch (WorldEditException e) {
|
||||
throw new IOException("Failed to load a block in the schematic");
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
if (biomesOut.getSize() != 0) {
|
||||
try (FaweInputStream fis = new FaweInputStream(new LZ4BlockInputStream(new FastByteArraysInputStream(biomesOut.toByteArrays())))) {
|
||||
int volume = width * length;
|
||||
for (int index = 0; index < volume; index++) {
|
||||
fc.setBiome(index, fis.read());
|
||||
}
|
||||
}
|
||||
}
|
||||
fc.setDimensions(new Vector(width, height, length));
|
||||
clipboard.init(region, fc);
|
||||
clipboard.setOrigin(origin);
|
||||
return clipboard;
|
||||
}
|
||||
|
||||
|
@ -19,32 +19,36 @@
|
||||
|
||||
package com.sk89q.worldedit.extent.clipboard.io;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import com.sk89q.jnbt.ByteArrayTag;
|
||||
import com.boydti.fawe.object.clipboard.FaweClipboard;
|
||||
import com.boydti.fawe.util.IOUtil;
|
||||
import com.sk89q.jnbt.CompoundTag;
|
||||
import com.sk89q.jnbt.IntArrayTag;
|
||||
import com.sk89q.jnbt.IntTag;
|
||||
import com.sk89q.jnbt.ListTag;
|
||||
import com.sk89q.jnbt.NBTConstants;
|
||||
import com.sk89q.jnbt.NBTOutputStream;
|
||||
import com.sk89q.jnbt.ShortTag;
|
||||
import com.sk89q.jnbt.StringTag;
|
||||
import com.sk89q.jnbt.Tag;
|
||||
import com.sk89q.worldedit.BlockVector;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||
import com.sk89q.worldedit.world.block.BlockState;
|
||||
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
|
||||
import com.sk89q.worldedit.extent.clipboard.Clipboard;
|
||||
import com.sk89q.worldedit.regions.Region;
|
||||
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
||||
import com.sk89q.worldedit.world.block.BlockState;
|
||||
import com.sk89q.worldedit.world.block.BlockTypes;
|
||||
import net.jpountz.lz4.LZ4BlockInputStream;
|
||||
import net.jpountz.lz4.LZ4BlockOutputStream;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutput;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* Writes schematic files using the Sponge schematic format.
|
||||
*/
|
||||
@ -65,26 +69,18 @@ public class SpongeSchematicWriter implements ClipboardWriter {
|
||||
|
||||
@Override
|
||||
public void write(Clipboard clipboard) throws IOException {
|
||||
// For now always write the latest version. Maybe provide support for earlier if more appear.
|
||||
outputStream.writeNamedTag("Schematic", new CompoundTag(write1(clipboard)));
|
||||
write1(clipboard);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a version 1 schematic file.
|
||||
*
|
||||
* @param clipboard The clipboard
|
||||
* @return The schematic map
|
||||
* @throws IOException If an error occurs
|
||||
*/
|
||||
private Map<String, Tag> write1(Clipboard clipboard) throws IOException {
|
||||
public void write1(Clipboard clipboard) throws IOException {
|
||||
// metadata
|
||||
Region region = clipboard.getRegion();
|
||||
Vector origin = clipboard.getOrigin();
|
||||
Vector min = region.getMinimumPoint();
|
||||
BlockVector min = region.getMinimumPoint().toBlockVector();
|
||||
Vector offset = min.subtract(origin);
|
||||
int width = region.getWidth();
|
||||
int height = region.getHeight();
|
||||
int length = region.getLength();
|
||||
|
||||
if (width > MAX_SIZE) {
|
||||
throw new IllegalArgumentException("Width of region too large for a .schematic");
|
||||
}
|
||||
@ -94,95 +90,118 @@ public class SpongeSchematicWriter implements ClipboardWriter {
|
||||
if (length > MAX_SIZE) {
|
||||
throw new IllegalArgumentException("Length of region too large for a .schematic");
|
||||
}
|
||||
// output
|
||||
final DataOutput rawStream = outputStream.getOutputStream();
|
||||
outputStream.writeLazyCompoundTag("Schematic", out -> {
|
||||
out.writeNamedTag("Version", 1);
|
||||
out.writeNamedTag("Width", (short) width);
|
||||
out.writeNamedTag("Height", (short) height);
|
||||
out.writeNamedTag("Length", (short) length);
|
||||
out.writeNamedTag("Offset", new int[]{
|
||||
min.getBlockX(),
|
||||
min.getBlockY(),
|
||||
min.getBlockZ(),
|
||||
});
|
||||
|
||||
Map<String, Tag> schematic = new HashMap<>();
|
||||
schematic.put("Version", new IntTag(1));
|
||||
out.writeLazyCompoundTag("Metadata", out1 -> {
|
||||
out1.writeNamedTag("WEOffsetX", offset.getBlockX());
|
||||
out1.writeNamedTag("WEOffsetY", offset.getBlockY());
|
||||
out1.writeNamedTag("WEOffsetZ", offset.getBlockZ());
|
||||
});
|
||||
|
||||
Map<String, Tag> metadata = new HashMap<>();
|
||||
metadata.put("WEOffsetX", new IntTag(offset.getBlockX()));
|
||||
metadata.put("WEOffsetY", new IntTag(offset.getBlockY()));
|
||||
metadata.put("WEOffsetZ", new IntTag(offset.getBlockZ()));
|
||||
ByteArrayOutputStream blocksCompressed = new ByteArrayOutputStream();
|
||||
DataOutputStream blocksOut = new DataOutputStream(new LZ4BlockOutputStream(blocksCompressed));
|
||||
|
||||
schematic.put("Metadata", new CompoundTag(metadata));
|
||||
ByteArrayOutputStream tilesCompressed = new ByteArrayOutputStream();
|
||||
NBTOutputStream tilesOut = new NBTOutputStream(new LZ4BlockOutputStream(tilesCompressed));
|
||||
int[] numTiles = {0};
|
||||
|
||||
schematic.put("Width", new ShortTag((short) width));
|
||||
schematic.put("Height", new ShortTag((short) height));
|
||||
schematic.put("Length", new ShortTag((short) length));
|
||||
List<Integer> paletteList = new ArrayList<>();
|
||||
char[] palette = new char[BlockTypes.states.length];
|
||||
Arrays.fill(palette, Character.MAX_VALUE);
|
||||
int[] paletteMax = {0};
|
||||
|
||||
// The Sponge format Offset refers to the 'min' points location in the world. That's our 'Origin'
|
||||
schematic.put("Offset", new IntArrayTag(new int[]{
|
||||
min.getBlockX(),
|
||||
min.getBlockY(),
|
||||
min.getBlockZ(),
|
||||
}));
|
||||
|
||||
int paletteMax = 0;
|
||||
Map<String, Integer> palette = new HashMap<>();
|
||||
|
||||
List<CompoundTag> tileEntities = new ArrayList<>();
|
||||
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream(width * height * length);
|
||||
|
||||
for (int y = 0; y < height; y++) {
|
||||
int y0 = min.getBlockY() + y;
|
||||
for (int z = 0; z < length; z++) {
|
||||
int z0 = min.getBlockZ() + z;
|
||||
for (int x = 0; x < width; x++) {
|
||||
int x0 = min.getBlockX() + x;
|
||||
BlockVector point = new BlockVector(x0, y0, z0);
|
||||
BlockStateHolder block = clipboard.getFullBlock(point);
|
||||
if (block.getNbtData() != null) {
|
||||
Map<String, Tag> values = new HashMap<>();
|
||||
for (Map.Entry<String, Tag> entry : block.getNbtData().getValue().entrySet()) {
|
||||
values.put(entry.getKey(), entry.getValue());
|
||||
FaweClipboard.BlockReader reader = new FaweClipboard.BlockReader() {
|
||||
@Override
|
||||
public void run(int x, int y, int z, BlockState block) {
|
||||
try {
|
||||
CompoundTag tile = block.getNbtData();
|
||||
if (tile != null) {
|
||||
Map<String, Tag> values = tile.getValue();
|
||||
values.remove("id"); // Remove 'id' if it exists. We want 'Id'
|
||||
// Positions are kept in NBT, we don't want that.
|
||||
values.remove("x");
|
||||
values.remove("y");
|
||||
values.remove("z");
|
||||
if (!values.containsKey("Id")) values.put("Id", new StringTag(block.getNbtId()));
|
||||
values.put("Pos", new IntArrayTag(new int[]{
|
||||
x,
|
||||
y,
|
||||
z
|
||||
}));
|
||||
numTiles[0]++;
|
||||
tilesOut.writeTagPayload(tile);
|
||||
}
|
||||
|
||||
values.remove("id"); // Remove 'id' if it exists. We want 'Id'
|
||||
|
||||
// Positions are kept in NBT, we don't want that.
|
||||
values.remove("x");
|
||||
values.remove("y");
|
||||
values.remove("z");
|
||||
|
||||
values.put("Id", new StringTag(block.getNbtId()));
|
||||
values.put("Pos", new IntArrayTag(new int[]{
|
||||
x,
|
||||
y,
|
||||
z
|
||||
}));
|
||||
|
||||
tileEntities.add(new CompoundTag(values));
|
||||
int ordinal = block.getOrdinal();
|
||||
char value = palette[ordinal];
|
||||
if (value == Character.MAX_VALUE) {
|
||||
int size = paletteMax[0]++;
|
||||
palette[ordinal] = value = (char) size;
|
||||
paletteList.add(ordinal);
|
||||
}
|
||||
while ((value & -128) != 0) {
|
||||
blocksOut.write(value & 127 | 128);
|
||||
value >>>= 7;
|
||||
}
|
||||
blocksOut.write(value);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
String blockKey = block.toImmutableState().getAsString();
|
||||
int blockId;
|
||||
if (palette.containsKey(blockKey)) {
|
||||
blockId = palette.get(blockKey);
|
||||
} else {
|
||||
blockId = paletteMax;
|
||||
palette.put(blockKey, blockId);
|
||||
paletteMax++;
|
||||
}
|
||||
|
||||
while ((blockId & -128) != 0) {
|
||||
buffer.write(blockId & 127 | 128);
|
||||
blockId >>>= 7;
|
||||
}
|
||||
buffer.write(blockId);
|
||||
}
|
||||
};
|
||||
if (clipboard instanceof BlockArrayClipboard) {
|
||||
((BlockArrayClipboard) clipboard).IMP.forEach(reader, true);
|
||||
} else {
|
||||
for (Vector pt : region) {
|
||||
BlockState block = clipboard.getBlock(pt);
|
||||
int x = pt.getBlockX() - min.getBlockX();
|
||||
int y = pt.getBlockY() - min.getBlockY();
|
||||
int z = pt.getBlockZ() - min.getBlockY();
|
||||
reader.run(x, y, z, block);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
schematic.put("PaletteMax", new IntTag(paletteMax));
|
||||
|
||||
Map<String, Tag> paletteTag = new HashMap<>();
|
||||
palette.forEach((key, value) -> paletteTag.put(key, new IntTag(value)));
|
||||
|
||||
schematic.put("Palette", new CompoundTag(paletteTag));
|
||||
schematic.put("BlockData", new ByteArrayTag(buffer.toByteArray()));
|
||||
schematic.put("TileEntities", new ListTag(CompoundTag.class, tileEntities));
|
||||
|
||||
return schematic;
|
||||
// close
|
||||
tilesOut.close();
|
||||
blocksOut.close();
|
||||
// palette max
|
||||
out.writeNamedTag("PaletteMax", paletteMax[0]);
|
||||
// palette
|
||||
out.writeLazyCompoundTag("Palette", out12 -> {
|
||||
for (int i = 0; i < paletteList.size(); i++) {
|
||||
int stateOrdinal = paletteList.get(i);
|
||||
BlockState state = BlockTypes.states[stateOrdinal];
|
||||
out12.writeNamedTag(state.getAsString(), i);
|
||||
}
|
||||
});
|
||||
// Block data
|
||||
out.writeNamedTagName("BlockData", NBTConstants.TYPE_BYTE_ARRAY);
|
||||
rawStream.writeInt(blocksOut.size());
|
||||
try (LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(blocksCompressed.toByteArray()))) {
|
||||
IOUtil.copy(in, rawStream);
|
||||
}
|
||||
// tiles
|
||||
if (numTiles[0] != 0) {
|
||||
out.writeNamedTagName("TileEntities", NBTConstants.TYPE_LIST);
|
||||
rawStream.write(NBTConstants.TYPE_COMPOUND);
|
||||
rawStream.writeInt(numTiles[0]);
|
||||
try (LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(tilesCompressed.toByteArray()))) {
|
||||
IOUtil.copy(in, rawStream);
|
||||
}
|
||||
} else {
|
||||
out.writeNamedEmptyList("TileEntities");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user