From f6fb7482e121fc0113518f8e07be6f5b33639578 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Sun, 9 Aug 2015 03:24:01 +1000 Subject: [PATCH] Fix plot auto merge + plot offset + add unmerge alias --- .../plot/commands/Unlink.java | 2 +- .../plot/database/AbstractDB.java | 2 +- .../plot/database/DBFunc.java | 4 +- .../plot/database/SQLManager.java | 64 ++++++++++++++----- .../plot/generator/HybridPlotManager.java | 4 +- .../plot/generator/HybridPlotWorld.java | 8 +-- .../plot/generator/SquarePlotManager.java | 16 ++--- .../plot/util/MainUtil.java | 24 +++++-- src/main/resources/plugin.yml | 2 + 9 files changed, 86 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Unlink.java b/src/main/java/com/intellectualcrafters/plot/commands/Unlink.java index 4a7a52833..ab9008a23 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Unlink.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Unlink.java @@ -33,7 +33,7 @@ import com.plotsquared.general.commands.CommandDeclaration; @CommandDeclaration( command = "unlink", - aliases = {"u"}, + aliases = {"u", "unmerge"}, description = "Unlink a mega-plot", usage = "/plot unlink", requiredType = RequiredType.NONE, diff --git a/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java b/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java index 48f7df5ce..df60249d8 100644 --- a/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java +++ b/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java @@ -337,7 +337,7 @@ public interface AbstractDB { */ void getComments(final Plot plot, final String inbox, RunnableVal whenDone); - void createPlotAndSettings(Plot plot); + void createPlotAndSettings(Plot plot, Runnable whenDone); void createCluster(PlotCluster cluster); diff --git a/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java b/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java index a95c40a55..82c74ea91 100644 --- a/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java +++ b/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java @@ -127,11 +127,11 @@ public class DBFunc { * * @param plot Plot to create */ - public static void createPlotAndSettings(final Plot plot) { + public static void createPlotAndSettings(final Plot plot, Runnable whenDone) { if (plot.temp == -1) { return; } - dbManager.createPlotAndSettings(plot); + dbManager.createPlotAndSettings(plot, whenDone); } /** diff --git a/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index 40f343b2b..9886060ad 100644 --- a/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -81,6 +81,23 @@ public class SQLManager implements AbstractDB { public SQLManager(final Connection c, final String p) { // Private final this.connection = c; + try { + if (this.connection.getAutoCommit()) { + this.connection.setAutoCommit(false); + } + TaskManager.runTaskRepeat(new Runnable() { + @Override + public void run() { + try { + SQLManager.this.connection.commit(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + }, 200); + } catch (SQLException e) { + e.printStackTrace(); + } this.prefix = p; // Set timout // setTimout(); @@ -145,10 +162,6 @@ public class SQLManager implements AbstractDB { @Override public void run() { try { - try { - connection.setAutoCommit(false); - } - catch (SQLException e) {} // Create the plots createPlots(myList, new Runnable() { @Override @@ -206,7 +219,6 @@ public class SQLManager implements AbstractDB { public void run() { try { connection.commit(); - connection.setAutoCommit(true); } catch (SQLException e) { e.printStackTrace(); } @@ -225,7 +237,6 @@ public class SQLManager implements AbstractDB { PS.debug("&7[WARN] " + "Failed to set all helpers for plots"); try { connection.commit(); - connection.setAutoCommit(true); } catch (SQLException e1) { e1.printStackTrace(); } @@ -237,7 +248,6 @@ public class SQLManager implements AbstractDB { PS.debug("&7[WARN] " + "Failed to set all helpers for plots"); try { connection.commit(); - connection.setAutoCommit(true); } catch (SQLException e1) { e1.printStackTrace(); } @@ -658,9 +668,17 @@ public class SQLManager implements AbstractDB { } }); } + + public void commit() { + try { + this.connection.commit(); + } catch (SQLException e) { + e.printStackTrace(); + } + } @Override - public void createPlotAndSettings(final Plot plot) { + public void createPlotAndSettings(final Plot plot, final Runnable whenDone) { TaskManager.runTaskAsync(new Runnable() { @Override public void run() { @@ -673,8 +691,17 @@ public class SQLManager implements AbstractDB { stmt.setString(4, plot.world); stmt.setTimestamp(5, new Timestamp(plot.getTimestamp())); stmt.executeUpdate(); + ResultSet keys = stmt.getGeneratedKeys(); + int id = -1; + if (keys.next()) { + plot.temp = keys.getInt(1); + stmt.close(); + } + else { + commit(); + } stmt.close(); - final int id = getId(plot); + id = getId(plot); stmt = SQLManager.this.connection.prepareStatement("INSERT INTO `" + SQLManager.this.prefix + "plot_settings`(`plot_plot_id`) VALUES(" + "?)"); stmt.setInt(1, id); stmt.executeUpdate(); @@ -683,6 +710,7 @@ public class SQLManager implements AbstractDB { e.printStackTrace(); PS.debug("&c[ERROR] " + "Failed to save plot " + plot.id); } + TaskManager.runTask(whenDone); } }); } @@ -827,6 +855,11 @@ public class SQLManager implements AbstractDB { } PreparedStatement stmt = null; try { + commit(); + commit(); + if (plot.temp > 0) { + return plot.temp; + } stmt = this.connection.prepareStatement("SELECT `id` FROM `" + this.prefix + "plot` WHERE `plot_id_x` = ? AND `plot_id_z` = ? AND world = ? ORDER BY `timestamp` ASC"); stmt.setInt(1, plot.id.x); stmt.setInt(2, plot.id.y); @@ -838,6 +871,12 @@ public class SQLManager implements AbstractDB { } r.close(); stmt.close(); + if (id == Integer.MAX_VALUE || id == 0) { + if (plot.temp > 0) { + return plot.temp; + } + id = 1/0; + } plot.temp = id; return id; } catch (final SQLException e) { @@ -845,7 +884,7 @@ public class SQLManager implements AbstractDB { } return Integer.MAX_VALUE; } - + public void updateTables() { if (PS.get().getVersion().equals(PS.get().getLastVersion()) || PS.get().getLastVersion() == null) { return; @@ -2276,15 +2315,11 @@ public class SQLManager implements AbstractDB { PS.debug("$1All DB transactions during this session are being validated (This may take a while if corrections need to be made)"); try { connection.commit(); - connection.setAutoCommit(false); } catch (SQLException e) {} ConcurrentHashMap> database = getPlots(); ArrayList toCreate = new ArrayList<>(); - ArrayList toTrusted = new ArrayList<>(); - ArrayList toMember = new ArrayList<>(); - ArrayList toDenied = new ArrayList<>(); for (Plot plot : PS.get().getPlotsRaw()) { if (plot.temp == -1) { @@ -2393,7 +2428,6 @@ public class SQLManager implements AbstractDB { PS.debug("$4Done!"); try { connection.commit(); - connection.setAutoCommit(true); } catch (SQLException e) {} } diff --git a/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java b/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java index 5245a8c62..4be2b347c 100644 --- a/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java +++ b/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java @@ -90,12 +90,12 @@ public class HybridPlotManager extends ClassicPlotManager { public void createSchemAbs(HybridPlotWorld hpw, Location pos1, Location pos2, int height, boolean clear) { final int size = hpw.SIZE; for (int x = pos1.getX(); x <= pos2.getX(); x++) { - short absX = (short) ((x - hpw.ROAD_OFFSET_X) % size); + short absX = (short) ((x - hpw.ROAD_OFFSET_X) & (size - 1)); if (absX < 0) { absX += size; } for (int z = pos1.getZ(); z <= pos2.getZ(); z++) { - short absZ = (short) ((z - hpw.ROAD_OFFSET_Z) % size); + short absZ = (short) ((z - hpw.ROAD_OFFSET_Z) & (size - 1)); if (absZ < 0) { absZ += size; } diff --git a/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java b/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java index 0beb6053d..0ac05d585 100644 --- a/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java +++ b/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java @@ -59,7 +59,7 @@ public class HybridPlotWorld extends ClassicPlotWorld { @Override public void loadConfiguration(final ConfigurationSection config) { super.loadConfiguration(config); - if ((this.ROAD_WIDTH % 2) == 0) { + if ((this.ROAD_WIDTH & 1) == 0) { this.PATH_WIDTH_LOWER = (short) (Math.floor(this.ROAD_WIDTH / 2) - 1); } else { this.PATH_WIDTH_LOWER = (short) (Math.floor(this.ROAD_WIDTH / 2)); @@ -83,7 +83,7 @@ public class HybridPlotWorld extends ClassicPlotWorld { final Schematic schem3 = SchematicHandler.manager.getSchematic(schem3Str); final int shift = (int) this.ROAD_WIDTH / 2; int oddshift = 0; - if ((this.ROAD_WIDTH % 2) != 0) { + if ((this.ROAD_WIDTH & 1) != 0) { oddshift = 1; } if (schem3 != null) { @@ -184,14 +184,14 @@ public class HybridPlotWorld extends ClassicPlotWorld { public static byte wrap(byte data, int start) { if (data >= start && data < start + 4) { - data = (byte) ((((data - start) + 2) % 4) + start); + data = (byte) ((((data - start) + 2) & 3) + start); } return data; } public static byte wrap2(byte data, int start) { if (data >= start && data < start + 2) { - data = (byte) ((((data - start) + 1) % 2) + start); + data = (byte) ((((data - start) + 1) & 1) + start); } return data; } diff --git a/src/main/java/com/intellectualcrafters/plot/generator/SquarePlotManager.java b/src/main/java/com/intellectualcrafters/plot/generator/SquarePlotManager.java index b85a39098..a7710ce38 100644 --- a/src/main/java/com/intellectualcrafters/plot/generator/SquarePlotManager.java +++ b/src/main/java/com/intellectualcrafters/plot/generator/SquarePlotManager.java @@ -53,16 +53,16 @@ public abstract class SquarePlotManager extends GridPlotManager { int idx; int idz; if (x < 0) { - idx = (x/size); - x = size + (x % size); + idx = ((x - 1)/size); + x = size + ((x - 1) % size); } else { idx = (x/size) + 1; x = (x % size); } if (z < 0) { - idz = (z/size); - z = size + (z % size); + idz = ((z - 1)/size); + z = size + ((z - 1) % size); } else { idz = (z/size) + 1; @@ -104,16 +104,16 @@ public abstract class SquarePlotManager extends GridPlotManager { int rx; int rz; if (x < 0) { - dx = (x/size); - rx = size + (x % size); + dx = ((x - 1) / size); + rx = size + ((x - 1) % size); } else { dx = (x/size) + 1; rx = (x % size); } if (z < 0) { - dz = (z/size); - rz = size + (z % size); + dz = ((z - 1)/size); + rz = size + ((z - 1) % size); } else { dz = (z/size) + 1; diff --git a/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java b/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java index f4af19420..4560621de 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java +++ b/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java @@ -944,14 +944,24 @@ public class MainUtil { if (MainUtil.worldBorder.containsKey(plot.world)) { updateWorldBorder(plot); } - final Plot p = createPlotAbs(uuid, plot); - if (p == null) { + final String w = plot.world; + if (PS.get().getPlot(plot.world, plot.id) != null) { + return true; + } + final Plot p = new Plot(w, plot.id, uuid); + if (p.owner == null) { return false; } - final PlotWorld plotworld = PS.get().getPlotWorld(plot.world); - if (plotworld.AUTO_MERGE) { - autoMerge(p, uuid, true); - } + PS.get().updatePlot(p); + DBFunc.createPlotAndSettings(p, new Runnable() { + @Override + public void run() { + final PlotWorld plotworld = PS.get().getPlotWorld(plot.world); + if (plotworld.AUTO_MERGE) { + autoMerge(p, uuid, true); + } + } + }); return true; } @@ -969,7 +979,7 @@ public class MainUtil { return null; } PS.get().updatePlot(p); - DBFunc.createPlotAndSettings(p); + DBFunc.createPlotAndSettings(p, null); return p; } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index f1ccf40da..f79f5d589 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -122,6 +122,8 @@ permissions: plots.untrust: true plots.undeny: true plots.kick: true + plots.download: true + plots.save: true plots.worldedit.bypass: default: false plots.gamemode.bypass: