3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-11-19 17:30:08 +01:00

fix: make FastSchematicWriterV3 work

Dieser Commit ist enthalten in:
Pierre Maurice Schwang 2024-06-10 21:03:42 +02:00
Ursprung ff580218b9
Commit c35cd6e816
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 37E613079F3E5BB9
3 geänderte Dateien mit 71 neuen und 31 gelöschten Zeilen

Datei anzeigen

@ -29,7 +29,7 @@ import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat; import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.MCEditSchematicReader; import com.sk89q.worldedit.extent.clipboard.io.MCEditSchematicReader;
import com.sk89q.worldedit.extent.clipboard.io.SpongeSchematicReader; import com.sk89q.worldedit.extent.clipboard.io.sponge.SpongeSchematicV3Reader;
import com.sk89q.worldedit.internal.util.LogManagerCompat; import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
@ -249,8 +249,8 @@ public class FaweDelegateSchematicHandler {
return null; return null;
} }
try { try {
SpongeSchematicReader schematicReader = SpongeSchematicV3Reader schematicReader =
new SpongeSchematicReader(new NBTInputStream(new GZIPInputStream(is))); new SpongeSchematicV3Reader(new NBTInputStream(new GZIPInputStream(is)));
Clipboard clip = schematicReader.read(); Clipboard clip = schematicReader.read();
return new Schematic(clip); return new Schematic(clip);
} catch (IOException e2) { } catch (IOException e2) {

Datei anzeigen

@ -25,6 +25,7 @@ import net.jpountz.lz4.LZ4BlockOutputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
@ -89,9 +90,9 @@ public class FastSchematicWriterV3 implements ClipboardWriter {
); );
schematic.writeLazyCompoundTag("Metadata", out -> this.writeMetadata(out, clipboard)); schematic.writeLazyCompoundTag("Metadata", out -> this.writeMetadata(out, clipboard));
schematic.writeNamedTag("Width", region.getWidth()); schematic.writeNamedTag("Width", (short) region.getWidth());
schematic.writeNamedTag("Height", region.getHeight()); schematic.writeNamedTag("Height", (short) region.getHeight());
schematic.writeNamedTag("Length", region.getLength()); schematic.writeNamedTag("Length", (short) region.getLength());
schematic.writeNamedTag("Offset", new int[]{ schematic.writeNamedTag("Offset", new int[]{
offset.x(), offset.y(), offset.z() offset.x(), offset.y(), offset.z()
@ -115,8 +116,9 @@ public class FastSchematicWriterV3 implements ClipboardWriter {
private void writeBlocks(NBTOutputStream blocks, Clipboard clipboard) throws IOException { private void writeBlocks(NBTOutputStream blocks, Clipboard clipboard) throws IOException {
final int[] tiles = new int[]{0}; final int[] tiles = new int[]{0};
try (ByteArrayOutputStream tileBytes = new ByteArrayOutputStream(); final ByteArrayOutputStream tileBytes = new ByteArrayOutputStream();
NBTOutputStream tileOut = new NBTOutputStream(new LZ4BlockOutputStream(tileBytes))) { try (LZ4BlockOutputStream lz4Stream = new LZ4BlockOutputStream(tileBytes);
NBTOutputStream tileOut = new NBTOutputStream(lz4Stream)) {
this.writePalette( this.writePalette(
blocks, blocks,
BlockTypesCache.states.length, BlockTypesCache.states.length,
@ -149,7 +151,8 @@ public class FastSchematicWriterV3 implements ClipboardWriter {
BlockStateHolder::getAsString, BlockStateHolder::getAsString,
clipboard clipboard
); );
lz4Stream.finish();
} finally {
// Write Tiles // Write Tiles
if (tiles[0] > 0) { if (tiles[0] > 0) {
blocks.writeNamedTagName("BlockEntities", NBTConstants.TYPE_LIST); blocks.writeNamedTagName("BlockEntities", NBTConstants.TYPE_LIST);
@ -173,27 +176,27 @@ public class FastSchematicWriterV3 implements ClipboardWriter {
); );
} }
private void writeEntity(NBTOutputStream blocks, Clipboard clipboard, Entity entity) throws IOException { private void writeEntity(NBTOutputStream out, Clipboard clipboard, Entity entity) throws IOException {
final BaseEntity state = entity.getState(); final BaseEntity state = entity.getState();
if (state == null) { if (state == null) {
return; throw new IOException("Entity has no state");
} }
blocks.writeNamedTag("Id", state.getType().id()); out.writeNamedTag("Id", state.getType().id());
blocks.writeNamedTagName("Pos", NBTConstants.TYPE_LIST); out.writeNamedTagName("Pos", NBTConstants.TYPE_LIST);
blocks.write(NBTConstants.TYPE_FLOAT); out.write(NBTConstants.TYPE_FLOAT);
blocks.write(3); out.writeInt(3);
blocks.writeFloat((float) entity.getLocation().x() - clipboard.getMinimumPoint().x()); out.writeFloat((float) entity.getLocation().x() - clipboard.getMinimumPoint().x());
blocks.writeFloat((float) entity.getLocation().y() - clipboard.getMinimumPoint().y()); out.writeFloat((float) entity.getLocation().y() - clipboard.getMinimumPoint().y());
blocks.writeFloat((float) entity.getLocation().z() - clipboard.getMinimumPoint().z()); out.writeFloat((float) entity.getLocation().z() - clipboard.getMinimumPoint().z());
blocks.writeLazyCompoundTag("Data", data -> { out.writeLazyCompoundTag("Data", data -> {
CompoundTag nbt = state.getNbtData(); CompoundTag nbt = state.getNbtData();
if (nbt != null) { if (nbt != null) {
final Map<String, Tag> value = nbt.getValue(); nbt.getValue().forEach((s, tag) -> {
value.remove("id"); if (s.equals("id") || s.equals("Rotation")) {
value.remove("Rotation"); return;
value.forEach((s, tag) -> { }
try { try {
data.writeNamedTag(s, tag); data.writeNamedTag(s, tag);
} catch (IOException e) { } catch (IOException e) {
@ -205,10 +208,12 @@ public class FastSchematicWriterV3 implements ClipboardWriter {
// Write rotation list // Write rotation list
data.writeNamedTagName("Rotation", NBTConstants.TYPE_LIST); data.writeNamedTagName("Rotation", NBTConstants.TYPE_LIST);
data.write(NBTConstants.TYPE_FLOAT); data.write(NBTConstants.TYPE_FLOAT);
data.write(2); data.writeInt(2);
data.writeFloat(entity.getLocation().getYaw()); data.writeFloat(entity.getLocation().getYaw());
data.writeFloat(entity.getLocation().getPitch()); data.writeFloat(entity.getLocation().getPitch());
}); });
out.write(NBTConstants.TYPE_END); // End the compound
} }
private <T> void writePalette( private <T> void writePalette(
@ -219,8 +224,8 @@ public class FastSchematicWriterV3 implements ClipboardWriter {
Clipboard clipboard Clipboard clipboard
) throws IOException { ) throws IOException {
int dataBytesUsed = 0; int dataBytesUsed = 0;
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ByteArrayOutputStream bytes = new ByteArrayOutputStream();
LZ4BlockOutputStream dataOut = new LZ4BlockOutputStream(bytes)) { try (LZ4BlockOutputStream dataOut = new LZ4BlockOutputStream(bytes)) {
int index = 0; int index = 0;
char[] palette = new char[capacity]; char[] palette = new char[capacity];
Arrays.fill(palette, Character.MAX_VALUE); Arrays.fill(palette, Character.MAX_VALUE);
@ -244,15 +249,20 @@ public class FastSchematicWriterV3 implements ClipboardWriter {
dataOut.write(value & 127 | 128); dataOut.write(value & 127 | 128);
value >>>= 7; value >>>= 7;
} }
dataOut.write(value);
dataBytesUsed++;
} }
// End Palette tag // End Palette tag
out.write(NBTConstants.TYPE_END); out.write(NBTConstants.TYPE_END);
dataOut.finish();
} finally {
// Write Data tag // Write Data tag
try (LZ4BlockInputStream reader = new LZ4BlockInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { if (dataBytesUsed > 0) {
out.writeNamedTagName("Data", NBTConstants.TYPE_BYTE_ARRAY); try (LZ4BlockInputStream reader = new LZ4BlockInputStream(new ByteArrayInputStream(bytes.toByteArray()))) {
out.writeInt(dataBytesUsed); out.writeNamedTagName("Data", NBTConstants.TYPE_BYTE_ARRAY);
IOUtil.copy(reader, dataOut); out.writeInt(dataBytesUsed);
IOUtil.copy(reader, (DataOutput) out);
}
} }
} }
} }

Datei anzeigen

@ -21,6 +21,7 @@ package com.sk89q.worldedit.extent.clipboard.io;
import com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicReaderV2; import com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicReaderV2;
import com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicWriterV2; import com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicWriterV2;
import com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicWriterV3;
import com.fastasyncworldedit.core.extent.clipboard.io.schematic.MinecraftStructure; import com.fastasyncworldedit.core.extent.clipboard.io.schematic.MinecraftStructure;
import com.fastasyncworldedit.core.extent.clipboard.io.schematic.PNGWriter; import com.fastasyncworldedit.core.extent.clipboard.io.schematic.PNGWriter;
import com.fastasyncworldedit.core.internal.io.ResettableFileInputStream; import com.fastasyncworldedit.core.internal.io.ResettableFileInputStream;
@ -57,6 +58,35 @@ import java.util.zip.GZIPOutputStream;
public enum BuiltInClipboardFormat implements ClipboardFormat { public enum BuiltInClipboardFormat implements ClipboardFormat {
//FAWE start - register fast clipboard io //FAWE start - register fast clipboard io
FAST_NEW("new_fast") { // For testing purposes
@Override
public ClipboardReader getReader(final InputStream inputStream) throws IOException {
return SPONGE_V3_SCHEMATIC.getReader(inputStream);
}
@Override
public ClipboardWriter getWriter(OutputStream outputStream) throws IOException {
OutputStream gzip;
if (outputStream instanceof ParallelGZIPOutputStream || outputStream instanceof GZIPOutputStream) {
gzip = outputStream;
} else {
outputStream = new BufferedOutputStream(outputStream);
gzip = new ParallelGZIPOutputStream(outputStream);
}
NBTOutputStream nbtStream = new NBTOutputStream(new BufferedOutputStream(gzip));
return new FastSchematicWriterV3(nbtStream);
}
@Override
public boolean isFormat(final File file) {
return FAST.isFormat(file);
}
@Override
public String getPrimaryFileExtension() {
return FAST.getPrimaryFileExtension();
}
},
FAST("fast", "fawe", "sponge", "schem") { FAST("fast", "fawe", "sponge", "schem") {
@Override @Override
public String getPrimaryFileExtension() { public String getPrimaryFileExtension() {