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

60 lines
1.9 KiB
Java
Raw Normal View History

2017-01-03 11:47:51 +01:00
package com.massivecraft.factions.engine;
2017-03-24 13:05:58 +01:00
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.massivecore.Engine;
import com.massivecraft.massivecore.util.MUtil;
2017-01-03 11:47:51 +01:00
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
2017-03-24 13:05:58 +01:00
import java.text.MessageFormat;
2017-01-03 11:47:51 +01:00
public class EngineTerritoryShield extends Engine
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static EngineTerritoryShield i = new EngineTerritoryShield();
public static EngineTerritoryShield get() { return i; }
// -------------------------------------------- //
// TERRITORY SHIELD
// -------------------------------------------- //
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void territoryShield(EntityDamageByEntityEvent event)
{
// If the entity is a player ...
Entity entity = event.getEntity();
if (MUtil.isntPlayer(entity)) return;
Player player = (Player)entity;
MPlayer mplayer = MPlayer.get(player);
// ... and the attacker is a player ...
Entity attacker = MUtil.getLiableDamager(event);
if (! (attacker instanceof Player)) return;
// ... and that player has a faction ...
if ( ! mplayer.hasFaction()) return;
// ... and that player is in their own territory ...
if ( ! mplayer.isInOwnTerritory()) return;
// ... and a territoryShieldFactor is configured ...
if (MConf.get().territoryShieldFactor <= 0) return;
// ... then scale the damage ...
double factor = 1D - MConf.get().territoryShieldFactor;
MUtil.scaleDamage(event, factor);
// ... and inform.
String perc = MessageFormat.format("{0,number,#%}", (MConf.get().territoryShieldFactor));
mplayer.msg("<i>Enemy damage reduced by <rose>%s<i>.", perc);
}
}