Fast async schematic exporting / uploading

This commit is contained in:
boy0001 2015-07-21 22:28:03 +10:00
parent bf7616eef0
commit 8c4e1571e8
2 changed files with 299 additions and 215 deletions

View File

@ -27,6 +27,7 @@ import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
@ -53,36 +54,32 @@ import com.intellectualcrafters.plot.object.schematic.StateWrapper;
public class BukkitSchematicHandler extends SchematicHandler {
@Override
public void getCompoundTag(final String world, final Location pos1, final Location pos2, RunnableVal<CompoundTag> whenDone) {
public void getCompoundTag(final String world, final Location pos1, final Location pos2, final RunnableVal<CompoundTag> whenDone) {
// create schematic one chunk at a time
// load chunk sync
// get blocks async
// add to schematic async
// save final async
// async
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
// Main positions
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 int sy = pos1.getY();
final int ey = pos2.getY();
int i = 0;
int j = 0;
try {
for (i = (pos1.getX() / 16) * 16; i < (16 + ((pos2.getX() / 16) * 16)); i += 16) {
for (j = (pos1.getZ() / 16) * 16; j < (16 + ((pos2.getZ() / 16) * 16)); j += 16) {
boolean result = ChunkManager.manager.loadChunk(world, new ChunkLoc(i, j));
if (!result) {
PS.log("&cIllegal selection. Cannot save non-existent chunk at " + (i / 16) + ", " + (j / 16));
whenDone.run();
return;
}
}
}
} catch (final Exception e) {
PS.log("&cIllegal selection. Cannot save corrupt chunk at " + (i / 16) + ", " + (j / 16));
whenDone.run();
return;
}
final int width = (pos2.getX() - pos1.getX()) + 1;
final int height = (pos2.getY() - pos1.getY()) + 1;
final int length = (pos2.getZ() - pos1.getZ()) + 1;
// Main Schematic tag
final HashMap<String, Tag> schematic = new HashMap<>();
schematic.put("Width", new ShortTag("Width", (short) width));
schematic.put("Length", new ShortTag("Length", (short) length));
schematic.put("Height", new ShortTag("Height", (short) height));
@ -93,29 +90,64 @@ public class BukkitSchematicHandler extends SchematicHandler {
schematic.put("WEOffsetX", new IntTag("WEOffsetX", 0));
schematic.put("WEOffsetY", new IntTag("WEOffsetY", 0));
schematic.put("WEOffsetZ", new IntTag("WEOffsetZ", 0));
// Arrays of data types
final List<Tag> tileEntities = new ArrayList<Tag>();
final byte[] blocks = new byte[width * height * length];
byte[] addBlocks = null;
final byte[] blockData = new byte[width * height * length];
final int sx = pos1.getX();
pos2.getX();
final int sz = pos1.getZ();
pos2.getZ();
final int sy = pos1.getY();
pos2.getY();
List<Tag> tileEntities = new ArrayList<Tag>();
World worldObj = Bukkit.getWorld(world);
// Generate list of chunks
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));
}
}
final World worldObj = Bukkit.getWorld(world);
// Main thread
TaskManager.runTask(new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
while (chunks.size() > 0 && System.currentTimeMillis() - start < 20) {
// save schematics
ChunkLoc chunk = chunks.remove(0);
for (int y = 0; y < height; y++) {
int i1 = (y * width * length);
int syy = sy + y;
for (int z = 0; z < length; z++) {
int i2 = i1 + (z * width);
int szz = sz + z;
for (int x = 0; x < width; x++) {
final int index = i2 + x;
Block block = worldObj.getBlockAt(sx + x, syy, szz);
Chunk bc = worldObj.getChunkAt(chunk.x, chunk.z);
if (!bc.load(false)) {
continue;
}
int X = chunk.x;
int Z = chunk.z;
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;
}
for (int y = sy; y <= Math.min(255, ey); y++) {
int ry = y - sy;
int i1 = (ry * width * length);
for (int z = zzb; z <= zzt; z++) {
int rz = z - p1z;
int i2 = i1 + (rz * width);
for (int x = xxb; x <= xxt; x++) {
int rx = x - p1x;
final int index = i2 + rx;
Block block = worldObj.getBlockAt(x, y, z);
int id = block.getTypeId();
switch(id) {
case 0:
@ -249,30 +281,54 @@ public class BukkitSchematicHandler extends SchematicHandler {
blockData[index] = block.getData();
}
}
if (id > 255) {
if (addBlocks == null) {
addBlocks = new byte[(blocks.length >> 1) + 1];
}
addBlocks[index >> 1] = (byte) (((index & 1) == 0) ? (addBlocks[index >> 1] & 0xF0) | ((id >> 8) & 0xF) : (addBlocks[index >> 1] & 0xF) | (((id >> 8) & 0xF) << 4));
}
// For optimization reasons, we are not supporting custom data types
// Especially since the most likely reason beyond this range is modded servers in which the blocks have NBT
// if (id > 255) {
// if (addBlocks == null) {
// addBlocks = new byte[(blocks.length >> 1) + 1];
// }
// addBlocks[index >> 1] = (byte) (((index & 1) == 0) ? (addBlocks[index >> 1] & 0xF0) | ((id >> 8) & 0xF) : (addBlocks[index >> 1] & 0xF) | (((id >> 8) & 0xF) << 4));
// }
blocks[index] = (byte) id;
}
}
}
}
if (chunks.size() != 0) {
TaskManager.runTaskLater(this, 1);
}
else {
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
schematic.put("Blocks", new ByteArrayTag("Blocks", blocks));
schematic.put("Data", new ByteArrayTag("Data", blockData));
schematic.put("Entities", new ListTag("Entities", CompoundTag.class, new ArrayList<Tag>()));
schematic.put("TileEntities", new ListTag("TileEntities", CompoundTag.class, tileEntities));
if (addBlocks != null) {
schematic.put("AddBlocks", new ByteArrayTag("AddBlocks", addBlocks));
}
whenDone.value = new CompoundTag("Schematic", schematic);
whenDone.run();
TaskManager.runTask(whenDone);
System.gc();
System.gc();
}
});
}
}
});
}
});
// end async
// create schematic one chunk at a time
// load chunk sync
// get blocks async
// add to schematic async
// save final async
}

View File

@ -1,16 +1,20 @@
package com.intellectualcrafters.plot.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@ -87,7 +91,13 @@ public abstract class SchematicHandler {
else {
directory = outputDir.getPath();
}
if (PS.get().worldEdit != null) {
Location top = plot.getTop();
Location bot = plot.getBottom();
int area = (1 + top.getX() - bot.getX()) * (1 + top.getZ() - bot.getZ());
if (area > 4096) {
PS.log("The plot is > 64 x 64 - Fast lossy schematic saving will be used");
}
if (area <= 4096 && PS.get().worldEdit != null) {
new WorldEditSchematic().saveSchematic(directory + File.separator + name + ".schematic", plot.world, plot.id);
}
else {
@ -343,6 +353,24 @@ public abstract class SchematicHandler {
nos.close();
output.close();
}
// try (Reader response = new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)) {
// final char[] buffer = new char[256];
// StringBuilder result = new StringBuilder();
// while (true) {
// int r = response.read(buffer);
// if (r < 0) {
// break;
// }
// result.append(buffer, 0, r);
// }
// System.out.print(result.toString() + " | " + result.length());
// if (!result.equals("The file plot.schematic has been uploaded.")) {
// return null;
// }
// }
// catch (Exception e) {
//
// }
int responseCode = ((HttpURLConnection) con).getResponseCode();
if (responseCode != 200) {
return null;