diff --git a/src/main/java/com/intellectualcrafters/plot/PS.java b/src/main/java/com/intellectualcrafters/plot/PS.java index f14efc858..943610c31 100644 --- a/src/main/java/com/intellectualcrafters/plot/PS.java +++ b/src/main/java/com/intellectualcrafters/plot/PS.java @@ -417,7 +417,7 @@ public class PS { * @return HashMap containing the world name, and another map with the plot id and the plot object */ @Deprecated - public ConcurrentHashMap> getAllPlotsRaw() { + public Map> getAllPlotsRaw() { return plots; } @@ -927,7 +927,7 @@ public class PS { */ public Set getPlots(final String world, final UUID uuid) { final ArrayList myplots = new ArrayList<>(); - for (final Plot plot : getPlots(world).values()) { + for (final Plot plot : getPlotsInWorld(world)) { if (plot.hasOwner()) { if (PlotHandler.isOwner(plot, uuid)) { myplots.add(plot); @@ -979,16 +979,37 @@ public class PS { * @return HashMap of PlotId to Plot */ @Deprecated - public Map getPlots(final String world) { + public HashMap getPlots(final String world) { + if (plots.containsKey(world)) { + return new HashMap<>(lastMap); + } + return new HashMap<>(); + } + + public Collection getPlotsInWorld(final String world) { + ConcurrentHashMap map = plots.get(world); + if (map == null) { + return new HashSet<>(); + } + return map.values(); + } + + public Plot getPlot(final String world, final PlotId id) { if (world == lastWorld) { - return lastMap; + if (lastMap != null) { + return lastMap.get(id); + } + return null; } lastWorld = world; if (plots.containsKey(world)) { lastMap = plots.get(world); - return lastMap; + if (lastMap != null) { + return lastMap.get(id); + } + return null; } - return new ConcurrentHashMap<>(); + return null; } diff --git a/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java b/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java index 7f6f32822..55a773db1 100644 --- a/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java +++ b/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java @@ -474,7 +474,7 @@ import com.plotsquared.bukkit.util.BukkitUtil; */ public Plot[] getPlots(final World world, final Player plr, final boolean just_owner) { final ArrayList pPlots = new ArrayList<>(); - for (final Plot plot : PS.get().getPlots(world.getName()).values()) { + for (final Plot plot : PS.get().getPlotsInWorld(world.getName())) { if (just_owner) { if ((plot.owner != null) && (plot.owner.equals(UUIDHandler.getUUID(BukkitUtil.getPlayer(plr))))) { pPlots.add(plot); @@ -499,7 +499,7 @@ import com.plotsquared.bukkit.util.BukkitUtil; * @see com.intellectualcrafters.plot.object.Plot */ public Plot[] getPlots(final World world) { - Collection plots = PS.get().getPlots(world.getName()).values(); + Collection plots = PS.get().getPlotsInWorld(world.getName()); return plots.toArray(new Plot[plots.size()]); } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Auto.java b/src/main/java/com/intellectualcrafters/plot/commands/Auto.java index 9bdeda809..3b7c37655 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Auto.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Auto.java @@ -211,7 +211,7 @@ public class Auto extends SubCommand { MainUtil.lastPlot.put(worldname, start); if (lastPlot) { } - if ((PS.get().getPlots(worldname).get(start) != null) && (PS.get().getPlots(worldname).get(start).owner != null)) { + if ((PS.get().getPlot(worldname, start) != null) && (PS.get().getPlot(worldname, start).owner != null)) { continue; } else { lastPlot = false; diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Claim.java b/src/main/java/com/intellectualcrafters/plot/commands/Claim.java index 8a669d28e..0fca54a3e 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Claim.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Claim.java @@ -66,7 +66,7 @@ public class Claim extends SubCommand { } final String world = plot.world; final PlotWorld plotworld = PS.get().getPlotWorld(world); - final Plot plot2 = PS.get().getPlots(world).get(plot.id); + final Plot plot2 = PS.get().getPlot(world, plot.id); if (plotworld.SCHEMATIC_ON_CLAIM) { Schematic sch; if (schematic.equals("")) { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Cluster.java b/src/main/java/com/intellectualcrafters/plot/commands/Cluster.java index 49310113a..c070220c2 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Cluster.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Cluster.java @@ -144,7 +144,7 @@ public class Cluster extends SubCommand { } ClusterManager.clusters.get(world).add(cluster); // Add any existing plots to the current cluster - for (final Plot plot : PS.get().getPlots(plr.getLocation().getWorld()).values()) { + for (final Plot plot : PS.get().getPlotsInWorld(plr.getLocation().getWorld())) { final PlotCluster current = ClusterManager.getCluster(plot); if (cluster.equals(current) && !cluster.isAdded(plot.owner)) { cluster.invited.add(plot.owner); @@ -221,7 +221,7 @@ public class Cluster extends SubCommand { final PlotWorld plotworld = PS.get().getPlotWorld(plr.getLocation().getWorld()); if (plotworld.TYPE == 2) { final ArrayList toRemove = new ArrayList<>(); - for (final Plot plot : PS.get().getPlots(plr.getLocation().getWorld()).values()) { + for (final Plot plot : PS.get().getPlotsInWorld(plr.getLocation().getWorld())) { final PlotCluster other = ClusterManager.getCluster(plot); if (cluster.equals(other)) { toRemove.add(plot); diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Condense.java b/src/main/java/com/intellectualcrafters/plot/commands/Condense.java index 7092f126e..4dd63e88d 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Condense.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Condense.java @@ -76,7 +76,7 @@ public class Condense extends SubCommand { return false; } final int radius = Integer.parseInt(args[2]); - final Collection plots = PS.get().getPlots(worldname).values(); + final Collection plots = PS.get().getPlotsInWorld(worldname); final int size = plots.size(); final int minimum_radius = (int) Math.ceil((Math.sqrt(size) / 2) + 1); if (radius < minimum_radius) { @@ -165,7 +165,7 @@ public class Condense extends SubCommand { return false; } final int radius = Integer.parseInt(args[2]); - final Collection plots = PS.get().getPlots(worldname).values(); + final Collection plots = PS.get().getPlotsInWorld(worldname); final int size = plots.size(); final int minimum_radius = (int) Math.ceil((Math.sqrt(size) / 2) + 1); if (radius < minimum_radius) { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/DebugClaimTest.java b/src/main/java/com/intellectualcrafters/plot/commands/DebugClaimTest.java index 646d7e00d..e98a1f9ff 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/DebugClaimTest.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/DebugClaimTest.java @@ -93,8 +93,7 @@ public class DebugClaimTest extends SubCommand { final ArrayList plots = new ArrayList<>(); for (final PlotId id : MainUtil.getPlotSelectionIds(min, max)) { final Plot plot = MainUtil.getPlot(world, id); - final boolean contains = PS.get().getPlots(world).containsKey(plot.id); - if (contains) { + if (PS.get().getPlot(world, plot.id) != null) { MainUtil.sendMessage(null, " - &cDB Already contains: " + plot.id); continue; } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java b/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java index 9ce4ecb9e..7bad6d7c5 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java @@ -72,6 +72,8 @@ import com.intellectualcrafters.plot.util.StringMan; import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.UUIDHandler; import com.plotsquared.bukkit.util.BukkitHybridUtils; +import com.plotsquared.general.commands.Command; +import com.plotsquared.general.commands.CommandCaller; import com.plotsquared.general.commands.CommandDeclaration; @CommandDeclaration( @@ -86,6 +88,19 @@ public class DebugExec extends SubCommand { private ScriptEngine engine; private Bindings scope; + public DebugExec() { + File file = new File(PS.get().IMP.getDirectory(), "scripts" + File.separator + "start.js"); + if (file.exists()) { + init(); + TaskManager.runTaskLater(new Runnable() { + @Override + public void run() { + onCommand(ConsolePlayer.getConsole(), new String[] {"runasync", "start.js"}); + } + }, 1); + } + } + public void init() { if (engine != null) { return; @@ -350,6 +365,33 @@ public class DebugExec extends SubCommand { MainUtil.sendMessage(player, "Possible sub commands: /plot debugexec <" + StringMan.join(allowed_params, "|") + ">"); return false; } + case "addcmd": { + try { + final String cmd = StringMan.join(Files.readLines(new File(new File(PS.get().IMP.getDirectory() + File.separator + "scripts"), args[1]), StandardCharsets.UTF_8), System.getProperty("line.separator")); + Command subcommand = new Command(args[1].split("\\.")[0]) { + @Override + public boolean onCommand(PlotPlayer plr, String[] args) { + try { + scope.put("PlotPlayer", plr); + scope.put("args", args); + engine.eval(cmd, scope); + return true; + } catch (ScriptException e) { + e.printStackTrace(); + MainUtil.sendMessage(player, C.COMMAND_WENT_WRONG); + return false; + } + } + }; + MainCommand.getInstance().addCommand(subcommand); + return true; + } + catch (Exception e) { + e.printStackTrace(); + MainUtil.sendMessage(player, C.COMMAND_SYNTAX, "/plot debugexec addcmd "); + return false; + } + } case "runasync": { async = true; } @@ -379,7 +421,7 @@ public class DebugExec extends SubCommand { } init(); scope.put("PlotPlayer", player); - System.out.print("> " + script); + PS.debug("> " + script); try { if (async) { final String toExec = script; diff --git a/src/main/java/com/intellectualcrafters/plot/commands/DebugFixFlags.java b/src/main/java/com/intellectualcrafters/plot/commands/DebugFixFlags.java index 86c03de01..26daf282d 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/DebugFixFlags.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/DebugFixFlags.java @@ -60,7 +60,7 @@ public class DebugFixFlags extends SubCommand { return false; } MainUtil.sendMessage(plr, "&8--- &6Starting task &8 ---"); - for (final Plot plot : PS.get().getPlots(world).values()) { + for (final Plot plot : PS.get().getPlotsInWorld(world)) { final HashMap flags = plot.getSettings().flags; Iterator> i = flags.entrySet().iterator(); boolean changed = false; diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Merge.java b/src/main/java/com/intellectualcrafters/plot/commands/Merge.java index 0501b1dff..da8ca3331 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Merge.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Merge.java @@ -156,7 +156,7 @@ public class Merge extends SubCommand { HashSet multiPlots = new HashSet<>(); final UUID u1 = plot.owner; for (final PlotId myid : plots) { - final Plot myplot = PS.get().getPlots(world).get(myid); + final Plot myplot = PS.get().getPlot(world, myid); if (myplot == null || myplot.owner == null) { MainUtil.sendMessage(plr, C.NO_PERM_MERGE.s().replaceAll("%plot%", myid.toString())); return false; diff --git a/src/main/java/com/intellectualcrafters/plot/commands/MusicSubcommand.java b/src/main/java/com/intellectualcrafters/plot/commands/MusicSubcommand.java index f5385fde6..5cc228809 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/MusicSubcommand.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/MusicSubcommand.java @@ -46,6 +46,9 @@ public class MusicSubcommand extends SubCommand { @Override public boolean onCommand(final PlotPlayer player, final String[] args) { + // TODO FIX PLOT MUSIC AS NPE FROM PLAYER.PLAYEFFECT + System.out.print("TODO MUSIC"); + final Location loc = player.getLocation(); final Plot plot = MainUtil.getPlot(loc); if (plot == null) { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Purge.java b/src/main/java/com/intellectualcrafters/plot/commands/Purge.java index 795e362dc..0ec980357 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Purge.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Purge.java @@ -100,7 +100,7 @@ public class Purge extends SubCommand { final PlotId id = getId(arg); if (id != null) { final HashSet ids = new HashSet(); - final int DBid = DBFunc.getId(worldname, id); + final int DBid = DBFunc.getId(MainUtil.getPlot(worldname, id)); if (DBid != Integer.MAX_VALUE) { ids.add(DBid); } @@ -117,7 +117,7 @@ public class Purge extends SubCommand { return finishPurge(length); } if (arg.equals("unknown")) { - final Collection plots = PS.get().getPlots(worldname).values(); + final Collection plots = PS.get().getPlotsInWorld(worldname); final Set ids = new HashSet<>(); for (final Plot plot : plots) { if (plot.owner != null) { @@ -135,7 +135,7 @@ public class Purge extends SubCommand { return finishPurge(length); } if (arg.equals("unowned")) { - final Collection plots = PS.get().getPlots(worldname).values(); + final Collection plots = PS.get().getPlotsInWorld(worldname); final Set ids = new HashSet<>(); for (final Plot plot : plots) { if (plot.owner == null) { diff --git a/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java b/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java index b3fe98a03..1e17c68f9 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java @@ -181,12 +181,11 @@ public class SchematicCmd extends SubCommand { MainUtil.sendMessage(null, "&cNeed world arg. Use &7/plots sch exportall "); return false; } - final Map plotmap = PS.get().getPlots(args[1]); - if ((plotmap == null) || (plotmap.size() == 0)) { + Collection plots = PS.get().getPlotsInWorld(args[1]); + if ((plots.size() == 0)) { MainUtil.sendMessage(plr, "&cInvalid world. Use &7/plots sch exportall "); return false; } - Collection plots = plotmap.values(); boolean result = SchematicHandler.manager.exportAll(plots, null, null, new Runnable() { @Override public void run() { @@ -199,7 +198,7 @@ public class SchematicCmd extends SubCommand { } else { MainUtil.sendMessage(plr, "&3PlotSquared&8->&3Schemaitc&8: &7Mass export has started. This may take a while."); - MainUtil.sendMessage(plr, "&3PlotSquared&8->&3Schemaitc&8: &7Found &c" + plotmap.size() + "&7 plots..."); + MainUtil.sendMessage(plr, "&3PlotSquared&8->&3Schemaitc&8: &7Found &c" + plots.size() + "&7 plots..."); } break; } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Set.java b/src/main/java/com/intellectualcrafters/plot/commands/Set.java index 29f796ef1..e284b48fc 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Set.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Set.java @@ -210,7 +210,7 @@ public class Set extends SubCommand { MainUtil.sendMessage(plr, C.ALIAS_TOO_LONG); return false; } - for (final Plot p : PS.get().getPlots(plr.getLocation().getWorld()).values()) { + for (final Plot p : PS.get().getPlotsInWorld(plr.getLocation().getWorld())) { if (p.getSettings().getAlias().equalsIgnoreCase(alias)) { MainUtil.sendMessage(plr, C.ALIAS_IS_TAKEN); return false; diff --git a/src/main/java/com/intellectualcrafters/plot/commands/SetOwner.java b/src/main/java/com/intellectualcrafters/plot/commands/SetOwner.java index 9ad41c2b3..b6fb89ecd 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/SetOwner.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/SetOwner.java @@ -107,7 +107,7 @@ public class SetOwner extends SubCommand { return false; } for (final PlotId id : plots) { - Plot current = PS.get().getPlots(world).get(id); + Plot current = PS.get().getPlot(world, id); if (current == null) { current = MainUtil.getPlot(world, id); current.owner = uuid; diff --git a/src/main/java/com/intellectualcrafters/plot/commands/TP.java b/src/main/java/com/intellectualcrafters/plot/commands/TP.java index 394f3938c..f0337a99f 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/TP.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/TP.java @@ -98,7 +98,7 @@ public class TP extends SubCommand { } return null; } - for (final Plot p : PS.get().getPlots(world).values()) { + for (final Plot p : PS.get().getPlotsInWorld(world)) { if ((p.getSettings().getAlias().length() > 0) && p.getSettings().getAlias().equalsIgnoreCase(a)) { return p; } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Target.java b/src/main/java/com/intellectualcrafters/plot/commands/Target.java index 7d744f869..8f6440305 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Target.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Target.java @@ -59,7 +59,7 @@ public class Target extends SubCommand { if (StringMan.isEqualIgnoreCaseToAny(args[0], "near", "nearest")) { Plot closest = null; int distance = Integer.MAX_VALUE; - for (Plot plot : PS.get().getPlots(ploc.getWorld()).values()) { + for (Plot plot : PS.get().getPlotsInWorld(ploc.getWorld())) { double current = plot.getBottom().getEuclideanDistanceSquared(ploc); if (current < distance) { distance = (int) current; diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Trim.java b/src/main/java/com/intellectualcrafters/plot/commands/Trim.java index 8ed8ecbfe..d7894974d 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Trim.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Trim.java @@ -116,7 +116,7 @@ public class Trim extends SubCommand { System.currentTimeMillis(); sendMessage("Collecting region data..."); final ArrayList plots = new ArrayList<>(); - plots.addAll(PS.get().getPlots(world).values()); + plots.addAll(PS.get().getPlotsInWorld(world)); final HashSet chunks = new HashSet<>(ChunkManager.manager.getChunkChunks(world)); sendMessage(" - MCA #: " + chunks.size()); sendMessage(" - CHUNKS: " + (chunks.size() * 1024) + " (max)"); diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Visit.java b/src/main/java/com/intellectualcrafters/plot/commands/Visit.java index 2fac3e57f..b00498e6a 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Visit.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Visit.java @@ -71,7 +71,7 @@ public class Visit extends SubCommand { plots = PS.get().sortPlots(PS.get().getPlots(user), SortType.CREATION_DATE, null); } else if (PS.get().isPlotWorld(args[0])) { // do plots by world - plots = PS.get().sortPlots(PS.get().getPlots(args[0]).values(), SortType.CREATION_DATE, null); + plots = PS.get().sortPlots(PS.get().getPlotsInWorld(args[0]), SortType.CREATION_DATE, null); } else { Plot plot = MainUtil.getPlotFromString(plr, args[0], true); diff --git a/src/main/java/com/intellectualcrafters/plot/commands/list.java b/src/main/java/com/intellectualcrafters/plot/commands/list.java index 3c1c2c4ae..1d326ee79 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/list.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/list.java @@ -163,7 +163,7 @@ public class list extends SubCommand { MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.world." + world); return false; } - plots = new ArrayList<>(PS.get().getPlots(world).values()); + plots = new ArrayList<>(PS.get().getPlotsInWorld(world)); break; } case "all": { @@ -268,7 +268,7 @@ public class list extends SubCommand { MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.world." + args[0]); return false; } - plots = new ArrayList<>(PS.get().getPlots(args[0]).values()); + plots = new ArrayList<>(PS.get().getPlotsInWorld(args[0])); break; } UUID uuid = UUIDHandler.getUUID(args[0], null); diff --git a/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java b/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java index 19196276f..48f7df5ce 100644 --- a/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java +++ b/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java @@ -104,7 +104,7 @@ public interface AbstractDB { * * @return Integer = Plot Entry Id */ - int getId(final String world, final PlotId id2); + int getId(final Plot plot); /** * Get the id of a given plot cluster diff --git a/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java b/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java index 934e19f12..a95c40a55 100644 --- a/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java +++ b/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java @@ -56,7 +56,7 @@ public class DBFunc { public static AbstractDB dbManager; public static void movePlot(final Plot originalPlot, final Plot newPlot) { - if (originalPlot.temp != -1 || newPlot.temp != -1) { + if (originalPlot.temp == -1 || newPlot.temp == -1) { return; } dbManager.movePlot(originalPlot, newPlot); @@ -95,7 +95,7 @@ public class DBFunc { * @param uuid New Owner */ public static void setOwner(final Plot plot, final UUID uuid) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.setOwner(plot, uuid); @@ -116,7 +116,7 @@ public class DBFunc { * @param plot Plot to create */ public static void createPlot(final Plot plot) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.createPlot(plot); @@ -128,7 +128,7 @@ public class DBFunc { * @param plot Plot to create */ public static void createPlotAndSettings(final Plot plot) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.createPlotAndSettings(plot); @@ -149,7 +149,7 @@ public class DBFunc { * @param plot Plot to delete */ public static void delete(final Plot plot) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.delete(plot); @@ -166,7 +166,7 @@ public class DBFunc { * @param plot Plot Object */ public static void createPlotSettings(final int id, final Plot plot) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.createPlotSettings(id, plot); @@ -190,8 +190,8 @@ public class DBFunc { * catch(SQLException e) { e.printStackTrace(); } return Integer.MAX_VALUE; * } */ - public static int getId(final String world, final PlotId id2) { - return dbManager.getId(world, id2); + public static int getId(final Plot plot) { + return dbManager.getId(plot); } /** @@ -202,14 +202,14 @@ public class DBFunc { } public static void setMerged(final Plot plot, final boolean[] merged) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.setMerged(plot, merged); } public static void setFlags(final Plot plot, final Collection flags) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.setFlags(plot, flags); @@ -224,7 +224,7 @@ public class DBFunc { * @param alias */ public static void setAlias(final Plot plot, final String alias) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.setAlias(plot, alias); @@ -243,7 +243,7 @@ public class DBFunc { * @param position */ public static void setPosition(final Plot plot, final String position) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.setPosition(plot, position); @@ -263,14 +263,14 @@ public class DBFunc { * @param comment */ public static void removeComment(final Plot plot, final PlotComment comment) { - if (plot != null && plot.temp != -1) { + if (plot != null && plot.temp == -1) { return; } dbManager.removeComment(plot, comment); } public static void clearInbox(final Plot plot, final String inbox) { - if (plot != null && plot.temp != -1) { + if (plot != null && plot.temp == -1) { return; } dbManager.clearInbox(plot, inbox); @@ -281,7 +281,7 @@ public class DBFunc { * @param comment */ public static void setComment(final Plot plot, final PlotComment comment) { - if (plot != null && plot.temp != -1) { + if (plot != null && plot.temp == -1) { return; } dbManager.setComment(plot, comment); @@ -291,7 +291,7 @@ public class DBFunc { * @param plot */ public static void getComments(final Plot plot, final String inbox, RunnableVal whenDone) { - if (plot != null && plot.temp != -1) { + if (plot != null && plot.temp == -1) { return; } dbManager.getComments(plot, inbox, whenDone); @@ -302,7 +302,7 @@ public class DBFunc { * @param uuid */ public static void removeTrusted(final Plot plot, final UUID uuid) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.removeTrusted(plot, uuid); @@ -337,7 +337,7 @@ public class DBFunc { * @param uuid */ public static void removeMember(final Plot plot, final UUID uuid) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.removeMember(plot, uuid); @@ -358,7 +358,7 @@ public class DBFunc { * @param uuid */ public static void setTrusted(final Plot plot, final UUID uuid) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.setTrusted(plot, uuid); @@ -374,7 +374,7 @@ public class DBFunc { * @param uuid */ public static void setMember(final Plot plot, final UUID uuid) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.setMember(plot, uuid); @@ -390,7 +390,7 @@ public class DBFunc { * @param uuid */ public static void removeDenied(final Plot plot, final UUID uuid) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.removeDenied(plot, uuid); @@ -402,21 +402,21 @@ public class DBFunc { * @param uuid */ public static void setDenied(final Plot plot, final UUID uuid) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.setDenied(plot, uuid); } public static HashMap getRatings(final Plot plot) { - if (plot.temp != -1) { + if (plot.temp == -1) { return new HashMap<>(); } return dbManager.getRatings(plot); } public static void setRating(Plot plot, UUID rater, int value) { - if (plot.temp != -1) { + if (plot.temp == -1) { return; } dbManager.setRating(plot, rater, value); diff --git a/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index 6f5536bcb..2ea3d887c 100644 --- a/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -34,6 +34,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; +import java.util.Map.Entry; import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -51,6 +52,7 @@ import com.intellectualcrafters.plot.object.PlotSettings; import com.intellectualcrafters.plot.object.RunnableVal; import com.intellectualcrafters.plot.object.comment.PlotComment; import com.intellectualcrafters.plot.util.ClusterManager; +import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.StringMan; import com.intellectualcrafters.plot.util.TaskManager; @@ -672,7 +674,7 @@ public class SQLManager implements AbstractDB { stmt.setTimestamp(5, new Timestamp(plot.getTimestamp())); stmt.executeUpdate(); stmt.close(); - final int id = getId(plot.world, plot.id); + final int 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(); @@ -760,7 +762,7 @@ public class SQLManager implements AbstractDB { @Override public void run() { PreparedStatement stmt = null; - final int id = getId(plot.world, plot.id); + final int id = getId(plot); try { stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + SQLManager.this.prefix + "plot_settings` WHERE `plot_plot_id` = ?"); stmt.setInt(1, id); @@ -817,15 +819,18 @@ public class SQLManager implements AbstractDB { } }); } - + @Override - public int getId(final String world, final PlotId id2) { + public int getId(Plot plot) { + if (plot.temp > 0) { + return plot.temp; + } PreparedStatement stmt = null; try { 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, id2.x); - stmt.setInt(2, id2.y); - stmt.setString(3, world); + stmt.setInt(1, plot.id.x); + stmt.setInt(2, plot.id.y); + stmt.setString(3, plot.world); final ResultSet r = stmt.executeQuery(); int id = Integer.MAX_VALUE; while (r.next()) { @@ -833,6 +838,7 @@ public class SQLManager implements AbstractDB { } r.close(); stmt.close(); + plot.temp = id; return id; } catch (final SQLException e) { e.printStackTrace(); @@ -1160,7 +1166,7 @@ public class SQLManager implements AbstractDB { } final PreparedStatement stmt = SQLManager.this.connection.prepareStatement("UPDATE `" + SQLManager.this.prefix + "plot_settings` SET `merged` = ? WHERE `plot_plot_id` = ?"); stmt.setInt(1, n); - stmt.setInt(2, getId(plot.world, plot.id)); + stmt.setInt(2, getId(plot)); stmt.execute(); stmt.close(); } catch (final SQLException e) { @@ -1183,8 +1189,8 @@ public class SQLManager implements AbstractDB { */ try { final String world = p1.world; - final int id1 = getId(world, p1.id); - final int id2 = getId(world, p2.id); + final int id1 = getId(p1); + final int id2 = getId(p2); final PlotId pos1 = p1.getId(); final PlotId pos2 = p2.getId(); PreparedStatement stmt = SQLManager.this.connection.prepareStatement("UPDATE `" + SQLManager.this.prefix + "plot` SET `plot_id_x` = ?, `plot_id_z` = ? WHERE `id` = ?"); @@ -1212,7 +1218,7 @@ public class SQLManager implements AbstractDB { @Override public void run() { try { - final int id = getId(original.world, original.id); + final int id = getId(original); final PreparedStatement stmt = SQLManager.this.connection.prepareStatement("UPDATE `" + SQLManager.this.prefix + "plot` SET `plot_id_x` = ?, `plot_id_z` = ? WHERE `id` = ?"); stmt.setInt(1, newPlot.id.x); stmt.setInt(2, newPlot.id.y); @@ -1243,7 +1249,7 @@ public class SQLManager implements AbstractDB { try { final PreparedStatement stmt = SQLManager.this.connection.prepareStatement("UPDATE `" + SQLManager.this.prefix + "plot_settings` SET `flags` = ? WHERE `plot_plot_id` = ?"); stmt.setString(1, flag_string.toString()); - stmt.setInt(2, getId(plot.world, plot.id)); + stmt.setInt(2, getId(plot)); stmt.execute(); stmt.close(); } catch (final SQLException e) { @@ -1282,7 +1288,7 @@ public class SQLManager implements AbstractDB { try { stmt = SQLManager.this.connection.prepareStatement("UPDATE `" + SQLManager.this.prefix + "plot_settings` SET `alias` = ? WHERE `plot_plot_id` = ?"); stmt.setString(1, alias); - stmt.setInt(2, getId(plot.world, plot.id)); + stmt.setInt(2, getId(plot)); stmt.executeUpdate(); stmt.close(); } catch (final SQLException e) { @@ -1375,7 +1381,7 @@ public class SQLManager implements AbstractDB { try { stmt = SQLManager.this.connection.prepareStatement("UPDATE `" + SQLManager.this.prefix + "plot_settings` SET `position` = ? WHERE `plot_plot_id` = ?"); stmt.setString(1, position); - stmt.setInt(2, getId(plot.world, plot.id)); + stmt.setInt(2, getId(plot)); stmt.executeUpdate(); stmt.close(); } catch (final SQLException e) { @@ -1566,7 +1572,7 @@ public class SQLManager implements AbstractDB { public void run() { try { final PreparedStatement statement = SQLManager.this.connection.prepareStatement("DELETE FROM `" + SQLManager.this.prefix + "plot_helpers` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); - statement.setInt(1, getId(plot.world, plot.id)); + statement.setInt(1, getId(plot)); statement.setString(2, uuid.toString()); statement.executeUpdate(); statement.close(); @@ -1585,7 +1591,7 @@ public class SQLManager implements AbstractDB { public void run() { try { final PreparedStatement statement = SQLManager.this.connection.prepareStatement("DELETE FROM `" + SQLManager.this.prefix + "plot_trusted` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); - statement.setInt(1, getId(plot.world, plot.id)); + statement.setInt(1, getId(plot)); statement.setString(2, uuid.toString()); statement.executeUpdate(); statement.close(); @@ -1604,7 +1610,7 @@ public class SQLManager implements AbstractDB { public void run() { try { final PreparedStatement statement = SQLManager.this.connection.prepareStatement("INSERT INTO `" + SQLManager.this.prefix + "plot_helpers` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); - statement.setInt(1, getId(plot.world, plot.id)); + statement.setInt(1, getId(plot)); statement.setString(2, uuid.toString()); statement.executeUpdate(); statement.close(); @@ -1641,7 +1647,7 @@ public class SQLManager implements AbstractDB { public void run() { try { final PreparedStatement statement = SQLManager.this.connection.prepareStatement("INSERT INTO `" + SQLManager.this.prefix + "plot_trusted` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); - statement.setInt(1, getId(plot.world, plot.id)); + statement.setInt(1, getId(plot)); statement.setString(2, uuid.toString()); statement.executeUpdate(); statement.close(); @@ -1660,7 +1666,7 @@ public class SQLManager implements AbstractDB { public void run() { try { final PreparedStatement statement = SQLManager.this.connection.prepareStatement("DELETE FROM `" + SQLManager.this.prefix + "plot_denied` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); - statement.setInt(1, getId(plot.world, plot.id)); + statement.setInt(1, getId(plot)); statement.setString(2, uuid.toString()); statement.executeUpdate(); statement.close(); @@ -1679,7 +1685,7 @@ public class SQLManager implements AbstractDB { public void run() { try { final PreparedStatement statement = SQLManager.this.connection.prepareStatement("INSERT INTO `" + SQLManager.this.prefix + "plot_denied` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); - statement.setInt(1, getId(plot.world, plot.id)); + statement.setInt(1, getId(plot)); statement.setString(2, uuid.toString()); statement.executeUpdate(); statement.close(); @@ -1696,7 +1702,7 @@ public class SQLManager implements AbstractDB { HashMap map = new HashMap(); try { final PreparedStatement statement = this.connection.prepareStatement("SELECT `rating`, `player` FROM `" + this.prefix + "plot_rating` WHERE `plot_plot_id` = ? "); - statement.setInt(1, getId(plot.world, plot.id)); + statement.setInt(1, getId(plot)); final ResultSet set = statement.executeQuery(); while (set.next()) { UUID uuid = UUID.fromString(set.getString("player")); @@ -1719,7 +1725,7 @@ public class SQLManager implements AbstractDB { public void run() { try { final PreparedStatement statement = SQLManager.this.connection.prepareStatement("INSERT INTO `" + SQLManager.this.prefix + "plot_rating` (`plot_plot_id`, `rating`, `player`) VALUES(?,?,?)"); - statement.setInt(1, getId(plot.world, plot.id)); + statement.setInt(1, getId(plot)); statement.setInt(2, value); statement.setString(3, rater.toString()); statement.executeUpdate(); @@ -2267,46 +2273,127 @@ public class SQLManager implements AbstractDB { @Override public void validateAllPlots(Set toValidate) { -// ConcurrentHashMap> database = getPlots(); -// -// ArrayList toCreate = new ArrayList<>(); -// ArrayList toTrust1 = new ArrayList<>(); -// ArrayList toTrust2 = new ArrayList<>(); -// -// for (Plot plot : plots) { -// if (plot.temp) { -// continue; -// } -// ConcurrentHashMap worldplots = database.get(plot.world); -// if (worldplots == null) { -// toCreate.add(plot); -// continue; -// } -// Plot dataplot = worldplots.get(plot.id); -// if (dataplot == null) { -// toCreate.add(plot); -// continue; -// } -// // owner -// if (!plot.owner.equals(dataplot)) { -// toSet.add(plot); -// continue; -// } -// plot. -// // trusted -// if (!plot.getTrusted().equals(dataplot.trusted)) { -// toSet.add(plot); -// continue; -// } -// if (!plot.getMembers().equals(dataplot.members)) { -// toSet.add(plot); -// continue; -// } -// if (!plot.getDenied().equals(dataplot.denied)) { -// toSet.add(plot); -// continue; -// } -// ssettings = plot.getSettings(); -// } + 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) { + continue; + } + ConcurrentHashMap worldplots = database.get(plot.world); + if (worldplots == null) { + PS.debug("&8 - &7Creating plot (1): " + plot); + toCreate.add(plot); + continue; + } + Plot dataplot = worldplots.remove(plot.id); + if (dataplot == null) { + PS.debug("&8 - &7Creating plot (2): " + plot); + toCreate.add(plot); + continue; + } + // owner + if (!plot.owner.equals(dataplot.owner)) { + PS.debug("&8 - &7Setting owner: " + plot +" -> " + MainUtil.getName(plot.owner)); + setOwner(plot, plot.owner); + } + // trusted + if (!plot.getTrusted().equals(dataplot.trusted)) { + HashSet toAdd = (HashSet) plot.getTrusted().clone(); + HashSet toRemove = (HashSet) dataplot.getTrusted().clone(); + toRemove.removeAll(plot.getTrusted()); + toAdd.removeAll(dataplot.getTrusted()); + PS.debug("&8 - &7Correcting " + (toAdd.size() + toRemove.size()) + " trusted for: " + plot); + if (toRemove.size() > 0) { + for (UUID uuid : toRemove) { + removeTrusted(plot, uuid); + } + } + if (toAdd.size() > 0) { + for (UUID uuid : toAdd) { + setTrusted(plot, uuid); + } + } + } + if (!plot.getMembers().equals(dataplot.members)) { + HashSet toAdd = (HashSet) plot.getMembers().clone(); + HashSet toRemove = (HashSet) dataplot.getMembers().clone(); + toRemove.removeAll(plot.getMembers()); + toAdd.removeAll(dataplot.getMembers()); + PS.debug("&8 - &7Correcting " + (toAdd.size() + toRemove.size()) + " members for: " + plot); + if (toRemove.size() > 0) { + for (UUID uuid : toRemove) { + removeMember(plot, uuid); + } + } + if (toAdd.size() > 0) { + for (UUID uuid : toAdd) { + setMember(plot, uuid); + } + } + } + if (!plot.getDenied().equals(dataplot.denied)) { + HashSet toAdd = (HashSet) plot.getDenied().clone(); + HashSet toRemove = (HashSet) dataplot.getDenied().clone(); + toRemove.removeAll(plot.getDenied()); + toAdd.removeAll(dataplot.getDenied()); + PS.debug("&8 - &7Correcting " + (toAdd.size() + toRemove.size()) + " denied for: " + plot); + if (toRemove.size() > 0) { + for (UUID uuid : toRemove) { + removeDenied(plot, uuid); + } + } + if (toAdd.size() > 0) { + for (UUID uuid : toAdd) { + setDenied(plot, uuid); + } + } + } + PlotSettings ps = plot.getSettings(); + PlotSettings ds = dataplot.getSettings(); + boolean[] pm = ps.getMerged(); + boolean[] dm = ds.getMerged(); + if (pm[0] != dm[0] || pm[1] != dm[1] || pm[1] != dm[1] || pm[1] != dm[1]) { + PS.debug("&8 - &7Correcting merge for: " + plot); + setMerged(dataplot, ps.getMerged()); + } + HashMap pf = ps.flags; + HashMap df = ds.flags; + if (pf.size() != 0 && df.size() != 0) { + if (pf.size() != df.size() || !StringMan.isEqual(StringMan.joinOrdered(pf.values(), ","),StringMan.joinOrdered(df.values(), ","))) { + PS.debug("&8 - &7Correcting flags for: " + plot); + setFlags(plot, pf.values()); + } + } + // TODO comments + // TODO ratings + // TODO alias + } + + for (Entry> entry : database.entrySet()) { + ConcurrentHashMap map = entry.getValue(); + if (map.size() > 0) { + for (Entry entry2 : map.entrySet()) { + PS.debug("$1Plot was deleted: " + entry.getValue() + "// TODO implement this when sure safe"); + } + } + } + + PS.debug("$4Done!"); + try { + connection.commit(); + connection.setAutoCommit(true); + } + catch (SQLException e) {} } } diff --git a/src/main/java/com/intellectualcrafters/plot/generator/SquarePlotManager.java b/src/main/java/com/intellectualcrafters/plot/generator/SquarePlotManager.java index ed97769d4..b85a39098 100644 --- a/src/main/java/com/intellectualcrafters/plot/generator/SquarePlotManager.java +++ b/src/main/java/com/intellectualcrafters/plot/generator/SquarePlotManager.java @@ -125,7 +125,7 @@ public abstract class SquarePlotManager extends GridPlotManager { // This means you are in the intersection final Location loc = new Location(plotworld.worldname, x + dpw.ROAD_WIDTH, 0, z + dpw.ROAD_WIDTH); final PlotId id = MainUtil.getPlotAbs(loc); - final Plot plot = PS.get().getPlots(plotworld.worldname).get(id); + final Plot plot = PS.get().getPlot(plotworld.worldname, id); if (plot == null) { return null; } @@ -138,7 +138,7 @@ public abstract class SquarePlotManager extends GridPlotManager { // You are on a road running West to East (yeah, I named the var poorly) final Location loc = new Location(plotworld.worldname, x, 0, z + dpw.ROAD_WIDTH); final PlotId id = MainUtil.getPlotAbs(loc); - final Plot plot = PS.get().getPlots(plotworld.worldname).get(id); + final Plot plot = PS.get().getPlot(plotworld.worldname, id); if (plot == null) { return null; } @@ -151,7 +151,7 @@ public abstract class SquarePlotManager extends GridPlotManager { // This is the road separating an Eastern and Western plot final Location loc = new Location(plotworld.worldname, x + dpw.ROAD_WIDTH, 0, z); final PlotId id = MainUtil.getPlotAbs(loc); - final Plot plot = PS.get().getPlots(plotworld.worldname).get(id); + final Plot plot = PS.get().getPlot(plotworld.worldname, id); if (plot == null) { return null; } @@ -161,7 +161,7 @@ public abstract class SquarePlotManager extends GridPlotManager { return null; } final PlotId id = new PlotId(dx, dz); - final Plot plot = PS.get().getPlots(plotworld.worldname).get(id); + final Plot plot = PS.get().getPlot(plotworld.worldname, id); if (plot == null) { return id; } diff --git a/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java b/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java index 054bd1067..3e1f957f7 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java +++ b/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java @@ -218,7 +218,7 @@ public class ExpireManager { } public static List getOldPlots(final String world) { - final ArrayList plots = new ArrayList<>(PS.get().getPlots(world).values()); + final ArrayList plots = new ArrayList<>(PS.get().getPlotsInWorld(world)); final List toRemove = new ArrayList<>(); Iterator iter = plots.iterator(); while (iter.hasNext()) { diff --git a/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java b/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java index 15f8eae5c..19e257ca1 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java +++ b/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.config.C; @@ -240,7 +241,7 @@ public class MainUtil { } worldname = PS.get().getPlotWorlds().iterator().next(); } - for (Plot p : PS.get().getPlots(worldname).values()) { + for (Plot p : PS.get().getPlotsInWorld(worldname)) { String name = p.getSettings().getAlias(); if (name.length() != 0 && name.equalsIgnoreCase(arg)) { return p; @@ -248,7 +249,7 @@ public class MainUtil { } for (String world : PS.get().getPlotWorlds()) { if (!world.endsWith(worldname)) { - for (Plot p : PS.get().getPlots(world).values()) { + for (Plot p : PS.get().getPlotsInWorld(world)) { String name = p.getSettings().getAlias(); if (name.length() != 0 && name.equalsIgnoreCase(arg)) { return p; @@ -305,7 +306,7 @@ public class MainUtil { final PlotWorld plotworld = PS.get().getPlotWorld(world); manager.startPlotUnlink(plotworld, ids); for (final PlotId id : ids) { - final Plot myplot = PS.get().getPlots(world).get(id); + final Plot myplot = PS.get().getPlot(world, id); if (plot == null) { continue; } @@ -395,8 +396,8 @@ public class MainUtil { public static ArrayList getMaxPlotSelectionIds(final String world, PlotId pos1, PlotId pos2) { - final Plot plot1 = PS.get().getPlots(world).get(pos1); - final Plot plot2 = PS.get().getPlots(world).get(pos2); + final Plot plot1 = PS.get().getPlot(world, pos1); + final Plot plot2 = PS.get().getPlot(world, pos2); if (plot1 != null) { pos1 = getBottomPlot(plot1).id; @@ -435,7 +436,7 @@ public class MainUtil { public static int getPlayerPlotCount(final String world, final PlotPlayer plr) { final UUID uuid = plr.getUUID(); int count = 0; - for (final Plot plot : PS.get().getPlots(world).values()) { + for (final Plot plot : PS.get().getPlotsInWorld(world)) { if (plot.hasOwner() && plot.owner.equals(uuid) && plot.countsTowardsMax) { count++; } @@ -536,7 +537,7 @@ public class MainUtil { if (!worldBorder.containsKey(world)) { worldBorder.put(world, 0); } - for (final Plot plot : PS.get().getPlots(world).values()) { + for (final Plot plot : PS.get().getPlotsInWorld(world)) { updateWorldBorder(plot); } } @@ -641,7 +642,7 @@ public class MainUtil { for (int x = pos1.x; x <= pos2.x; x++) { for (int y = pos1.y; y <= pos2.y; y++) { final PlotId id = new PlotId(x, y); - final Plot plot = PS.get().getPlots(world).get(id); + final Plot plot = PS.get().getPlot(world, id); if (removeRoads) { removeSign(plot); } @@ -652,7 +653,7 @@ public class MainUtil { final boolean lx = x < pos2.x; final boolean ly = y < pos2.y; final PlotId id = new PlotId(x, y); - final Plot plot = PS.get().getPlots(world).get(id); + final Plot plot = PS.get().getPlot(world, id); Plot plot2 = null; if (lx) { if (ly) { @@ -663,7 +664,7 @@ public class MainUtil { } } if (!plot.getSettings().getMerged(1)) { - plot2 = PS.get().getPlots(world).get(new PlotId(x + 1, y)); + plot2 = PS.get().getPlot(world, new PlotId(x + 1, y)); mergePlot(world, plot, plot2, removeRoads); plot.getSettings().setMerged(1, true); plot2.getSettings().setMerged(3, true); @@ -671,7 +672,7 @@ public class MainUtil { } if (ly) { if (!plot.getSettings().getMerged(2)) { - plot2 = PS.get().getPlots(world).get(new PlotId(x, y + 1)); + plot2 = PS.get().getPlot(world, new PlotId(x, y + 1)); mergePlot(world, plot, plot2, removeRoads); plot.getSettings().setMerged(2, true); plot2.getSettings().setMerged(0, true); @@ -684,7 +685,7 @@ public class MainUtil { for (int x = pos1.x; x <= pos2.x; x++) { for (int y = pos1.y; y <= pos2.y; y++) { final PlotId id = new PlotId(x, y); - final Plot plot = PS.get().getPlots(world).get(id); + final Plot plot = PS.get().getPlot(world, id); DBFunc.setMerged(plot, plot.getSettings().getMerged()); } } @@ -901,7 +902,7 @@ public class MainUtil { final PlotId id_min = plots.get(0); final PlotId id_max = plots.get(plots.size() - 1); for (final PlotId myid : plots) { - final Plot myplot = PS.get().getPlots(world).get(myid); + final Plot myplot = PS.get().getPlot(world, myid); if ((myplot == null) || myplot.owner == null || !(myplot.owner.equals(uuid))) { return false; } @@ -958,8 +959,7 @@ public class MainUtil { */ public static Plot createPlotAbs(final UUID uuid, final Plot plot) { final String w = plot.world; - Map plots = PS.get().getPlots(plot.world); - Plot p = plots.get(plot.id); + Plot p = PS.get().getPlot(plot.world, plot.id); if (p != null) { return p; } @@ -1250,7 +1250,7 @@ public class MainUtil { * @return Location top of mega plot */ public static Location getPlotTopLoc(final String world, PlotId id) { - final Plot plot = PS.get().getPlots(world).get(id); + final Plot plot = PS.get().getPlot(world, id); if (plot != null) { id = getTopPlot(plot).id; } @@ -1269,7 +1269,7 @@ public class MainUtil { * @return Location bottom of mega plot */ public static Location getPlotBottomLoc(final String world, PlotId id) { - final Plot plot = PS.get().getPlots(world).get(id); + final Plot plot = PS.get().getPlot(world, id); if (plot != null) { id = getBottomPlot(plot).id; } @@ -1310,8 +1310,8 @@ public class MainUtil { for (int x = pos1.x; x <= pos2.x; x++) { for (int y = pos1.y; y <= pos2.y; y++) { final PlotId id = new PlotId(x, y); - if (PS.get().getPlots(world).get(id) != null) { - if (PS.get().getPlots(world).get(id).owner != null) { + if (PS.get().getPlot(world, id) != null) { + if (PS.get().getPlot(world, id).owner != null) { return false; } } @@ -1321,8 +1321,8 @@ public class MainUtil { } public static boolean swap(final String world, final PlotId current, final PlotId newPlot, final Runnable whenDone) { - Plot p1 = PS.get().getPlots(world).get(current); - Plot p2 = PS.get().getPlots(world).get(newPlot); + Plot p1 = PS.get().getPlot(world, current); + Plot p2 = PS.get().getPlot(world, newPlot); if (p1==null || p2 == null || p1.owner == null || !p1.owner.equals(p2.owner)) { return false; } @@ -1334,20 +1334,21 @@ public class MainUtil { p1.id.y = p2.id.y.intValue(); p2.id.x = temp.x; p2.id.y = temp.y; - PS.get().getPlots(world).remove(p1.id); - PS.get().getPlots(world).remove(p2.id); + Map> raw = PS.get().getAllPlotsRaw(); + raw.get(world).remove(p1.id); + raw.get(world).remove(p2.id); p1.id.recalculateHash(); p2.id.recalculateHash(); - PS.get().getPlots(world).put(p1.id, p1); - PS.get().getPlots(world).put(p2.id, p2); + raw.get(world).put(p1.id, p1); + raw.get(world).put(p2.id, p2); // Swap database DBFunc.dbManager.swapPlots(p2, p1); return true; } public static boolean swapData(final String world, final PlotId current, final PlotId newPlot, final Runnable whenDone) { - Plot p1 = PS.get().getPlots(world).get(current); - Plot p2 = PS.get().getPlots(world).get(newPlot); + Plot p1 = PS.get().getPlot(world, current); + Plot p2 = PS.get().getPlot(world, newPlot); if (p1 == null || p1.owner == null) { if (p2 != null && p2.owner != null) { moveData(p2, p1, whenDone); @@ -1368,12 +1369,13 @@ public class MainUtil { p1.id.y = p2.id.y.intValue(); p2.id.x = temp.x; p2.id.y = temp.y; - PS.get().getPlots(world).remove(p1.id); - PS.get().getPlots(world).remove(p2.id); + Map> raw = PS.get().getAllPlotsRaw(); + raw.get(world).remove(p1.id); + raw.get(world).remove(p2.id); p1.id.recalculateHash(); p2.id.recalculateHash(); - PS.get().getPlots(world).put(p1.id, p1); - PS.get().getPlots(world).put(p2.id, p2); + raw.get(world).put(p1.id, p1); + raw.get(world).put(p2.id, p2); // Swap database DBFunc.dbManager.swapPlots(p2, p1); TaskManager.runTask(whenDone); @@ -1397,12 +1399,13 @@ public class MainUtil { final ArrayList selection = getPlotSelectionIds(pos1.id, pos2.id); for (final PlotId id : selection) { DBFunc.movePlot(getPlot(plot1.world, new PlotId(id.x, id.y)), getPlot(plot2.world, new PlotId(id.x + offset_x, id.y + offset_y))); - final Plot plot = PS.get().getPlots(plot1.world).get(id); - PS.get().getPlots(plot1.world).remove(id); + final Plot plot = PS.get().getPlot(plot1.world, id); + Map> raw = PS.get().getAllPlotsRaw(); + raw.get(plot1.world).remove(id); plot.id.x += offset_x; plot.id.y += offset_y; plot.id.recalculateHash(); - PS.get().getPlots(plot2.world).put(plot.id, plot); + raw.get(plot2.world).put(plot.id, plot); } TaskManager.runTaskLater(whenDone, 1); return true; @@ -1431,12 +1434,13 @@ public class MainUtil { for (final PlotId id : selection) { String worldOriginal = plot1.world; PlotId idOriginal = new PlotId(id.x, id.y); - final Plot plot = PS.get().getPlots(plot1.world).get(id); - PS.get().getPlots(plot1.world).remove(id); + final Plot plot = PS.get().getPlot(plot1.world, id); + Map> raw = PS.get().getAllPlotsRaw(); + raw.get(plot1.world).remove(id); plot.id.x += offset_x; plot.id.y += offset_y; plot.id.recalculateHash(); - PS.get().getPlots(plot2.world).put(plot.id, plot); + raw.get(plot2.world).put(plot.id, plot); DBFunc.movePlot(getPlot(worldOriginal, idOriginal), getPlot(plot2.world, new PlotId(id.x + offset_x, id.y + offset_y))); } ChunkManager.manager.copyRegion(bot1, top, bot2, new Runnable() { @@ -1499,7 +1503,7 @@ public class MainUtil { DBFunc.setDenied(plot, denied); } } - PS.get().getPlots(world).put(plot.id, plot); + PS.get().updatePlot(plot); } ChunkManager.manager.copyRegion(bot1, top, bot2, whenDone); return true; @@ -1628,14 +1632,14 @@ public class MainUtil { public static Plot getBottomPlot(final Plot plot) { if (plot.getSettings().getMerged(0)) { - final Plot p = PS.get().getPlots(plot.world).get(new PlotId(plot.id.x, plot.id.y - 1)); + final Plot p = PS.get().getPlot(plot.world, new PlotId(plot.id.x, plot.id.y - 1)); if (p == null) { return plot; } return getBottomPlot(p); } if (plot.getSettings().getMerged(3)) { - final Plot p = PS.get().getPlots(plot.world).get(new PlotId(plot.id.x - 1, plot.id.y)); + final Plot p = PS.get().getPlot(plot.world, new PlotId(plot.id.x - 1, plot.id.y)); if (p == null) { return plot; } @@ -1646,14 +1650,14 @@ public class MainUtil { public static Plot getTopPlot(final Plot plot) { if (plot.getSettings().getMerged(2)) { - final Plot p = PS.get().getPlots(plot.world).get(new PlotId(plot.id.x, plot.id.y + 1)); + final Plot p = PS.get().getPlot(plot.world, new PlotId(plot.id.x, plot.id.y + 1)); if (p == null) { return plot; } return getTopPlot(p); } if (plot.getSettings().getMerged(1)) { - final Plot p = PS.get().getPlots(plot.world).get(new PlotId(plot.id.x + 1, plot.id.y)); + final Plot p = PS.get().getPlot(plot.world, new PlotId(plot.id.x + 1, plot.id.y)); if (p == null) { return plot; } @@ -1679,8 +1683,9 @@ public class MainUtil { if (id == null) { return null; } - if (PS.get().getPlots(world).containsKey(id)) { - return PS.get().getPlots(world).get(id); + Plot plot = PS.get().getPlot(world, id); + if (plot != null) { + return plot; } return new Plot(world, id, null); } diff --git a/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java b/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java index 4537d7b35..a98c13a60 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java +++ b/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java @@ -693,7 +693,7 @@ public abstract class SchematicHandler { * @return tag */ public void getCompoundTag(final String world, final PlotId id, RunnableVal whenDone) { - if (!PS.get().getPlots(world).containsKey(id)) { + if (PS.get().getPlot(world, id) == null) { whenDone.run(); return; } diff --git a/src/main/java/com/intellectualcrafters/plot/util/StringMan.java b/src/main/java/com/intellectualcrafters/plot/util/StringMan.java index 799df7657..fd1bda24a 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/StringMan.java +++ b/src/main/java/com/intellectualcrafters/plot/util/StringMan.java @@ -1,7 +1,9 @@ package com.intellectualcrafters.plot.util; import java.lang.reflect.Array; +import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -125,6 +127,18 @@ public class StringMan { return join(collection.toArray(), delimiter); } + public static String joinOrdered(Collection collection, String delimiter) { + Object[] array = collection.toArray(); + Arrays.sort(array, new Comparator() { + @Override + public int compare(Object a, Object b) { + return a.hashCode() - b.hashCode(); + } + + }); + return join(array, delimiter); + } + public static String join(Collection collection, char delimiter) { return join(collection.toArray(), delimiter + ""); } diff --git a/src/main/java/com/plotsquared/bukkit/database/plotme/LikePlotMeConverter.java b/src/main/java/com/plotsquared/bukkit/database/plotme/LikePlotMeConverter.java index 46b6a6811..eb69bbfb1 100644 --- a/src/main/java/com/plotsquared/bukkit/database/plotme/LikePlotMeConverter.java +++ b/src/main/java/com/plotsquared/bukkit/database/plotme/LikePlotMeConverter.java @@ -232,7 +232,7 @@ public class LikePlotMeConverter { for (final String world : plots.keySet()) { int duplicate = 0; for (final Plot plot : plots.get(world).values()) { - if (!PS.get().getPlots(world).containsKey(plot.id)) { + if (PS.get().getPlot(world, plot.id) == null) { createdPlots.add(plot); } else { duplicate++; diff --git a/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java b/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java index 321b0e2d3..6979c2532 100644 --- a/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java +++ b/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java @@ -1618,8 +1618,7 @@ public class PlayerEvents extends com.plotsquared.listener.PlotListener implemen WEManager.bypass.remove(pp.getName()); } if (Settings.DELETE_PLOTS_ON_BAN && event.getPlayer().isBanned()) { - final Collection plots = PS.get().getPlots(pp.getName()).values(); - for (final Plot plot : plots) { + for (final Plot plot : PS.get().getPlotsInWorld(pp.getName())) { plot.deletePlot(null); PS.debug(String.format("&cPlot &6%s &cwas deleted + cleared due to &6%s&c getting banned", plot.getId(), event.getPlayer().getName())); } diff --git a/src/main/java/com/plotsquared/bukkit/listeners/worldedit/WEManager.java b/src/main/java/com/plotsquared/bukkit/listeners/worldedit/WEManager.java index 80129c803..925888b8f 100644 --- a/src/main/java/com/plotsquared/bukkit/listeners/worldedit/WEManager.java +++ b/src/main/java/com/plotsquared/bukkit/listeners/worldedit/WEManager.java @@ -26,7 +26,7 @@ public class WEManager { public static HashSet getMask(PlotPlayer player) { HashSet regions = new HashSet<>(); UUID uuid = player.getUUID(); - for (Plot plot : PS.get().getPlots(player.getLocation().getWorld()).values()) { + for (Plot plot : PS.get().getPlotsInWorld(player.getLocation().getWorld())) { if (!plot.getMerged(0) && !plot.getMerged(3)) { if (Settings.WE_ALLOW_HELPER ? plot.isAdded(uuid) : (plot.isOwner(uuid) || plot.getTrusted().contains(uuid))) { Location pos1 = MainUtil.getPlotBottomLoc(plot.world, plot.id).add(1, 0, 1); diff --git a/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java b/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java index 80cf4ba5f..f9679113f 100644 --- a/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java +++ b/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java @@ -222,7 +222,8 @@ public class BukkitPlayer extends PlotPlayer { @Override public void playMusic(Location loc, int id) { - player.playEffect(BukkitUtil.getLocation(loc), Effect.RECORD_PLAY, Material.getMaterial(id)); + System.out.print("RECORD: " + id); + player.playEffect(BukkitUtil.getLocation(loc), Effect.RECORD_PLAY, id); } @Override diff --git a/src/main/java/com/plotsquared/bukkit/util/BukkitPlayerFunctions.java b/src/main/java/com/plotsquared/bukkit/util/BukkitPlayerFunctions.java index 8456b2123..165e83e09 100644 --- a/src/main/java/com/plotsquared/bukkit/util/BukkitPlayerFunctions.java +++ b/src/main/java/com/plotsquared/bukkit/util/BukkitPlayerFunctions.java @@ -87,8 +87,8 @@ public class BukkitPlayerFunctions { } public static ArrayList getMaxPlotSelectionIds(final String world, PlotId pos1, PlotId pos2) { - final Plot plot1 = PS.get().getPlots(world).get(pos1); - final Plot plot2 = PS.get().getPlots(world).get(pos2); + final Plot plot1 = PS.get().getPlot(world, pos1); + final Plot plot2 = PS.get().getPlot(world, pos2); if (plot1 != null) { pos1 = MainUtil.getBottomPlot(plot1).id; } @@ -120,10 +120,7 @@ public class BukkitPlayerFunctions { if (id == null) { return null; } - if (PS.get().getPlots(world).containsKey(id)) { - return PS.get().getPlots(world).get(id); - } - return new Plot(world, id, null); + return MainUtil.getPlot(world, id); } /**