From 466d570ea26e1b4be3e706a2dd87ec1b25bb064e Mon Sep 17 00:00:00 2001 From: boy0001 Date: Wed, 4 Feb 2015 15:40:39 +1100 Subject: [PATCH] Added additional checks for plot expiry system --- .../intellectualcrafters/plot/PlotMain.java | 2 + .../plot/commands/DebugExec.java | 33 ++++++++- .../plot/commands/Trim.java | 2 +- .../plot/config/Settings.java | 1 + .../plot/database/PlotMeConverter.java | 2 +- .../plot/util/ExpireManager.java | 72 ++++++++++++++----- 6 files changed, 91 insertions(+), 21 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java index bad683125..24a252aad 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java @@ -808,6 +808,7 @@ public class PlotMain extends JavaPlugin implements Listener { options.put("debug", true); options.put("clear.auto.enabled", false); options.put("clear.auto.days", 365); + options.put("clear.check-disk", Settings.AUTO_CLEAR_CHECK_DISK); options.put("clear.on.ban", false); options.put("max_plots", Settings.MAX_PLOTS); options.put("schematics.save_path", Settings.SCHEMATIC_SAVE_PATH); @@ -841,6 +842,7 @@ public class PlotMain extends JavaPlugin implements Listener { + "inding"); Settings.METRICS = config.getBoolean("metrics"); Settings.AUTO_CLEAR_DAYS = config.getInt("clear.auto.days"); + Settings.AUTO_CLEAR_CHECK_DISK = config.getBoolean("clear.check-disk"); Settings.MAX_AUTO_SIZE = config.getInt("claim.max-auto-area"); Settings.AUTO_CLEAR = config.getBoolean("clear.auto.enabled"); Settings.TITLES = config.getBoolean("titles"); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java index 822a3ed2b..d84b2c943 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java @@ -21,11 +21,16 @@ package com.intellectualcrafters.plot.commands; +import java.sql.Timestamp; import java.util.Arrays; +import java.util.Date; import java.util.List; +import java.util.Map.Entry; +import java.util.UUID; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.World; import org.bukkit.entity.Player; @@ -42,7 +47,7 @@ public class DebugExec extends SubCommand { @Override public boolean execute(final Player player, final String... args) { - List allowed_params = Arrays.asList(new String[]{"stop-expire","start-expire", "show-expired", "update-expired"}); + List allowed_params = Arrays.asList(new String[]{"stop-expire","start-expire", "show-expired", "update-expired", "seen"}); if (args.length > 0) { String arg = args[0].toLowerCase(); switch (arg) { @@ -80,12 +85,34 @@ public class DebugExec extends SubCommand { return PlayerFunctions.sendMessage(null, "Invalid world: "+args[1]); } PlayerFunctions.sendMessage(null, "Expired plots (" + ExpireManager.expiredPlots.get(args[1]).size() + "):"); - for (Plot plot : ExpireManager.expiredPlots.get(args[1])) { - PlayerFunctions.sendMessage(null, " - " + plot.world + ";" + plot.id.x + ";" + plot.id.y + ";" + UUIDHandler.getName(plot.owner)); + for (Entry entry : ExpireManager.expiredPlots.get(args[1]).entrySet()) { + Plot plot = entry.getKey(); + Long stamp = entry.getValue(); + PlayerFunctions.sendMessage(null, " - " + plot.world + ";" + plot.id.x + ";" + plot.id.y + ";" + UUIDHandler.getName(plot.owner) +" : " + stamp); } return true; } return PlayerFunctions.sendMessage(null, "Use /plot debugexec show-expired "); + case "seen": + if (args.length != 1) { + return PlayerFunctions.sendMessage(null, "Use /plot debugexec seen "); + } + UUID uuid = UUIDHandler.getUUID(args[1]); + if (uuid == null) { + return PlayerFunctions.sendMessage(null, "player not found: " + args[1]); + } + OfflinePlayer op = UUIDHandler.uuidWrapper.getOfflinePlayer(uuid); + if (op == null || !op.hasPlayedBefore()) { + return PlayerFunctions.sendMessage(null, "player hasn't connected before: " + args[1]); + } + Timestamp stamp = new Timestamp(op.getLastPlayed()); + Date date = new Date(stamp.getTime()); + PlayerFunctions.sendMessage(null, "PLAYER: " + args[1]); + PlayerFunctions.sendMessage(null, "UUID: " + uuid); + PlayerFunctions.sendMessage(null, "Object: " + date.toGMTString()); + PlayerFunctions.sendMessage(null, "GMT: " + date.toGMTString()); + PlayerFunctions.sendMessage(null, "Local: " + date.toLocaleString()); + return true; } } PlayerFunctions.sendMessage(player, "Possible sub commands: /plot debugexec <" + StringUtils.join(allowed_params, "|") + ">"); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Trim.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Trim.java index 4a57d532e..46fe45187 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Trim.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Trim.java @@ -52,7 +52,7 @@ import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.UUIDHandler; -@SuppressWarnings({"unused", "deprecated", "javadoc"}) public class Trim extends SubCommand { +public class Trim extends SubCommand { public static boolean TASK = false; private static int TASK_ID = 0; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java index 36878a6bd..185501830 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Settings.java @@ -123,6 +123,7 @@ public class Settings { * Days until a plot gets cleared */ public static int AUTO_CLEAR_DAYS = 360; + public static boolean AUTO_CLEAR_CHECK_DISK = true; public static int MIN_BLOCKS_CHANGED = -1; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/PlotMeConverter.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/PlotMeConverter.java index 467cf893d..2b238840c 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/PlotMeConverter.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/PlotMeConverter.java @@ -84,7 +84,7 @@ public class PlotMeConverter { if (!plotMeFile.exists()) { return; } - sendMessage("Conversion has started"); + sendMessage("PlotMe conversion has started. To disable this, please set 'plotme-convert.enabled' in the 'settings.yml'"); sendMessage("Connecting to PlotMe DB"); final FileConfiguration plotConfig = YamlConfiguration.loadConfiguration(plotMeFile); int count = 0; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java index 3a99499c5..9f9ad0543 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java @@ -1,7 +1,9 @@ package com.intellectualcrafters.plot.util; +import java.io.File; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import java.util.Set; import java.util.UUID; @@ -23,7 +25,7 @@ import com.intellectualcrafters.plot.object.PlotManager; public class ExpireManager { private static long timestamp = 0; - public static ConcurrentHashMap> expiredPlots = new ConcurrentHashMap<>(); + public static ConcurrentHashMap> expiredPlots = new ConcurrentHashMap<>(); public static ConcurrentHashMap updatingPlots = new ConcurrentHashMap<>(); public static int task; @@ -35,7 +37,7 @@ public class ExpireManager { TaskManager.runTask(new Runnable() { @Override public void run() { - ArrayList plots = getOldPlots(world); + HashMap plots = getOldPlots(world); PlotMain.sendConsoleSenderMessage("&cFound " + plots.size() + " expired plots!"); expiredPlots.put(world, plots); updatingPlots.put(world, false); @@ -52,28 +54,31 @@ public class ExpireManager { @Override public void run() { for (String world : PlotMain.getPlotWorldsString()) { - if (!ExpireManager.updatingPlots.contains(world)) { + if (!ExpireManager.updatingPlots.containsKey(world)) { ExpireManager.updatingPlots.put(world, false); } Boolean updating = ExpireManager.updatingPlots.get(world); if (updating) { return; } - ArrayList plots = expiredPlots.get(world); + if (!expiredPlots.containsKey(world)) { + updateExpired(world); + return; + } + Set plots = expiredPlots.get(world).keySet(); if (plots == null || plots.size() == 0) { updateExpired(world); return; } - Plot plot = plots.get(0); - + Plot plot = plots.iterator().next(); if (plot.owner != null) { if (UUIDHandler.uuidWrapper.getPlayer(plot.owner) != null) { - expiredPlots.get(world).remove(0); + expiredPlots.get(world).remove(plot); return; } } if (!isExpired(plot.owner)) { - expiredPlots.get(world).remove(0); + expiredPlots.get(world).remove(plot); return; } final PlotDeleteEvent event = new PlotDeleteEvent(world, plot.id); @@ -96,7 +101,7 @@ public class ExpireManager { PlotHelper.removeSign(worldobj, plot); DBFunc.delete(world, plot); PlotMain.removePlot(world, plot.id, true); - expiredPlots.get(world).remove(0); + expiredPlots.get(world).remove(plot); PlotMain.sendConsoleSenderMessage("&cDeleted expired plot: " + plot.id); PlotMain.sendConsoleSenderMessage("&3 - World: "+plot.world); if (plot.hasOwner()) { @@ -127,15 +132,15 @@ public class ExpireManager { return false; } - public static ArrayList getOldPlots(String world) { + public static HashMap getOldPlots(String world) { final Collection plots = PlotMain.getPlots(world).values(); - final ArrayList toRemove = new ArrayList<>(); - Set remove = new HashSet<>(); + final HashMap toRemove = new HashMap<>(); + HashMap remove = new HashMap<>(); Set keep = new HashSet<>(); for (Plot plot : plots) { UUID uuid = plot.owner; - if (uuid == null || remove.contains(uuid)) { - toRemove.add(plot); + if (uuid == null || remove.containsKey(uuid)) { + toRemove.put(plot, remove.get(uuid)); continue; } if (keep.contains(uuid)) { @@ -153,8 +158,43 @@ public class ExpireManager { long last = op.getLastPlayed(); long compared = System.currentTimeMillis() - last; if (compared >= 86400000 * Settings.AUTO_CLEAR_DAYS) { - toRemove.add(plot); - remove.add(uuid); + if (Settings.AUTO_CLEAR_CHECK_DISK) { + String worldname = Bukkit.getWorlds().get(0).getName(); + String foldername; + String filename = null; + if (PlotMain.checkVersion()) { + foldername = "playerdata"; + filename = uuid.toString() + ".dat"; + } + else { + foldername = "players"; + String playername = UUIDHandler.getName(uuid); + if (playername != null) { + filename = playername + ".dat"; + } + } + if (filename != null) { + File playerFile = new File(worldname + File.separator + foldername + File.separator + filename); + if (!playerFile.exists()) { + PlotMain.sendConsoleSenderMessage("Could not find file: " + filename); + } + else { + try { + last = playerFile.lastModified(); + compared = System.currentTimeMillis() - last; + if (compared < 86400000 * Settings.AUTO_CLEAR_DAYS) { + keep.add(uuid); + continue; + } + } + catch (Exception e) { + PlotMain.sendConsoleSenderMessage("Please disable disk checking in old plot auto clearing; Could not read file: " + filename); + } + } + } + } + toRemove.put(plot, last); + remove.put(uuid, last); } keep.add(uuid); }