Factions/src/com/massivecraft/factions/engine/EngineCleanInactivity.java

98 lines
2.6 KiB
Java
Raw Normal View History

2017-05-19 08:47:42 +02:00
package com.massivecraft.factions.engine;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.entity.MPlayerColl;
import com.massivecraft.massivecore.Engine;
2017-07-25 12:12:50 +02:00
import com.massivecraft.massivecore.event.EventMassiveCorePlayerCleanInactivityToleranceMillis;
2017-05-19 08:47:42 +02:00
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import java.util.Map;
import java.util.Map.Entry;
2017-07-25 12:12:50 +02:00
public class EngineCleanInactivity extends Engine
2017-05-19 08:47:42 +02:00
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
2017-07-25 12:12:50 +02:00
private static EngineCleanInactivity i = new EngineCleanInactivity();
public static EngineCleanInactivity get() { return i; }
2017-05-19 08:47:42 +02:00
// -------------------------------------------- //
// REMOVE PLAYER MILLIS
// -------------------------------------------- //
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
2017-07-25 12:12:50 +02:00
public void ageBonus(EventMassiveCorePlayerCleanInactivityToleranceMillis event)
2017-05-19 08:47:42 +02:00
{
if (event.getColl() != MPlayerColl.get()) return;
applyPlayerAgeBonus(event);
applyFactionAgeBonus(event);
}
2017-07-25 12:12:50 +02:00
public void applyPlayerAgeBonus(EventMassiveCorePlayerCleanInactivityToleranceMillis event)
2017-05-19 08:47:42 +02:00
{
// Calculate First Played
Long firstPlayed = event.getEntity().getFirstPlayed();
Long age = 0L;
if (firstPlayed != null)
{
age = System.currentTimeMillis() - firstPlayed;
}
// Calculate the Bonus!
2017-07-25 12:12:50 +02:00
Long bonus = calculateBonus(age, MConf.get().cleanInactivityToleranceMillisPlayerAgeToBonus);
2017-05-19 08:47:42 +02:00
if (bonus == null) return;
// Apply
event.getToleranceCauseMillis().put("Player Age Bonus", bonus);
}
2017-07-25 12:12:50 +02:00
public void applyFactionAgeBonus(EventMassiveCorePlayerCleanInactivityToleranceMillis event)
2017-05-19 08:47:42 +02:00
{
// Calculate Faction Age
Faction faction = ((MPlayer)event.getEntity()).getFaction();
long age = 0L;
if ( ! faction.isNone())
{
age = faction.getAge();
}
// Calculate the Bonus!
2017-07-25 12:12:50 +02:00
Long bonus = calculateBonus(age, MConf.get().cleanInactivityToleranceMillisFactionAgeToBonus);
2017-05-19 08:47:42 +02:00
if (bonus == null) return;
// Apply
event.getToleranceCauseMillis().put("Faction Age Bonus", bonus);
}
private Long calculateBonus(long age, Map<Long, Long> ageToBonus)
{
if (ageToBonus.isEmpty()) return null;
Long bonus = 0L;
for (Entry<Long, Long> entry : ageToBonus.entrySet())
{
Long key = entry.getKey();
if (key == null) continue;
Long value = entry.getValue();
if (value == null) continue;
if (age >= key)
{
bonus = value;
break;
}
}
return bonus;
}
}