From 39ae80cb6552b487393ec22702431380051ca68f Mon Sep 17 00:00:00 2001 From: boy0001 Date: Wed, 7 Jan 2015 17:44:03 +1100 Subject: [PATCH] Extended functionality of purge --- .../intellectualcrafters/plot/PlotMain.java | 10 ++ .../plot/commands/Purge.java | 117 ++++++++----- .../intellectualcrafters/plot/config/C.java | 4 +- .../plot/database/AbstractDB.java | 4 +- .../plot/database/DBFunc.java | 8 +- .../plot/database/SQLManager.java | 164 ++++++------------ .../plot/util/PlayerFunctions.java | 19 +- 7 files changed, 154 insertions(+), 172 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java index b1abea64e..27d0e0c34 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java @@ -337,6 +337,16 @@ import java.util.concurrent.TimeUnit; */ public static Set getPlots(final World world, final Player player) { final UUID uuid = UUIDHandler.getUUID(player); + return getPlots(world, uuid); + } + + /** + * @param world plot world + * @param player plot owner + * + * @return players plots + */ + public static Set getPlots(final World world, final UUID uuid) { final ArrayList myplots = new ArrayList<>(); for (final Plot plot : getPlots(world).values()) { if (plot.hasOwner()) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Purge.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Purge.java index 7631d6cb0..117edc2bb 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Purge.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Purge.java @@ -21,15 +21,22 @@ package com.intellectualcrafters.plot.commands; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; import java.util.UUID; import com.intellectualcrafters.plot.PlotMain; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.database.DBFunc; +import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.UUIDHandler; +import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.entity.Player; @SuppressWarnings({"unused", "deprecated", "javadoc"}) public class Purge extends SubCommand { @@ -54,7 +61,6 @@ import org.bukkit.entity.Player; PlayerFunctions.sendMessage(plr, (C.NOT_CONSOLE)); return false; } - if (args.length == 1) { String arg = args[0].toLowerCase(); PlotId id = getId(arg); @@ -79,52 +85,81 @@ import org.bukkit.entity.Player; PlayerFunctions.sendMessage(plr, "/plot unknown &l"); return false; } - PlayerFunctions.sendMessage(plr, "Use /plot "); - return false; - } - - if (args.length != 2) { - if (args.length == 1) { - try { - final String[] split = args[0].split(";"); - final String world = split[0]; - final PlotId id = new PlotId(Integer.parseInt(split[1]), Integer.parseInt(split[2])); - - if (!PlotMain.isPlotWorld(world)) { - PlayerFunctions.sendMessage(null, C.NOT_VALID_PLOT_WORLD); - return false; - } - - PlotMain.getPlots(world).remove(id); - DBFunc.purge(world, id); - PlayerFunctions.sendMessage(null, "&aPurge of '" + args[0] + "' was successful!"); - return true; - } catch (final Exception e) { - if (args.length == 1) { - PlayerFunctions.sendMessage(plr, "Use /plots purge ;;"); - PlayerFunctions.sendMessage(plr, "To purge all plots use /plots purge -o"); - return false; - } - PlayerFunctions.sendMessage(plr, C.NOT_VALID_PLOT_ID); - } + if (arg.equals("all")) { + PlayerFunctions.sendMessage(plr, "/plot all &l"); + return false; } PlayerFunctions.sendMessage(plr, C.PURGE_SYNTAX); return false; } - if (args[1].equals("-o")) { - if (PlotMain.getPlots(args[0]) == null) { - PlayerFunctions.sendMessage(plr, C.NOT_VALID_PLOT_WORLD); - return false; - } - PlotMain.removePlots(args[0]); - DBFunc.purge(args[0]); - PlayerFunctions.sendMessage(plr, (C.PURGE_SUCCESS)); - return true; - } else { - PlayerFunctions.sendMessage(plr, "Use /plots purge ;;"); - PlayerFunctions.sendMessage(plr, "To purge all plots use /plots purge -o"); + if (args.length != 2) { + PlayerFunctions.sendMessage(plr, C.PURGE_SYNTAX); return false; } + World world = Bukkit.getWorld(args[1]); + if (world == null || !PlotMain.isPlotWorld(world)) { + PlayerFunctions.sendMessage(null, C.NOT_VALID_PLOT_WORLD); + return false; + } + String worldname = world.getName(); + String arg = args[0].toLowerCase(); + PlotId id = getId(arg); + if (id != null) { + HashSet ids = new HashSet(); + int DBid = DBFunc.getId(worldname, id); + if (DBid != Integer.MAX_VALUE) { + ids.add(DBid); + } + DBFunc.purgeIds(worldname, ids); + return finishPurge(DBid == Integer.MAX_VALUE ? 1 : 0); + } + UUID uuid = UUIDHandler.getUUID(args[0]); + if (uuid != null) { + Set plots = PlotMain.getPlots(world,uuid); + Set ids = new HashSet<>(); + for (Plot plot : plots) { + ids.add(plot.id); + } + DBFunc.purge(worldname, ids); + return finishPurge(ids.size()); + } + if (arg.equals("all")) { + Set ids = PlotMain.getPlots(world).keySet(); + DBFunc.purge(worldname, ids); + return finishPurge(ids.size()); + } + if (arg.equals("unknown")) { + Collection plots = PlotMain.getPlots(world).values(); + Set ids = new HashSet<>(); + for (Plot plot : plots) { + if (plot.owner != null) { + String name = UUIDHandler.getName(plot.owner); + if (name == null) { + ids.add(plot.id); + } + } + } + DBFunc.purge(worldname, ids); + return finishPurge(ids.size()); + } + if (arg.equals("unowned")) { + Collection plots = PlotMain.getPlots(world).values(); + Set ids = new HashSet<>(); + for (Plot plot : plots) { + if (plot.owner == null) { + ids.add(plot.id); + } + } + DBFunc.purge(worldname, ids); + return finishPurge(ids.size()); + } + PlayerFunctions.sendMessage(plr, C.PURGE_SYNTAX); + return false; + } + + private boolean finishPurge(int amount) { + PlayerFunctions.sendMessage(null, C.PURGE_SUCCESS, amount + ""); + return false; } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java index ac2bcb08f..a3efabd3d 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -182,8 +182,8 @@ public enum C { /* * purge */ - PURGE_SYNTAX("&c/plots purge "), - PURGE_SUCCESS("All plots for the specified world have now been purged."), + PURGE_SYNTAX("Use /plot "), + PURGE_SUCCESS("Successfully purge %s plots"), /* * No */ diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java index cafe9befb..9e22f4c51 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java @@ -148,14 +148,14 @@ public interface AbstractDB { * @param world World in which the plot is located * @param id Plot ID */ - public void purge(final String world, final PlotId id); + public void purgeIds(final String world, final Set uniqueIds); /** * Purge a whole world * * @param world World in which the plots should be purged */ - public void purge(final String world); + public void purge(final String world, final Set plotIds); /** * Set Plot Home Position diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java index b6cb5f2f6..60fe21d5f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java @@ -170,12 +170,12 @@ public class DBFunc { dbManager.setAlias(world, plot, alias); } - public static void purge(final String world, final PlotId id) { - dbManager.purge(world, id); + public static void purgeIds(final String world, final Set uniqueIds) { + dbManager.purgeIds(world, uniqueIds); } - public static void purge(final String world) { - dbManager.purge(world); + public static void purge(final String world, final Set plotIds) { + dbManager.purge(world, plotIds); } /** diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index aaf24b44f..e08704ea6 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -29,6 +29,7 @@ import com.intellectualcrafters.plot.object.PlotComment; import com.intellectualcrafters.plot.object.PlotHomePosition; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.util.UUIDHandler; + import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; @@ -816,133 +817,68 @@ public class SQLManager implements AbstractDB { PlotMain.getMain().getServer().getScheduler().runTaskAsynchronously(PlotMain.getMain(), r); } - @Override - public void purge(final String world, final PlotId id) { - runTask(new Runnable() { - @Override - public void run() { - final ArrayList ids = new ArrayList(); + public void purgeIds(final String world, final Set uniqueIds) { + if (uniqueIds.size() > 0) { + try { - // Fetching a list of plot IDs for a world - try { - final PreparedStatement stmt = SQLManager.this.connection.prepareStatement("SELECT `id` FROM `" + SQLManager.this.prefix + "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); - final ResultSet result = stmt.executeQuery(); - while (result.next()) { - final int id = result.getInt("id"); - ids.add(id); - } - } catch (final SQLException e) { - e.printStackTrace(); - PlotMain.sendConsoleSenderMessage("&7[WARN] "+"FAILED TO PURGE WORLD '" + world + "'!"); - return; + String prefix = ""; + final StringBuilder idstr = new StringBuilder(""); + + for (final Integer id : uniqueIds) { + idstr.append(prefix + id); + prefix = " OR `plot_plot_id` = "; } - if (ids.size() > 0) { - try { - String p = ""; - final StringBuilder idstr = new StringBuilder(""); + PreparedStatement stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_helpers` WHERE `plot_plot_id` = " + idstr + ""); + stmt.executeUpdate(); + stmt.close(); - for (final Integer id : ids) { - idstr.append(p + id); - p = " OR `plot_plot_id` = "; - } - - PreparedStatement stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_helpers` WHERE `plot_plot_id` = " + idstr + ""); - stmt.executeUpdate(); - stmt.close(); + stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_denied` WHERE `plot_plot_id` = " + idstr + ""); + stmt.executeUpdate(); + stmt.close(); - stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_denied` WHERE `plot_plot_id` = " + idstr + ""); - stmt.executeUpdate(); - stmt.close(); + stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_settings` WHERE `plot_plot_id` = " + idstr + ""); + stmt.executeUpdate(); + stmt.close(); - stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_settings` WHERE `plot_plot_id` = " + idstr + ""); - stmt.executeUpdate(); - stmt.close(); + stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_trusted` WHERE `plot_plot_id` = " + idstr + ""); + stmt.executeUpdate(); + stmt.close(); - stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_trusted` WHERE `plot_plot_id` = " + idstr + ""); - stmt.executeUpdate(); - stmt.close(); - - stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot` WHERE `id` = " + idstr + ""); - stmt.executeUpdate(); - stmt.close(); - } catch (final SQLException e) { - e.printStackTrace(); - PlotMain.sendConsoleSenderMessage("&c[ERROR] "+"FAILED TO PURGE PLOT FROM DB '" + world + "' , '" + id + "' !"); - return; - } - } - PlotMain.sendConsoleSenderMessage("&6[INFO] "+"SUCCESSFULLY PURGED PLOT FROM DB '" + world + "' , '" + id + "'!"); + stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot` WHERE `world` = ?"); + stmt.setString(1, world); + stmt.executeUpdate(); + stmt.close(); + } catch (final SQLException e) { + e.printStackTrace(); + PlotMain.sendConsoleSenderMessage("&c[ERROR] "+"FAILED TO PURGE WORLD '" + world + "'!"); + return; } - }); + } + PlotMain.sendConsoleSenderMessage("&6[INFO] "+"SUCCESSFULLY PURGED WORLD '" + world + "'!"); } - @Override - public void purge(final String world) { - runTask(new Runnable() { - @Override - public void run() { - final ArrayList ids = new ArrayList(); - - // Fetching a list of plot IDs for a world - try { - final PreparedStatement stmt = SQLManager.this.connection.prepareStatement("SELECT `id` FROM `" + SQLManager.this.prefix + "plot` WHERE `world` = ?"); - stmt.setString(1, world); - final ResultSet result = stmt.executeQuery(); - while (result.next()) { - final int id = result.getInt("id"); - ids.add(id); - } - } catch (final SQLException e) { - e.printStackTrace(); - PlotMain.sendConsoleSenderMessage("&7[WARN] "+"FAILED TO PURGE WORLD '" + world + "'!"); - return; + public void purge(final String world, Set plots) { + PreparedStatement stmt; + try { + stmt = SQLManager.this.connection.prepareStatement("SELECT `id`, `plot_id_x`, `plot_id_z` FROM `" + this.prefix + "plot` WHERE `world` = ?"); + stmt.setString(1, world); + ResultSet r = stmt.executeQuery(); + PlotId plot_id; + Set ids = new HashSet<>(); + while (r.next()) { + plot_id = new PlotId(r.getInt("plot_id_x"), r.getInt("plot_id_z")); + if (plots.contains(plot_id)) { + ids.add(r.getInt("id")); } - if (ids.size() > 0) { - try { - - String prefix = ""; - final StringBuilder idstr = new StringBuilder(""); - - for (final Integer id : ids) { - idstr.append(prefix + id); - prefix = " OR `plot_plot_id` = "; - } - - PreparedStatement stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_helpers` WHERE `plot_plot_id` = " + idstr + ""); - stmt.executeUpdate(); - stmt.close(); - - stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_denied` WHERE `plot_plot_id` = " + idstr + ""); - stmt.executeUpdate(); - stmt.close(); - - stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_settings` WHERE `plot_plot_id` = " + idstr + ""); - stmt.executeUpdate(); - stmt.close(); - - stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_trusted` WHERE `plot_plot_id` = " + idstr + ""); - stmt.executeUpdate(); - stmt.close(); - - stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot` WHERE `world` = ?"); - stmt.setString(1, world); - stmt.executeUpdate(); - stmt.close(); - } catch (final SQLException e) { - e.printStackTrace(); - PlotMain.sendConsoleSenderMessage("&c[ERROR] "+"FAILED TO PURGE WORLD '" + world + "'!"); - return; - } - } - PlotMain.sendConsoleSenderMessage("&6[INFO] "+"SUCCESSFULLY PURGED WORLD '" + world + "'!"); } - }); + purgeIds(world, ids); + } catch (SQLException e) { + e.printStackTrace(); + PlotMain.sendConsoleSenderMessage("&c[ERROR] "+"FAILED TO PURGE WORLD '" + world + "'!"); + } } - + /** * @param plot * @param position diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlayerFunctions.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlayerFunctions.java index dbc602ee4..8569d0408 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlayerFunctions.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlayerFunctions.java @@ -310,17 +310,18 @@ import java.util.*; */ public static boolean sendMessage(final Player plr, final C c, final String... args) { if (c.s().length() > 1) { - if (plr == null) { - PlotMain.sendConsoleSenderMessage(c); - } else { - String msg = c.s(); - if ((args != null) && (args.length > 0)) { - for (final String str : args) { - if (msg.contains("%s")) { - msg = msg.replaceFirst("%s", str); - } + String msg = c.s(); + if ((args != null) && (args.length > 0)) { + for (final String str : args) { + if (msg.contains("%s")) { + msg = msg.replaceFirst("%s", str); } } + } + if (plr == null) { + PlotMain.sendConsoleSenderMessage(c); + } + else { sendMessage(plr, msg); } }