PlotSquared/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java

235 lines
9.6 KiB
Java
Raw Normal View History

2015-01-09 15:05:10 +01:00
package com.intellectualcrafters.plot.util;
import java.io.File;
2015-01-09 15:05:10 +01:00
import java.util.Collection;
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;
import org.bukkit.World;
2015-01-10 11:20:20 +01:00
import org.bukkit.entity.Player;
2015-01-09 15:05:10 +01:00
2015-02-19 09:51:10 +01:00
import com.intellectualcrafters.plot.PlotSquared;
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;
import com.intellectualcrafters.plot.object.PlotWorld;
2015-02-19 07:08:15 +01:00
import com.intellectualcrafters.plot.util.bukkit.BukkitTaskManager;
2015-01-09 15:05:10 +01:00
public class ExpireManager {
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);
}
else {
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);
long now = System.currentTimeMillis();
2015-02-05 10:24:07 +01:00
if (now > getTimeStamp(world)) {
timestamp.put(world, now + 86400000l);
2015-02-19 07:08:15 +01:00
BukkitTaskManager.runTaskAsync(new Runnable() {
2015-01-09 15:05:10 +01:00
@Override
public void run() {
HashMap<Plot, Long> plots = getOldPlots(world);
2015-02-19 09:51:10 +01:00
PlotSquared.sendConsoleSenderMessage("&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-01-09 15:05:10 +01:00
}
else {
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 09:51:10 +01:00
ExpireManager.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(PlotSquared.getMain(), new Runnable() {
2015-01-09 15:05:10 +01:00
@Override
public void run() {
2015-02-19 09:51:10 +01:00
for (String world : PlotSquared.getPlotWorldsString()) {
if (!ExpireManager.updatingPlots.containsKey(world)) {
2015-01-09 15:05:10 +01:00
ExpireManager.updatingPlots.put(world, false);
}
Boolean updating = ExpireManager.updatingPlots.get(world);
if (updating) {
return;
}
if (!expiredPlots.containsKey(world)) {
updateExpired(world);
return;
}
Set<Plot> plots = expiredPlots.get(world).keySet();
2015-01-09 15:05:10 +01:00
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
}
Plot plot = plots.iterator().next();
2015-01-09 15:05:10 +01:00
if (plot.owner != null) {
if (UUIDHandler.uuidWrapper.getPlayer(plot.owner) != null) {
expiredPlots.get(world).remove(plot);
2015-01-09 15:05:10 +01:00
return;
}
}
if (!isExpired(plot.owner)) {
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-01-10 11:37:46 +01:00
for (UUID helper : plot.helpers) {
Player player = UUIDHandler.uuidWrapper.getPlayer(helper);
if (player != null) {
PlayerFunctions.sendMessage(player, C.PLOT_REMOVED_HELPER, plot.id.toString());
}
}
2015-01-09 15:05:10 +01:00
final World worldobj = Bukkit.getWorld(world);
2015-02-19 09:51:10 +01:00
final PlotManager manager = PlotSquared.getPlotManager(world);
if (plot.settings.isMerged()) {
Unlink.unlinkPlot(Bukkit.getWorld(world), plot);
}
2015-02-19 09:51:10 +01:00
PlotWorld plotworld = PlotSquared.getWorldSettings(world);
2015-02-17 17:14:34 +01:00
manager.clearPlot(worldobj, plotworld, plot, false, null);
2015-01-09 15:05:10 +01:00
PlotHelper.removeSign(worldobj, plot);
DBFunc.delete(world, plot);
2015-02-19 09:51:10 +01:00
PlotSquared.removePlot(world, plot.id, true);
expiredPlots.get(world).remove(plot);
2015-02-19 09:51:10 +01:00
PlotSquared.sendConsoleSenderMessage("&cDeleted expired plot: " + plot.id);
PlotSquared.sendConsoleSenderMessage("&3 - World: "+plot.world);
2015-01-16 08:32:58 +01:00
if (plot.hasOwner()) {
2015-02-19 09:51:10 +01:00
PlotSquared.sendConsoleSenderMessage("&3 - Owner: "+UUIDHandler.getName(plot.owner));
2015-01-16 08:32:58 +01:00
}
else {
2015-02-19 09:51:10 +01:00
PlotSquared.sendConsoleSenderMessage("&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
}
public static boolean isExpired(UUID uuid) {
2015-01-23 19:02:04 +01:00
String name = UUIDHandler.getName(uuid);
if (name != null) {
OfflinePlayer op = Bukkit.getOfflinePlayer(name);
if (op.hasPlayedBefore()) {
long last = op.getLastPlayed();
long compared = System.currentTimeMillis() - last;
2015-02-04 06:41:23 +01:00
if (compared >= 86400000l * Settings.AUTO_CLEAR_DAYS) {
2015-01-23 19:02:04 +01:00
return true;
}
}
}
2015-01-09 15:05:10 +01:00
return false;
}
public static HashMap<Plot, Long> getOldPlots(String world) {
2015-02-19 09:51:10 +01:00
final Collection<Plot> plots = PlotSquared.getPlots(world).values();
final HashMap<Plot, Long> toRemove = new HashMap<>();
HashMap<UUID, Long> remove = new HashMap<>();
2015-01-09 15:05:10 +01:00
Set<UUID> keep = new HashSet<>();
for (Plot plot : plots) {
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;
}
else {
stamp = remove.get(uuid);
}
toRemove.put(plot, stamp);
2015-01-09 15:05:10 +01:00
continue;
}
if (keep.contains(uuid)) {
continue;
}
2015-01-10 11:20:20 +01:00
Player player = UUIDHandler.uuidWrapper.getPlayer(uuid);
if (player != null) {
keep.add(uuid);
continue;
}
2015-01-09 15:05:10 +01:00
OfflinePlayer op = UUIDHandler.uuidWrapper.getOfflinePlayer(uuid);
2015-01-18 21:30:32 +01:00
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-04 06:41:23 +01:00
if (compared >= 86400000l * Settings.AUTO_CLEAR_DAYS) {
if (Settings.AUTO_CLEAR_CHECK_DISK) {
String worldname = Bukkit.getWorlds().get(0).getName();
String foldername;
String filename = null;
2015-02-19 09:51:10 +01:00
if (PlotSquared.checkVersion(1, 7, 5)) {
foldername = "playerdata";
2015-02-04 05:44:27 +01:00
try {
filename = op.getUniqueId() +".dat";
}
catch (Throwable e) {
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()) {
2015-02-19 09:51:10 +01:00
PlotSquared.sendConsoleSenderMessage("Could not find file: " + filename);
}
else {
try {
last = playerFile.lastModified();
compared = System.currentTimeMillis() - last;
2015-02-04 06:41:23 +01:00
if (compared < 86400000l * Settings.AUTO_CLEAR_DAYS) {
keep.add(uuid);
continue;
}
}
catch (Exception e) {
2015-02-19 09:51:10 +01:00
PlotSquared.sendConsoleSenderMessage("Please disable disk checking in old plot auto clearing; Could not read file: " + filename);
}
}
}
}
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;
}
}