Make the recurring tasks non-tps-dependent.

This commit is contained in:
Olof Larsson 2013-04-18 13:36:52 +02:00
parent 1c5c5557b0
commit af094b6647
5 changed files with 78 additions and 105 deletions

View File

@ -122,22 +122,26 @@ public class FPlayerColl extends SenderColl<FPlayer>
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();
}
}
}

View File

@ -189,8 +189,9 @@ public class FactionColl extends Coll<Faction>
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())
{

View File

@ -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
// -------------------------------------------- //

View File

@ -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);
}
}

View File

@ -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);
}
}
}