Saber-Factions/src/main/java/com/massivecraft/factions/util/AutoLeaveProcessTask.java

94 lines
3.3 KiB
Java
Raw Normal View History

package com.massivecraft.factions.util;
2014-04-04 20:55:21 +02:00
import com.massivecraft.factions.*;
import com.massivecraft.factions.struct.Role;
2014-04-04 20:55:21 +02:00
import org.bukkit.scheduler.BukkitRunnable;
2014-04-04 20:55:21 +02:00
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.logging.Level;
2014-04-04 20:55:21 +02:00
public class AutoLeaveProcessTask extends BukkitRunnable {
2014-04-04 20:55:21 +02:00
private transient boolean readyToGo = false;
private transient boolean finished = false;
private transient ListIterator<FPlayer> iterator;
private transient double toleranceMillis;
public AutoLeaveProcessTask() {
ArrayList<FPlayer> fplayers = (ArrayList<FPlayer>) FPlayers.getInstance().getAllFPlayers();
2014-07-01 22:10:18 +02:00
this.iterator = fplayers.listIterator();
this.toleranceMillis = Conf.autoLeaveAfterDaysOfInactivity * 24 * 60 * 60 * 1000;
this.readyToGo = true;
2014-04-04 20:55:21 +02:00
this.finished = false;
}
public void run() {
if (Conf.autoLeaveAfterDaysOfInactivity <= 0.0 || Conf.autoLeaveRoutineMaxMillisecondsPerTick <= 0.0) {
2014-07-01 22:10:18 +02:00
this.stop();
return;
2014-04-04 20:55:21 +02:00
}
if (!readyToGo) {
2014-07-01 22:10:18 +02:00
return;
}
2014-04-04 20:55:21 +02:00
// this is set so it only does one iteration at a time, no matter how frequently the timer fires
readyToGo = false;
// and this is tracked to keep one iteration from dragging on too long and possibly choking the system if there are a very large number of players to go through
long loopStartTime = System.currentTimeMillis();
while (iterator.hasNext()) {
long now = System.currentTimeMillis();
// if this iteration has been running for maximum time, stop to take a breather until next tick
if (now > loopStartTime + Conf.autoLeaveRoutineMaxMillisecondsPerTick) {
2014-07-01 22:10:18 +02:00
readyToGo = true;
return;
2014-04-04 20:55:21 +02:00
}
FPlayer fplayer = iterator.next();
// Check if they should be exempt from this.
if (!fplayer.willAutoLeave()) {
2018-11-07 06:38:43 +01:00
SavageFactions.plugin.debug(Level.INFO, fplayer.getName() + " was going to be auto-removed but was set not to.");
continue;
}
2014-04-04 20:55:21 +02:00
if (fplayer.isOffline() && now - fplayer.getLastLoginTime() > toleranceMillis) {
2014-07-01 21:49:42 +02:00
if (Conf.logFactionLeave || Conf.logFactionKick) {
2018-11-07 06:38:43 +01:00
SavageFactions.plugin.log("Player " + fplayer.getName() + " was auto-removed due to inactivity.");
2014-07-01 21:49:42 +02:00
}
2014-04-04 20:55:21 +02:00
// if player is faction admin, sort out the faction since he's going away
2018-11-07 06:38:43 +01:00
if (fplayer.getRole() == Role.LEADER) {
2014-04-04 20:55:21 +02:00
Faction faction = fplayer.getFaction();
2014-07-01 22:10:18 +02:00
if (faction != null) {
fplayer.getFaction().promoteNewLeader(true);
2014-07-01 22:10:18 +02:00
}
2014-04-04 20:55:21 +02:00
}
2014-07-01 22:10:18 +02:00
fplayer.leave(false);
iterator.remove(); // go ahead and remove this list's link to the FPlayer object
2015-05-13 06:17:22 +02:00
if (Conf.autoLeaveDeleteFPlayerData) {
fplayer.remove();
}
2014-04-04 20:55:21 +02:00
}
}
// looks like we've finished
this.stop();
}
// we're done, shut down
public void stop() {
2014-07-01 22:10:18 +02:00
readyToGo = false;
finished = true;
2014-04-04 20:55:21 +02:00
this.cancel();
}
public boolean isFinished() {
return finished;
}
}