Extended functionality of purge

This commit is contained in:
boy0001 2015-01-07 17:44:03 +11:00
parent 941b3d8c42
commit 39ae80cb65
7 changed files with 154 additions and 172 deletions

View File

@ -337,6 +337,16 @@ import java.util.concurrent.TimeUnit;
*/ */
public static Set<Plot> getPlots(final World world, final Player player) { public static Set<Plot> getPlots(final World world, final Player player) {
final UUID uuid = UUIDHandler.getUUID(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<Plot> getPlots(final World world, final UUID uuid) {
final ArrayList<Plot> myplots = new ArrayList<>(); final ArrayList<Plot> myplots = new ArrayList<>();
for (final Plot plot : getPlots(world).values()) { for (final Plot plot : getPlots(world).values()) {
if (plot.hasOwner()) { if (plot.hasOwner()) {

View File

@ -21,15 +21,22 @@
package com.intellectualcrafters.plot.commands; 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 java.util.UUID;
import com.intellectualcrafters.plot.PlotMain; import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.PlayerFunctions;
import com.intellectualcrafters.plot.util.UUIDHandler; import com.intellectualcrafters.plot.util.UUIDHandler;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@SuppressWarnings({"unused", "deprecated", "javadoc"}) public class Purge extends SubCommand { @SuppressWarnings({"unused", "deprecated", "javadoc"}) public class Purge extends SubCommand {
@ -54,7 +61,6 @@ import org.bukkit.entity.Player;
PlayerFunctions.sendMessage(plr, (C.NOT_CONSOLE)); PlayerFunctions.sendMessage(plr, (C.NOT_CONSOLE));
return false; return false;
} }
if (args.length == 1) { if (args.length == 1) {
String arg = args[0].toLowerCase(); String arg = args[0].toLowerCase();
PlotId id = getId(arg); PlotId id = getId(arg);
@ -79,52 +85,81 @@ import org.bukkit.entity.Player;
PlayerFunctions.sendMessage(plr, "/plot unknown &l<world>"); PlayerFunctions.sendMessage(plr, "/plot unknown &l<world>");
return false; return false;
} }
PlayerFunctions.sendMessage(plr, "Use /plot <x;z|player|unowned|unkown>"); if (arg.equals("all")) {
return false; PlayerFunctions.sendMessage(plr, "/plot all &l<world>");
} 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 <world>;<x>;<z>");
PlayerFunctions.sendMessage(plr, "To purge all plots use /plots purge <world> -o");
return false;
}
PlayerFunctions.sendMessage(plr, C.NOT_VALID_PLOT_ID);
}
} }
PlayerFunctions.sendMessage(plr, C.PURGE_SYNTAX); PlayerFunctions.sendMessage(plr, C.PURGE_SYNTAX);
return false; return false;
} }
if (args[1].equals("-o")) { if (args.length != 2) {
if (PlotMain.getPlots(args[0]) == null) { PlayerFunctions.sendMessage(plr, C.PURGE_SYNTAX);
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 <world>;<x>;<z>");
PlayerFunctions.sendMessage(plr, "To purge all plots use /plots purge <world> -o");
return false; 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<Integer> ids = new HashSet<Integer>();
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<Plot> plots = PlotMain.getPlots(world,uuid);
Set<PlotId> ids = new HashSet<>();
for (Plot plot : plots) {
ids.add(plot.id);
}
DBFunc.purge(worldname, ids);
return finishPurge(ids.size());
}
if (arg.equals("all")) {
Set<PlotId> ids = PlotMain.getPlots(world).keySet();
DBFunc.purge(worldname, ids);
return finishPurge(ids.size());
}
if (arg.equals("unknown")) {
Collection<Plot> plots = PlotMain.getPlots(world).values();
Set<PlotId> 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<Plot> plots = PlotMain.getPlots(world).values();
Set<PlotId> 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;
} }
} }

View File

@ -182,8 +182,8 @@ public enum C {
/* /*
* purge * purge
*/ */
PURGE_SYNTAX("&c/plots purge <world|world;x,z>"), PURGE_SYNTAX("Use /plot <x;z|player|unowned|unknown|all> <world>"),
PURGE_SUCCESS("All plots for the specified world have now been purged."), PURGE_SUCCESS("Successfully purge %s plots"),
/* /*
* No <plot> * No <plot>
*/ */

View File

@ -148,14 +148,14 @@ public interface AbstractDB {
* @param world World in which the plot is located * @param world World in which the plot is located
* @param id Plot ID * @param id Plot ID
*/ */
public void purge(final String world, final PlotId id); public void purgeIds(final String world, final Set<Integer> uniqueIds);
/** /**
* Purge a whole world * Purge a whole world
* *
* @param world World in which the plots should be purged * @param world World in which the plots should be purged
*/ */
public void purge(final String world); public void purge(final String world, final Set<PlotId> plotIds);
/** /**
* Set Plot Home Position * Set Plot Home Position

View File

@ -170,12 +170,12 @@ public class DBFunc {
dbManager.setAlias(world, plot, alias); dbManager.setAlias(world, plot, alias);
} }
public static void purge(final String world, final PlotId id) { public static void purgeIds(final String world, final Set<Integer> uniqueIds) {
dbManager.purge(world, id); dbManager.purgeIds(world, uniqueIds);
} }
public static void purge(final String world) { public static void purge(final String world, final Set<PlotId> plotIds) {
dbManager.purge(world); dbManager.purge(world, plotIds);
} }
/** /**

View File

@ -29,6 +29,7 @@ import com.intellectualcrafters.plot.object.PlotComment;
import com.intellectualcrafters.plot.object.PlotHomePosition; import com.intellectualcrafters.plot.object.PlotHomePosition;
import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.util.UUIDHandler; import com.intellectualcrafters.plot.util.UUIDHandler;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -816,131 +817,66 @@ public class SQLManager implements AbstractDB {
PlotMain.getMain().getServer().getScheduler().runTaskAsynchronously(PlotMain.getMain(), r); PlotMain.getMain().getServer().getScheduler().runTaskAsynchronously(PlotMain.getMain(), r);
} }
@Override public void purgeIds(final String world, final Set<Integer> uniqueIds) {
public void purge(final String world, final PlotId id) { if (uniqueIds.size() > 0) {
runTask(new Runnable() { try {
@Override
public void run() {
final ArrayList<Integer> ids = new ArrayList<Integer>();
// Fetching a list of plot IDs for a world String prefix = "";
try { final StringBuilder idstr = new StringBuilder("");
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); for (final Integer id : uniqueIds) {
stmt.setInt(2, id.x); idstr.append(prefix + id);
stmt.setInt(3, id.y); prefix = " OR `plot_plot_id` = ";
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;
} }
if (ids.size() > 0) {
try {
String p = ""; PreparedStatement stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_helpers` WHERE `plot_plot_id` = " + idstr + "");
final StringBuilder idstr = new StringBuilder(""); stmt.executeUpdate();
stmt.close();
for (final Integer id : ids) { stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_denied` WHERE `plot_plot_id` = " + idstr + "");
idstr.append(p + id); stmt.executeUpdate();
p = " OR `plot_plot_id` = "; stmt.close();
}
PreparedStatement stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_helpers` WHERE `plot_plot_id` = " + idstr + ""); stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_settings` WHERE `plot_plot_id` = " + idstr + "");
stmt.executeUpdate(); stmt.executeUpdate();
stmt.close(); stmt.close();
stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_denied` WHERE `plot_plot_id` = " + idstr + ""); stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_trusted` WHERE `plot_plot_id` = " + idstr + "");
stmt.executeUpdate(); stmt.executeUpdate();
stmt.close(); stmt.close();
stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_settings` WHERE `plot_plot_id` = " + idstr + ""); stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot` WHERE `world` = ?");
stmt.executeUpdate(); stmt.setString(1, world);
stmt.close(); stmt.executeUpdate();
stmt.close();
stmt = SQLManager.this.connection.prepareStatement("DELETE FROM `" + prefix + "plot_trusted` WHERE `plot_plot_id` = " + idstr + ""); } catch (final SQLException e) {
stmt.executeUpdate(); e.printStackTrace();
stmt.close(); PlotMain.sendConsoleSenderMessage("&c[ERROR] "+"FAILED TO PURGE WORLD '" + world + "'!");
return;
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 + "'!");
} }
}); }
PlotMain.sendConsoleSenderMessage("&6[INFO] "+"SUCCESSFULLY PURGED WORLD '" + world + "'!");
} }
@Override @Override
public void purge(final String world) { public void purge(final String world, Set<PlotId> plots) {
runTask(new Runnable() { PreparedStatement stmt;
@Override try {
public void run() { stmt = SQLManager.this.connection.prepareStatement("SELECT `id`, `plot_id_x`, `plot_id_z` FROM `" + this.prefix + "plot` WHERE `world` = ?");
final ArrayList<Integer> ids = new ArrayList<Integer>(); stmt.setString(1, world);
ResultSet r = stmt.executeQuery();
// Fetching a list of plot IDs for a world PlotId plot_id;
try { Set<Integer> ids = new HashSet<>();
final PreparedStatement stmt = SQLManager.this.connection.prepareStatement("SELECT `id` FROM `" + SQLManager.this.prefix + "plot` WHERE `world` = ?"); while (r.next()) {
stmt.setString(1, world); plot_id = new PlotId(r.getInt("plot_id_x"), r.getInt("plot_id_z"));
final ResultSet result = stmt.executeQuery(); if (plots.contains(plot_id)) {
while (result.next()) { ids.add(r.getInt("id"));
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;
} }
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 + "'!");
}
} }
/** /**

View File

@ -310,17 +310,18 @@ import java.util.*;
*/ */
public static boolean sendMessage(final Player plr, final C c, final String... args) { public static boolean sendMessage(final Player plr, final C c, final String... args) {
if (c.s().length() > 1) { if (c.s().length() > 1) {
if (plr == null) { String msg = c.s();
PlotMain.sendConsoleSenderMessage(c); if ((args != null) && (args.length > 0)) {
} else { for (final String str : args) {
String msg = c.s(); if (msg.contains("%s")) {
if ((args != null) && (args.length > 0)) { msg = msg.replaceFirst("%s", str);
for (final String str : args) {
if (msg.contains("%s")) {
msg = msg.replaceFirst("%s", str);
}
} }
} }
}
if (plr == null) {
PlotMain.sendConsoleSenderMessage(c);
}
else {
sendMessage(plr, msg); sendMessage(plr, msg);
} }
} }