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

246 lines
11 KiB
Java
Raw Normal View History

2015-01-09 15:05:10 +01:00
package com.intellectualcrafters.plot.util;
2015-07-05 17:44:10 +02:00
import java.io.File;
2015-07-14 16:12:12 +02:00
import java.util.ArrayDeque;
2015-07-05 17:44:10 +02:00
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import com.intellectualcrafters.plot.PS;
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.flag.Flag;
import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.generator.ClassicPlotManager;
import com.intellectualcrafters.plot.generator.HybridUtils;
2015-07-05 17:44:10 +02:00
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotAnalysis;
2015-07-05 17:44:10 +02:00
import com.intellectualcrafters.plot.object.PlotHandler;
import com.intellectualcrafters.plot.object.PlotManager;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.object.RunnableVal;
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 {
public static ConcurrentHashMap<String, List<Plot>> 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<>();
public static ConcurrentHashMap<UUID, Long> dates = new ConcurrentHashMap<>();
2015-01-09 15:05:10 +01:00
public static int task;
2015-02-23 02:32:27 +01:00
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;
}
}
2015-02-23 02:32:27 +01:00
2015-02-05 10:24:07 +01:00
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() {
try {
final List<Plot> plots = getOldPlots(world);
PS.log("$2[&5Expire&dManager$2] $4Found " + plots.size() + " expired plots for " + world + "!");
expiredPlots.put(world, plots);
updatingPlots.put(world, false);
}
catch (Exception e) {
e.printStackTrace();
}
2015-01-09 15:05:10 +01:00
}
});
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
}
}
2015-02-23 02:32:27 +01:00
2015-01-09 15:05:10 +01:00
public static void runTask() {
ExpireManager.task = TaskManager.runTaskRepeat(new Runnable() {
2015-01-09 15:05:10 +01:00
@Override
public void run() {
try {
for (final String world : PS.get().getPlotWorldsString()) {
if (!ExpireManager.updatingPlots.containsKey(world)) {
ExpireManager.updatingPlots.put(world, false);
}
final Boolean updating = ExpireManager.updatingPlots.get(world);
if (updating) {
PS.log("$2[&5Expire&dManager$2] $4Waiting on fetch...");
2015-02-05 10:24:07 +01:00
return;
}
if (!expiredPlots.containsKey(world)) {
PS.log("$2[&5Expire&dManager$2] $4Updating expired plots for: " + world);
updateExpired(world);
return;
}
final List<Plot> plots = expiredPlots.get(world);
if ((plots == null) || (plots.size() == 0)) {
if (updateExpired(world)) {
PS.log("$2[&5Expire&dManager$2] $4Re-evaluating expired plots for: " + world);
return;
}
continue;
2015-01-10 11:37:46 +01:00
}
final Plot plot = plots.iterator().next();
if (!isExpired(plot)) {
expiredPlots.get(world).remove(plot);
PS.log("$2[&5Expire&dManager$2] &bSkipping no longer expired: " + plot);
return;
}
for (final UUID helper : plot.trusted) {
final PlotPlayer player = UUIDHandler.getPlayer(helper);
if (player != null) {
MainUtil.sendMessage(player, C.PLOT_REMOVED_USER, plot.id.toString());
}
}
for (final UUID helper : plot.members) {
final PlotPlayer player = UUIDHandler.getPlayer(helper);
if (player != null) {
MainUtil.sendMessage(player, C.PLOT_REMOVED_USER, plot.id.toString());
}
}
final PlotManager manager = PS.get().getPlotManager(world);
if (manager == null) {
PS.log("$2[&5Expire&dManager$2] &cThis is a friendly reminder to create or delete " + world +" as it is currently setup incorrectly");
expiredPlots.get(world).remove(plot);
return;
}
final PlotWorld plotworld = PS.get().getPlotWorld(world);
RunnableVal<PlotAnalysis> run = new RunnableVal<PlotAnalysis>() {
@Override
public void run() {
PlotAnalysis changed = this.value;
if (Settings.CLEAR_THRESHOLD != -1 && plotworld.TYPE == 0 && changed != null) {
if (changed.getComplexity() > Settings.CLEAR_THRESHOLD) {
PS.log("$2[&5Expire&dManager$2] &bIgnoring modified plot: " + plot + " : " + changed.getComplexity() + " - " + changed.changes);
FlagManager.addPlotFlag(plot, new Flag(FlagManager.getFlag("modified-blocks"), value));
expiredPlots.get(world).remove(plot);
return;
}
}
if (plot.settings.isMerged()) {
MainUtil.unlinkPlot(plot);
}
plot.delete();
expiredPlots.get(world).remove(plot);
int complexity = changed == null ? 0 : changed.getComplexity();
int modified = changed == null ? 0 : changed.changes;
PS.log("$2[&5Expire&dManager$2] &cDeleted expired plot: " + plot + " : " + complexity + " - " + modified);
PS.log("$4 - World: " + plot.world);
if (plot.hasOwner()) {
PS.log("$4 - Owner: " + UUIDHandler.getName(plot.owner));
} else {
PS.log("$4 - Owner: Unowned");
}
}
};
if (Settings.CLEAR_THRESHOLD != -1 && plotworld.TYPE == 0) {
PlotAnalysis analysis = plot.getComplexity();
if (analysis != null) {
if (analysis.getComplexity() > Settings.CLEAR_THRESHOLD) {
PS.log("$2[&5Expire&dManager$2] &bSkipping modified: " + plot);
2015-06-15 18:47:56 +02:00
expiredPlots.get(world).remove(plot);
this.run();
2015-06-15 18:47:56 +02:00
return;
}
}
HybridUtils.manager.analyzePlot(plot, run);
}
else {
run.value = null;
run.run();
}
return;
2015-01-09 15:05:10 +01:00
}
}
catch (Exception e) {
e.printStackTrace();
2015-01-09 15:05:10 +01:00
}
}
}, Settings.CLEAR_INTERVAL * 20);
2015-01-09 15:05:10 +01:00
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public static boolean isExpired(final UUID uuid) {
2015-03-20 03:13:27 +01:00
if (UUIDHandler.getPlayer(uuid) != null) {
return false;
}
2015-02-20 07:34:19 +01:00
final String name = UUIDHandler.getName(uuid);
if (name != null) {
long last;
if (dates.contains(uuid)) {
last = dates.get(uuid);
}
else {
final OfflinePlayer op = Bukkit.getOfflinePlayer(name);
if (op.hasPlayedBefore()) {
last = op.getLastPlayed();
dates.put(uuid, last);
}
else {
return false;
2015-02-20 07:34:19 +01:00
}
}
if (last == 0) {
return false;
}
final long compared = System.currentTimeMillis() - last;
if (compared >= (86400000l * Settings.AUTO_CLEAR_DAYS)) {
return true;
}
2015-02-20 07:34:19 +01:00
}
2015-01-09 15:05:10 +01:00
return false;
}
2015-03-20 03:13:27 +01:00
public static boolean isExpired(Plot plot) {
for (UUID owner : PlotHandler.getOwners(plot)) {
if (!isExpired(owner)) {
return false;
}
}
return true;
}
2015-02-23 02:32:27 +01:00
public static List<Plot> getOldPlots(final String world) {
2015-07-14 16:12:12 +02:00
final ArrayList<Plot> plots = new ArrayList<>(PS.get().getPlots(world).values());
final List<Plot> toRemove = new ArrayList<>();
2015-05-15 10:12:52 +02:00
Iterator<Plot> iter = plots.iterator();
while (iter.hasNext()) {
Plot plot = iter.next();
final Flag keepFlag = FlagManager.getPlotFlag(plot, "keep");
if (keepFlag != null && (Boolean) keepFlag.getValue()) {
continue;
}
2015-03-20 05:11:02 +01:00
final UUID uuid = plot.owner;
if (uuid == null) {
toRemove.add(plot);
2015-01-09 15:05:10 +01:00
continue;
}
2015-02-21 13:52:50 +01:00
final PlotPlayer player = UUIDHandler.getPlayer(uuid);
2015-01-10 11:20:20 +01:00
if (player != null) {
continue;
}
if (isExpired(plot)) {
toRemove.add(plot);
2015-01-09 15:05:10 +01:00
}
}
return toRemove;
}
}