3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-12-24 18:10:08 +01:00
Use sponge schematic format instead of structure block
Fix VS undo running on main thread
Fix missing sections when setting blocks
Dieser Commit ist enthalten in:
Jesse Boyd 2018-09-18 12:49:33 +10:00
Ursprung 83464013ba
Commit 5b5336cc83
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 59F1DE6293AF6E1F
19 geänderte Dateien mit 431 neuen und 322 gelöschten Zeilen

Datei anzeigen

@ -512,26 +512,21 @@ public class Sniper {
public void undo(int amount) { public void undo(int amount) {
FawePlayer<Object> fp = FawePlayer.wrap(getPlayer()); FawePlayer<Object> fp = FawePlayer.wrap(getPlayer());
if (!fp.runAction(new Runnable() { int count = 0;
@Override for (int i = 0; i < amount; i++) {
public void run() { EditSession es = fp.getSession().undo(null, fp.getPlayer());
int count = 0; if (es == null) {
for (int i = 0; i < amount; i++) { break;
EditSession es = fp.getSession().undo(null, fp.getPlayer()); } else {
if (es == null) { es.flushQueue();
break;
} else {
es.flushQueue();
}
count++;
}
if (count > 0) {
BBC.COMMAND_UNDO_SUCCESS.send(fp);
} else {
BBC.COMMAND_UNDO_ERROR.send(fp);
}
} }
}, true, false)); count++;
}
if (count > 0) {
BBC.COMMAND_UNDO_SUCCESS.send(fp);
} else {
BBC.COMMAND_UNDO_ERROR.send(fp);
}
} }
public void reset(String toolId) { public void reset(String toolId) {

Datei anzeigen

@ -1,5 +1,6 @@
package com.thevoxelbox.voxelsniper; package com.thevoxelbox.voxelsniper;
import com.boydti.fawe.config.BBC;
import com.boydti.fawe.object.FawePlayer; import com.boydti.fawe.object.FawePlayer;
import com.sk89q.minecraft.util.commands.CommandException; import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.worldedit.extension.platform.CommandManager; import com.sk89q.worldedit.extension.platform.CommandManager;
@ -78,30 +79,38 @@ public class VoxelSniperListener implements Listener
} }
FawePlayer fp = FawePlayer.wrap(player); FawePlayer fp = FawePlayer.wrap(player);
ExceptionConverter exceptionConverter = CommandManager.getInstance().getExceptionConverter(); if (!fp.runAction(new Runnable() {
try { @Override
try { public void run() {
return found.onCommand(player, split); ExceptionConverter exceptionConverter = CommandManager.getInstance().getExceptionConverter();
} catch (Throwable t) { try {
Throwable next = t; try {
exceptionConverter.convert(next); found.onCommand(player, split);
while (next.getCause() != null) { return;
next = next.getCause(); } catch (Throwable t) {
exceptionConverter.convert(next); Throwable next = t;
exceptionConverter.convert(next);
while (next.getCause() != null) {
next = next.getCause();
exceptionConverter.convert(next);
}
throw next;
}
} catch (CommandException e) {
String message = e.getMessage();
if (message != null) {
fp.sendMessage(e.getMessage());
return;
}
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
} }
throw next; fp.sendMessage("An unknown FAWE error has occurred! Please see console.");
} }
} catch (CommandException e) { }, false, true)) {
String message = e.getMessage(); BBC.WORLDEDIT_COMMAND_LIMIT.send(fp);
if (message != null) {
fp.sendMessage(e.getMessage());
return true;
}
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
} }
fp.sendMessage("An unknown FAWE error has occurred! Please see console.");
return true; return true;
} }

Datei anzeigen

@ -273,7 +273,7 @@ public final class Spigot_v1_13_R2 extends CachedBukkitAdapter implements Bukkit
if (existing == blockData) return true; if (existing == blockData) return true;
if (section == null) { if (section == null) {
if (blockData.isAir()) return true; if (blockData.isAir()) return true;
sections[y4] = new ChunkSection(y4 << 4, nmsWorld.worldProvider.g()); sections[y4] = section = new ChunkSection(y4 << 4, nmsWorld.worldProvider.g());
} }
if (existing.e() != blockData.e() || existing.getMaterial().f() != blockData.getMaterial().f()) { if (existing.e() != blockData.e() || existing.getMaterial().f() != blockData.getMaterial().f()) {
nmsChunk.a(pos = new BlockPosition(x, y, z), blockData, false); nmsChunk.a(pos = new BlockPosition(x, y, z), blockData, false);

Datei anzeigen

@ -299,47 +299,48 @@ public class AsyncBlock implements Block {
return null; return null;
} }
public Block getBukkitBlock() { @Deprecated
private Block getUnsafeBlock() {
return world.getBukkitWorld().getBlockAt(x, y, z); return world.getBukkitWorld().getBlockAt(x, y, z);
} }
@Override @Override
public boolean breakNaturally() { public boolean breakNaturally() {
return TaskManager.IMP.sync(() -> getBukkitBlock().breakNaturally()); return TaskManager.IMP.sync(() -> getUnsafeBlock().breakNaturally());
} }
@Override @Override
public boolean breakNaturally(ItemStack tool) { public boolean breakNaturally(ItemStack tool) {
return TaskManager.IMP.sync(() -> getBukkitBlock().breakNaturally(tool)); return TaskManager.IMP.sync(() -> getUnsafeBlock().breakNaturally(tool));
} }
@Override @Override
public Collection<ItemStack> getDrops() { public Collection<ItemStack> getDrops() {
return TaskManager.IMP.sync(() -> getBukkitBlock().getDrops()); return TaskManager.IMP.sync(() -> getUnsafeBlock().getDrops());
} }
@Override @Override
public Collection<ItemStack> getDrops(ItemStack tool) { public Collection<ItemStack> getDrops(ItemStack tool) {
return TaskManager.IMP.sync(() -> getBukkitBlock().getDrops(tool)); return TaskManager.IMP.sync(() -> getUnsafeBlock().getDrops(tool));
} }
@Override @Override
public void setMetadata(String metadataKey, MetadataValue newMetadataValue) { public void setMetadata(String metadataKey, MetadataValue newMetadataValue) {
this.getBukkitBlock().setMetadata(metadataKey, newMetadataValue); this.getUnsafeBlock().setMetadata(metadataKey, newMetadataValue);
} }
@Override @Override
public List<MetadataValue> getMetadata(String metadataKey) { public List<MetadataValue> getMetadata(String metadataKey) {
return this.getBukkitBlock().getMetadata(metadataKey); return this.getUnsafeBlock().getMetadata(metadataKey);
} }
@Override @Override
public boolean hasMetadata(String metadataKey) { public boolean hasMetadata(String metadataKey) {
return this.getBukkitBlock().hasMetadata(metadataKey); return this.getUnsafeBlock().hasMetadata(metadataKey);
} }
@Override @Override
public void removeMetadata(String metadataKey, Plugin owningPlugin) { public void removeMetadata(String metadataKey, Plugin owningPlugin) {
this.getBukkitBlock().removeMetadata(metadataKey, owningPlugin); this.getUnsafeBlock().removeMetadata(metadataKey, owningPlugin);
} }
} }

Datei anzeigen

@ -4,6 +4,9 @@ import com.boydti.fawe.config.BBC;
import com.boydti.fawe.object.RunnableVal2; import com.boydti.fawe.object.RunnableVal2;
import com.boydti.fawe.object.exception.FaweException; import com.boydti.fawe.object.exception.FaweException;
import com.sk89q.jnbt.NBTInputStream; import com.sk89q.jnbt.NBTInputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -87,6 +90,8 @@ public class NBTStreamer {
public abstract void run(int index, int byteValue); public abstract void run(int index, int byteValue);
} }
public static abstract class LazyReader implements BiConsumer<Integer, DataInputStream> {}
public static abstract class LongReader implements BiConsumer<Integer, Long> { public static abstract class LongReader implements BiConsumer<Integer, Long> {
@Override @Override
public void accept(Integer index, Long value) { public void accept(Integer index, Long value) {

Datei anzeigen

@ -103,11 +103,9 @@ public abstract class FaweClipboard {
return tiles; return tiles;
} }
public void close() { public void close() {}
}
public void flush() { public void flush() {}
}
/** /**
* Stores entity data. * Stores entity data.

Datei anzeigen

@ -40,13 +40,7 @@ public class LazyClipboardHolder extends URIClipboardHolder {
try (InputStream in = source.openBufferedStream()) { try (InputStream in = source.openBufferedStream()) {
final ClipboardReader reader = format.getReader(in); final ClipboardReader reader = format.getReader(in);
final Clipboard clipboard; final Clipboard clipboard;
if (reader instanceof SchematicReader) { this.clipboard = reader.read(uuid);
this.clipboard = ((SchematicReader) reader).read(uuid);
} else if (reader instanceof StructureFormat) {
this.clipboard = ((StructureFormat) reader).read(uuid);
} else {
this.clipboard = reader.read();
}
} }
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();

Datei anzeigen

@ -23,11 +23,11 @@ public abstract class ReadOnlyClipboard extends FaweClipboard {
this.region = region; this.region = region;
} }
public static ReadOnlyClipboard of(final EditSession editSession, final Region region) { public static ReadOnlyClipboard of(final Extent editSession, final Region region) {
return of(editSession, region, true, false); return of(editSession, region, true, false);
} }
public static ReadOnlyClipboard of(final EditSession editSession, final Region region, boolean copyEntities, boolean copyBiomes) { public static ReadOnlyClipboard of(final Extent editSession, final Region region, boolean copyEntities, boolean copyBiomes) {
return new WorldCopyClipboard(editSession, region, copyEntities, copyBiomes); return new WorldCopyClipboard(editSession, region, copyEntities, copyBiomes);
} }

Datei anzeigen

@ -8,7 +8,7 @@ import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MutableBlockVector2D; import com.sk89q.worldedit.MutableBlockVector2D;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.function.RegionFunction; import com.sk89q.worldedit.function.RegionFunction;
@ -17,7 +17,6 @@ import com.sk89q.worldedit.function.visitor.RegionVisitor;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.world.biome.BaseBiome; import com.sk89q.worldedit.world.biome.BaseBiome;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -29,13 +28,13 @@ public class WorldCopyClipboard extends ReadOnlyClipboard {
private final boolean hasBiomes; private final boolean hasBiomes;
private final boolean hasEntities; private final boolean hasEntities;
private MutableBlockVector2D mutableBlockVector2D = new MutableBlockVector2D(); private MutableBlockVector2D mutableBlockVector2D = new MutableBlockVector2D();
public final EditSession editSession; public final Extent extent;
public WorldCopyClipboard(EditSession editSession, Region region) { public WorldCopyClipboard(Extent editSession, Region region) {
this(editSession, region, true, false); this(editSession, region, true, false);
} }
public WorldCopyClipboard(EditSession editSession, Region region, boolean hasEntities, boolean hasBiomes) { public WorldCopyClipboard(Extent editSession, Region region, boolean hasEntities, boolean hasBiomes) {
super(region); super(region);
this.hasBiomes = hasBiomes; this.hasBiomes = hasBiomes;
this.hasEntities = hasEntities; this.hasEntities = hasEntities;
@ -43,27 +42,27 @@ public class WorldCopyClipboard extends ReadOnlyClipboard {
this.mx = origin.getBlockX(); this.mx = origin.getBlockX();
this.my = origin.getBlockY(); this.my = origin.getBlockY();
this.mz = origin.getBlockZ(); this.mz = origin.getBlockZ();
this.editSession = editSession; this.extent = editSession;
} }
@Override @Override
public BlockState getBlock(int x, int y, int z) { public BlockState getBlock(int x, int y, int z) {
return editSession.getLazyBlock(mx + x, my + y, mz + z); return extent.getLazyBlock(mx + x, my + y, mz + z);
} }
public BlockState getBlockAbs(int x, int y, int z) { public BlockState getBlockAbs(int x, int y, int z) {
return editSession.getLazyBlock(x, y, z); return extent.getLazyBlock(x, y, z);
} }
@Override @Override
public BaseBiome getBiome(int x, int z) { public BaseBiome getBiome(int x, int z) {
return editSession.getBiome(mutableBlockVector2D.setComponents(mx + x, mz + z)); return extent.getBiome(mutableBlockVector2D.setComponents(mx + x, mz + z));
} }
@Override @Override
public List<? extends Entity> getEntities() { public List<? extends Entity> getEntities() {
if (!hasEntities) return new ArrayList<>(); if (!hasEntities) return new ArrayList<>();
return editSession.getEntities(getRegion()); return extent.getEntities(getRegion());
} }
@Override @Override
@ -96,7 +95,7 @@ public class WorldCopyClipboard extends ReadOnlyClipboard {
task.run(x, y, z, block); task.run(x, y, z, block);
return true; return true;
} }
}, editSession); }, extent instanceof EditSession ? (EditSession) extent : null);
Operations.completeBlindly(visitor); Operations.completeBlindly(visitor);
} else { } else {
CuboidRegion cuboidEquivalent = new CuboidRegion(region.getMinimumPoint(), region.getMaximumPoint()); CuboidRegion cuboidEquivalent = new CuboidRegion(region.getMinimumPoint(), region.getMaximumPoint());
@ -124,7 +123,7 @@ public class WorldCopyClipboard extends ReadOnlyClipboard {
} }
return true; return true;
} }
}, editSession); }, extent instanceof EditSession ? (EditSession) extent : null);
Operations.completeBlindly(visitor); Operations.completeBlindly(visitor);
} }
} else { } else {

Datei anzeigen

@ -1,10 +1,8 @@
package com.boydti.fawe.object.clipboard; package com.boydti.fawe.object.clipboard;
import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.world.block.BlockStateHolder;
public class WorldCutClipboard extends WorldCopyClipboard { public class WorldCutClipboard extends WorldCopyClipboard {
public WorldCutClipboard(EditSession editSession, Region region, boolean copyEntities, boolean copyBiome) { public WorldCutClipboard(EditSession editSession, Region region, boolean copyEntities, boolean copyBiome) {
@ -20,20 +18,24 @@ public class WorldCutClipboard extends WorldCopyClipboard {
int xx = mx + x; int xx = mx + x;
int yy = my + y; int yy = my + y;
int zz = mz + z; int zz = mz + z;
BlockState block = editSession.getLazyBlock(xx, yy, zz); BlockState block = extent.getLazyBlock(xx, yy, zz);
editSession.setBlock(xx, yy, zz, EditSession.nullBlock); extent.setBlock(xx, yy, zz, EditSession.nullBlock);
return block; return block;
} }
public BlockState getBlockAbs(int x, int y, int z) { public BlockState getBlockAbs(int x, int y, int z) {
BlockState block = editSession.getLazyBlock(x, y, z); BlockState block = extent.getLazyBlock(x, y, z);
editSession.setBlock(x, y, z, EditSession.nullBlock); extent.setBlock(x, y, z, EditSession.nullBlock);
return block; return block;
} }
@Override @Override
public void forEach(BlockReader task, boolean air) { public void forEach(BlockReader task, boolean air) {
super.forEach(task, air); super.forEach(task, air);
editSession.flushQueue(); if (extent instanceof EditSession) {
((EditSession) extent).flushQueue();
} else {
extent.commit();
}
} }
} }

Datei anzeigen

@ -34,7 +34,7 @@ public final class IOUtil {
out.write((v >>> 0) & 0xFF); out.write((v >>> 0) & 0xFF);
} }
public static void writeVarInt(OutputStream out, int i) throws IOException { public static final void writeVarInt(final OutputStream out, int i) throws IOException {
while((i & -128) != 0) { while((i & -128) != 0) {
out.write(i & 127 | 128); out.write(i & 127 | 128);
i >>>= 7; i >>>= 7;
@ -42,7 +42,7 @@ public final class IOUtil {
out.write(i); out.write(i);
} }
public static int readVarInt(InputStream in) throws IOException { public static final int readVarInt(InputStream in) throws IOException {
int i = 0; int i = 0;
int offset = 0; int offset = 0;
int b; int b;
@ -53,4 +53,39 @@ public final class IOUtil {
i |= b << offset; i |= b << offset;
return i; return i;
} }
public static final void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
while (true) {
int r = in.read(buf);
if (r == -1) {
break;
}
out.write(buf, 0, r);
}
}
public static final int copy(InputStream in, OutputStream out, int len) throws IOException {
byte[] buf = new byte[8192];
while (len > 0) {
int r = in.read(buf, 0, Math.min(buf.length, len));
if (r == -1) {
break;
}
len -= r;
out.write(buf, 0, r);
}
return len;
}
public static final void copy(InputStream in, DataOutput out) throws IOException {
byte[] buf = new byte[8192];
while (true) {
int r = in.read(buf);
if (r == -1) {
break;
}
out.write(buf, 0, r);
}
}
} }

Datei anzeigen

@ -44,7 +44,7 @@ import java.util.function.Function;
*/ */
public final class NBTInputStream implements Closeable { public final class NBTInputStream implements Closeable {
private final DataInput is; private final DataInputStream is;
/** /**
* Creates a new {@code NBTInputStream}, which will source its data * Creates a new {@code NBTInputStream}, which will source its data
@ -61,8 +61,8 @@ public final class NBTInputStream implements Closeable {
this.is = dis; this.is = dis;
} }
public NBTInputStream(DataInput di) { public DataInputStream getInputStream() {
this.is = di; return is;
} }
/** /**
@ -178,7 +178,7 @@ public final class NBTInputStream implements Closeable {
NBTStreamer.ByteReader byteReader = (NBTStreamer.ByteReader) reader; NBTStreamer.ByteReader byteReader = (NBTStreamer.ByteReader) reader;
int i = 0; int i = 0;
if (is instanceof InputStream) { if (is instanceof InputStream) {
DataInputStream dis = (DataInputStream) is; DataInputStream dis = is;
if (length > 1024) { if (length > 1024) {
if (buf == null) { if (buf == null) {
buf = new byte[1024]; buf = new byte[1024];
@ -211,6 +211,8 @@ public final class NBTInputStream implements Closeable {
byteReader.run(i, is.readByte() & 0xFF); byteReader.run(i, is.readByte() & 0xFF);
} }
} }
} else if (reader instanceof NBTStreamer.LazyReader) {
reader.accept(length, is);
} else { } else {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
reader.accept(i, is.readByte()); reader.accept(i, is.readByte());
@ -273,6 +275,8 @@ public final class NBTInputStream implements Closeable {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
byteReader.run(i, is.readInt()); byteReader.run(i, is.readInt());
} }
} else if (reader instanceof NBTStreamer.LazyReader) {
reader.accept(length, is);
} else { } else {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
reader.accept(i, is.readInt()); reader.accept(i, is.readInt());
@ -296,6 +300,8 @@ public final class NBTInputStream implements Closeable {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
longReader.run(i, is.readLong()); longReader.run(i, is.readLong());
} }
} else if (reader instanceof NBTStreamer.LazyReader) {
reader.accept(length, is);
} else { } else {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
reader.accept(i, is.readLong()); reader.accept(i, is.readLong());

Datei anzeigen

@ -201,7 +201,7 @@ public final class NBTOutputStream implements Closeable {
* @param tag The tag. * @param tag The tag.
* @throws IOException if an I/O error occurs. * @throws IOException if an I/O error occurs.
*/ */
private void writeTagPayload(Tag tag) throws IOException { public void writeTagPayload(Tag tag) throws IOException {
int type = NBTUtils.getTypeCode(tag.getClass()); int type = NBTUtils.getTypeCode(tag.getClass());
switch (type) { switch (type) {
case NBTConstants.TYPE_END: case NBTConstants.TYPE_END:
@ -408,6 +408,4 @@ public final class NBTOutputStream implements Closeable {
public void flush() throws IOException { public void flush() throws IOException {
if (os instanceof Flushable) ((Flushable) os).flush(); if (os instanceof Flushable) ((Flushable) os).flush();
} }
} }

Datei anzeigen

@ -294,7 +294,7 @@ public class SchematicCommands extends MethodCommands {
@Command(aliases = {"save"}, usage = "[format] <filename>", desc = "Save a schematic into your clipboard") @Command(aliases = {"save"}, usage = "[format] <filename>", desc = "Save a schematic into your clipboard")
@Deprecated @Deprecated
@CommandPermissions({"worldedit.clipboard.save", "worldedit.schematic.save", "worldedit.schematic.save.other"}) @CommandPermissions({"worldedit.clipboard.save", "worldedit.schematic.save", "worldedit.schematic.save.other"})
public void save(final Player player, final LocalSession session, @Optional("nbt") final String formatName, String filename, @Switch('g') boolean global) throws CommandException, WorldEditException { public void save(final Player player, final LocalSession session, @Optional("schem") final String formatName, String filename, @Switch('g') boolean global) throws CommandException, WorldEditException {
final LocalConfiguration config = this.worldEdit.getConfiguration(); final LocalConfiguration config = this.worldEdit.getConfiguration();
final ClipboardFormat format = ClipboardFormat.findByAlias(formatName); final ClipboardFormat format = ClipboardFormat.findByAlias(formatName);
if (format == null) { if (format == null) {

Datei anzeigen

@ -21,8 +21,10 @@ package com.sk89q.worldedit.extent;
import com.boydti.fawe.jnbt.anvil.generator.*; import com.boydti.fawe.jnbt.anvil.generator.*;
import com.boydti.fawe.object.PseudoRandom; import com.boydti.fawe.object.PseudoRandom;
import com.boydti.fawe.object.clipboard.WorldCopyClipboard;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.util.Countable; import com.sk89q.worldedit.util.Countable;
import com.sk89q.worldedit.world.block.*; import com.sk89q.worldedit.world.block.*;
@ -359,6 +361,19 @@ public interface Extent extends InputExtent, OutputExtent {
return distribution; 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 @Nullable
@Override @Override
default Operation commit() { default Operation commit() {

Datei anzeigen

@ -94,6 +94,7 @@ public enum ClipboardFormat {
@Override @Override
public boolean isFormat(File file) { public boolean isFormat(File file) {
if (!file.getName().toLowerCase().endsWith(".schematic")) return false;
DataInputStream str = null; DataInputStream str = null;
try { try {
str = new DataInputStream(new GZIPInputStream(new FileInputStream(file))); str = new DataInputStream(new GZIPInputStream(new FileInputStream(file)));
@ -151,6 +152,7 @@ public enum ClipboardFormat {
@Override @Override
public boolean isFormat(File file) { public boolean isFormat(File file) {
if (!file.getName().toLowerCase().endsWith(".schem")) return false;
DataInputStream str = null; DataInputStream str = null;
try { try {
str = new DataInputStream(new GZIPInputStream(new FileInputStream(file))); str = new DataInputStream(new GZIPInputStream(new FileInputStream(file)));
@ -496,13 +498,7 @@ public enum ClipboardFormat {
LocalSession session = WorldEdit.getInstance().getSessionManager().get(player); LocalSession session = WorldEdit.getInstance().getSessionManager().get(player);
session.setClipboard(null); session.setClipboard(null);
if (reader instanceof SchematicReader) { clipboard = reader.read(player.getUniqueId());
clipboard = ((SchematicReader) reader).read(player.getUniqueId());
} else if (reader instanceof StructureFormat) {
clipboard = ((StructureFormat) reader).read(player.getUniqueId());
} else {
clipboard = reader.read();
}
URIClipboardHolder holder = new URIClipboardHolder(uri, clipboard); URIClipboardHolder holder = new URIClipboardHolder(uri, clipboard);
session.setClipboard(holder); session.setClipboard(holder);
return holder; return holder;

Datei anzeigen

@ -24,6 +24,7 @@ import com.sk89q.worldedit.world.registry.Registries;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.util.UUID;
/** /**
* Reads {@code Clipboard}s. * Reads {@code Clipboard}s.
@ -40,4 +41,7 @@ public interface ClipboardReader extends Closeable {
*/ */
Clipboard read() throws IOException; Clipboard read() throws IOException;
} default Clipboard read(UUID uuid) throws IOException {
return read();
}
}

Datei anzeigen

@ -19,6 +19,18 @@
package com.sk89q.worldedit.extent.clipboard.io; 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.google.common.collect.Maps;
import com.sk89q.jnbt.ByteArrayTag; import com.sk89q.jnbt.ByteArrayTag;
import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.CompoundTag;
@ -28,11 +40,13 @@ import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.NBTInputStream; import com.sk89q.jnbt.NBTInputStream;
import com.sk89q.jnbt.NamedTag; import com.sk89q.jnbt.NamedTag;
import com.sk89q.jnbt.ShortTag; import com.sk89q.jnbt.ShortTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag; import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.input.ParserContext;
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;
@ -40,12 +54,21 @@ import com.sk89q.worldedit.extent.clipboard.io.legacycompat.NBTCompatibilityHand
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.world.block.BlockState; 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.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -77,141 +100,151 @@ public class SpongeSchematicReader extends NBTSchematicReader {
@Override @Override
public Clipboard read() throws IOException { public Clipboard read() throws IOException {
NamedTag rootTag = inputStream.readNamedTag(); return read(UUID.randomUUID());
if (!rootTag.getName().equals("Schematic")) { }
throw new IOException("Tag 'Schematic' does not exist or is not first");
}
CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
// Check @Override
Map<String, Tag> schematic = schematicTag.getValue(); public Clipboard read(UUID uuid) throws IOException {
int version = requireTag(schematic, "Version", IntTag.class).getValue(); return readVersion1(uuid);
switch (version) { }
case 1:
return readVersion1(schematic); private int width, height, length;
default: private int offsetX, offsetY, offsetZ;
throw new IOException("This schematic version is currently not supported"); 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 { private Clipboard readVersion1(UUID uuid) throws IOException {
Vector origin; width = height = length = offsetX = offsetY = offsetZ = Integer.MIN_VALUE;
Region region;
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(); NBTStreamer streamer = new NBTStreamer(inputStream);
int height = requireTag(schematic, "Height", ShortTag.class).getValue(); streamer.addReader("Schematic.Width", (BiConsumer<Integer, Short>) (i, v) -> width = v);
int length = requireTag(schematic, "Length", ShortTag.class).getValue(); streamer.addReader("Schematic.Height", (BiConsumer<Integer, Short>) (i, v) -> height = v);
streamer.addReader("Schematic.Length", (BiConsumer<Integer, Short>) (i, v) -> length = v);
int[] offsetParts = requireTag(schematic, "Offset", IntArrayTag.class).getValue(); streamer.addReader("Schematic.Offset", (BiConsumer<Integer, int[]>) (i, v) -> min = new BlockVector(v[0], v[1], v[2]));
if (offsetParts.length != 3) { streamer.addReader("Schematic.Metadata.WEOffsetX", (BiConsumer<Integer, Integer>) (i, v) -> offsetX = v);
throw new IOException("Invalid offset specified in schematic."); 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) -> {
Vector min = new Vector(offsetParts[0], offsetParts[1], offsetParts[2]); palette = new char[v.size()];
for (Map.Entry<String, Tag> entry : v.entrySet()) {
if (metadata.containsKey("WEOffsetX")) { BlockState state = BlockState.get(entry.getKey());
// We appear to have WorldEdit Metadata int index = ((IntTag) entry.getValue()).getValue();
int offsetX = requireTag(metadata, "WEOffsetX", IntTag.class).getValue(); palette[index] = (char) state.getOrdinal();
int offsetY = requireTag(metadata, "WEOffsetY", IntTag.class).getValue(); }
int offsetZ = requireTag(metadata, "WEOffsetZ", IntTag.class).getValue(); });
Vector offset = new Vector(offsetX, offsetY, offsetZ); streamer.addReader("Schematic.BlockData.#", new NBTStreamer.LazyReader() {
origin = min.subtract(offset); @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)); region = new CuboidRegion(min, min.add(width, height, length).subtract(Vector.ONE));
} else { } else {
origin = min; region = new CuboidRegion(min, min.add(width, height, length).subtract(Vector.ONE));
region = new CuboidRegion(origin, origin.add(width, height, length).subtract(Vector.ONE));
} }
if (blocksOut.getSize() != 0) {
int paletteMax = requireTag(schematic, "PaletteMax", IntTag.class).getValue(); try (FaweInputStream fis = new FaweInputStream(new LZ4BlockInputStream(new FastByteArraysInputStream(blocksOut.toByteArrays())))) {
Map<String, Tag> paletteObject = requireTag(schematic, "Palette", CompoundTag.class).getValue(); int volume = width * height * length;
if (paletteObject.size() != paletteMax) { if (palette.length < 128) {
throw new IOException("Differing given palette size to actual size"); for (int index = 0; index < volume; index++) {
} BlockState state = BlockTypes.states[palette[fis.read()]];
fc.setBlock(index, state);
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);
}
} }
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 { } 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; return clipboard;
} }

Datei anzeigen

@ -19,32 +19,36 @@
package com.sk89q.worldedit.extent.clipboard.io; package com.sk89q.worldedit.extent.clipboard.io;
import static com.google.common.base.Preconditions.checkNotNull; import com.boydti.fawe.object.clipboard.FaweClipboard;
import com.boydti.fawe.util.IOUtil;
import com.sk89q.jnbt.ByteArrayTag;
import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.IntArrayTag; import com.sk89q.jnbt.IntArrayTag;
import com.sk89q.jnbt.IntTag; import com.sk89q.jnbt.NBTConstants;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.NBTOutputStream; import com.sk89q.jnbt.NBTOutputStream;
import com.sk89q.jnbt.ShortTag;
import com.sk89q.jnbt.StringTag; import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag; import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.regions.Region; 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.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* Writes schematic files using the Sponge schematic format. * Writes schematic files using the Sponge schematic format.
*/ */
@ -65,26 +69,18 @@ public class SpongeSchematicWriter implements ClipboardWriter {
@Override @Override
public void write(Clipboard clipboard) throws IOException { public void write(Clipboard clipboard) throws IOException {
// For now always write the latest version. Maybe provide support for earlier if more appear. write1(clipboard);
outputStream.writeNamedTag("Schematic", new CompoundTag(write1(clipboard)));
} }
/** public void write1(Clipboard clipboard) throws IOException {
* Writes a version 1 schematic file. // metadata
*
* @param clipboard The clipboard
* @return The schematic map
* @throws IOException If an error occurs
*/
private Map<String, Tag> write1(Clipboard clipboard) throws IOException {
Region region = clipboard.getRegion(); Region region = clipboard.getRegion();
Vector origin = clipboard.getOrigin(); Vector origin = clipboard.getOrigin();
Vector min = region.getMinimumPoint(); BlockVector min = region.getMinimumPoint().toBlockVector();
Vector offset = min.subtract(origin); Vector offset = min.subtract(origin);
int width = region.getWidth(); int width = region.getWidth();
int height = region.getHeight(); int height = region.getHeight();
int length = region.getLength(); int length = region.getLength();
if (width > MAX_SIZE) { if (width > MAX_SIZE) {
throw new IllegalArgumentException("Width of region too large for a .schematic"); throw new IllegalArgumentException("Width of region too large for a .schematic");
} }
@ -94,95 +90,118 @@ 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");
} }
// 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<>(); out.writeLazyCompoundTag("Metadata", out1 -> {
schematic.put("Version", new IntTag(1)); out1.writeNamedTag("WEOffsetX", offset.getBlockX());
out1.writeNamedTag("WEOffsetY", offset.getBlockY());
out1.writeNamedTag("WEOffsetZ", offset.getBlockZ());
});
Map<String, Tag> metadata = new HashMap<>(); ByteArrayOutputStream blocksCompressed = new ByteArrayOutputStream();
metadata.put("WEOffsetX", new IntTag(offset.getBlockX())); DataOutputStream blocksOut = new DataOutputStream(new LZ4BlockOutputStream(blocksCompressed));
metadata.put("WEOffsetY", new IntTag(offset.getBlockY()));
metadata.put("WEOffsetZ", new IntTag(offset.getBlockZ()));
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)); List<Integer> paletteList = new ArrayList<>();
schematic.put("Height", new ShortTag((short) height)); char[] palette = new char[BlockTypes.states.length];
schematic.put("Length", new ShortTag((short) 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; FaweClipboard.BlockReader reader = new FaweClipboard.BlockReader() {
Map<String, Integer> palette = new HashMap<>(); @Override
public void run(int x, int y, int z, BlockState block) {
List<CompoundTag> tileEntities = new ArrayList<>(); try {
CompoundTag tile = block.getNbtData();
ByteArrayOutputStream buffer = new ByteArrayOutputStream(width * height * length); if (tile != null) {
Map<String, Tag> values = tile.getValue();
for (int y = 0; y < height; y++) { values.remove("id"); // Remove 'id' if it exists. We want 'Id'
int y0 = min.getBlockY() + y; // Positions are kept in NBT, we don't want that.
for (int z = 0; z < length; z++) { values.remove("x");
int z0 = min.getBlockZ() + z; values.remove("y");
for (int x = 0; x < width; x++) { values.remove("z");
int x0 = min.getBlockX() + x; if (!values.containsKey("Id")) values.put("Id", new StringTag(block.getNbtId()));
BlockVector point = new BlockVector(x0, y0, z0); values.put("Pos", new IntArrayTag(new int[]{
BlockStateHolder block = clipboard.getFullBlock(point); x,
if (block.getNbtData() != null) { y,
Map<String, Tag> values = new HashMap<>(); z
for (Map.Entry<String, Tag> entry : block.getNbtData().getValue().entrySet()) { }));
values.put(entry.getKey(), entry.getValue()); numTiles[0]++;
tilesOut.writeTagPayload(tile);
} }
int ordinal = block.getOrdinal();
values.remove("id"); // Remove 'id' if it exists. We want 'Id' char value = palette[ordinal];
if (value == Character.MAX_VALUE) {
// Positions are kept in NBT, we don't want that. int size = paletteMax[0]++;
values.remove("x"); palette[ordinal] = value = (char) size;
values.remove("y"); paletteList.add(ordinal);
values.remove("z"); }
while ((value & -128) != 0) {
values.put("Id", new StringTag(block.getNbtId())); blocksOut.write(value & 127 | 128);
values.put("Pos", new IntArrayTag(new int[]{ value >>>= 7;
x, }
y, blocksOut.write(value);
z } catch (IOException e) {
})); throw new RuntimeException(e);
tileEntities.add(new CompoundTag(values));
} }
}
String blockKey = block.toImmutableState().getAsString(); };
int blockId; if (clipboard instanceof BlockArrayClipboard) {
if (palette.containsKey(blockKey)) { ((BlockArrayClipboard) clipboard).IMP.forEach(reader, true);
blockId = palette.get(blockKey); } else {
} else { for (Vector pt : region) {
blockId = paletteMax; BlockState block = clipboard.getBlock(pt);
palette.put(blockKey, blockId); int x = pt.getBlockX() - min.getBlockX();
paletteMax++; int y = pt.getBlockY() - min.getBlockY();
} int z = pt.getBlockZ() - min.getBlockY();
reader.run(x, y, z, block);
while ((blockId & -128) != 0) {
buffer.write(blockId & 127 | 128);
blockId >>>= 7;
}
buffer.write(blockId);
} }
} }
} // close
tilesOut.close();
schematic.put("PaletteMax", new IntTag(paletteMax)); blocksOut.close();
// palette max
Map<String, Tag> paletteTag = new HashMap<>(); out.writeNamedTag("PaletteMax", paletteMax[0]);
palette.forEach((key, value) -> paletteTag.put(key, new IntTag(value))); // palette
out.writeLazyCompoundTag("Palette", out12 -> {
schematic.put("Palette", new CompoundTag(paletteTag)); for (int i = 0; i < paletteList.size(); i++) {
schematic.put("BlockData", new ByteArrayTag(buffer.toByteArray())); int stateOrdinal = paletteList.get(i);
schematic.put("TileEntities", new ListTag(CompoundTag.class, tileEntities)); BlockState state = BlockTypes.states[stateOrdinal];
out12.writeNamedTag(state.getAsString(), i);
return schematic; }
});
// 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 @Override