Fix plotsquared schematic hook

This commit is contained in:
Jesse Boyd 2019-04-01 21:30:21 +11:00
parent 49e3102779
commit 6e6a3f9035
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
6 changed files with 136 additions and 31 deletions

View File

@ -1,10 +1,14 @@
package com.boydti.fawe.regions.general.plot; package com.boydti.fawe.regions.general.plot;
import com.boydti.fawe.FaweCache; import com.boydti.fawe.FaweCache;
import com.boydti.fawe.object.FaweOutputStream;
import com.boydti.fawe.object.FaweQueue; import com.boydti.fawe.object.FaweQueue;
import com.boydti.fawe.object.clipboard.ReadOnlyClipboard; import com.boydti.fawe.object.clipboard.ReadOnlyClipboard;
import com.boydti.fawe.object.io.FastByteArrayOutputStream;
import com.boydti.fawe.object.io.FastByteArraysInputStream;
import com.boydti.fawe.object.io.PGZIPOutputStream; import com.boydti.fawe.object.io.PGZIPOutputStream;
import com.boydti.fawe.util.EditSessionBuilder; import com.boydti.fawe.util.EditSessionBuilder;
import com.boydti.fawe.util.IOUtil;
import com.boydti.fawe.util.SetQueue; import com.boydti.fawe.util.SetQueue;
import com.boydti.fawe.util.TaskManager; import com.boydti.fawe.util.TaskManager;
import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.PlotSquared;
@ -15,12 +19,23 @@ import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.SchematicHandler; import com.github.intellectualsites.plotsquared.plot.util.SchematicHandler;
import com.github.intellectualsites.plotsquared.plot.util.block.LocalBlockQueue; import com.github.intellectualsites.plotsquared.plot.util.block.LocalBlockQueue;
import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.CompressedCompoundTag;
import com.sk89q.jnbt.CompressedSchematicTag;
import com.sk89q.jnbt.NBTOutputStream; import com.sk89q.jnbt.NBTOutputStream;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard; import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.SpongeSchematicWriter;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
import net.jpountz.lz4.LZ4BlockInputStream;
import net.jpountz.lz4.LZ4BlockOutputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -30,6 +45,7 @@ import java.net.URL;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.zip.GZIPInputStream;
public class FaweSchematicHandler extends SchematicHandler { public class FaweSchematicHandler extends SchematicHandler {
@Override @Override
@ -62,10 +78,8 @@ public class FaweSchematicHandler extends SchematicHandler {
ReadOnlyClipboard clipboard = ReadOnlyClipboard.of(editSession, region); ReadOnlyClipboard clipboard = ReadOnlyClipboard.of(editSession, region);
Clipboard holder = new BlockArrayClipboard(region, clipboard); Clipboard holder = new BlockArrayClipboard(region, clipboard);
// TODO FIXME CompressedSchematicTag tag = new CompressedSchematicTag(holder);
// com.sk89q.jnbt.CompoundTag weTag = SchematicWriter.writeTag(holder); whenDone.run(tag);
// CompoundTag tag = new CompoundTag((Map<String, Tag>) (Map<?, ?>) weTag.getValue());
// whenDone.run(tag);
} }
}); });
} }
@ -77,12 +91,29 @@ public class FaweSchematicHandler extends SchematicHandler {
return false; return false;
} }
try { try {
PlotSquared.debug("Saving " + path);
File tmp = MainUtil.getFile(PlotSquared.get().IMP.getDirectory(), path); File tmp = MainUtil.getFile(PlotSquared.get().IMP.getDirectory(), path);
PlotSquared.debug(tmp);
tmp.getParentFile().mkdirs(); tmp.getParentFile().mkdirs();
com.sk89q.jnbt.CompoundTag weTag = (com.sk89q.jnbt.CompoundTag) FaweCache.asTag(tag); if (tag instanceof CompressedCompoundTag) {
try (OutputStream stream = new FileOutputStream(tmp); NBTOutputStream output = new NBTOutputStream(new PGZIPOutputStream(stream))) { CompressedCompoundTag cTag = (CompressedCompoundTag) tag;
Map<String, com.sk89q.jnbt.Tag> map = weTag.getValue(); if (cTag instanceof CompressedSchematicTag) {
output.writeNamedTag("Schematic", map.containsKey("Schematic") ? map.get("Schematic") : weTag); System.out.println("Write directly");
Clipboard clipboard = (Clipboard) cTag.getSource();
try (OutputStream stream = new FileOutputStream(tmp); NBTOutputStream output = new NBTOutputStream(new BufferedOutputStream(new PGZIPOutputStream(stream)))) {
new SpongeSchematicWriter(output).write(clipboard);
}
} else {
try (OutputStream stream = new FileOutputStream(tmp); BufferedOutputStream output = new BufferedOutputStream(new PGZIPOutputStream(stream))) {
DataInputStream is = cTag.adapt(cTag.getSource());
IOUtil.copy(is, stream);
}
}
} else {
try (OutputStream stream = new FileOutputStream(tmp); NBTOutputStream output = new NBTOutputStream(new PGZIPOutputStream(stream))) {
Map<String, com.sk89q.jnbt.Tag> map = tag.getValue();
output.writeNamedTag("Schematic", map.containsKey("Schematic") ? map.get("Schematic") : tag);
}
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -27,7 +27,7 @@ import java.util.Map;
/** /**
* The {@code TAG_Compound} tag. * The {@code TAG_Compound} tag.
*/ */
public final class CompoundTag extends Tag { public class CompoundTag extends Tag {
private final Map<String, Tag> value; private final Map<String, Tag> value;
@ -57,7 +57,7 @@ public final class CompoundTag extends Tag {
* @return true if the tag contains the given key * @return true if the tag contains the given key
*/ */
public boolean containsKey(String key) { public boolean containsKey(String key) {
return value.containsKey(key); return getValue().containsKey(key);
} }
@Override @Override
@ -94,7 +94,7 @@ public final class CompoundTag extends Tag {
* @return a byte array * @return a byte array
*/ */
public byte[] getByteArray(String key) { public byte[] getByteArray(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof ByteArrayTag) { if (tag instanceof ByteArrayTag) {
return ((ByteArrayTag) tag).getValue(); return ((ByteArrayTag) tag).getValue();
} else { } else {
@ -112,7 +112,7 @@ public final class CompoundTag extends Tag {
* @return a byte * @return a byte
*/ */
public byte getByte(String key) { public byte getByte(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof ByteTag) { if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue(); return ((ByteTag) tag).getValue();
} else { } else {
@ -130,7 +130,7 @@ public final class CompoundTag extends Tag {
* @return a double * @return a double
*/ */
public double getDouble(String key) { public double getDouble(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof DoubleTag) { if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue(); return ((DoubleTag) tag).getValue();
} else { } else {
@ -149,7 +149,7 @@ public final class CompoundTag extends Tag {
* @return a double * @return a double
*/ */
public double asDouble(String key) { public double asDouble(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof ByteTag) { if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue(); return ((ByteTag) tag).getValue();
@ -183,7 +183,7 @@ public final class CompoundTag extends Tag {
* @return a float * @return a float
*/ */
public float getFloat(String key) { public float getFloat(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof FloatTag) { if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue(); return ((FloatTag) tag).getValue();
} else { } else {
@ -201,7 +201,7 @@ public final class CompoundTag extends Tag {
* @return an int array * @return an int array
*/ */
public int[] getIntArray(String key) { public int[] getIntArray(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof IntArrayTag) { if (tag instanceof IntArrayTag) {
return ((IntArrayTag) tag).getValue(); return ((IntArrayTag) tag).getValue();
} else { } else {
@ -219,7 +219,7 @@ public final class CompoundTag extends Tag {
* @return an int * @return an int
*/ */
public int getInt(String key) { public int getInt(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof IntTag) { if (tag instanceof IntTag) {
return ((IntTag) tag).getValue(); return ((IntTag) tag).getValue();
} else { } else {
@ -238,7 +238,7 @@ public final class CompoundTag extends Tag {
* @return an int * @return an int
*/ */
public int asInt(String key) { public int asInt(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof ByteTag) { if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue(); return ((ByteTag) tag).getValue();
@ -272,7 +272,7 @@ public final class CompoundTag extends Tag {
* @return a list of tags * @return a list of tags
*/ */
public List<Tag> getList(String key) { public List<Tag> getList(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof ListTag) { if (tag instanceof ListTag) {
return ((ListTag) tag).getValue(); return ((ListTag) tag).getValue();
} else { } else {
@ -290,7 +290,7 @@ public final class CompoundTag extends Tag {
* @return a tag list instance * @return a tag list instance
*/ */
public ListTag getListTag(String key) { public ListTag getListTag(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof ListTag) { if (tag instanceof ListTag) {
return (ListTag) tag; return (ListTag) tag;
} else { } else {
@ -313,7 +313,7 @@ public final class CompoundTag extends Tag {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends Tag> List<T> getList(String key, Class<T> listType) { public <T extends Tag> List<T> getList(String key, Class<T> listType) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof ListTag) { if (tag instanceof ListTag) {
ListTag listTag = (ListTag) tag; ListTag listTag = (ListTag) tag;
if (listTag.getType().equals(listType)) { if (listTag.getType().equals(listType)) {
@ -336,7 +336,7 @@ public final class CompoundTag extends Tag {
* @return an int array * @return an int array
*/ */
public long[] getLongArray(String key) { public long[] getLongArray(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof LongArrayTag) { if (tag instanceof LongArrayTag) {
return ((LongArrayTag) tag).getValue(); return ((LongArrayTag) tag).getValue();
} else { } else {
@ -354,7 +354,7 @@ public final class CompoundTag extends Tag {
* @return a long * @return a long
*/ */
public long getLong(String key) { public long getLong(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof LongTag) { if (tag instanceof LongTag) {
return ((LongTag) tag).getValue(); return ((LongTag) tag).getValue();
} else { } else {
@ -373,7 +373,7 @@ public final class CompoundTag extends Tag {
* @return a long * @return a long
*/ */
public long asLong(String key) { public long asLong(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof ByteTag) { if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue(); return ((ByteTag) tag).getValue();
@ -407,7 +407,7 @@ public final class CompoundTag extends Tag {
* @return a short * @return a short
*/ */
public short getShort(String key) { public short getShort(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof ShortTag) { if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue(); return ((ShortTag) tag).getValue();
} else { } else {
@ -425,7 +425,7 @@ public final class CompoundTag extends Tag {
* @return a string * @return a string
*/ */
public String getString(String key) { public String getString(String key) {
Tag tag = value.get(key); Tag tag = getValue().get(key);
if (tag instanceof StringTag) { if (tag instanceof StringTag) {
return ((StringTag) tag).getValue(); return ((StringTag) tag).getValue();
} else { } else {
@ -436,8 +436,8 @@ public final class CompoundTag extends Tag {
@Override @Override
public String toString() { public String toString() {
StringBuilder bldr = new StringBuilder(); StringBuilder bldr = new StringBuilder();
bldr.append("TAG_Compound").append(": ").append(value.size()).append(" entries\r\n{\r\n"); bldr.append("TAG_Compound").append(": ").append(getValue().size()).append(" entries\r\n{\r\n");
for (Map.Entry<String, Tag> entry : value.entrySet()) { for (Map.Entry<String, Tag> entry : getValue().entrySet()) {
bldr.append(" ").append(entry.getValue().toString().replaceAll("\r\n", "\r\n ")).append("\r\n"); bldr.append(" ").append(entry.getValue().toString().replaceAll("\r\n", "\r\n ")).append("\r\n");
} }
bldr.append("}"); bldr.append("}");

View File

@ -0,0 +1,42 @@
package com.sk89q.jnbt;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public abstract class CompressedCompoundTag<T> extends CompoundTag {
private T in;
public CompressedCompoundTag(T in) {
super(new HashMap<>());
this.in = in;
}
@Override
public Map<String, Tag> getValue() {
if (in != null) decompress();
return super.getValue();
}
public abstract DataInputStream adapt(T src) throws IOException;
public T getSource() {
return in;
}
private void decompress() {
try (NBTInputStream nbtIn = new NBTInputStream(adapt(in))) {
in = null;
CompoundTag tag = (CompoundTag) nbtIn.readTag();
Map<String, Tag> value = tag.getValue();
Map<String, Tag> raw = super.getValue();
for (Map.Entry<String, Tag> entry : value.entrySet()) {
raw.put(entry.getKey(), entry.getValue());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,33 @@
package com.sk89q.jnbt;
import com.boydti.fawe.object.io.FastByteArrayOutputStream;
import com.boydti.fawe.object.io.FastByteArraysInputStream;
import com.boydti.fawe.object.io.PGZIPOutputStream;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.SpongeSchematicWriter;
import net.jpountz.lz4.LZ4BlockInputStream;
import net.jpountz.lz4.LZ4BlockOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
public class CompressedSchematicTag extends CompressedCompoundTag<Clipboard> {
public CompressedSchematicTag(Clipboard holder) {
super(holder);
}
@Override
public DataInputStream adapt(Clipboard src) throws IOException {
System.out.println("Decompress");
FastByteArrayOutputStream blocksOut = new FastByteArrayOutputStream();
try (LZ4BlockOutputStream lz4out = new LZ4BlockOutputStream(blocksOut)) {
NBTOutputStream nbtOut = new NBTOutputStream(lz4out);
new SpongeSchematicWriter(nbtOut).write(getSource());
} catch (IOException e) {
throw new RuntimeException(e);
}
FastByteArraysInputStream in = new FastByteArraysInputStream(blocksOut.toByteArrays());
return new DataInputStream(new LZ4BlockInputStream(in));
}
}

View File

@ -216,12 +216,12 @@ public class SpongeSchematicReader extends NBTSchematicReader {
}); });
streamer.readFully(); streamer.readFully();
if (fc == null) setupClipboard(length * width * height, uuid); if (fc == null) setupClipboard(length * width * height, uuid);
else fc.setDimensions(BlockVector3.at(width, height, length)); fc.setDimensions(BlockVector3.at(width, height, length));
BlockVector3 origin = min; BlockVector3 origin = min;
CuboidRegion region; CuboidRegion region;
if (offsetX != Integer.MIN_VALUE && offsetY != Integer.MIN_VALUE && offsetZ != Integer.MIN_VALUE) { if (offsetX != Integer.MIN_VALUE && offsetY != Integer.MIN_VALUE && offsetZ != Integer.MIN_VALUE) {
origin = origin.subtract(BlockVector3.at(offsetX, offsetY, offsetZ)); origin = origin.subtract(BlockVector3.at(offsetX, offsetY, offsetZ));
region = new CuboidRegion(min, min.add(width, height, length).subtract(BlockVector3.ONE)); region = new CuboidRegion(origin, origin.add(width, height, length).subtract(BlockVector3.ONE));
} else { } else {
region = new CuboidRegion(min, min.add(width, height, length).subtract(BlockVector3.ONE)); region = new CuboidRegion(min, min.add(width, height, length).subtract(BlockVector3.ONE));
} }

View File

@ -91,7 +91,6 @@ public class SpongeSchematicWriter implements ClipboardWriter {
if (length > MAX_SIZE) { if (length > MAX_SIZE) {
throw new IllegalArgumentException("Length of region too large for a .schematic"); throw new IllegalArgumentException("Length of region too large for a .schematic");
} }
//<<<<<<< HEAD
// output // output
final DataOutput rawStream = outputStream.getOutputStream(); final DataOutput rawStream = outputStream.getOutputStream();
outputStream.writeLazyCompoundTag("Schematic", out -> { outputStream.writeLazyCompoundTag("Schematic", out -> {