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) {
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<>();
for (final Plot plot : getPlots(world).values()) {
if (plot.hasOwner()) {

View File

@ -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<world>");
return false;
}
PlayerFunctions.sendMessage(plr, "Use /plot <x;z|player|unowned|unkown>");
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);
}
if (arg.equals("all")) {
PlayerFunctions.sendMessage(plr, "/plot all &l<world>");
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 <world>;<x>;<z>");
PlayerFunctions.sendMessage(plr, "To purge all plots use /plots purge <world> -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<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_SYNTAX("&c/plots purge <world|world;x,z>"),
PURGE_SUCCESS("All plots for the specified world have now been purged."),
PURGE_SYNTAX("Use /plot <x;z|player|unowned|unknown|all> <world>"),
PURGE_SUCCESS("Successfully purge %s plots"),
/*
* No <plot>
*/

View File

@ -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<Integer> 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<PlotId> plotIds);
/**
* Set Plot Home Position

View File

@ -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<Integer> uniqueIds) {
dbManager.purgeIds(world, uniqueIds);
}
public static void purge(final String world) {
dbManager.purge(world);
public static void purge(final String world, final Set<PlotId> plotIds) {
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.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<Integer> ids = new ArrayList<Integer>();
public void purgeIds(final String world, final Set<Integer> 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<Integer> ids = new ArrayList<Integer>();
// 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<PlotId> 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<Integer> 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

View File

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