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

64 lines
1.9 KiB
Java
Raw Normal View History

2013-04-23 12:14:36 +02:00
package com.massivecraft.factions.task;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import com.massivecraft.factions.entity.MConf;
2013-04-25 13:25:15 +02:00
import com.massivecraft.factions.entity.UConf;
2013-04-23 12:14:36 +02:00
import com.massivecraft.factions.entity.UPlayer;
import com.massivecraft.factions.event.FactionsEventPowerChange;
import com.massivecraft.factions.event.FactionsEventPowerChange.PowerChangeReason;
import com.massivecraft.mcore.ModuloRepeatTask;
import com.massivecraft.mcore.util.TimeUnit;
public class TaskPlayerPowerUpdate extends ModuloRepeatTask
2013-04-23 12:14:36 +02:00
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static TaskPlayerPowerUpdate i = new TaskPlayerPowerUpdate();
public static TaskPlayerPowerUpdate get() { return i; }
2013-04-23 12:14:36 +02:00
// -------------------------------------------- //
// OVERRIDE: MODULO REPEAT TASK
// -------------------------------------------- //
@Override
public long getDelayMillis()
{
return (long) (MConf.get().taskPlayerPowerUpdateMinutes * TimeUnit.MILLIS_PER_MINUTE);
2013-04-23 12:14:36 +02:00
}
@Override
public void setDelayMillis(long delayMillis)
{
MConf.get().taskPlayerPowerUpdateMinutes = delayMillis / (double) 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();
for (Player player : Bukkit.getOnlinePlayers())
{
2013-04-25 13:25:15 +02:00
// Check disabled
if (UConf.isDisabled(player)) continue;
2013-04-23 12:14:36 +02:00
if (player.isDead()) continue;
UPlayer uplayer = UPlayer.get(player);
double newPower = uplayer.getPower() + uplayer.getPowerPerHour() * millis / TimeUnit.MILLIS_PER_HOUR;
2013-04-23 12:14:36 +02:00
FactionsEventPowerChange event = new FactionsEventPowerChange(null, uplayer, PowerChangeReason.TIME, newPower);
event.run();
if (event.isCancelled()) continue;
newPower = event.getNewPower();
uplayer.setPower(newPower);
}
}
}