2015-01-09 15:05:10 +01:00
|
|
|
package com.intellectualcrafters.plot.util;
|
|
|
|
|
2015-02-04 05:40:39 +01:00
|
|
|
import java.io.File;
|
2015-01-09 15:05:10 +01:00
|
|
|
import java.util.Collection;
|
2015-02-04 05:40:39 +01:00
|
|
|
import java.util.HashMap;
|
2015-01-09 15:05:10 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2015-01-10 11:37:46 +01:00
|
|
|
|
2015-01-09 15:05:10 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.OfflinePlayer;
|
2015-01-10 11:20:20 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2015-01-09 15:05:10 +01:00
|
|
|
|
2015-02-19 16:13:43 +01:00
|
|
|
import com.intellectualcrafters.plot.BukkitMain;
|
2015-02-19 09:51:10 +01:00
|
|
|
import com.intellectualcrafters.plot.PlotSquared;
|
2015-02-10 07:03:59 +01:00
|
|
|
import com.intellectualcrafters.plot.commands.Unlink;
|
2015-01-10 11:37:46 +01:00
|
|
|
import com.intellectualcrafters.plot.config.C;
|
2015-01-09 15:05:10 +01:00
|
|
|
import com.intellectualcrafters.plot.config.Settings;
|
|
|
|
import com.intellectualcrafters.plot.database.DBFunc;
|
|
|
|
import com.intellectualcrafters.plot.events.PlotDeleteEvent;
|
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotManager;
|
2015-02-17 12:51:58 +01:00
|
|
|
import com.intellectualcrafters.plot.object.PlotWorld;
|
2015-02-21 05:27:01 +01:00
|
|
|
import com.intellectualcrafters.plot.util.bukkit.BukkitUtil;
|
2015-02-20 07:28:21 +01:00
|
|
|
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
|
2015-01-09 15:05:10 +01:00
|
|
|
|
|
|
|
public class ExpireManager {
|
2015-02-04 05:40:39 +01:00
|
|
|
public static ConcurrentHashMap<String, HashMap<Plot, Long>> expiredPlots = new ConcurrentHashMap<>();
|
2015-01-09 15:05:10 +01:00
|
|
|
public static ConcurrentHashMap<String, Boolean> updatingPlots = new ConcurrentHashMap<>();
|
2015-02-05 10:24:07 +01:00
|
|
|
public static ConcurrentHashMap<String, Long> timestamp = new ConcurrentHashMap<>();
|
2015-01-09 15:05:10 +01:00
|
|
|
public static int task;
|
|
|
|
|
2015-02-05 10:24:07 +01:00
|
|
|
public static long getTimeStamp(final String world) {
|
|
|
|
if (timestamp.containsKey(world)) {
|
|
|
|
return timestamp.get(world);
|
2015-02-20 07:34:19 +01:00
|
|
|
} else {
|
2015-02-05 10:24:07 +01:00
|
|
|
timestamp.put(world, 0l);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean updateExpired(final String world) {
|
2015-01-09 15:05:10 +01:00
|
|
|
updatingPlots.put(world, true);
|
2015-02-20 07:34:19 +01:00
|
|
|
final long now = System.currentTimeMillis();
|
2015-02-05 10:24:07 +01:00
|
|
|
if (now > getTimeStamp(world)) {
|
|
|
|
timestamp.put(world, now + 86400000l);
|
2015-02-20 07:34:19 +01:00
|
|
|
TaskManager.runTaskAsync(new Runnable() {
|
2015-01-09 15:05:10 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-02-20 07:34:19 +01:00
|
|
|
final HashMap<Plot, Long> plots = getOldPlots(world);
|
2015-02-19 11:23:36 +01:00
|
|
|
PlotSquared.log("&cFound " + plots.size() + " expired plots for " + world + "!");
|
2015-01-09 15:05:10 +01:00
|
|
|
expiredPlots.put(world, plots);
|
|
|
|
updatingPlots.put(world, false);
|
|
|
|
}
|
|
|
|
});
|
2015-02-05 10:24:07 +01:00
|
|
|
return true;
|
2015-02-20 07:34:19 +01:00
|
|
|
} else {
|
2015-01-09 15:05:10 +01:00
|
|
|
updatingPlots.put(world, false);
|
2015-02-05 10:24:07 +01:00
|
|
|
return false;
|
2015-01-09 15:05:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void runTask() {
|
2015-02-19 16:13:43 +01:00
|
|
|
ExpireManager.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(BukkitMain.THIS, new Runnable() {
|
2015-01-09 15:05:10 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-02-20 07:34:19 +01:00
|
|
|
for (final String world : PlotSquared.getPlotWorldsString()) {
|
2015-02-04 05:40:39 +01:00
|
|
|
if (!ExpireManager.updatingPlots.containsKey(world)) {
|
2015-01-09 15:05:10 +01:00
|
|
|
ExpireManager.updatingPlots.put(world, false);
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final Boolean updating = ExpireManager.updatingPlots.get(world);
|
2015-01-09 15:05:10 +01:00
|
|
|
if (updating) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-04 05:40:39 +01:00
|
|
|
if (!expiredPlots.containsKey(world)) {
|
|
|
|
updateExpired(world);
|
|
|
|
return;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final Set<Plot> plots = expiredPlots.get(world).keySet();
|
|
|
|
if ((plots == null) || (plots.size() == 0)) {
|
2015-02-05 10:24:07 +01:00
|
|
|
if (updateExpired(world)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
continue;
|
2015-01-09 15:05:10 +01:00
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final Plot plot = plots.iterator().next();
|
2015-01-09 15:05:10 +01:00
|
|
|
if (plot.owner != null) {
|
|
|
|
if (UUIDHandler.uuidWrapper.getPlayer(plot.owner) != null) {
|
2015-02-04 05:40:39 +01:00
|
|
|
expiredPlots.get(world).remove(plot);
|
2015-01-09 15:05:10 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isExpired(plot.owner)) {
|
2015-02-04 05:40:39 +01:00
|
|
|
expiredPlots.get(world).remove(plot);
|
2015-01-09 15:05:10 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
final PlotDeleteEvent event = new PlotDeleteEvent(world, plot.id);
|
|
|
|
Bukkit.getServer().getPluginManager().callEvent(event);
|
|
|
|
if (event.isCancelled()) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
return;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
for (final UUID helper : plot.helpers) {
|
|
|
|
final Player player = UUIDHandler.uuidWrapper.getPlayer(helper);
|
2015-01-10 11:37:46 +01:00
|
|
|
if (player != null) {
|
2015-02-21 05:27:01 +01:00
|
|
|
MainUtil.sendMessage(BukkitUtil.getPlayer(player), C.PLOT_REMOVED_HELPER, plot.id.toString());
|
2015-01-10 11:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-19 09:51:10 +01:00
|
|
|
final PlotManager manager = PlotSquared.getPlotManager(world);
|
2015-02-10 07:03:59 +01:00
|
|
|
if (plot.settings.isMerged()) {
|
|
|
|
Unlink.unlinkPlot(Bukkit.getWorld(world), plot);
|
|
|
|
}
|
2015-02-20 09:55:04 +01:00
|
|
|
final PlotWorld plotworld = PlotSquared.getPlotWorld(world);
|
2015-02-19 16:13:43 +01:00
|
|
|
manager.clearPlot(plotworld, plot, false, null);
|
2015-02-20 12:23:48 +01:00
|
|
|
MainUtil.removeSign(plot);
|
2015-01-09 15:05:10 +01:00
|
|
|
DBFunc.delete(world, plot);
|
2015-02-19 09:51:10 +01:00
|
|
|
PlotSquared.removePlot(world, plot.id, true);
|
2015-02-04 05:40:39 +01:00
|
|
|
expiredPlots.get(world).remove(plot);
|
2015-02-19 11:23:36 +01:00
|
|
|
PlotSquared.log("&cDeleted expired plot: " + plot.id);
|
2015-02-20 07:34:19 +01:00
|
|
|
PlotSquared.log("&3 - World: " + plot.world);
|
2015-01-16 08:32:58 +01:00
|
|
|
if (plot.hasOwner()) {
|
2015-02-20 07:34:19 +01:00
|
|
|
PlotSquared.log("&3 - Owner: " + UUIDHandler.getName(plot.owner));
|
|
|
|
} else {
|
|
|
|
PlotSquared.log("&3 - Owner: Unowned");
|
2015-01-09 15:05:10 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-01-10 11:37:46 +01:00
|
|
|
}, 2400, 2400);
|
2015-01-09 15:05:10 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 07:34:19 +01:00
|
|
|
public static boolean isExpired(final UUID uuid) {
|
|
|
|
final String name = UUIDHandler.getName(uuid);
|
|
|
|
if (name != null) {
|
|
|
|
final OfflinePlayer op = Bukkit.getOfflinePlayer(name);
|
|
|
|
if (op.hasPlayedBefore()) {
|
|
|
|
final long last = op.getLastPlayed();
|
|
|
|
final long compared = System.currentTimeMillis() - last;
|
|
|
|
if (compared >= (86400000l * Settings.AUTO_CLEAR_DAYS)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-09 15:05:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-20 07:34:19 +01:00
|
|
|
public static HashMap<Plot, Long> getOldPlots(final String world) {
|
2015-02-19 09:51:10 +01:00
|
|
|
final Collection<Plot> plots = PlotSquared.getPlots(world).values();
|
2015-02-04 05:40:39 +01:00
|
|
|
final HashMap<Plot, Long> toRemove = new HashMap<>();
|
2015-02-20 07:34:19 +01:00
|
|
|
final HashMap<UUID, Long> remove = new HashMap<>();
|
|
|
|
final Set<UUID> keep = new HashSet<>();
|
|
|
|
for (final Plot plot : plots) {
|
|
|
|
final UUID uuid = plot.owner;
|
|
|
|
if ((uuid == null) || remove.containsKey(uuid)) {
|
2015-02-04 06:41:23 +01:00
|
|
|
Long stamp;
|
|
|
|
if (uuid == null) {
|
|
|
|
stamp = 0l;
|
2015-02-20 07:34:19 +01:00
|
|
|
} else {
|
2015-02-04 06:41:23 +01:00
|
|
|
stamp = remove.get(uuid);
|
|
|
|
}
|
|
|
|
toRemove.put(plot, stamp);
|
2015-01-09 15:05:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (keep.contains(uuid)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final Player player = UUIDHandler.uuidWrapper.getPlayer(uuid);
|
2015-01-10 11:20:20 +01:00
|
|
|
if (player != null) {
|
|
|
|
keep.add(uuid);
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final OfflinePlayer op = UUIDHandler.uuidWrapper.getOfflinePlayer(uuid);
|
|
|
|
if ((op == null) || !op.hasPlayedBefore()) {
|
2015-01-09 15:05:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
long last = op.getLastPlayed();
|
|
|
|
long compared = System.currentTimeMillis() - last;
|
2015-02-20 07:34:19 +01:00
|
|
|
if (compared >= (86400000l * Settings.AUTO_CLEAR_DAYS)) {
|
2015-02-04 05:40:39 +01:00
|
|
|
if (Settings.AUTO_CLEAR_CHECK_DISK) {
|
2015-02-20 07:34:19 +01:00
|
|
|
final String worldname = Bukkit.getWorlds().get(0).getName();
|
2015-02-04 05:40:39 +01:00
|
|
|
String foldername;
|
|
|
|
String filename = null;
|
2015-02-19 16:13:43 +01:00
|
|
|
if (BukkitMain.checkVersion(1, 7, 5)) {
|
2015-02-04 05:40:39 +01:00
|
|
|
foldername = "playerdata";
|
2015-02-04 05:44:27 +01:00
|
|
|
try {
|
2015-02-20 07:34:19 +01:00
|
|
|
filename = op.getUniqueId() + ".dat";
|
|
|
|
} catch (final Throwable e) {
|
2015-02-04 05:44:27 +01:00
|
|
|
filename = uuid.toString() + ".dat";
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
} else {
|
2015-02-04 05:40:39 +01:00
|
|
|
foldername = "players";
|
2015-02-20 07:34:19 +01:00
|
|
|
final String playername = UUIDHandler.getName(uuid);
|
2015-02-04 05:40:39 +01:00
|
|
|
if (playername != null) {
|
|
|
|
filename = playername + ".dat";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (filename != null) {
|
2015-02-20 07:34:19 +01:00
|
|
|
final File playerFile = new File(worldname + File.separator + foldername + File.separator + filename);
|
2015-02-04 05:40:39 +01:00
|
|
|
if (!playerFile.exists()) {
|
2015-02-19 11:23:36 +01:00
|
|
|
PlotSquared.log("Could not find file: " + filename);
|
2015-02-20 07:34:19 +01:00
|
|
|
} else {
|
2015-02-04 05:40:39 +01:00
|
|
|
try {
|
|
|
|
last = playerFile.lastModified();
|
|
|
|
compared = System.currentTimeMillis() - last;
|
2015-02-20 07:34:19 +01:00
|
|
|
if (compared < (86400000l * Settings.AUTO_CLEAR_DAYS)) {
|
2015-02-04 05:40:39 +01:00
|
|
|
keep.add(uuid);
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
} catch (final Exception e) {
|
2015-02-19 11:23:36 +01:00
|
|
|
PlotSquared.log("Please disable disk checking in old plot auto clearing; Could not read file: " + filename);
|
2015-02-04 05:40:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
toRemove.put(plot, last);
|
|
|
|
remove.put(uuid, last);
|
2015-02-04 06:41:23 +01:00
|
|
|
continue;
|
2015-01-09 15:05:10 +01:00
|
|
|
}
|
|
|
|
keep.add(uuid);
|
|
|
|
}
|
|
|
|
return toRemove;
|
|
|
|
}
|
|
|
|
}
|