2016-02-23 05:11:28 +01:00
|
|
|
package com.intellectualcrafters.plot.util;
|
|
|
|
|
2016-03-14 07:18:04 +01:00
|
|
|
import com.intellectualcrafters.jnbt.ByteArrayTag;
|
|
|
|
import com.intellectualcrafters.jnbt.CompoundTag;
|
|
|
|
import com.intellectualcrafters.jnbt.IntTag;
|
|
|
|
import com.intellectualcrafters.jnbt.ListTag;
|
|
|
|
import com.intellectualcrafters.jnbt.NBTInputStream;
|
|
|
|
import com.intellectualcrafters.jnbt.NBTOutputStream;
|
|
|
|
import com.intellectualcrafters.jnbt.ShortTag;
|
|
|
|
import com.intellectualcrafters.jnbt.StringTag;
|
|
|
|
import com.intellectualcrafters.jnbt.Tag;
|
2016-02-23 05:11:28 +01:00
|
|
|
import com.intellectualcrafters.json.JSONArray;
|
|
|
|
import com.intellectualcrafters.json.JSONException;
|
|
|
|
import com.intellectualcrafters.plot.PS;
|
|
|
|
import com.intellectualcrafters.plot.config.Settings;
|
2016-02-29 16:01:52 +01:00
|
|
|
import com.intellectualcrafters.plot.flag.Flag;
|
2016-02-23 05:11:28 +01:00
|
|
|
import com.intellectualcrafters.plot.generator.ClassicPlotWorld;
|
2016-03-31 19:23:26 +02:00
|
|
|
import com.intellectualcrafters.plot.object.BlockLoc;
|
2016-03-14 07:18:04 +01:00
|
|
|
import com.intellectualcrafters.plot.object.ChunkLoc;
|
|
|
|
import com.intellectualcrafters.plot.object.Location;
|
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotArea;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotBlock;
|
|
|
|
import com.intellectualcrafters.plot.object.RegionWrapper;
|
|
|
|
import com.intellectualcrafters.plot.object.RunnableVal;
|
2016-06-13 06:47:50 +02:00
|
|
|
import com.intellectualcrafters.plot.util.block.LocalBlockQueue;
|
2016-03-14 07:18:04 +01:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
2016-02-23 05:11:28 +01:00
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLConnection;
|
|
|
|
import java.nio.channels.Channels;
|
|
|
|
import java.nio.channels.ReadableByteChannel;
|
2016-03-14 07:18:04 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.UUID;
|
2016-02-23 05:11:28 +01:00
|
|
|
import java.util.zip.GZIPInputStream;
|
|
|
|
import java.util.zip.GZIPOutputStream;
|
|
|
|
|
|
|
|
public abstract class SchematicHandler {
|
|
|
|
public static SchematicHandler manager;
|
|
|
|
|
|
|
|
private boolean exportAll = false;
|
2016-03-29 01:30:55 +02:00
|
|
|
|
|
|
|
public boolean exportAll(Collection<Plot> collection, final File outputDir, final String namingScheme, final Runnable ifSuccess) {
|
|
|
|
if (this.exportAll) {
|
2016-02-23 05:11:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (collection.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
this.exportAll = true;
|
2016-03-14 07:18:04 +01:00
|
|
|
final ArrayList<Plot> plots = new ArrayList<>(collection);
|
2016-02-23 05:11:28 +01:00
|
|
|
TaskManager.runTask(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (plots.isEmpty()) {
|
2016-03-29 01:30:55 +02:00
|
|
|
SchematicHandler.this.exportAll = false;
|
2016-02-23 05:11:28 +01:00
|
|
|
TaskManager.runTask(ifSuccess);
|
|
|
|
return;
|
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
Iterator<Plot> i = plots.iterator();
|
2016-02-23 05:11:28 +01:00
|
|
|
final Plot plot = i.next();
|
|
|
|
i.remove();
|
|
|
|
String o = UUIDHandler.getName(plot.owner);
|
|
|
|
if (o == null) {
|
|
|
|
o = "unknown";
|
|
|
|
}
|
|
|
|
final String name;
|
|
|
|
if (namingScheme == null) {
|
2016-05-12 23:09:35 +02:00
|
|
|
name = plot.getId().x + ";" + plot.getId().y + ',' + plot.getArea() + ',' + o;
|
2016-02-23 05:11:28 +01:00
|
|
|
} else {
|
|
|
|
name = namingScheme.replaceAll("%owner%", o).replaceAll("%id%", plot.getId().toString()).replaceAll("%idx%", plot.getId().x + "").replaceAll("%idy%", plot.getId().y + "")
|
|
|
|
.replaceAll("%world%", plot.getArea().toString());
|
|
|
|
}
|
|
|
|
final String directory;
|
|
|
|
if (outputDir == null) {
|
2016-06-10 19:00:34 +02:00
|
|
|
directory = Settings.Paths.SCHEMATICS;
|
2016-02-23 05:11:28 +01:00
|
|
|
} else {
|
2016-03-11 05:33:18 +01:00
|
|
|
directory = outputDir.getAbsolutePath();
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
final Runnable THIS = this;
|
|
|
|
SchematicHandler.manager.getCompoundTag(plot, new RunnableVal<CompoundTag>() {
|
|
|
|
@Override
|
|
|
|
public void run(final CompoundTag value) {
|
|
|
|
if (value == null) {
|
|
|
|
MainUtil.sendMessage(null, "&7 - Skipped plot &c" + plot.getId());
|
|
|
|
} else {
|
|
|
|
TaskManager.runTaskAsync(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
MainUtil.sendMessage(null, "&6ID: " + plot.getId());
|
2016-03-29 01:30:55 +02:00
|
|
|
boolean result = SchematicHandler.manager.save(value, directory + File.separator + name + ".schematic");
|
2016-02-23 05:11:28 +01:00
|
|
|
if (!result) {
|
|
|
|
MainUtil.sendMessage(null, "&7 - Failed to save &c" + plot.getId());
|
|
|
|
} else {
|
|
|
|
MainUtil.sendMessage(null, "&7 - &a success: " + plot.getId());
|
|
|
|
}
|
|
|
|
TaskManager.runTask(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
THIS.run();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-02 07:42:04 +01:00
|
|
|
|
2016-02-23 05:11:28 +01:00
|
|
|
/**
|
2016-03-29 01:30:55 +02:00
|
|
|
* Paste a schematic.
|
2016-02-23 05:11:28 +01:00
|
|
|
*
|
|
|
|
* @param schematic the schematic object to paste
|
|
|
|
* @param plot plot to paste in
|
2016-03-29 01:30:55 +02:00
|
|
|
* @param xOffset offset x to paste it from plot origin
|
|
|
|
* @param zOffset offset z to paste it from plot origin
|
2016-02-23 05:11:28 +01:00
|
|
|
*
|
|
|
|
* @return boolean true if succeeded
|
|
|
|
*/
|
2016-06-13 06:47:50 +02:00
|
|
|
public void paste(final Schematic schematic, final Plot plot, final int xOffset, final int yOffset, final int zOffset, final boolean autoHeight, final RunnableVal<Boolean> whenDone) {
|
2016-02-23 05:11:28 +01:00
|
|
|
TaskManager.runTask(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (whenDone != null) {
|
|
|
|
whenDone.value = false;
|
|
|
|
}
|
|
|
|
if (schematic == null) {
|
|
|
|
PS.debug("Schematic == null :|");
|
|
|
|
TaskManager.runTask(whenDone);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2016-02-29 16:01:52 +01:00
|
|
|
// Set flags
|
|
|
|
if (plot.hasOwner()) {
|
|
|
|
Map<String, Tag> flags = schematic.getFlags();
|
|
|
|
if (!flags.isEmpty()) {
|
|
|
|
for (Map.Entry<String, Tag> entry : flags.entrySet()) {
|
2016-04-19 22:59:10 +02:00
|
|
|
//plot.setFlag(entry.getKey(), StringTag.class.cast(entry.getValue()).getValue());
|
2016-02-29 16:01:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-06-13 06:47:50 +02:00
|
|
|
final LocalBlockQueue queue = plot.getArea().getQueue(false);
|
2016-03-29 21:47:59 +02:00
|
|
|
Dimension dimension = schematic.getSchematicDimension();
|
|
|
|
final int WIDTH = dimension.getX();
|
|
|
|
final int LENGTH = dimension.getZ();
|
|
|
|
final int HEIGHT = dimension.getY();
|
2016-02-23 05:11:28 +01:00
|
|
|
// Validate dimensions
|
|
|
|
RegionWrapper region = plot.getLargestRegion();
|
2016-03-29 01:30:55 +02:00
|
|
|
if (((region.maxX - region.minX + xOffset + 1) < WIDTH) || ((region.maxZ - region.minZ + zOffset + 1) < LENGTH) || (HEIGHT
|
2016-03-21 00:35:40 +01:00
|
|
|
> 256)) {
|
2016-02-23 05:11:28 +01:00
|
|
|
PS.debug("Schematic is too large");
|
2016-05-12 23:09:35 +02:00
|
|
|
PS.debug("(" + WIDTH + ',' + LENGTH + ',' + HEIGHT + ") is bigger than (" + (region.maxX - region.minX) + ',' + (region.maxZ - region.minZ) + ",256)");
|
2016-02-23 05:11:28 +01:00
|
|
|
TaskManager.runTask(whenDone);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// block type and data arrays
|
|
|
|
final short[] ids = schematic.ids;
|
|
|
|
final byte[] datas = schematic.datas;
|
|
|
|
// Calculate the optimal height to paste the schematic at
|
2016-03-02 07:42:04 +01:00
|
|
|
final int y_offset_actual;
|
|
|
|
if (autoHeight) {
|
|
|
|
if (HEIGHT >= 256) {
|
2016-03-29 01:30:55 +02:00
|
|
|
y_offset_actual = yOffset;
|
2016-02-23 05:11:28 +01:00
|
|
|
} else {
|
2016-03-02 07:42:04 +01:00
|
|
|
PlotArea pw = plot.getArea();
|
|
|
|
if (pw instanceof ClassicPlotWorld) {
|
2016-03-29 01:30:55 +02:00
|
|
|
y_offset_actual = yOffset + ((ClassicPlotWorld) pw).PLOT_HEIGHT;
|
2016-03-02 07:42:04 +01:00
|
|
|
} else {
|
2017-09-25 08:57:42 +02:00
|
|
|
y_offset_actual = yOffset + 1 + MainUtil.getHeighestBlock(plot.getWorldName(), region.minX + 1, region.minZ + 1);
|
2016-03-02 07:42:04 +01:00
|
|
|
}
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-02 07:42:04 +01:00
|
|
|
else {
|
2016-03-29 01:30:55 +02:00
|
|
|
y_offset_actual = yOffset;
|
2016-03-02 07:42:04 +01:00
|
|
|
}
|
2017-03-23 01:10:29 +01:00
|
|
|
Location pos1 = new Location(plot.getWorldName(), region.minX + xOffset, y_offset_actual, region.minZ + zOffset);
|
2016-03-29 01:30:55 +02:00
|
|
|
Location pos2 = pos1.clone().add(WIDTH - 1, HEIGHT - 1, LENGTH - 1);
|
2016-02-23 05:11:28 +01:00
|
|
|
// TODO switch to ChunkManager.chunkTask(pos1, pos2, task, whenDone, allocate);
|
|
|
|
final int p1x = pos1.getX();
|
|
|
|
final int p1z = pos1.getZ();
|
|
|
|
final int p2x = pos2.getX();
|
|
|
|
final int p2z = pos2.getZ();
|
|
|
|
final int bcx = p1x >> 4;
|
|
|
|
final int bcz = p1z >> 4;
|
|
|
|
final int tcx = p2x >> 4;
|
|
|
|
final int tcz = p2z >> 4;
|
|
|
|
final ArrayList<ChunkLoc> chunks = new ArrayList<ChunkLoc>();
|
|
|
|
for (int x = bcx; x <= tcx; x++) {
|
|
|
|
for (int z = bcz; z <= tcz; z++) {
|
|
|
|
chunks.add(new ChunkLoc(x, z));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TaskManager.runTaskAsync(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
int count = 0;
|
|
|
|
while (!chunks.isEmpty() && count < 256) {
|
|
|
|
count++;
|
2016-03-29 01:30:55 +02:00
|
|
|
ChunkLoc chunk = chunks.remove(0);
|
|
|
|
int x = chunk.x;
|
|
|
|
int z = chunk.z;
|
2016-02-23 05:11:28 +01:00
|
|
|
int xxb = x << 4;
|
|
|
|
int zzb = z << 4;
|
|
|
|
int xxt = xxb + 15;
|
|
|
|
int zzt = zzb + 15;
|
|
|
|
if (x == bcx) {
|
|
|
|
xxb = p1x;
|
|
|
|
}
|
|
|
|
if (x == tcx) {
|
|
|
|
xxt = p2x;
|
|
|
|
}
|
|
|
|
if (z == bcz) {
|
|
|
|
zzb = p1z;
|
|
|
|
}
|
|
|
|
if (z == tcz) {
|
|
|
|
zzt = p2z;
|
|
|
|
}
|
|
|
|
// Paste schematic here
|
|
|
|
|
|
|
|
for (int ry = 0; ry < Math.min(256, HEIGHT); ry++) {
|
2016-03-29 01:30:55 +02:00
|
|
|
int yy = y_offset_actual + ry;
|
2016-02-23 05:11:28 +01:00
|
|
|
if (yy > 255) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
int i1 = ry * WIDTH * LENGTH;
|
2016-02-23 05:11:28 +01:00
|
|
|
for (int rz = zzb - p1z; rz <= (zzt - p1z); rz++) {
|
2016-03-29 01:30:55 +02:00
|
|
|
int i2 = (rz * WIDTH) + i1;
|
2016-02-23 05:11:28 +01:00
|
|
|
for (int rx = xxb - p1x; rx <= (xxt - p1x); rx++) {
|
2016-03-29 01:30:55 +02:00
|
|
|
int i = i2 + rx;
|
|
|
|
int xx = p1x + rx;
|
|
|
|
int zz = p1z + rz;
|
2016-02-23 05:11:28 +01:00
|
|
|
int id = ids[i];
|
|
|
|
switch (id) {
|
|
|
|
case 0:
|
|
|
|
case 2:
|
|
|
|
case 4:
|
|
|
|
case 13:
|
|
|
|
case 14:
|
|
|
|
case 15:
|
|
|
|
case 20:
|
|
|
|
case 21:
|
|
|
|
case 22:
|
|
|
|
case 30:
|
|
|
|
case 32:
|
|
|
|
case 37:
|
|
|
|
case 39:
|
|
|
|
case 40:
|
|
|
|
case 41:
|
|
|
|
case 42:
|
|
|
|
case 45:
|
|
|
|
case 46:
|
|
|
|
case 47:
|
|
|
|
case 48:
|
|
|
|
case 49:
|
|
|
|
case 51:
|
|
|
|
case 55:
|
|
|
|
case 56:
|
|
|
|
case 57:
|
|
|
|
case 58:
|
|
|
|
case 60:
|
|
|
|
case 7:
|
|
|
|
case 8:
|
|
|
|
case 9:
|
|
|
|
case 10:
|
|
|
|
case 11:
|
|
|
|
case 73:
|
|
|
|
case 74:
|
|
|
|
case 78:
|
|
|
|
case 79:
|
|
|
|
case 80:
|
|
|
|
case 81:
|
|
|
|
case 82:
|
|
|
|
case 83:
|
|
|
|
case 85:
|
|
|
|
case 87:
|
|
|
|
case 88:
|
|
|
|
case 101:
|
|
|
|
case 102:
|
|
|
|
case 103:
|
|
|
|
case 110:
|
|
|
|
case 112:
|
|
|
|
case 113:
|
|
|
|
case 121:
|
|
|
|
case 122:
|
|
|
|
case 129:
|
|
|
|
case 133:
|
|
|
|
case 165:
|
|
|
|
case 166:
|
|
|
|
case 169:
|
|
|
|
case 170:
|
|
|
|
case 172:
|
|
|
|
case 173:
|
|
|
|
case 174:
|
|
|
|
case 181:
|
|
|
|
case 182:
|
|
|
|
case 188:
|
|
|
|
case 189:
|
|
|
|
case 190:
|
|
|
|
case 191:
|
|
|
|
case 192:
|
2016-06-13 06:47:50 +02:00
|
|
|
queue.setBlock(xx, yy, zz, id);
|
2016-02-23 05:11:28 +01:00
|
|
|
break;
|
|
|
|
default:
|
2016-06-13 06:47:50 +02:00
|
|
|
queue.setBlock(xx, yy, zz, PlotBlock.get((short) id, datas[i]));
|
2016-02-23 05:11:28 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!chunks.isEmpty()) {
|
2016-06-13 06:47:50 +02:00
|
|
|
this.run();
|
2016-02-23 05:11:28 +01:00
|
|
|
} else {
|
2016-06-16 09:31:02 +02:00
|
|
|
queue.flush();
|
2017-08-10 09:18:35 +02:00
|
|
|
HashMap<BlockLoc, CompoundTag> tiles = schematic.getTiles();
|
|
|
|
if (!tiles.isEmpty()) {
|
|
|
|
TaskManager.IMP.sync(new RunnableVal<Object>() {
|
|
|
|
@Override
|
|
|
|
public void run(Object value) {
|
|
|
|
for (Map.Entry<BlockLoc, CompoundTag> entry : schematic.getTiles().entrySet()) {
|
|
|
|
BlockLoc loc = entry.getKey();
|
|
|
|
restoreTile(queue, entry.getValue(), p1x + xOffset + loc.x, loc.y + y_offset_actual, p1z + zOffset + loc.z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-06-13 06:47:50 +02:00
|
|
|
if (whenDone != null) {
|
|
|
|
whenDone.value = true;
|
|
|
|
whenDone.run();
|
|
|
|
}
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-03-29 01:30:55 +02:00
|
|
|
} catch (Exception e) {
|
2016-02-23 05:11:28 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
TaskManager.runTask(whenDone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
|
|
|
|
public Schematic getSchematic(CompoundTag tag) {
|
|
|
|
Map<String, Tag> tagMap = tag.getValue();
|
2017-07-24 22:39:59 +02:00
|
|
|
byte[] addBlocks = null;
|
|
|
|
if (tagMap.containsKey("AddBlocks")) {
|
|
|
|
addBlocks = ByteArrayTag.class.cast(tagMap.get("AddBlocks")).getValue();
|
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
|
|
|
|
short width = ShortTag.class.cast(tagMap.get("Width")).getValue();
|
|
|
|
short length = ShortTag.class.cast(tagMap.get("Length")).getValue();
|
|
|
|
short height = ShortTag.class.cast(tagMap.get("Height")).getValue();
|
|
|
|
byte[] block_sml = ByteArrayTag.class.cast(tagMap.get("Blocks")).getValue();
|
|
|
|
byte[] data = ByteArrayTag.class.cast(tagMap.get("Data")).getValue();
|
|
|
|
Map<String, Tag> flags;
|
2016-02-29 16:01:52 +01:00
|
|
|
if (tagMap.containsKey("Flags")) {
|
|
|
|
flags = CompoundTag.class.cast(tagMap.get("Flags")).getValue();
|
|
|
|
} else {
|
|
|
|
flags = null;
|
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
|
|
|
|
short[] block = new short[block_sml.length];
|
2016-02-23 05:11:28 +01:00
|
|
|
for (int i = 0; i < block.length; i++) {
|
|
|
|
short id = block_sml[i];
|
|
|
|
if (id < 0) {
|
|
|
|
id = (short) (id & 0xFF);
|
|
|
|
}
|
|
|
|
block[i] = id;
|
|
|
|
}
|
2017-07-24 22:39:59 +02:00
|
|
|
|
|
|
|
if (addBlocks != null) {
|
|
|
|
if (addBlocks.length == block.length) {
|
|
|
|
for (int i = 0; i < addBlocks.length; i++) {
|
|
|
|
byte val = addBlocks[i];
|
|
|
|
if (val != 0) {
|
|
|
|
block[i] |= (val << 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int index = 0; index < block.length; index++) {
|
|
|
|
if ((index & 1) == 0) {
|
|
|
|
block[index] = (short) (((addBlocks[index >> 1] & 0x0F) << 8) + (block[index]));
|
|
|
|
} else {
|
|
|
|
block[index] = (short) (((addBlocks[index >> 1] & 0xF0) << 4) + (block[index]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 05:11:28 +01:00
|
|
|
// Slow as wrapper for each block
|
|
|
|
// final DataCollection[] collection = new DataCollection[b.length];
|
|
|
|
// for (int x = 0; x < b.length; x++) {
|
|
|
|
// collection[x] = new DataCollection(blocks[x], d[x]);
|
|
|
|
// }
|
|
|
|
// Schematic schem = new Schematic(collection, dimension, file);
|
2016-03-29 01:30:55 +02:00
|
|
|
|
|
|
|
Dimension dimensions = new Dimension(width, height, length);
|
|
|
|
Schematic schem = new Schematic(block, data, dimensions, flags);
|
2016-02-23 05:11:28 +01:00
|
|
|
// Slow
|
|
|
|
try {
|
2016-03-29 01:30:55 +02:00
|
|
|
List<Tag> blockStates = ListTag.class.cast(tagMap.get("TileEntities")).getValue();
|
|
|
|
for (Tag stateTag : blockStates) {
|
2016-02-23 05:11:28 +01:00
|
|
|
try {
|
2016-03-29 01:30:55 +02:00
|
|
|
CompoundTag ct = (CompoundTag) stateTag;
|
|
|
|
Map<String, Tag> state = ct.getValue();
|
|
|
|
short x = IntTag.class.cast(state.get("x")).getValue().shortValue();
|
|
|
|
short y = IntTag.class.cast(state.get("y")).getValue().shortValue();
|
|
|
|
short z = IntTag.class.cast(state.get("z")).getValue().shortValue();
|
2016-03-31 19:23:26 +02:00
|
|
|
schem.addTile(new BlockLoc(x, y, z), ct);
|
2016-03-29 01:30:55 +02:00
|
|
|
} catch (Exception e) {
|
2016-02-23 05:11:28 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
} catch (Exception e) {
|
2016-02-23 05:11:28 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return schem;
|
|
|
|
}
|
2016-03-29 21:47:59 +02:00
|
|
|
|
2016-06-16 02:31:25 +02:00
|
|
|
public abstract boolean restoreTile(LocalBlockQueue queue, CompoundTag tag, int x, int y, int z);
|
2016-02-23 05:11:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a schematic
|
|
|
|
*
|
|
|
|
* @param name to check
|
|
|
|
*
|
|
|
|
* @return schematic if found, else null
|
|
|
|
*/
|
2016-03-29 01:30:55 +02:00
|
|
|
public Schematic getSchematic(String name) {
|
2016-06-10 19:00:34 +02:00
|
|
|
File parent = MainUtil.getFile(PS.get().IMP.getDirectory(), Settings.Paths.SCHEMATICS);
|
2016-02-23 05:11:28 +01:00
|
|
|
if (!parent.exists()) {
|
|
|
|
if (!parent.mkdir()) {
|
|
|
|
throw new RuntimeException("Could not create schematic parent directory");
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
File file = MainUtil.getFile(PS.get().IMP.getDirectory(),
|
2016-06-10 19:00:34 +02:00
|
|
|
Settings.Paths.SCHEMATICS + File.separator + name + (name.endsWith(".schematic") ? "" : ".schematic"));
|
2016-02-23 05:11:28 +01:00
|
|
|
return getSchematic(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a schematic
|
|
|
|
*
|
|
|
|
* @param file to check
|
|
|
|
*
|
|
|
|
* @return schematic if found, else null
|
|
|
|
*/
|
2016-03-29 01:30:55 +02:00
|
|
|
public Schematic getSchematic(File file) {
|
2016-02-23 05:11:28 +01:00
|
|
|
if (!file.exists()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return getSchematic(new FileInputStream(file));
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
|
|
|
|
public Schematic getSchematic(URL url) {
|
2016-02-23 05:11:28 +01:00
|
|
|
try {
|
2016-03-29 01:30:55 +02:00
|
|
|
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
|
|
|
|
InputStream is = Channels.newInputStream(rbc);
|
2016-02-23 05:11:28 +01:00
|
|
|
return getSchematic(is);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
|
|
|
|
public Schematic getSchematic(InputStream is) {
|
2016-02-23 05:11:28 +01:00
|
|
|
if (is == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
2016-03-29 01:30:55 +02:00
|
|
|
NBTInputStream stream = new NBTInputStream(new GZIPInputStream(is));
|
|
|
|
CompoundTag tag = (CompoundTag) stream.readTag(1073741824);
|
2016-02-23 05:11:28 +01:00
|
|
|
is.close();
|
|
|
|
stream.close();
|
|
|
|
return getSchematic(tag);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
PS.debug(is.toString() + " | " + is.getClass().getCanonicalName() + " is not in GZIP format : " + e.getMessage());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
|
|
|
|
public List<String> getSaves(UUID uuid) {
|
|
|
|
StringBuilder rawJSON = new StringBuilder();
|
2016-02-23 05:11:28 +01:00
|
|
|
try {
|
2016-06-10 19:00:34 +02:00
|
|
|
String website = Settings.Web.URL + "list.php?" + uuid.toString();
|
2016-03-29 01:30:55 +02:00
|
|
|
URL url = new URL(website);
|
|
|
|
URLConnection connection = new URL(url.toString()).openConnection();
|
2016-02-23 05:11:28 +01:00
|
|
|
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
|
2016-03-29 01:30:55 +02:00
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
2016-02-23 05:11:28 +01:00
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
rawJSON.append(line);
|
|
|
|
}
|
|
|
|
reader.close();
|
2016-03-29 01:30:55 +02:00
|
|
|
JSONArray array = new JSONArray(rawJSON.toString());
|
|
|
|
List<String> schematics = new ArrayList<>();
|
2016-02-23 05:11:28 +01:00
|
|
|
for (int i = 0; i < array.length(); i++) {
|
2016-03-29 01:30:55 +02:00
|
|
|
String schematic = array.getString(i);
|
2016-02-23 05:11:28 +01:00
|
|
|
schematics.add(schematic);
|
|
|
|
}
|
2016-06-16 09:31:02 +02:00
|
|
|
return schematics;
|
2016-02-23 05:11:28 +01:00
|
|
|
} catch (JSONException | IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
PS.debug("ERROR PARSING: " + rawJSON);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-17 10:11:07 +01:00
|
|
|
public void upload(final CompoundTag tag, UUID uuid, String file, RunnableVal<URL> whenDone) {
|
2016-02-23 05:11:28 +01:00
|
|
|
if (tag == null) {
|
|
|
|
PS.debug("&cCannot save empty tag");
|
2016-03-17 10:11:07 +01:00
|
|
|
TaskManager.runTask(whenDone);
|
|
|
|
return;
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
2016-03-17 10:11:07 +01:00
|
|
|
MainUtil.upload(uuid, file, "schematic", new RunnableVal<OutputStream>() {
|
|
|
|
@Override
|
|
|
|
public void run(OutputStream output) {
|
|
|
|
try {
|
2016-09-14 02:12:44 +02:00
|
|
|
try (GZIPOutputStream gzip = new GZIPOutputStream(output, true)) {
|
|
|
|
try (NBTOutputStream nos = new NBTOutputStream(gzip)) {
|
|
|
|
nos.writeTag(tag);
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 10:11:07 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
2016-03-17 10:11:07 +01:00
|
|
|
}, whenDone);
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-29 01:30:55 +02:00
|
|
|
* Saves a schematic to a file path.
|
2016-02-23 05:11:28 +01:00
|
|
|
*
|
|
|
|
* @param tag to save
|
|
|
|
* @param path to save in
|
|
|
|
*
|
|
|
|
* @return true if succeeded
|
|
|
|
*/
|
2016-03-29 01:30:55 +02:00
|
|
|
public boolean save(CompoundTag tag, String path) {
|
2016-02-23 05:11:28 +01:00
|
|
|
if (tag == null) {
|
|
|
|
PS.debug("&cCannot save empty tag");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
2016-03-29 01:30:55 +02:00
|
|
|
File tmp = MainUtil.getFile(PS.get().IMP.getDirectory(), path);
|
2016-02-23 05:11:28 +01:00
|
|
|
tmp.getParentFile().mkdirs();
|
2016-03-07 09:37:53 +01:00
|
|
|
try (OutputStream stream = new FileOutputStream(tmp); NBTOutputStream output = new NBTOutputStream(new GZIPOutputStream(stream))) {
|
2016-02-23 05:11:28 +01:00
|
|
|
output.writeTag(tag);
|
|
|
|
}
|
2016-04-28 22:38:51 +02:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
2016-03-29 01:30:55 +02:00
|
|
|
} catch (IOException e) {
|
2016-02-23 05:11:28 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a compound tag from blocks
|
|
|
|
* - Untested
|
|
|
|
* @param blocks
|
2016-03-29 21:47:59 +02:00
|
|
|
* @param blockData
|
2016-03-29 01:30:55 +02:00
|
|
|
* @param dimension
|
2016-02-23 05:11:28 +01:00
|
|
|
* @return
|
|
|
|
*/
|
2016-03-29 21:47:59 +02:00
|
|
|
public CompoundTag createTag(byte[] blocks, byte[] blockData, Dimension dimension) {
|
2016-03-29 01:30:55 +02:00
|
|
|
HashMap<String, Tag> schematic = new HashMap<>();
|
|
|
|
schematic.put("Width", new ShortTag("Width", (short) dimension.getX()));
|
|
|
|
schematic.put("Length", new ShortTag("Length", (short) dimension.getZ()));
|
|
|
|
schematic.put("Height", new ShortTag("Height", (short) dimension.getY()));
|
2016-02-23 05:11:28 +01:00
|
|
|
schematic.put("Materials", new StringTag("Materials", "Alpha"));
|
|
|
|
schematic.put("WEOriginX", new IntTag("WEOriginX", 0));
|
|
|
|
schematic.put("WEOriginY", new IntTag("WEOriginY", 0));
|
|
|
|
schematic.put("WEOriginZ", new IntTag("WEOriginZ", 0));
|
|
|
|
schematic.put("WEOffsetX", new IntTag("WEOffsetX", 0));
|
|
|
|
schematic.put("WEOffsetY", new IntTag("WEOffsetY", 0));
|
|
|
|
schematic.put("WEOffsetZ", new IntTag("WEOffsetZ", 0));
|
|
|
|
schematic.put("Blocks", new ByteArrayTag("Blocks", blocks));
|
2016-03-29 21:47:59 +02:00
|
|
|
schematic.put("Data", new ByteArrayTag("Data", blockData));
|
2016-02-23 05:11:28 +01:00
|
|
|
schematic.put("Entities", new ListTag("Entities", CompoundTag.class, new ArrayList<Tag>()));
|
|
|
|
schematic.put("TileEntities", new ListTag("TileEntities", CompoundTag.class, new ArrayList<Tag>()));
|
|
|
|
return new CompoundTag("Schematic", schematic);
|
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
|
|
|
|
public abstract void getCompoundTag(String world, Set<RegionWrapper> regions, RunnableVal<CompoundTag> whenDone);
|
2016-02-23 05:11:28 +01:00
|
|
|
|
2016-02-29 16:01:52 +01:00
|
|
|
public void getCompoundTag(final Plot plot, final RunnableVal<CompoundTag> whenDone) {
|
2017-03-23 01:10:29 +01:00
|
|
|
getCompoundTag(plot.getWorldName(), plot.getRegions(), new RunnableVal<CompoundTag>() {
|
2016-02-29 16:01:52 +01:00
|
|
|
@Override
|
|
|
|
public void run(CompoundTag value) {
|
2016-03-14 07:18:04 +01:00
|
|
|
if (!plot.getFlags().isEmpty()) {
|
2016-02-29 16:01:52 +01:00
|
|
|
HashMap<String, Tag> flagMap = new HashMap<>();
|
2016-04-19 22:59:10 +02:00
|
|
|
for (Map.Entry<Flag<?>, Object> entry : plot.getFlags().entrySet()) {
|
|
|
|
String key = entry.getKey().getName();
|
|
|
|
flagMap.put(key, new StringTag(key, entry.getKey().valueToString(entry.getValue())));
|
2016-02-29 16:01:52 +01:00
|
|
|
}
|
|
|
|
CompoundTag tag = new CompoundTag("Flags", flagMap);
|
2016-03-14 07:18:04 +01:00
|
|
|
HashMap<String, Tag> map = new HashMap<>(value.getValue());
|
2016-02-29 16:01:52 +01:00
|
|
|
map.put("Flags", tag);
|
|
|
|
value.setValue(map);
|
|
|
|
}
|
2016-03-07 07:34:57 +01:00
|
|
|
whenDone.run(value);
|
2016-02-29 16:01:52 +01:00
|
|
|
}
|
|
|
|
});
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-29 01:30:55 +02:00
|
|
|
* Schematic Dimensions.
|
2016-02-23 05:11:28 +01:00
|
|
|
*/
|
|
|
|
public static class Dimension {
|
|
|
|
|
|
|
|
private final int x;
|
|
|
|
private final int y;
|
|
|
|
private final int z;
|
|
|
|
|
2016-03-29 01:30:55 +02:00
|
|
|
public Dimension(int x, int y, int z) {
|
2016-02-23 05:11:28 +01:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.z = z;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getX() {
|
2016-03-29 01:30:55 +02:00
|
|
|
return this.x;
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getY() {
|
2016-03-29 01:30:55 +02:00
|
|
|
return this.y;
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getZ() {
|
2016-03-29 01:30:55 +02:00
|
|
|
return this.z;
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schematic Class
|
|
|
|
*
|
|
|
|
|
|
|
|
*/
|
|
|
|
public class Schematic {
|
|
|
|
// Lossy but fast
|
|
|
|
private final short[] ids;
|
|
|
|
private final byte[] datas;
|
|
|
|
private final Dimension schematicDimension;
|
2016-03-14 07:18:04 +01:00
|
|
|
private Map<String, Tag> flags;
|
2016-03-31 19:23:26 +02:00
|
|
|
private HashMap<BlockLoc, CompoundTag> tiles;
|
2016-02-23 05:11:28 +01:00
|
|
|
|
2016-03-29 01:30:55 +02:00
|
|
|
public Schematic(short[] i, byte[] b, Dimension d, Map<String, Tag> flags) {
|
|
|
|
this.ids = i;
|
|
|
|
this.datas = b;
|
|
|
|
this.schematicDimension = d;
|
2016-02-29 16:01:52 +01:00
|
|
|
setFlags(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Tag> getFlags() {
|
2016-03-29 01:30:55 +02:00
|
|
|
return this.flags;
|
2016-02-29 16:01:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setFlags(Map<String, Tag> flags) {
|
|
|
|
this.flags = flags == null ? new HashMap<String, Tag>() : flags;
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-31 19:23:26 +02:00
|
|
|
* Add a tile entity
|
|
|
|
* @param loc
|
|
|
|
* @param tag
|
2016-02-23 05:11:28 +01:00
|
|
|
*/
|
2016-03-31 19:23:26 +02:00
|
|
|
public void addTile(BlockLoc loc, CompoundTag tag) {
|
|
|
|
if (this.tiles == null) {
|
|
|
|
this.tiles = new HashMap<>();
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
2016-03-31 19:23:26 +02:00
|
|
|
this.tiles.put(loc, tag);
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-31 19:23:26 +02:00
|
|
|
* Get the tile entities
|
|
|
|
* @return Map of block location to tag
|
2016-02-23 05:11:28 +01:00
|
|
|
*/
|
2016-03-31 19:23:26 +02:00
|
|
|
public HashMap<BlockLoc, CompoundTag> getTiles() {
|
2016-04-05 18:37:11 +02:00
|
|
|
return this.tiles == null ? new HashMap<BlockLoc, CompoundTag>() : this.tiles;
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-29 01:30:55 +02:00
|
|
|
* Get the schematic dimensions.
|
2016-02-23 05:11:28 +01:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Dimension getSchematicDimension() {
|
2016-03-29 01:30:55 +02:00
|
|
|
return this.schematicDimension;
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-29 01:30:55 +02:00
|
|
|
* Get the block type array.
|
2016-02-23 05:11:28 +01:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public short[] getIds() {
|
2016-03-29 01:30:55 +02:00
|
|
|
return this.ids;
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-29 01:30:55 +02:00
|
|
|
* Get the block data array.
|
2016-02-23 05:11:28 +01:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public byte[] getDatas() {
|
2016-03-29 01:30:55 +02:00
|
|
|
return this.datas;
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Schematic copySection(RegionWrapper region) {
|
|
|
|
|
|
|
|
int x1 = region.minX;
|
|
|
|
int x2 = region.maxX;
|
|
|
|
|
|
|
|
int z1 = region.minZ;
|
|
|
|
int z2 = region.maxZ;
|
|
|
|
|
|
|
|
int y1 = region.minY;
|
|
|
|
int y2 = Math.min(region.maxY, 255);
|
|
|
|
|
|
|
|
int width = x2 - x1 + 1;
|
|
|
|
int length = z2 - z1 + 1;
|
|
|
|
int height = y2 - y1 + 1;
|
|
|
|
|
|
|
|
short[] ids2 = new short[width * length * height];
|
|
|
|
byte[] datas2 = new byte[width * length * height];
|
|
|
|
|
2016-03-29 01:30:55 +02:00
|
|
|
int dx = this.schematicDimension.getX();
|
|
|
|
int dy = this.schematicDimension.getY();
|
|
|
|
int dz = this.schematicDimension.getZ();
|
2016-02-23 05:11:28 +01:00
|
|
|
|
|
|
|
for (int y = y1; y <= y2; y++) {
|
2016-03-21 00:35:40 +01:00
|
|
|
int yy = y >= 0 ? y < dy ? y : y - dy : y + dy;
|
2016-02-23 05:11:28 +01:00
|
|
|
int i1 = yy * dx * dz;
|
|
|
|
int j1 = (y - y1) * width * length;
|
|
|
|
for (int z = z1; z <= z2; z++) {
|
2016-03-21 00:35:40 +01:00
|
|
|
int zz = z >= 0 ? z < dz ? z : z - dz : z + dz;
|
2016-02-23 05:11:28 +01:00
|
|
|
int i2 = i1 + zz * dx;
|
|
|
|
int j2 = j1 + (z - z1) * width;
|
|
|
|
for (int x = x1; x <= x2; x++) {
|
2016-03-21 00:35:40 +01:00
|
|
|
int xx = x >= 0 ? x < dx ? x : x - dx : x + dx;
|
2016-02-23 05:11:28 +01:00
|
|
|
int i3 = i2 + xx;
|
|
|
|
int j3 = j2 + (x - x1);
|
2016-03-29 01:30:55 +02:00
|
|
|
ids2[j3] = this.ids[i3];
|
|
|
|
datas2[j3] = this.datas[i3];
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-29 16:01:52 +01:00
|
|
|
return new Schematic(ids2, datas2, new Dimension(width, height, length), null);
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 01:30:55 +02:00
|
|
|
public void save(File file) {
|
|
|
|
byte[] ids2 = new byte[this.ids.length];
|
|
|
|
for (int i = 0; i < this.ids.length; i++) {
|
|
|
|
ids2[i] = (byte) this.ids[i];
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
2016-03-29 01:30:55 +02:00
|
|
|
CompoundTag tag = createTag(ids2, this.datas, this.schematicDimension);
|
2016-03-11 05:33:18 +01:00
|
|
|
SchematicHandler.this.save(tag, file.getAbsolutePath());
|
2016-02-23 05:11:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|