From 1bf621fb4b97b5c9daa4567afd1e59083d206213 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sun, 23 Dec 2018 22:49:43 +0000 Subject: [PATCH] Fix schematic saving (and improve performance slightly) --- .../bukkit/util/BukkitSchematicHandler.java | 51 ++++---- .../plotsquared/plot/commands/Save.java | 52 ++++---- .../plotsquared/plot/util/MainUtil.java | 112 +++++++++--------- .../plot/util/SchematicHandler.java | 14 +-- 4 files changed, 110 insertions(+), 119 deletions(-) diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitSchematicHandler.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitSchematicHandler.java index 5ae07bfba..76462a11e 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitSchematicHandler.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitSchematicHandler.java @@ -78,7 +78,6 @@ public class BukkitSchematicHandler extends SchematicHandler { new ListTag(CompoundTag.class, tileEntities)); whenDone.value = new CompoundTag(schematic); TaskManager.runTask(whenDone); - System.gc(); }); return; } @@ -87,41 +86,40 @@ public class BukkitSchematicHandler extends SchematicHandler { Location pos1 = new Location(world, region.minX, region.minY, region.minZ); Location pos2 = new Location(world, region.maxX, region.maxY, region.maxZ); final int p1x = pos1.getX(); + final int sy = pos1.getY(); final int p1z = pos1.getZ(); final int p2x = pos2.getX(); final int p2z = pos2.getZ(); - final int sy = pos1.getY(); final int ey = pos2.getY(); - Iterator yiter = IntStream.range(sy, ey).iterator(); - TaskManager.runTask(new Runnable() { + Iterator yiter = IntStream.range(sy, ey + 1).iterator(); + final Runnable yTask = new Runnable() { @Override public void run() { - final Runnable yTask = this; long ystart = System.currentTimeMillis(); while (yiter.hasNext() && System.currentTimeMillis() - ystart < 20) { - final int fy = yiter.next(); - Iterator ziter = IntStream.range(p1z, p2z).iterator(); - TaskManager.runTask(new Runnable() { + final int y = yiter.next(); + Iterator ziter = + IntStream.range(p1z, p2z + 1).iterator(); + final Runnable zTask = new Runnable() { @Override public void run() { - final Runnable zTask = this; long zstart = System.currentTimeMillis(); - Iterator xiter = - IntStream.range(p1x, p2x).iterator(); while (ziter.hasNext() && System.currentTimeMillis() - zstart < 20) { - final int fz = ziter.next(); - TaskManager.runTask(new Runnable() { + final int z = ziter.next(); + Iterator xiter = + IntStream.range(p1x, p2x + 1).iterator(); + final Runnable xTask = new Runnable() { @Override public void run() { long xstart = System.currentTimeMillis(); + final int ry = y - sy; + final int rz = z - p1z; while (xiter.hasNext() && System.currentTimeMillis() - xstart < 20) { final int x = xiter.next(); - int rx = x - p1x; - int ry = fy - sy; - int rz = fz - p1z; + final int rx = x - p1x; BlockVector3 point = - BlockVector3.at(x, fy, fz); + BlockVector3.at(x, y, z); BaseBlock block = cuboidRegion.getWorld() .getFullBlock(point); @@ -169,28 +167,27 @@ public class BukkitSchematicHandler extends SchematicHandler { buffer.write(blockId); } if (xiter.hasNext()) { - TaskManager.runTaskLater(this, 1); - } else { - zTask.run(); + this.run(); } } - }); + }; + xTask.run(); } if (ziter.hasNext()) { - TaskManager.runTaskLater(zTask, 1); - } else { - yTask.run(); + this.run(); } } - }); + }; + zTask.run(); } if (yiter.hasNext()) { - TaskManager.runTaskLater(yTask, 1); + TaskManager.runTaskLater(this, 1); } else { regionTask.run(); } } - }); + }; + yTask.run(); } }); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Save.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Save.java index b4722772e..684cc860c 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Save.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Save.java @@ -43,34 +43,32 @@ public class Save extends SubCommand { plot.addRunning(); SchematicHandler.manager.getCompoundTag(plot, new RunnableVal() { @Override public void run(final CompoundTag value) { - TaskManager.runTaskAsync(new Runnable() { - @Override public void run() { - String time = (System.currentTimeMillis() / 1000) + ""; - Location[] corners = plot.getCorners(); - corners[0].setY(0); - corners[1].setY(255); - int size = (corners[1].getX() - corners[0].getX()) + 1; - PlotId id = plot.getId(); - String world = plot.getArea().toString().replaceAll(";", "-") - .replaceAll("[^A-Za-z0-9]", ""); - final String file = - time + '_' + world + '_' + id.x + '_' + id.y + '_' + size; - UUID uuid = player.getUUID(); - SchematicHandler.manager.upload(value, uuid, file, new RunnableVal() { - @Override public void run(URL url) { - plot.removeRunning(); - if (url == null) { - MainUtil.sendMessage(player, C.SAVE_FAILED); - return; - } - MainUtil.sendMessage(player, C.SAVE_SUCCESS); - List schematics = player.getMeta("plot_schematics"); - if (schematics != null) { - schematics.add(file); - } + TaskManager.runTaskAsync(() -> { + String time = (System.currentTimeMillis() / 1000) + ""; + Location[] corners = plot.getCorners(); + corners[0].setY(0); + corners[1].setY(255); + int size = (corners[1].getX() - corners[0].getX()) + 1; + PlotId id = plot.getId(); + String world1 = plot.getArea().toString().replaceAll(";", "-") + .replaceAll("[^A-Za-z0-9]", ""); + final String file = + time + '_' + world1 + '_' + id.x + '_' + id.y + '_' + size; + UUID uuid = player.getUUID(); + SchematicHandler.manager.upload(value, uuid, file, new RunnableVal() { + @Override public void run(URL url) { + plot.removeRunning(); + if (url == null) { + MainUtil.sendMessage(player, C.SAVE_FAILED); + return; } - }); - } + MainUtil.sendMessage(player, C.SAVE_SUCCESS); + List schematics = player.getMeta("plot_schematics"); + if (schematics != null) { + schematics.add(file); + } + } + }); }); } }); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/MainUtil.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/MainUtil.java index eaa9629d3..0480f54c8 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/MainUtil.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/MainUtil.java @@ -114,68 +114,66 @@ public class MainUtil { whenDone.run(); return; } - TaskManager.runTaskAsync(new Runnable() { - @Override public void run() { - try { - String boundary = Long.toHexString(System.currentTimeMillis()); - URLConnection con = new URL(website).openConnection(); - con.setDoOutput(true); - con.setRequestProperty("Content-Type", - "multipart/form-data; boundary=" + boundary); - try (OutputStream output = con.getOutputStream(); - PrintWriter writer = new PrintWriter( - new OutputStreamWriter(output, StandardCharsets.UTF_8), true)) { - String CRLF = "\r\n"; - writer.append("--" + boundary).append(CRLF); - writer.append("Content-Disposition: form-data; name=\"param\"") - .append(CRLF); - writer.append("Content-Type: text/plain; charset=" + StandardCharsets.UTF_8 - .displayName()).append(CRLF); - String param = "value"; - writer.append(CRLF).append(param).append(CRLF).flush(); - writer.append("--" + boundary).append(CRLF); - writer.append( - "Content-Disposition: form-data; name=\"schematicFile\"; filename=\"" - + filename + '"').append(CRLF); - writer.append( - "Content-Type: " + URLConnection.guessContentTypeFromName(filename)) - .append(CRLF); - writer.append("Content-Transfer-Encoding: binary").append(CRLF); - writer.append(CRLF).flush(); - writeTask.value = new AbstractDelegateOutputStream(output) { - @Override public void close() { - } // Don't close - }; - writeTask.run(); - output.flush(); - writer.append(CRLF).flush(); - writer.append("--" + boundary + "--").append(CRLF).flush(); + TaskManager.runTaskAsync(() -> { + try { + String boundary = Long.toHexString(System.currentTimeMillis()); + URLConnection con = new URL(website).openConnection(); + con.setDoOutput(true); + con.setRequestProperty("Content-Type", + "multipart/form-data; boundary=" + boundary); + try (OutputStream output = con.getOutputStream(); + PrintWriter writer = new PrintWriter( + new OutputStreamWriter(output, StandardCharsets.UTF_8), true)) { + String CRLF = "\r\n"; + writer.append("--" + boundary).append(CRLF); + writer.append("Content-Disposition: form-data; name=\"param\"") + .append(CRLF); + writer.append("Content-Type: text/plain; charset=" + StandardCharsets.UTF_8 + .displayName()).append(CRLF); + String param = "value"; + writer.append(CRLF).append(param).append(CRLF).flush(); + writer.append("--" + boundary).append(CRLF); + writer.append( + "Content-Disposition: form-data; name=\"schematicFile\"; filename=\"" + + filename + '"').append(CRLF); + writer.append( + "Content-Type: " + URLConnection.guessContentTypeFromName(filename)) + .append(CRLF); + writer.append("Content-Transfer-Encoding: binary").append(CRLF); + writer.append(CRLF).flush(); + writeTask.value = new AbstractDelegateOutputStream(output) { + @Override public void close() { + } // Don't close + }; + writeTask.run(); + output.flush(); + writer.append(CRLF).flush(); + writer.append("--" + boundary + "--").append(CRLF).flush(); + } + try (Reader response = new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)) { + final char[] buffer = new char[256]; + final StringBuilder result = new StringBuilder(); + while (true) { + final int r = response.read(buffer); + if (r < 0) { + break; + } + result.append(buffer, 0, r); } - // try (Reader response = new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)) { - // final char[] buffer = new char[256]; - // final StringBuilder result = new StringBuilder(); - // while (true) { - // final int r = response.read(buffer); - // if (r < 0) { - // break; - // } - // result.append(buffer, 0, r); - // } - // if (!result.toString().startsWith("Success")) { - // PS.debug(result); - // } - // } catch (IOException e) { - // e.printStackTrace(); - // } - int responseCode = ((HttpURLConnection) con).getResponseCode(); - if (responseCode == 200) { - whenDone.value = url; + if (!result.toString().startsWith("Success")) { + PlotSquared.debug(result); } - TaskManager.runTask(whenDone); } catch (IOException e) { e.printStackTrace(); - TaskManager.runTask(whenDone); } + int responseCode = ((HttpURLConnection) con).getResponseCode(); + if (responseCode == 200) { + whenDone.value = url; + } + TaskManager.runTask(whenDone); + } catch (IOException e) { + e.printStackTrace(); + TaskManager.runTask(whenDone); } }); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java index a49fb8ae7..912703e1a 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java @@ -412,14 +412,11 @@ public abstract class SchematicHandler { } MainUtil.upload(uuid, file, "schematic", new RunnableVal() { @Override public void run(OutputStream output) { - try { - try (GZIPOutputStream gzip = new GZIPOutputStream(output, true)) { - try (NBTOutputStream nos = new NBTOutputStream(gzip)) { - nos.writeNamedTag("Schematic", tag); - } - } - } catch (IOException e) { - e.printStackTrace(); + try (NBTOutputStream nos = new NBTOutputStream( + new GZIPOutputStream(output, true))) { + nos.writeNamedTag("Schematic", tag); + } catch (IOException e1) { + e1.printStackTrace(); } } }, whenDone); @@ -476,6 +473,7 @@ public abstract class SchematicHandler { }); } + public class UnsupportedFormatException extends Exception { /** * Throw with a message.