From de3c7436fee8879dad90bf90714d42927c2a5f63 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Sun, 26 Feb 2012 16:55:58 -0600 Subject: [PATCH] "autoLeaveAfterDaysOfInactivity" routine now runs only once every few minutes instead of running every time a player logs in. New setting "autoLeaveRoutineRunsEveryXMinutes" (default 5 minutes) to determine just how often the routine is run. The routine is also slightly more careful about how it calculates how long a player has been offline. --- src/com/massivecraft/factions/Conf.java | 1 + src/com/massivecraft/factions/FPlayers.java | 2 +- src/com/massivecraft/factions/P.java | 27 ++++++++++++++++++- .../listeners/FactionsPlayerListener.java | 10 +++++-- .../factions/util/AutoLeaveTask.java | 24 +++++++++++++++++ 5 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 src/com/massivecraft/factions/util/AutoLeaveTask.java diff --git a/src/com/massivecraft/factions/Conf.java b/src/com/massivecraft/factions/Conf.java index 157e445d..5c8494c7 100644 --- a/src/com/massivecraft/factions/Conf.java +++ b/src/com/massivecraft/factions/Conf.java @@ -81,6 +81,7 @@ public class Conf public static String allianceChatFormat = ChatColor.LIGHT_PURPLE+"%s:"+ChatColor.WHITE+" %s"; public static double autoLeaveAfterDaysOfInactivity = 10.0; + public static double autoLeaveRoutineRunsEveryXMinutes = 5.0; public static boolean removePlayerDataWhenBanned = true; public static boolean worldGuardChecking = false; diff --git a/src/com/massivecraft/factions/FPlayers.java b/src/com/massivecraft/factions/FPlayers.java index 186b47a8..2929caa1 100644 --- a/src/com/massivecraft/factions/FPlayers.java +++ b/src/com/massivecraft/factions/FPlayers.java @@ -60,7 +60,7 @@ public class FPlayers extends PlayerEntityCollection for (FPlayer fplayer : FPlayers.i.get()) { - if (now - fplayer.getLastLoginTime() > toleranceMillis) + if (fplayer.isOffline() && now - fplayer.getLastLoginTime() > toleranceMillis) { if (Conf.logFactionLeave || Conf.logFactionKick) P.p.log("Player "+fplayer.getName()+" was auto-removed due to inactivity."); diff --git a/src/com/massivecraft/factions/P.java b/src/com/massivecraft/factions/P.java index e2c2f806..c4b8b569 100644 --- a/src/com/massivecraft/factions/P.java +++ b/src/com/massivecraft/factions/P.java @@ -34,6 +34,7 @@ import com.massivecraft.factions.struct.ChatMode; import com.massivecraft.factions.struct.FFlag; import com.massivecraft.factions.struct.FPerm; import com.massivecraft.factions.struct.Rel; +import com.massivecraft.factions.util.AutoLeaveTask; import com.massivecraft.factions.zcore.MPlugin; import com.google.gson.GsonBuilder; @@ -56,6 +57,7 @@ public class P extends MPlugin private boolean locked = false; public boolean getLocked() {return this.locked;} public void setLocked(boolean val) {this.locked = val; this.setAutoSave(val);} + private Integer AutoLeaveTask = null; // Commands public FCmdRoot cmdBase; @@ -99,6 +101,9 @@ public class P extends MPlugin Worldguard.init(this); } + // start up task which runs the autoLeaveAfterDaysOfInactivity routine + startAutoLeaveTask(false); + // Register Event Handlers getServer().getPluginManager().registerEvents(playerListener, this); getServer().getPluginManager().registerEvents(chatEarlyListener, this); @@ -131,9 +136,29 @@ public class P extends MPlugin Board.save(); Conf.save(); EssentialsFeatures.unhookChat(); + if (AutoLeaveTask != null) + { + this.getServer().getScheduler().cancelTask(AutoLeaveTask); + AutoLeaveTask = null; + } super.onDisable(); } - + + public void startAutoLeaveTask(boolean restartIfRunning) + { + if (AutoLeaveTask != null) + { + if ( ! restartIfRunning) return; + this.getServer().getScheduler().cancelTask(AutoLeaveTask); + } + + if (Conf.autoLeaveRoutineRunsEveryXMinutes > 0.0) + { + long ticks = (long)(20 * 60 * Conf.autoLeaveRoutineRunsEveryXMinutes); + AutoLeaveTask = getServer().getScheduler().scheduleSyncRepeatingTask(this, new AutoLeaveTask(), ticks, ticks); + } + } + @Override public void postAutoSave() { diff --git a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 1b266c31..3d4af5e9 100644 --- a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -150,10 +150,12 @@ public class FactionsPlayerListener implements Listener // Update the lastLoginTime for this fplayer me.setLastLoginTime(System.currentTimeMillis()); - + +/* This is now done in a separate task which runs every few minutes // Run the member auto kick routine. Twice to get to the admins... FPlayers.i.autoLeaveOnInactivityRoutine(); FPlayers.i.autoLeaveOnInactivityRoutine(); + */ SpoutFeatures.updateAppearancesShortly(event.getPlayer()); } @@ -161,9 +163,13 @@ public class FactionsPlayerListener implements Listener @EventHandler(priority = EventPriority.NORMAL) public void onPlayerQuit(PlayerQuitEvent event) { - // Make sure player's power is up to date when they log off. FPlayer me = FPlayers.i.get(event.getPlayer()); + + // Make sure player's power is up to date when they log off. me.getPower(); + // and update their last login time to point to when the logged off, for auto-remove routine + me.setLastLoginTime(System.currentTimeMillis()); + SpoutFeatures.playerDisconnect(me); } diff --git a/src/com/massivecraft/factions/util/AutoLeaveTask.java b/src/com/massivecraft/factions/util/AutoLeaveTask.java new file mode 100644 index 00000000..bccb33ff --- /dev/null +++ b/src/com/massivecraft/factions/util/AutoLeaveTask.java @@ -0,0 +1,24 @@ +package com.massivecraft.factions.util; + +import com.massivecraft.factions.Conf; +import com.massivecraft.factions.FPlayers; +import com.massivecraft.factions.P; + +public class AutoLeaveTask implements Runnable +{ + double rate; + + public AutoLeaveTask() + { + this.rate = Conf.autoLeaveRoutineRunsEveryXMinutes; + } + + public void run() + { + FPlayers.i.autoLeaveOnInactivityRoutine(); + + // maybe setting has been changed? if so, restart task at new rate + if (this.rate != Conf.autoLeaveRoutineRunsEveryXMinutes) + P.p.startAutoLeaveTask(true); + } +}