Fix plot auto merge + plot offset + add unmerge alias

This commit is contained in:
boy0001 2015-08-09 03:24:01 +10:00
parent cdcfd8c1b5
commit f6fb7482e1
9 changed files with 86 additions and 40 deletions

View File

@ -33,7 +33,7 @@ import com.plotsquared.general.commands.CommandDeclaration;
@CommandDeclaration( @CommandDeclaration(
command = "unlink", command = "unlink",
aliases = {"u"}, aliases = {"u", "unmerge"},
description = "Unlink a mega-plot", description = "Unlink a mega-plot",
usage = "/plot unlink", usage = "/plot unlink",
requiredType = RequiredType.NONE, requiredType = RequiredType.NONE,

View File

@ -337,7 +337,7 @@ public interface AbstractDB {
*/ */
void getComments(final Plot plot, final String inbox, RunnableVal whenDone); void getComments(final Plot plot, final String inbox, RunnableVal whenDone);
void createPlotAndSettings(Plot plot); void createPlotAndSettings(Plot plot, Runnable whenDone);
void createCluster(PlotCluster cluster); void createCluster(PlotCluster cluster);

View File

@ -127,11 +127,11 @@ public class DBFunc {
* *
* @param plot Plot to create * @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) { if (plot.temp == -1) {
return; return;
} }
dbManager.createPlotAndSettings(plot); dbManager.createPlotAndSettings(plot, whenDone);
} }
/** /**

View File

@ -81,6 +81,23 @@ public class SQLManager implements AbstractDB {
public SQLManager(final Connection c, final String p) { public SQLManager(final Connection c, final String p) {
// Private final // Private final
this.connection = c; 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; this.prefix = p;
// Set timout // Set timout
// setTimout(); // setTimout();
@ -145,10 +162,6 @@ public class SQLManager implements AbstractDB {
@Override @Override
public void run() { public void run() {
try { try {
try {
connection.setAutoCommit(false);
}
catch (SQLException e) {}
// Create the plots // Create the plots
createPlots(myList, new Runnable() { createPlots(myList, new Runnable() {
@Override @Override
@ -206,7 +219,6 @@ public class SQLManager implements AbstractDB {
public void run() { public void run() {
try { try {
connection.commit(); connection.commit();
connection.setAutoCommit(true);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -225,7 +237,6 @@ public class SQLManager implements AbstractDB {
PS.debug("&7[WARN] " + "Failed to set all helpers for plots"); PS.debug("&7[WARN] " + "Failed to set all helpers for plots");
try { try {
connection.commit(); connection.commit();
connection.setAutoCommit(true);
} catch (SQLException e1) { } catch (SQLException e1) {
e1.printStackTrace(); e1.printStackTrace();
} }
@ -237,7 +248,6 @@ public class SQLManager implements AbstractDB {
PS.debug("&7[WARN] " + "Failed to set all helpers for plots"); PS.debug("&7[WARN] " + "Failed to set all helpers for plots");
try { try {
connection.commit(); connection.commit();
connection.setAutoCommit(true);
} catch (SQLException e1) { } catch (SQLException e1) {
e1.printStackTrace(); e1.printStackTrace();
} }
@ -658,9 +668,17 @@ public class SQLManager implements AbstractDB {
} }
}); });
} }
public void commit() {
try {
this.connection.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override @Override
public void createPlotAndSettings(final Plot plot) { public void createPlotAndSettings(final Plot plot, final Runnable whenDone) {
TaskManager.runTaskAsync(new Runnable() { TaskManager.runTaskAsync(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -673,8 +691,17 @@ public class SQLManager implements AbstractDB {
stmt.setString(4, plot.world); stmt.setString(4, plot.world);
stmt.setTimestamp(5, new Timestamp(plot.getTimestamp())); stmt.setTimestamp(5, new Timestamp(plot.getTimestamp()));
stmt.executeUpdate(); stmt.executeUpdate();
ResultSet keys = stmt.getGeneratedKeys();
int id = -1;
if (keys.next()) {
plot.temp = keys.getInt(1);
stmt.close();
}
else {
commit();
}
stmt.close(); 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 = SQLManager.this.connection.prepareStatement("INSERT INTO `" + SQLManager.this.prefix + "plot_settings`(`plot_plot_id`) VALUES(" + "?)");
stmt.setInt(1, id); stmt.setInt(1, id);
stmt.executeUpdate(); stmt.executeUpdate();
@ -683,6 +710,7 @@ public class SQLManager implements AbstractDB {
e.printStackTrace(); e.printStackTrace();
PS.debug("&c[ERROR] " + "Failed to save plot " + plot.id); 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; PreparedStatement stmt = null;
try { 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 = 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(1, plot.id.x);
stmt.setInt(2, plot.id.y); stmt.setInt(2, plot.id.y);
@ -838,6 +871,12 @@ public class SQLManager implements AbstractDB {
} }
r.close(); r.close();
stmt.close(); stmt.close();
if (id == Integer.MAX_VALUE || id == 0) {
if (plot.temp > 0) {
return plot.temp;
}
id = 1/0;
}
plot.temp = id; plot.temp = id;
return id; return id;
} catch (final SQLException e) { } catch (final SQLException e) {
@ -845,7 +884,7 @@ public class SQLManager implements AbstractDB {
} }
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
public void updateTables() { public void updateTables() {
if (PS.get().getVersion().equals(PS.get().getLastVersion()) || PS.get().getLastVersion() == null) { if (PS.get().getVersion().equals(PS.get().getLastVersion()) || PS.get().getLastVersion() == null) {
return; 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)"); PS.debug("$1All DB transactions during this session are being validated (This may take a while if corrections need to be made)");
try { try {
connection.commit(); connection.commit();
connection.setAutoCommit(false);
} }
catch (SQLException e) {} catch (SQLException e) {}
ConcurrentHashMap<String, ConcurrentHashMap<PlotId, Plot>> database = getPlots(); ConcurrentHashMap<String, ConcurrentHashMap<PlotId, Plot>> database = getPlots();
ArrayList<Plot> toCreate = new ArrayList<>(); ArrayList<Plot> toCreate = new ArrayList<>();
ArrayList<UUID> toTrusted = new ArrayList<>();
ArrayList<Plot> toMember = new ArrayList<>();
ArrayList<Plot> toDenied = new ArrayList<>();
for (Plot plot : PS.get().getPlotsRaw()) { for (Plot plot : PS.get().getPlotsRaw()) {
if (plot.temp == -1) { if (plot.temp == -1) {
@ -2393,7 +2428,6 @@ public class SQLManager implements AbstractDB {
PS.debug("$4Done!"); PS.debug("$4Done!");
try { try {
connection.commit(); connection.commit();
connection.setAutoCommit(true);
} }
catch (SQLException e) {} catch (SQLException e) {}
} }

View File

@ -90,12 +90,12 @@ public class HybridPlotManager extends ClassicPlotManager {
public void createSchemAbs(HybridPlotWorld hpw, Location pos1, Location pos2, int height, boolean clear) { public void createSchemAbs(HybridPlotWorld hpw, Location pos1, Location pos2, int height, boolean clear) {
final int size = hpw.SIZE; final int size = hpw.SIZE;
for (int x = pos1.getX(); x <= pos2.getX(); x++) { 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) { if (absX < 0) {
absX += size; absX += size;
} }
for (int z = pos1.getZ(); z <= pos2.getZ(); z++) { 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) { if (absZ < 0) {
absZ += size; absZ += size;
} }

View File

@ -59,7 +59,7 @@ public class HybridPlotWorld extends ClassicPlotWorld {
@Override @Override
public void loadConfiguration(final ConfigurationSection config) { public void loadConfiguration(final ConfigurationSection config) {
super.loadConfiguration(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); this.PATH_WIDTH_LOWER = (short) (Math.floor(this.ROAD_WIDTH / 2) - 1);
} else { } else {
this.PATH_WIDTH_LOWER = (short) (Math.floor(this.ROAD_WIDTH / 2)); 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 Schematic schem3 = SchematicHandler.manager.getSchematic(schem3Str);
final int shift = (int) this.ROAD_WIDTH / 2; final int shift = (int) this.ROAD_WIDTH / 2;
int oddshift = 0; int oddshift = 0;
if ((this.ROAD_WIDTH % 2) != 0) { if ((this.ROAD_WIDTH & 1) != 0) {
oddshift = 1; oddshift = 1;
} }
if (schem3 != null) { if (schem3 != null) {
@ -184,14 +184,14 @@ public class HybridPlotWorld extends ClassicPlotWorld {
public static byte wrap(byte data, int start) { public static byte wrap(byte data, int start) {
if (data >= start && data < start + 4) { if (data >= start && data < start + 4) {
data = (byte) ((((data - start) + 2) % 4) + start); data = (byte) ((((data - start) + 2) & 3) + start);
} }
return data; return data;
} }
public static byte wrap2(byte data, int start) { public static byte wrap2(byte data, int start) {
if (data >= start && data < start + 2) { if (data >= start && data < start + 2) {
data = (byte) ((((data - start) + 1) % 2) + start); data = (byte) ((((data - start) + 1) & 1) + start);
} }
return data; return data;
} }

View File

@ -53,16 +53,16 @@ public abstract class SquarePlotManager extends GridPlotManager {
int idx; int idx;
int idz; int idz;
if (x < 0) { if (x < 0) {
idx = (x/size); idx = ((x - 1)/size);
x = size + (x % size); x = size + ((x - 1) % size);
} }
else { else {
idx = (x/size) + 1; idx = (x/size) + 1;
x = (x % size); x = (x % size);
} }
if (z < 0) { if (z < 0) {
idz = (z/size); idz = ((z - 1)/size);
z = size + (z % size); z = size + ((z - 1) % size);
} }
else { else {
idz = (z/size) + 1; idz = (z/size) + 1;
@ -104,16 +104,16 @@ public abstract class SquarePlotManager extends GridPlotManager {
int rx; int rx;
int rz; int rz;
if (x < 0) { if (x < 0) {
dx = (x/size); dx = ((x - 1) / size);
rx = size + (x % size); rx = size + ((x - 1) % size);
} }
else { else {
dx = (x/size) + 1; dx = (x/size) + 1;
rx = (x % size); rx = (x % size);
} }
if (z < 0) { if (z < 0) {
dz = (z/size); dz = ((z - 1)/size);
rz = size + (z % size); rz = size + ((z - 1) % size);
} }
else { else {
dz = (z/size) + 1; dz = (z/size) + 1;

View File

@ -944,14 +944,24 @@ public class MainUtil {
if (MainUtil.worldBorder.containsKey(plot.world)) { if (MainUtil.worldBorder.containsKey(plot.world)) {
updateWorldBorder(plot); updateWorldBorder(plot);
} }
final Plot p = createPlotAbs(uuid, plot); final String w = plot.world;
if (p == null) { 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; return false;
} }
final PlotWorld plotworld = PS.get().getPlotWorld(plot.world); PS.get().updatePlot(p);
if (plotworld.AUTO_MERGE) { DBFunc.createPlotAndSettings(p, new Runnable() {
autoMerge(p, uuid, true); @Override
} public void run() {
final PlotWorld plotworld = PS.get().getPlotWorld(plot.world);
if (plotworld.AUTO_MERGE) {
autoMerge(p, uuid, true);
}
}
});
return true; return true;
} }
@ -969,7 +979,7 @@ public class MainUtil {
return null; return null;
} }
PS.get().updatePlot(p); PS.get().updatePlot(p);
DBFunc.createPlotAndSettings(p); DBFunc.createPlotAndSettings(p, null);
return p; return p;
} }

View File

@ -122,6 +122,8 @@ permissions:
plots.untrust: true plots.untrust: true
plots.undeny: true plots.undeny: true
plots.kick: true plots.kick: true
plots.download: true
plots.save: true
plots.worldedit.bypass: plots.worldedit.bypass:
default: false default: false
plots.gamemode.bypass: plots.gamemode.bypass: