Factions3/src/com/massivecraft/factions/task/TaskPlayerPowerUpdate.java

73 lines
2.4 KiB
Java
Raw Normal View History

2013-04-23 12:14:36 +02:00
package com.massivecraft.factions.task;
import com.massivecraft.factions.entity.BoardColl;
import com.massivecraft.factions.entity.Faction;
2013-04-23 12:14:36 +02:00
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MFlag;
import com.massivecraft.factions.entity.MPlayer;
2014-06-04 16:47:01 +02:00
import com.massivecraft.factions.event.EventFactionsPowerChange;
import com.massivecraft.factions.event.EventFactionsPowerChange.PowerChangeReason;
2014-06-04 14:02:23 +02:00
import com.massivecraft.massivecore.ModuloRepeatTask;
import com.massivecraft.massivecore.ps.PS;
2014-12-03 02:13:39 +01:00
import com.massivecraft.massivecore.util.MUtil;
2014-06-04 14:02:23 +02:00
import com.massivecraft.massivecore.util.TimeUnit;
2017-03-24 13:05:58 +01:00
import org.bukkit.entity.Player;
2013-04-23 12:14:36 +02:00
public class TaskPlayerPowerUpdate extends ModuloRepeatTask
2013-04-23 12:14:36 +02:00
{
// -------------------------------------------- //
2017-03-14 05:07:14 +01:00
// INSTANCE
2013-04-23 12:14:36 +02:00
// -------------------------------------------- //
private static TaskPlayerPowerUpdate i = new TaskPlayerPowerUpdate();
public static TaskPlayerPowerUpdate get() { return i; }
2013-04-23 12:14:36 +02:00
// -------------------------------------------- //
2017-03-14 05:07:14 +01:00
// OVERRIDE
2013-04-23 12:14:36 +02:00
// -------------------------------------------- //
@Override
public long getDelayMillis()
{
2017-03-14 05:07:14 +01:00
// The interval is determined by the MConf rather than being set with setDelayMillis.
return (long) (MConf.get().taskPlayerPowerUpdateMinutes * TimeUnit.MILLIS_PER_MINUTE);
2013-04-23 12:14:36 +02:00
}
@Override
2013-04-24 08:59:43 +02:00
public void invoke(long now)
2013-04-23 12:14:36 +02:00
{
long millis = this.getDelayMillis();
2017-03-14 05:07:14 +01:00
MFlag flagPowerGain = MFlag.getFlagPowergain();
2013-04-23 12:14:36 +02:00
2017-03-14 05:07:14 +01:00
// For each player ...
2014-12-03 02:13:39 +01:00
for (Player player : MUtil.getOnlinePlayers())
2013-04-23 12:14:36 +02:00
{
2017-03-14 05:07:14 +01:00
// ... that is a living player ...
2015-05-16 14:30:49 +02:00
if (MUtil.isntPlayer(player)) continue;
2013-04-23 12:14:36 +02:00
if (player.isDead()) continue;
2017-03-14 05:07:14 +01:00
// ... in a faction territory that permits power gain ...
2016-07-01 15:47:48 +02:00
Faction faction = BoardColl.get().getFactionAt(PS.valueOf(player));
2017-03-14 05:07:14 +01:00
if (!faction.getFlag(flagPowerGain)) return;
2017-03-14 05:07:14 +01:00
// ... in a world that permits power gain ...
if (!MConf.get().worldsPowerGainEnabled.contains(player)) return;
MPlayer mplayer = MPlayer.get(player);
2017-03-14 05:07:14 +01:00
// ... calculate new power ...
double newPower = mplayer.getPower() + mplayer.getPowerPerHour() * millis / TimeUnit.MILLIS_PER_HOUR;
2013-04-23 12:14:36 +02:00
2017-03-14 05:07:14 +01:00
// ... and if other plugins don't object ...
EventFactionsPowerChange event = new EventFactionsPowerChange(null, mplayer, PowerChangeReason.TIME, newPower);
2013-04-23 12:14:36 +02:00
event.run();
if (event.isCancelled()) continue;
2017-03-14 05:07:14 +01:00
// ... set the new power for the player.
newPower = event.getNewPower();
mplayer.setPower(newPower);
2013-04-23 12:14:36 +02:00
}
}
2017-01-03 11:47:51 +01:00
2013-04-23 12:14:36 +02:00
}