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;
|
|
|
|
|
2013-04-24 08:45:52 +02:00
|
|
|
public class TaskPlayerPowerUpdate extends ModuloRepeatTask
|
2013-04-23 12:14:36 +02:00
|
|
|
{
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// INSTANCE & CONSTRUCT
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2013-04-24 08:45:52 +02:00
|
|
|
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()
|
|
|
|
{
|
2013-04-24 08:45:52 +02:00
|
|
|
return (long) (MConf.get().taskPlayerPowerUpdateMinutes * TimeUnit.MILLIS_PER_MINUTE);
|
2013-04-23 12:14:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setDelayMillis(long delayMillis)
|
|
|
|
{
|
2013-04-24 08:45:52 +02:00
|
|
|
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);
|
2013-04-23 17:01:43 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|