diff --git a/src/com/massivecraft/factions/FPlayerColl.java b/src/com/massivecraft/factions/FPlayerColl.java index db08a9cd..9468185a 100644 --- a/src/com/massivecraft/factions/FPlayerColl.java +++ b/src/com/massivecraft/factions/FPlayerColl.java @@ -122,22 +122,26 @@ public class FPlayerColl extends SenderColl Long lastPlayed = Mixin.getLastPlayed(fplayer.getId()); if (lastPlayed == null) continue; - if (fplayer.isOffline() && now - lastPlayed > toleranceMillis) + if (fplayer.isOnline()) continue; + if (now - lastPlayed <= toleranceMillis) continue; + + if (ConfServer.logFactionLeave || ConfServer.logFactionKick) { - if (ConfServer.logFactionLeave || ConfServer.logFactionKick) - Factions.get().log("Player "+fplayer.getName()+" was auto-removed due to inactivity."); - - // if player is faction leader, sort out the faction since he's going away - if (fplayer.getRole() == Rel.LEADER) - { - Faction faction = fplayer.getFaction(); - if (faction != null) - fplayer.getFaction().promoteNewLeader(); - } - - fplayer.leave(false); - fplayer.detach(); + Factions.get().log("Player "+fplayer.getName()+" was auto-removed due to inactivity."); } + + // if player is faction leader, sort out the faction since he's going away + if (fplayer.getRole() == Rel.LEADER) + { + Faction faction = fplayer.getFaction(); + if (faction != null) + { + fplayer.getFaction().promoteNewLeader(); + } + } + + fplayer.leave(false); + fplayer.detach(); } } } diff --git a/src/com/massivecraft/factions/FactionColl.java b/src/com/massivecraft/factions/FactionColl.java index a8ec6095..5b43ccb1 100644 --- a/src/com/massivecraft/factions/FactionColl.java +++ b/src/com/massivecraft/factions/FactionColl.java @@ -189,8 +189,9 @@ public class FactionColl extends Coll public void econLandRewardRoutine() { - if ( ! Econ.shouldBeUsed()) return; - + if (!Econ.shouldBeUsed()) return; + if (ConfServer.econLandReward == 0.0) return; + Factions.get().log("Running econLandRewardRoutine..."); for (Faction faction : this.getAll()) { diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 253f78b0..debd0922 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -46,10 +46,6 @@ public class Factions extends MPlugin public FactionsChatListener chatListener; public FactionsEntityListener entityListener; public FactionsExploitListener exploitListener; - - // Task Ids - private Integer AutoLeaveTask = null; - private Integer econLandRewardTaskID = null; // -------------------------------------------- // // OVERRIDE @@ -82,11 +78,9 @@ public class Factions extends MPlugin Worldguard.init(this); } - // start up task which runs the autoLeaveAfterDaysOfInactivity routine - startAutoLeaveTask(false); - - // start up task which runs the econLandRewardRoutine - startEconLandRewardTask(false); + // Schedule recurring non-tps-dependent tasks + AutoLeaveTask.get().schedule(this); + EconLandRewardTask.get().schedule(this); // Register Event Handlers MainListener.get().setup(); @@ -119,54 +113,6 @@ public class Factions extends MPlugin ; } - @Override - public void onDisable() - { - 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 (ConfServer.autoLeaveRoutineRunsEveryXMinutes > 0.0) - { - long ticks = (long)(20 * 60 * ConfServer.autoLeaveRoutineRunsEveryXMinutes); - AutoLeaveTask = getServer().getScheduler().scheduleSyncRepeatingTask(this, new AutoLeaveTask(), ticks, ticks); - } - } - - public void startEconLandRewardTask(boolean restartIfRunning) - { - if (econLandRewardTaskID != null) - { - if (!restartIfRunning) return; - this.getServer().getScheduler().cancelTask(econLandRewardTaskID); - } - - if - ( - ConfServer.econEnabled && - ConfServer.econLandRewardTaskRunsEveryXMinutes > 0.0 && - ConfServer.econLandReward > 0.0 - ) - { - long ticks = (long)(20 * 60 * ConfServer.econLandRewardTaskRunsEveryXMinutes); - econLandRewardTaskID = getServer().getScheduler().scheduleSyncRepeatingTask(this, new EconLandRewardTask(), ticks, ticks); - } - } - // -------------------------------------------- // // Functions for other plugins to hook into // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/task/AutoLeaveTask.java b/src/com/massivecraft/factions/task/AutoLeaveTask.java index 18c62cbf..b55129ac 100644 --- a/src/com/massivecraft/factions/task/AutoLeaveTask.java +++ b/src/com/massivecraft/factions/task/AutoLeaveTask.java @@ -2,24 +2,38 @@ package com.massivecraft.factions.task; import com.massivecraft.factions.ConfServer; import com.massivecraft.factions.FPlayerColl; -import com.massivecraft.factions.Factions; +import com.massivecraft.mcore.ModuloRepeatTask; +import com.massivecraft.mcore.util.TimeUnit; -public class AutoLeaveTask implements Runnable +public class AutoLeaveTask extends ModuloRepeatTask { - double rate; - - public AutoLeaveTask() + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static AutoLeaveTask i = new AutoLeaveTask(); + public static AutoLeaveTask get() { return i; } + + // -------------------------------------------- // + // OVERRIDE: MODULO REPEAT TASK + // -------------------------------------------- // + + @Override + public long getDelayMillis() { - this.rate = ConfServer.autoLeaveRoutineRunsEveryXMinutes; + return (long) (ConfServer.autoLeaveRoutineRunsEveryXMinutes * TimeUnit.MILLIS_PER_MINUTE); } - - public void run() + + @Override + public void setDelayMillis(long delayMillis) + { + throw new RuntimeException("operation not supported"); + } + + @Override + public void invoke() { FPlayerColl.get().autoLeaveOnInactivityRoutine(); - - // TODO: Make it a polling and non-tps-dependent system instead. - // maybe setting has been changed? if so, restart task at new rate - if (this.rate != ConfServer.autoLeaveRoutineRunsEveryXMinutes) - Factions.get().startAutoLeaveTask(true); } + } diff --git a/src/com/massivecraft/factions/task/EconLandRewardTask.java b/src/com/massivecraft/factions/task/EconLandRewardTask.java index a236bfa1..6b4b0997 100644 --- a/src/com/massivecraft/factions/task/EconLandRewardTask.java +++ b/src/com/massivecraft/factions/task/EconLandRewardTask.java @@ -2,30 +2,38 @@ package com.massivecraft.factions.task; import com.massivecraft.factions.ConfServer; import com.massivecraft.factions.FactionColl; -import com.massivecraft.factions.Factions; +import com.massivecraft.mcore.ModuloRepeatTask; +import com.massivecraft.mcore.util.TimeUnit; -public class EconLandRewardTask implements Runnable +public class EconLandRewardTask extends ModuloRepeatTask { - double rate; + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static EconLandRewardTask i = new EconLandRewardTask(); + public static EconLandRewardTask get() { return i; } + + // -------------------------------------------- // + // OVERRIDE: MODULO REPEAT TASK + // -------------------------------------------- // - public EconLandRewardTask() - { - this.rate = ConfServer.econLandRewardTaskRunsEveryXMinutes; - } - @Override - public void run() + public long getDelayMillis() + { + return (long) (ConfServer.econLandRewardTaskRunsEveryXMinutes * TimeUnit.MILLIS_PER_MINUTE); + } + + @Override + public void setDelayMillis(long delayMillis) + { + throw new RuntimeException("operation not supported"); + } + + @Override + public void invoke() { FactionColl.get().econLandRewardRoutine(); - - // TODO: This technique is TPS dependent and wrong. - // Instead of restarting a TPS dependent task the task should poll every once in a while for the system millis. - // With such a setup the need for restarts are gone. - - // maybe setting has been changed? if so, restart task at new rate - if (this.rate != ConfServer.econLandRewardTaskRunsEveryXMinutes) - { - Factions.get().startEconLandRewardTask(true); - } } + }