From 193948d4fded39ee7fff5fdd48383c7df26ed6fd Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Mon, 17 Apr 2017 11:56:10 +1000 Subject: [PATCH] Fix claim --- .../plot/commands/Auto.java | 108 ++++++------------ .../plot/commands/Database.java | 26 ++++- .../plot/commands/DebugImportWorlds.java | 1 - .../intellectualcrafters/plot/config/C.java | 1 + .../plot/database/SQLManager.java | 6 +- .../plot/object/Plot.java | 2 +- .../plot/object/PlotArea.java | 40 ++++++- .../plot/object/PlotId.java | 29 +++++ .../plot/object/worlds/SinglePlot.java | 9 +- .../plot/object/worlds/SinglePlotArea.java | 4 +- .../object/worlds/SinglePlotAreaManager.java | 10 +- .../com/plotsquared/sponge/SpongeMain.java | 1 - .../sponge/util/SpongeSetupUtils.java | 2 - 13 files changed, 147 insertions(+), 92 deletions(-) diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Auto.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Auto.java index d54241dee..67d4fc007 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Auto.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Auto.java @@ -25,33 +25,9 @@ import com.plotsquared.general.commands.CommandDeclaration; usage = "/plot auto [length,width]") public class Auto extends SubCommand { + @Deprecated public static PlotId getNextPlotId(PlotId id, int step) { - int absX = Math.abs(id.x); - int absY = Math.abs(id.y); - if (absX > absY) { - if (id.x > 0) { - return new PlotId(id.x, id.y + 1); - } else { - return new PlotId(id.x, id.y - 1); - } - } else if (absY > absX) { - if (id.y > 0) { - return new PlotId(id.x - 1, id.y); - } else { - return new PlotId(id.x + 1, id.y); - } - } else { - if (id.x == id.y && id.x > 0) { - return new PlotId(id.x, id.y + step); - } - if (id.x == absX) { - return new PlotId(id.x, id.y + 1); - } - if (id.y == absY) { - return new PlotId(id.x, id.y - 1); - } - return new PlotId(id.x + 1, id.y); - } + return id.getNextId(step); } @Override @@ -176,10 +152,10 @@ public class Auto extends SubCommand { return false; } while (true) { - PlotId start = getNextPlotId(getLastPlotId(plotarea), 1); + PlotId start = plotarea.getMeta("lastPlot", new PlotId(0, 0)).getNextId(1); PlotId end = new PlotId(start.x + size_x - 1, start.y + size_z - 1); - plotarea.setMeta("lastPlot", start); if (plotarea.canClaim(player, start, end)) { + plotarea.setMeta("lastPlot", start); for (int i = start.x; i <= end.x; i++) { for (int j = start.y; j <= end.y; j++) { Plot plot = plotarea.getPlotAbs(new PlotId(i, j)); @@ -187,10 +163,8 @@ public class Auto extends SubCommand { plot.claim(player, teleport, null); } } - if (size_x != 1 || size_z != 1) { - if (!plotarea.mergePlots(MainUtil.getPlotSelectionIds(start, end), true, true)) { - return false; - } + if (!plotarea.mergePlots(MainUtil.getPlotSelectionIds(start, end), true, true)) { + return false; } break; } @@ -199,51 +173,39 @@ public class Auto extends SubCommand { } } - public void autoClaimSafe(final PlotPlayer player, final PlotArea area, PlotId start, final RunnableVal whenDone) { - if (area.TYPE == 2) { - PlotId min = area.getMin(); - PlotId max = area.getMax(); - if (start == null) start = new PlotId(min.x, min.y); - while (!area.canClaim(player, start, start)) { - if (++start.x > max.x) { - start.x = min.x; - if (++start.y > max.y) { - whenDone.run(null); - return; - } - } - start.recalculateHash(); - } - } else { - if (start == null) start = getLastPlotId(area); - while (!area.canClaim(player, start, start)) { - start = getNextPlotId(start, 1); + public static PlotId getNextPlot(PlotId start) { + int plots; + PlotId center; + center = new PlotId(0, 0); + plots = Integer.MAX_VALUE; + PlotId currentId = new PlotId(0, 0); + for (int i = 0; i < plots; i++) { + if (start == null) { + start = new PlotId(0, 0); + } else { + start = start.getNextId(1); } + currentId.x = center.x + start.x; + currentId.y = center.y + start.y; + currentId.recalculateHash(); + return start; } - Plot plot = area.getPlotAbs(start); - if (plot.canClaim(player)) { - plot.owner = player.getUUID(); - final PlotId finalStart = getNextPlotId(start, 1); - area.setMeta("lastPlot", finalStart); - whenDone.value = plot; - DBFunc.createPlotSafe(plot, whenDone, new Runnable() { - @Override - public void run() { - autoClaimSafe(player, area, finalStart, whenDone); - } - }); - return; - } - autoClaimSafe(player, area, start, whenDone); + return null; } - public PlotId getLastPlotId(PlotArea area) { - PlotId value = (PlotId) area.getMeta("lastPlot"); - if (value == null) { - value = new PlotId(0, 0); - area.setMeta("lastPlot", value); - return value; + public void autoClaimSafe(final PlotPlayer player, final PlotArea area, PlotId start, final RunnableVal whenDone) { + final Plot plot = area.getNextFreePlot(player, start); + if (plot == null) { + whenDone.run(null); + return; } - return value; + whenDone.value = plot; + plot.owner = player.getUUID(); + DBFunc.createPlotSafe(plot, whenDone, new Runnable() { + @Override + public void run() { + autoClaimSafe(player, area, plot.getId(), whenDone); + } + }); } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Database.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Database.java index ebd7cfd18..5f9c14108 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Database.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Database.java @@ -9,10 +9,10 @@ import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotArea; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.object.worlds.SinglePlotArea; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.TaskManager; import com.plotsquared.general.commands.CommandDeclaration; - import java.io.File; import java.sql.SQLException; import java.util.ArrayList; @@ -81,7 +81,7 @@ public class Database extends SubCommand { switch (args[0].toLowerCase()) { case "import": if (args.length < 2) { - MainUtil.sendMessage(player, "/plot database import [sqlite file] [prefix]"); + MainUtil.sendMessage(player, "/plot database import [prefix]"); return false; } File file = MainUtil.getFile(PS.get().IMP.getDirectory(), args[1].endsWith(".db") ? args[1] : args[1] + ".db"); @@ -101,11 +101,29 @@ public class Database extends SubCommand { for (Entry entry2 : entry.getValue().entrySet()) { Plot plot = entry2.getValue(); if (pa.getOwnedPlotAbs(plot.getId()) != null) { + if (pa instanceof SinglePlotArea) { + Plot newPlot = pa.getNextFreePlot(null, plot.getId()); + if (newPlot != null) { + PlotId newId = newPlot.getId(); + PlotId id = plot.getId(); + File worldFile = new File(PS.imp().getWorldContainer(), id.toCommaSeparatedString()); + if (worldFile.exists()) { + File newFile = new File(PS.imp().getWorldContainer(), newId.toCommaSeparatedString()); + worldFile.renameTo(newFile); + } + id.x = newId.x; + id.y = newId.y; + id.recalculateHash(); + plot.setArea(pa); + plots.add(plot); + continue; + } + } MainUtil.sendMessage(player, "Skipping duplicate plot: " + plot + " | id=" + plot.temp); continue; } - plot.create(); - plots.add(entry2.getValue()); + plot.setArea(pa); + plots.add(plot); } } else { HashMap plotmap = PS.get().plots_tmp.get(areaname); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugImportWorlds.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugImportWorlds.java index e7618a14e..1fd648271 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugImportWorlds.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugImportWorlds.java @@ -15,7 +15,6 @@ import com.plotsquared.general.commands.Command; import com.plotsquared.general.commands.CommandDeclaration; import java.io.File; import java.util.UUID; -import java.util.zip.DeflaterOutputStream; @CommandDeclaration( command = "debugimportworlds", diff --git a/Core/src/main/java/com/intellectualcrafters/plot/config/C.java b/Core/src/main/java/com/intellectualcrafters/plot/config/C.java index 845f4cdcd..ecb26e7a3 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -484,6 +484,7 @@ public enum C { */ NO_FREE_PLOTS("$2There are no free plots available", "Errors"), NOT_IN_PLOT("$2You're not in a plot", "Errors"), + NOT_LOADED("$2The plot could not be loaded", "Errors"), NOT_IN_CLUSTER("$2You must be within a plot cluster to perform that action", "Errors"), NOT_IN_PLOT_WORLD("$2You're not in a plot area", "Errors"), PLOTWORLD_INCOMPATIBLE("$2The two worlds must be compatible", "Errors"), diff --git a/Core/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/Core/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index 93c84c467..58b073ead 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -120,9 +120,9 @@ public class SQLManager implements AbstractDB { this.CREATE_PLOT = "INSERT INTO `" + this.prefix + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`, `timestamp`) VALUES(?, ?, ?, ?, ?)"; if (mySQL) { - this.CREATE_PLOT_SAFE = "INSERT OR IGNORE INTO `" + this.prefix + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`, `timestamp`) SELECT ?, ?, ?, ?, ? FROM DUAL WHERE NOT EXISTS (SELECT null FROM `" + this.prefix + "plot` WHERE `world` = ? AND `plot_id_x` = ? AND `plot_id_z` = ?)"; + this.CREATE_PLOT_SAFE = "INSERT IGNORE INTO `" + this.prefix + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`, `timestamp`) SELECT ?, ?, ?, ?, ? FROM DUAL WHERE NOT EXISTS (SELECT null FROM `" + this.prefix + "plot` WHERE `world` = ? AND `plot_id_x` = ? AND `plot_id_z` = ?)"; } else { - this.CREATE_PLOT_SAFE = "INSERT OR IGNORE INTO `" + this.prefix + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`, `timestamp`) SELECT ?, ?, ?, ?, ? WHERE NOT EXISTS (SELECT null FROM `" + this.prefix + "plot` WHERE `world` = ? AND `plot_id_x` = ? AND `plot_id_z` = ?)"; + this.CREATE_PLOT_SAFE = "INSERT INTO `" + this.prefix + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`, `timestamp`) SELECT ?, ?, ?, ?, ? WHERE NOT EXISTS (SELECT null FROM `" + this.prefix + "plot` WHERE `world` = ? AND `plot_id_x` = ? AND `plot_id_z` = ?)"; } this.CREATE_CLUSTER = "INSERT INTO `" + this.prefix + "cluster`(`pos1_x`, `pos1_z`, `pos2_x`, `pos2_z`, `owner`, `world`) VALUES(?, ?, ?, ?, ?, ?)"; @@ -1019,7 +1019,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement(SQLManager.this.CREATE_PLOT_SAFE, Statement.KEEP_CURRENT_RESULT ); + return SQLManager.this.connection.prepareStatement(SQLManager.this.CREATE_PLOT_SAFE, Statement.RETURN_GENERATED_KEYS ); } @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java b/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java index cc2ad11e8..3b8cd29b7 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java @@ -2010,7 +2010,7 @@ public class Plot { */ public boolean canClaim(PlotPlayer player) { PlotCluster cluster = this.getCluster(); - if (cluster != null) { + if (cluster != null && player != null) { if (!cluster.isAdded(player.getUUID()) && !Permissions.hasPermission(player, "plots.admin.command.claim")) { return false; } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java index 5e4b649dd..a6254be52 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java @@ -13,12 +13,12 @@ import com.intellectualcrafters.plot.generator.IndependentPlotGenerator; import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.EventUtil; import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.MathMan; import com.intellectualcrafters.plot.util.PlotGameMode; import com.intellectualcrafters.plot.util.StringMan; import com.intellectualcrafters.plot.util.area.QuadMap; import com.intellectualcrafters.plot.util.block.GlobalBlockQueue; import com.intellectualcrafters.plot.util.block.LocalBlockQueue; - import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -31,6 +31,7 @@ import java.util.Map.Entry; import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import javax.annotation.Nullable; /** * @author Jesse Boyd @@ -602,6 +603,11 @@ public abstract class PlotArea { this.meta.put(key, value); } + public T getMeta(String key, T def) { + Object v = getMeta(key); + return v == null ? def : (T) v; + } + /** * Get the metadata for a key
*
@@ -656,6 +662,38 @@ public abstract class PlotArea { } return this.plots.put(plot.getId(), plot) == null; } + + public Plot getNextFreePlot(PlotPlayer player, @Nullable PlotId start) { + int plots; + PlotId center; + PlotId min = getMin(); + PlotId max = getMax(); + if (TYPE == 2) { + center = new PlotId(MathMan.average(min.x, max.x), MathMan.average(min.y, max.y)); + plots = Math.max(max.x - min.x, max.y - min.y) + 1; + if (start != null) start = new PlotId(start.x - center.x, start.y - center.y); + } else { + center = new PlotId(0, 0); + plots = Integer.MAX_VALUE; + } + PlotId currentId = new PlotId(0, 0); + for (int i = 0; i < plots; i++) { + if (start == null) { + start = getMeta("lastPlot", new PlotId(0, 0)); + } else { + start = start.getNextId(1); + } + currentId.x = center.x + start.x; + currentId.y = center.y + start.y; + currentId.recalculateHash(); + Plot plot = getPlotAbs(currentId); + if (plot != null && plot.canClaim(player)) { + setMeta("lastPlot", currentId); + return plot; + } + } + return null; + } public boolean addPlotIfAbsent(Plot plot) { if (this.plots.putIfAbsent(plot.getId(), plot) == null) { diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotId.java b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotId.java index 97d2ac8bd..ec17c18eb 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotId.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotId.java @@ -48,6 +48,35 @@ public class PlotId { return new PlotId(x, y); } + public PlotId getNextId(int step) { + int absX = Math.abs(x); + int absY = Math.abs(y); + if (absX > absY) { + if (x > 0) { + return new PlotId(x, y + 1); + } else { + return new PlotId(x, y - 1); + } + } else if (absY > absX) { + if (y > 0) { + return new PlotId(x - 1, y); + } else { + return new PlotId(x + 1, y); + } + } else { + if (x == y && x > 0) { + return new PlotId(x, y + step); + } + if (x == absX) { + return new PlotId(x, y + 1); + } + if (y == absY) { + return new PlotId(x, y - 1); + } + return new PlotId(x + 1, y); + } + } + /** * Get the PlotId from the HashCode
* Note: Only accurate for small x,z values (short) diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlot.java b/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlot.java index 3c33577d2..d1535d222 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlot.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlot.java @@ -1,5 +1,6 @@ package com.intellectualcrafters.plot.object.worlds; +import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.flag.Flag; import com.intellectualcrafters.plot.object.BlockLoc; import com.intellectualcrafters.plot.object.Plot; @@ -39,8 +40,12 @@ public class SinglePlot extends Plot { } public boolean teleportPlayer(final PlotPlayer player) { - getArea().loadWorld(getId()); - return super.teleportPlayer(player); + if (isLoaded()) { + return super.teleportPlayer(player); + } else { + C.NOT_LOADED.send(player); + return false; + } } @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlotArea.java b/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlotArea.java index f378dd9cf..950fa4b6c 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlotArea.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlotArea.java @@ -122,13 +122,13 @@ public class SinglePlotArea extends GridPlotWorld { return super.addPlotIfAbsent(plot); } - private Plot adapt(Plot p) { + protected Plot adapt(Plot p) { if (p instanceof SinglePlot) { return p; } PlotSettings s = p.getSettings(); p = new SinglePlot(p.getId(), p.owner, p.getTrusted(), p.getMembers(), p.getDenied(), s.alias, s.getPosition(), null, this, s.merged, p.getTimestamp(), p.temp); - s.flags = s.flags; + p.getSettings().flags = s.flags; return p; } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlotAreaManager.java b/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlotAreaManager.java index 4c323df57..50c4b7d39 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlotAreaManager.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlotAreaManager.java @@ -7,7 +7,7 @@ import com.intellectualcrafters.plot.util.ArrayUtil; import com.intellectualcrafters.plot.util.SetupUtils; public class SinglePlotAreaManager extends DefaultPlotAreaManager { - private final SinglePlotArea area; + private SinglePlotArea area; private final SinglePlotArea[] array; private PlotArea[] all; @@ -61,6 +61,12 @@ public class SinglePlotAreaManager extends DefaultPlotAreaManager { return true; } + public void setArea(SinglePlotArea area) { + this.area = area; + array[0] = area; + all = ArrayUtil.concatAll(super.getAllPlotAreas(), array); + } + @Override public PlotArea getApplicablePlotArea(Location location) { PlotArea found = super.getApplicablePlotArea(location); @@ -73,7 +79,7 @@ public class SinglePlotAreaManager extends DefaultPlotAreaManager { public PlotArea getPlotArea(Location location) { PlotArea found = super.getPlotArea(location); if (found != null) return found; - return isWorld(location.getWorld()) ? area : null; + return isWorld(location.getWorld()) || location.getWorld().equals("*") ? area : null; } @Override diff --git a/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java b/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java index ef5bec076..dd537f821 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java @@ -174,7 +174,6 @@ public class SpongeMain implements IPlotMain { return; } } - System.out.println("Unload " + world); Sponge.getServer().unloadWorld(world); } } diff --git a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeSetupUtils.java b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeSetupUtils.java index 78fa67f57..5d8fc2f67 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeSetupUtils.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeSetupUtils.java @@ -162,8 +162,6 @@ public class SpongeSetupUtils extends SetupUtils { // create world with generator GeneratorWrapper gw = SetupUtils.generators.get(object.setupGenerator); WorldGeneratorModifier wgm = (WorldGeneratorModifier) gw.getPlatformGenerator(); - System.out.println("GW " + gw + " | " + wgm); - WorldArchetype settings = WorldArchetype.builder() .loadsOnStartup(true) .keepsSpawnLoaded(true)