From c0383851a89e826bd712e4b80f307605f996b6c4 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Sun, 19 Oct 2014 19:26:37 +1100 Subject: [PATCH] Added plot purge for plot ID --- .../src/com/intellectualcrafters/plot/C.java | 2 +- .../intellectualcrafters/plot/PlotMain.java | 27 ++++++-- .../plot/commands/Claim.java | 1 - .../plot/commands/Purge.java | 53 +++++++++++---- .../plot/database/DBFunc.java | 66 +++++++++++++++++++ 5 files changed, 130 insertions(+), 19 deletions(-) diff --git a/PlotSquared/src/com/intellectualcrafters/plot/C.java b/PlotSquared/src/com/intellectualcrafters/plot/C.java index 14a7f1075..a17c740d6 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/C.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/C.java @@ -136,7 +136,7 @@ public enum C { /* * purge */ - PURGE_SYNTAX("&c/plots purge {world}"), + PURGE_SYNTAX("&c/plots purge {world|world;x,z}"), PURGE_SUCCESS("All plots for the specified world have now been purged."), /* * No {plot} diff --git a/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java index 641157c15..b79c19f08 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java @@ -282,6 +282,13 @@ public class PlotMain extends JavaPlugin { } return new HashSet(myplots); } + + public static HashMap getPlots(String world) { + if (plots.containsKey(world)) { + return plots.get(world); + } + return new HashMap(); + } /** * @param world @@ -309,12 +316,20 @@ public class PlotMain extends JavaPlugin { } /** - * @param world - * @return - */ - public static boolean isPlotWorld(World world) { - return (worlds.containsKey(world.getName())); - } + * @param world + * @return + */ + public static boolean isPlotWorld(World world) { + return (worlds.containsKey(world.getName())); + } + + /** + * @param world + * @return + */ + public static boolean isPlotWorld(String world) { + return (worlds.containsKey(world)); + } /** * @param world diff --git a/PlotSquared/src/com/intellectualcrafters/plot/commands/Claim.java b/PlotSquared/src/com/intellectualcrafters/plot/commands/Claim.java index 99147738f..4d0c4455b 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/commands/Claim.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/commands/Claim.java @@ -82,7 +82,6 @@ public class Claim extends SubCommand { return false; } return true; - } public static boolean claimPlot(Player player, Plot plot, boolean teleport) { diff --git a/PlotSquared/src/com/intellectualcrafters/plot/commands/Purge.java b/PlotSquared/src/com/intellectualcrafters/plot/commands/Purge.java index 7c483e223..c2c382519 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/commands/Purge.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/commands/Purge.java @@ -12,6 +12,7 @@ import org.bukkit.entity.Player; import com.intellectualcrafters.plot.C; import com.intellectualcrafters.plot.PlayerFunctions; +import com.intellectualcrafters.plot.PlotId; import com.intellectualcrafters.plot.PlotMain; import com.intellectualcrafters.plot.PlotWorld; import com.intellectualcrafters.plot.database.DBFunc; @@ -27,22 +28,52 @@ public class Purge extends SubCommand { @Override public boolean execute(Player plr, String... args) { - if (args.length!=1) { + if (args.length!=2) { + if (args.length==1) { + try { + String[] split = args[0].split(";"); + String world = split[0]; + PlotId id = new PlotId(Integer.parseInt(split[1]), Integer.parseInt(split[2])); + + System.out.print("VALID ID"); + + if (plr!=null) { + PlayerFunctions.sendMessage(plr, (C.NOT_CONSOLE)); + return false; + } + + if (!PlotMain.isPlotWorld(world)) { + PlayerFunctions.sendMessage(plr, C.NOT_VALID_PLOT_WORLD); + return false; + } + PlotMain.getPlots(world).remove(id); + DBFunc.purge(world, id); + } + catch (Exception e) { + PlayerFunctions.sendMessage(plr, C.NOT_VALID_PLOT_ID); + } + } PlayerFunctions.sendMessage(plr, C.PURGE_SYNTAX); return false; } - PlotWorld plotworld = PlotMain.getWorldSettings(args[0]); - if (plotworld == null) { - PlayerFunctions.sendMessage(plr, C.NOT_VALID_PLOT_WORLD); - return false; + if (args[1].equals("-o")) { + PlotWorld plotworld = PlotMain.getWorldSettings(args[0]); + if (plotworld == null) { + PlayerFunctions.sendMessage(plr, C.NOT_VALID_PLOT_WORLD); + return false; + } + if (plr!=null) { + PlayerFunctions.sendMessage(plr, (C.NOT_CONSOLE)); + return false; + } + DBFunc.purge(args[0]); + PlayerFunctions.sendMessage(plr, (C.PURGE_SUCCESS)); + return true; } - if (plr!=null) { - PlayerFunctions.sendMessage(plr, (C.NOT_CONSOLE)); - return false; + else { + PlayerFunctions.sendMessage(plr, "This is a dangerous command, if you are sure, use /plot purge {world} -o"); + return false; } - DBFunc.purge(args[0]); - PlayerFunctions.sendMessage(plr, (C.PURGE_SUCCESS)); - return true; } } diff --git a/PlotSquared/src/com/intellectualcrafters/plot/database/DBFunc.java b/PlotSquared/src/com/intellectualcrafters/plot/database/DBFunc.java index 1f7cda3f8..ee537e3ca 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/database/DBFunc.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/database/DBFunc.java @@ -588,6 +588,72 @@ public class DBFunc { private static void runTask(Runnable r) { PlotMain.getMain().getServer().getScheduler().runTaskAsynchronously(PlotMain.getMain(), r); } + + public static void purge(final String world, final PlotId id) { + runTask(new Runnable() { + @Override + public void run() { + ArrayList ids = new ArrayList(); + + // Fetching a list of plot IDs for a world + try { + PreparedStatement stmt = connection.prepareStatement("SELECT `id` FROM `plot` WHERE `world` = ? AND `plot_id_x` = ? AND `plot_id_z` = ?"); + stmt.setString(1, world); + stmt.setInt(2, id.x); + stmt.setInt(3, id.y); + ResultSet result = stmt.executeQuery(); + while (result.next()) { + int id = result.getInt("id"); + ids.add(id); + } + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.WARNING, "FAILED TO PURGE WORLD '"+world+"'!"); + return; + } + if (ids.size() > 0) { + try { + + String prefix = ""; + StringBuilder idstr = new StringBuilder(""); + + for (Integer id:ids) { + idstr.append(prefix + id); + prefix = " OR `plot_plot_id` = "; + } + + PreparedStatement stmt = connection.prepareStatement("DELETE FROM `plot_helpers` WHERE `plot_plot_id` = "+idstr+""); + stmt.executeUpdate(); + stmt.close(); + + stmt = connection.prepareStatement("DELETE FROM `plot_denied` WHERE `plot_plot_id` = "+idstr+""); + stmt.executeUpdate(); + stmt.close(); + + stmt = connection.prepareStatement("DELETE FROM `plot_settings` WHERE `plot_plot_id` = "+idstr+""); + stmt.executeUpdate(); + stmt.close(); + + stmt = connection.prepareStatement("DELETE FROM `plot_trusted` WHERE `plot_plot_id` = "+idstr+""); + stmt.executeUpdate(); + stmt.close(); + + stmt = connection.prepareStatement("DELETE FROM `plot` WHERE `world` = ?"); + stmt.setString(1, world); + stmt.executeUpdate(); + stmt.close(); + } + catch (SQLException e) { + e.printStackTrace(); + Logger.add(LogLevel.DANGER, "FAILED TO PURGE PLOT FROM DB '"+world+"' , '"+id+"' !"); + return; + } + } + Logger.add(LogLevel.GENERAL, "SUCCESSFULLY PURGED PLOT FROM DB '"+world+"' , '"+id+"'!"); + } + }); + } public static void purge(final String world) { runTask(new Runnable() {