Fix claim

This commit is contained in:
Jesse Boyd 2017-04-17 11:56:10 +10:00
parent 37977f1da4
commit 193948d4fd
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
13 changed files with 147 additions and 92 deletions

View File

@ -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,11 +163,9 @@ 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;
}
}
break;
}
}
@ -199,51 +173,39 @@ public class Auto extends SubCommand {
}
}
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;
}
return null;
}
public void autoClaimSafe(final PlotPlayer player, final PlotArea area, PlotId start, final RunnableVal<Plot> 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) {
final Plot plot = area.getNextFreePlot(player, start);
if (plot == null) {
whenDone.run(null);
return;
}
}
start.recalculateHash();
}
} else {
if (start == null) start = getLastPlotId(area);
while (!area.canClaim(player, start, start)) {
start = getNextPlotId(start, 1);
}
}
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;
plot.owner = player.getUUID();
DBFunc.createPlotSafe(plot, whenDone, new Runnable() {
@Override
public void run() {
autoClaimSafe(player, area, finalStart, whenDone);
autoClaimSafe(player, area, plot.getId(), whenDone);
}
});
return;
}
autoClaimSafe(player, area, start, whenDone);
}
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;
}
return value;
}
}

View File

@ -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 <sqlite file> [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<PlotId, Plot> 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<PlotId, Plot> plotmap = PS.get().plots_tmp.get(areaname);

View File

@ -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",

View File

@ -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"),

View File

@ -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

View File

@ -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;
}

View File

@ -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> T getMeta(String key, T def) {
Object v = getMeta(key);
return v == null ? def : (T) v;
}
/**
* Get the metadata for a key<br>
* <br>
@ -657,6 +663,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) {
for (PlotPlayer pp : plot.getPlayersInPlot()) {

View File

@ -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<br>
* Note: Only accurate for small x,z values (short)

View File

@ -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());
if (isLoaded()) {
return super.teleportPlayer(player);
} else {
C.NOT_LOADED.send(player);
return false;
}
}
@Override

View File

@ -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;
}

View File

@ -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

View File

@ -174,7 +174,6 @@ public class SpongeMain implements IPlotMain {
return;
}
}
System.out.println("Unload " + world);
Sponge.getServer().unloadWorld(world);
}
}

View File

@ -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)