2011-07-18 22:06:02 +02:00
package com.massivecraft.factions.listeners ;
2011-02-06 13:36:11 +01:00
2014-04-04 20:55:21 +02:00
import com.massivecraft.factions.* ;
import com.massivecraft.factions.event.PowerLossEvent ;
import com.massivecraft.factions.struct.Relation ;
import com.massivecraft.factions.util.MiscUtil ;
2015-01-23 20:25:14 +01:00
import com.massivecraft.factions.zcore.util.TL ;
2012-12-19 13:18:56 +01:00
import org.bukkit.Bukkit ;
2011-08-02 03:22:16 +02:00
import org.bukkit.Location ;
2015-01-05 16:58:32 +01:00
import org.bukkit.TravelAgent ;
New setting "handleExploitTNTWaterlog" (default false/disabled) which, if enabled, will cause TNT which explodes in liquid to actually destroy a single adjacent block in all 6 directions. This will only apply to blocks which can be destroyed by TNT normally, specifically anything other than air, bedrock, water, lava, obsidian, and enchanting tables. The destruction of these blocks will be handled as if they had been mined, dropping the appropriate item.
TNT in water/lava doesn't normally destroy any surrounding blocks, which is usually desired behavior. That's the reason this setting is disabled by default. However, it is available because it provides a method to get through waterwalls with enough persistence, and it makes cheap (non-obsidian) TNT cannons require minor maintenance between shots. Both are useful things for my server.
2012-03-19 17:18:40 +01:00
import org.bukkit.block.Block ;
2014-04-04 20:55:21 +02:00
import org.bukkit.entity.* ;
2013-04-23 15:52:39 +02:00
import org.bukkit.entity.minecart.ExplosiveMinecart ;
2012-01-28 11:37:55 +01:00
import org.bukkit.event.EventHandler ;
import org.bukkit.event.EventPriority ;
import org.bukkit.event.Listener ;
2014-04-04 20:55:21 +02:00
import org.bukkit.event.entity.* ;
2012-10-31 22:16:19 +01:00
import org.bukkit.event.hanging.HangingBreakByEntityEvent ;
import org.bukkit.event.hanging.HangingBreakEvent ;
2012-11-03 12:21:28 +01:00
import org.bukkit.event.hanging.HangingBreakEvent.RemoveCause ;
2012-10-31 22:16:19 +01:00
import org.bukkit.event.hanging.HangingPlaceEvent ;
2015-01-05 16:58:32 +01:00
import org.bukkit.event.player.PlayerPortalEvent ;
2012-02-23 07:09:19 +01:00
import org.bukkit.potion.PotionEffect ;
import org.bukkit.potion.PotionEffectType ;
2014-04-06 00:00:33 +02:00
import org.bukkit.projectiles.ProjectileSource ;
2011-07-18 22:06:02 +02:00
2014-04-04 20:55:21 +02:00
import java.util.* ;
public class FactionsEntityListener implements Listener {
2014-08-05 17:17:27 +02:00
2014-04-04 20:55:21 +02:00
public P p ;
public FactionsEntityListener ( P p ) {
this . p = p ;
}
@EventHandler ( priority = EventPriority . NORMAL )
public void onEntityDeath ( EntityDeathEvent event ) {
2014-07-01 22:10:18 +02:00
Entity entity = event . getEntity ( ) ;
if ( ! ( entity instanceof Player ) ) {
2014-04-04 20:55:21 +02:00
return ;
}
2014-07-01 22:10:18 +02:00
Player player = ( Player ) entity ;
2014-10-19 07:37:25 +02:00
FPlayer fplayer = FPlayers . getInstance ( ) . getByPlayer ( player ) ;
Faction faction = Board . getInstance ( ) . getFactionAt ( new FLocation ( player . getLocation ( ) ) ) ;
2014-04-04 20:55:21 +02:00
PowerLossEvent powerLossEvent = new PowerLossEvent ( faction , fplayer ) ;
// Check for no power loss conditions
if ( faction . isWarZone ( ) ) {
// war zones always override worldsNoPowerLoss either way, thus this layout
if ( ! Conf . warZonePowerLoss ) {
2015-01-23 20:25:14 +01:00
powerLossEvent . setMessage ( TL . PLAYER_POWER_NOLOSS_WARZONE . toString ( ) ) ;
2014-04-04 20:55:21 +02:00
powerLossEvent . setCancelled ( true ) ;
2014-07-01 22:10:18 +02:00
}
if ( Conf . worldsNoPowerLoss . contains ( player . getWorld ( ) . getName ( ) ) ) {
2015-01-23 20:25:14 +01:00
powerLossEvent . setMessage ( TL . PLAYER_POWER_LOSS_WARZONE . toString ( ) ) ;
2014-04-04 20:55:21 +02:00
}
2015-09-08 18:46:48 +02:00
} else if ( faction . isWilderness ( ) & & ! Conf . wildernessPowerLoss & & ! Conf . worldsNoWildernessProtection . contains ( player . getWorld ( ) . getName ( ) ) ) {
2015-01-23 20:25:14 +01:00
powerLossEvent . setMessage ( TL . PLAYER_POWER_NOLOSS_WILDERNESS . toString ( ) ) ;
2014-04-04 20:55:21 +02:00
powerLossEvent . setCancelled ( true ) ;
2014-07-01 21:52:40 +02:00
} else if ( Conf . worldsNoPowerLoss . contains ( player . getWorld ( ) . getName ( ) ) ) {
2015-01-23 20:25:14 +01:00
powerLossEvent . setMessage ( TL . PLAYER_POWER_NOLOSS_WORLD . toString ( ) ) ;
2014-04-04 20:55:21 +02:00
powerLossEvent . setCancelled ( true ) ;
2014-07-01 21:52:40 +02:00
} else if ( Conf . peacefulMembersDisablePowerLoss & & fplayer . hasFaction ( ) & & fplayer . getFaction ( ) . isPeaceful ( ) ) {
2015-01-23 20:25:14 +01:00
powerLossEvent . setMessage ( TL . PLAYER_POWER_NOLOSS_PEACEFUL . toString ( ) ) ;
2014-04-04 20:55:21 +02:00
powerLossEvent . setCancelled ( true ) ;
2014-07-01 21:52:40 +02:00
} else {
2015-01-23 20:25:14 +01:00
powerLossEvent . setMessage ( TL . PLAYER_POWER_NOW . toString ( ) ) ;
2014-04-04 20:55:21 +02:00
}
// call Event
Bukkit . getPluginManager ( ) . callEvent ( powerLossEvent ) ;
// Call player onDeath if the event is not cancelled
if ( ! powerLossEvent . isCancelled ( ) ) {
fplayer . onDeath ( ) ;
}
// Send the message from the powerLossEvent
2014-07-01 22:10:18 +02:00
final String msg = powerLossEvent . getMessage ( ) ;
if ( msg ! = null & & ! msg . isEmpty ( ) ) {
2014-04-04 20:55:21 +02:00
fplayer . msg ( msg , fplayer . getPowerRounded ( ) , fplayer . getPowerMaxRounded ( ) ) ;
}
}
/ * *
* Who can I hurt ? I can never hurt members or allies . I can always hurt enemies . I can hurt neutrals as long as
* they are outside their own territory .
* /
2015-02-23 20:33:23 +01:00
@EventHandler ( priority = EventPriority . NORMAL , ignoreCancelled = true )
2014-04-04 20:55:21 +02:00
public void onEntityDamage ( EntityDamageEvent event ) {
if ( event instanceof EntityDamageByEntityEvent ) {
EntityDamageByEntityEvent sub = ( EntityDamageByEntityEvent ) event ;
if ( ! this . canDamagerHurtDamagee ( sub , true ) ) {
event . setCancelled ( true ) ;
}
2015-05-13 05:16:47 +02:00
// event is not cancelled by factions
Entity damagee = sub . getEntity ( ) ;
Entity damager = sub . getDamager ( ) ;
if ( damagee ! = null & & damagee instanceof Player ) {
cancelFStuckTeleport ( ( Player ) damagee ) ;
2018-03-04 23:13:32 +01:00
cancelFFly ( ( Player ) damagee ) ;
2015-05-13 05:16:47 +02:00
}
if ( damager instanceof Player ) {
cancelFStuckTeleport ( ( Player ) damager ) ;
2018-03-04 23:13:32 +01:00
cancelFFly ( ( Player ) damager ) ;
2015-05-13 05:16:47 +02:00
}
2014-07-01 21:52:40 +02:00
} else if ( Conf . safeZonePreventAllDamageToPlayers & & isPlayerInSafeZone ( event . getEntity ( ) ) ) {
2014-04-04 20:55:21 +02:00
// Players can not take any damage in a Safe Zone
event . setCancelled ( true ) ;
}
2015-05-13 05:16:47 +02:00
// entity took generic damage?
Entity entity = event . getEntity ( ) ;
if ( entity instanceof Player ) {
2015-05-25 06:42:31 +02:00
Player player = ( Player ) entity ;
FPlayer me = FPlayers . getInstance ( ) . getByPlayer ( player ) ;
cancelFStuckTeleport ( player ) ;
if ( me . isWarmingUp ( ) ) {
me . clearWarmup ( ) ;
me . msg ( TL . WARMUPS_CANCELLED ) ;
}
2015-05-13 05:16:47 +02:00
}
}
2018-03-04 23:13:32 +01:00
private void cancelFFly ( Player player ) {
if ( player = = null ) {
return ;
}
FPlayer fPlayer = FPlayers . getInstance ( ) . getByPlayer ( player ) ;
if ( fPlayer . isFlying ( ) ) {
fPlayer . setFFlying ( false , true ) ;
}
}
2015-05-13 05:16:47 +02:00
public void cancelFStuckTeleport ( Player player ) {
2015-05-13 06:17:22 +02:00
if ( player = = null ) {
return ;
}
2015-05-13 05:16:47 +02:00
UUID uuid = player . getUniqueId ( ) ;
if ( P . p . getStuckMap ( ) . containsKey ( uuid ) ) {
FPlayers . getInstance ( ) . getByPlayer ( player ) . msg ( TL . COMMAND_STUCK_CANCELLED ) ;
P . p . getStuckMap ( ) . remove ( uuid ) ;
}
2014-04-04 20:55:21 +02:00
}
2015-02-23 20:33:23 +01:00
@EventHandler ( priority = EventPriority . NORMAL , ignoreCancelled = true )
2014-04-04 20:55:21 +02:00
public void onEntityExplode ( EntityExplodeEvent event ) {
2014-07-01 22:10:18 +02:00
Location loc = event . getLocation ( ) ;
Entity boomer = event . getEntity ( ) ;
2014-10-19 07:37:25 +02:00
Faction faction = Board . getInstance ( ) . getFactionAt ( new FLocation ( loc ) ) ;
2014-04-04 20:55:21 +02:00
2015-09-08 19:55:42 +02:00
if ( faction . noExplosionsInTerritory ( ) | | ( faction . isPeaceful ( ) & & Conf . peacefulTerritoryDisableBoom ) ) {
2014-04-04 20:55:21 +02:00
// faction is peaceful and has explosions set to disabled
2014-07-01 22:10:18 +02:00
event . setCancelled ( true ) ;
return ;
2014-04-04 20:55:21 +02:00
}
boolean online = faction . hasPlayersOnline ( ) ;
2014-05-20 23:45:13 +02:00
//TODO: :(
2015-09-08 18:46:48 +02:00
if ( boomer instanceof Creeper & & ( ( faction . isWilderness ( ) & & Conf . wildernessBlockCreepers & & ! Conf . worldsNoWildernessProtection . contains ( loc . getWorld ( ) . getName ( ) ) ) | |
2018-01-05 02:40:27 +01:00
( faction . isNormal ( ) & & ( online ? Conf . territoryBlockCreepers : Conf . territoryBlockCreepersWhenOffline ) ) | |
( faction . isWarZone ( ) & & Conf . warZoneBlockCreepers ) | |
faction . isSafeZone ( ) ) ) {
2014-04-04 20:55:21 +02:00
// creeper which needs prevention
event . setCancelled ( true ) ;
2014-07-01 21:52:40 +02:00
} else if (
2014-04-04 20:55:21 +02:00
// it's a bit crude just using fireball protection for Wither boss too, but I'd rather not add in a whole new set of xxxBlockWitherExplosion or whatever
2018-01-05 02:40:27 +01:00
( boomer instanceof Fireball | | boomer instanceof WitherSkull | | boomer instanceof Wither ) & & ( ( faction . isWilderness ( ) & & Conf . wildernessBlockFireballs & & ! Conf . worldsNoWildernessProtection . contains ( loc . getWorld ( ) . getName ( ) ) ) | |
( faction . isNormal ( ) & & ( online ? Conf . territoryBlockFireballs : Conf . territoryBlockFireballsWhenOffline ) ) | |
( faction . isWarZone ( ) & & Conf . warZoneBlockFireballs ) | |
faction . isSafeZone ( ) ) ) {
2014-04-04 20:55:21 +02:00
// ghast fireball which needs prevention
event . setCancelled ( true ) ;
2015-09-08 18:46:48 +02:00
} else if ( ( boomer instanceof TNTPrimed | | boomer instanceof ExplosiveMinecart ) & & ( ( faction . isWilderness ( ) & & Conf . wildernessBlockTNT & & ! Conf . worldsNoWildernessProtection . contains ( loc . getWorld ( ) . getName ( ) ) ) | |
2018-01-05 02:40:27 +01:00
( faction . isNormal ( ) & & ( online ? Conf . territoryBlockTNT : Conf . territoryBlockTNTWhenOffline ) ) | |
( faction . isWarZone ( ) & & Conf . warZoneBlockTNT ) | |
( faction . isSafeZone ( ) & & Conf . safeZoneBlockTNT ) ) ) {
2014-04-04 20:55:21 +02:00
// TNT which needs prevention
event . setCancelled ( true ) ;
2014-07-01 21:52:40 +02:00
} else if ( ( boomer instanceof TNTPrimed | | boomer instanceof ExplosiveMinecart ) & & Conf . handleExploitTNTWaterlog ) {
2014-04-04 20:55:21 +02:00
// TNT in water/lava doesn't normally destroy any surrounding blocks, which is usually desired behavior, but...
// this change below provides workaround for waterwalling providing perfect protection,
// and makes cheap (non-obsidian) TNT cannons require minor maintenance between shots
2014-07-01 22:10:18 +02:00
Block center = loc . getBlock ( ) ;
if ( center . isLiquid ( ) ) {
2014-04-04 20:55:21 +02:00
// a single surrounding block in all 6 directions is broken if the material is weak enough
2017-12-19 11:18:13 +01:00
List < Block > targets = new ArrayList < > ( ) ;
2014-07-01 22:10:18 +02:00
targets . add ( center . getRelative ( 0 , 0 , 1 ) ) ;
targets . add ( center . getRelative ( 0 , 0 , - 1 ) ) ;
targets . add ( center . getRelative ( 0 , 1 , 0 ) ) ;
targets . add ( center . getRelative ( 0 , - 1 , 0 ) ) ;
targets . add ( center . getRelative ( 1 , 0 , 0 ) ) ;
targets . add ( center . getRelative ( - 1 , 0 , 0 ) ) ;
for ( Block target : targets ) {
2014-04-04 20:55:21 +02:00
int id = target . getTypeId ( ) ;
// ignore air, bedrock, water, lava, obsidian, enchanting table, etc.... too bad we can't get a blast resistance value through Bukkit yet
2014-07-01 21:49:42 +02:00
if ( id ! = 0 & & ( id < 7 | | id > 11 ) & & id ! = 49 & & id ! = 90 & & id ! = 116 & & id ! = 119 & & id ! = 120 & & id ! = 130 ) {
2014-04-04 20:55:21 +02:00
target . breakNaturally ( ) ;
2014-07-01 21:49:42 +02:00
}
2014-04-04 20:55:21 +02:00
}
}
}
}
// mainly for flaming arrows; don't want allies or people in safe zones to be ignited even after damage event is cancelled
2015-02-23 20:33:23 +01:00
@EventHandler ( priority = EventPriority . NORMAL , ignoreCancelled = true )
2014-04-04 20:55:21 +02:00
public void onEntityCombustByEntity ( EntityCombustByEntityEvent event ) {
2016-12-13 17:08:28 +01:00
EntityDamageByEntityEvent sub = new EntityDamageByEntityEvent ( event . getCombuster ( ) , event . getEntity ( ) , EntityDamageEvent . DamageCause . FIRE , 0d ) ;
2014-07-01 22:10:18 +02:00
if ( ! this . canDamagerHurtDamagee ( sub , false ) ) {
event . setCancelled ( true ) ;
}
2014-04-04 20:55:21 +02:00
}
2017-12-19 11:18:13 +01:00
private static final Set < PotionEffectType > badPotionEffects = new LinkedHashSet < > ( Arrays . asList ( PotionEffectType . BLINDNESS , PotionEffectType . CONFUSION , PotionEffectType . HARM , PotionEffectType . HUNGER , PotionEffectType . POISON , PotionEffectType . SLOW , PotionEffectType . SLOW_DIGGING , PotionEffectType . WEAKNESS , PotionEffectType . WITHER ) ) ;
2014-04-04 20:55:21 +02:00
2015-02-23 20:33:23 +01:00
@EventHandler ( priority = EventPriority . NORMAL , ignoreCancelled = true )
2014-04-04 20:55:21 +02:00
public void onPotionSplashEvent ( PotionSplashEvent event ) {
// see if the potion has a harmful effect
2014-07-01 22:10:18 +02:00
boolean badjuju = false ;
for ( PotionEffect effect : event . getPotion ( ) . getEffects ( ) ) {
2014-04-04 20:55:21 +02:00
if ( badPotionEffects . contains ( effect . getType ( ) ) ) {
2014-07-01 22:10:18 +02:00
badjuju = true ;
break ;
2014-04-04 20:55:21 +02:00
}
2014-07-01 22:10:18 +02:00
}
if ( ! badjuju ) {
return ;
}
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
ProjectileSource thrower = event . getPotion ( ) . getShooter ( ) ;
if ( ! ( thrower instanceof Entity ) ) {
2014-04-06 00:00:33 +02:00
return ;
}
2014-04-04 20:55:21 +02:00
2014-11-06 01:36:47 +01:00
if ( thrower instanceof Player ) {
2014-10-01 00:20:06 +02:00
Player player = ( Player ) thrower ;
2014-10-19 07:37:25 +02:00
FPlayer fPlayer = FPlayers . getInstance ( ) . getByPlayer ( player ) ;
2014-11-06 01:36:47 +01:00
if ( badjuju & & fPlayer . getFaction ( ) . isPeaceful ( ) ) {
2014-10-01 00:20:06 +02:00
event . setCancelled ( true ) ;
return ;
}
}
2014-04-04 20:55:21 +02:00
// scan through affected entities to make sure they're all valid targets
2017-12-19 11:18:13 +01:00
for ( LivingEntity target : event . getAffectedEntities ( ) ) {
2014-04-06 00:00:33 +02:00
EntityDamageByEntityEvent sub = new EntityDamageByEntityEvent ( ( Entity ) thrower , target , EntityDamageEvent . DamageCause . CUSTOM , 0 ) ;
2014-07-01 21:49:42 +02:00
if ( ! this . canDamagerHurtDamagee ( sub , true ) ) {
2014-04-04 20:55:21 +02:00
event . setIntensity ( target , 0 . 0 ) ; // affected entity list doesn't accept modification (so no iter.remove()), but this works
2014-07-01 22:10:18 +02:00
}
2014-04-04 20:55:21 +02:00
}
}
public boolean isPlayerInSafeZone ( Entity damagee ) {
if ( ! ( damagee instanceof Player ) ) {
return false ;
2014-07-01 22:10:18 +02:00
}
2017-12-19 11:18:13 +01:00
return Board . getInstance ( ) . getFactionAt ( new FLocation ( damagee . getLocation ( ) ) ) . isSafeZone ( ) ;
2014-04-04 20:55:21 +02:00
}
public boolean canDamagerHurtDamagee ( EntityDamageByEntityEvent sub ) {
return canDamagerHurtDamagee ( sub , true ) ;
}
public boolean canDamagerHurtDamagee ( EntityDamageByEntityEvent sub , boolean notify ) {
2014-07-01 22:10:18 +02:00
Entity damager = sub . getDamager ( ) ;
Entity damagee = sub . getEntity ( ) ;
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
if ( ! ( damagee instanceof Player ) ) {
return true ;
}
2014-04-04 20:55:21 +02:00
2014-10-19 07:37:25 +02:00
FPlayer defender = FPlayers . getInstance ( ) . getByPlayer ( ( Player ) damagee ) ;
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
if ( defender = = null | | defender . getPlayer ( ) = = null ) {
return true ;
}
2014-04-04 20:55:21 +02:00
Location defenderLoc = defender . getPlayer ( ) . getLocation ( ) ;
2014-10-19 07:37:25 +02:00
Faction defLocFaction = Board . getInstance ( ) . getFactionAt ( new FLocation ( defenderLoc ) ) ;
2014-04-04 20:55:21 +02:00
// for damage caused by projectiles, getDamager() returns the projectile... what we need to know is the source
2014-04-06 00:00:33 +02:00
if ( damager instanceof Projectile ) {
2014-12-07 20:20:48 +01:00
Projectile projectile = ( Projectile ) damager ;
if ( ! ( projectile . getShooter ( ) instanceof Entity ) ) {
return true ;
}
damager = ( Entity ) projectile . getShooter ( ) ;
2014-04-06 00:00:33 +02:00
}
2014-04-04 20:55:21 +02:00
2014-06-17 07:54:50 +02:00
if ( damager = = damagee ) // ender pearl usage and other self-inflicted damage
2014-07-01 22:10:18 +02:00
{
return true ;
}
2014-04-04 20:55:21 +02:00
// Players can not take attack damage in a SafeZone, or possibly peaceful territory
if ( defLocFaction . noPvPInTerritory ( ) ) {
if ( damager instanceof Player ) {
if ( notify ) {
2014-10-19 07:37:25 +02:00
FPlayer attacker = FPlayers . getInstance ( ) . getByPlayer ( ( Player ) damager ) ;
2015-01-27 16:46:56 +01:00
attacker . msg ( TL . PLAYER_CANTHURT , ( defLocFaction . isSafeZone ( ) ? TL . REGION_SAFEZONE . toString ( ) : TL . REGION_PEACEFUL . toString ( ) ) ) ;
2014-07-01 22:10:18 +02:00
}
return false ;
}
return ! defLocFaction . noMonstersInTerritory ( ) ;
2014-04-04 20:55:21 +02:00
}
2014-07-01 22:10:18 +02:00
if ( ! ( damager instanceof Player ) ) {
return true ;
}
2014-04-04 20:55:21 +02:00
2014-10-19 07:37:25 +02:00
FPlayer attacker = FPlayers . getInstance ( ) . getByPlayer ( ( Player ) damager ) ;
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
if ( attacker = = null | | attacker . getPlayer ( ) = = null ) {
return true ;
}
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
if ( Conf . playersWhoBypassAllProtection . contains ( attacker . getName ( ) ) ) {
return true ;
}
2014-04-04 20:55:21 +02:00
if ( attacker . hasLoginPvpDisabled ( ) ) {
2014-07-01 21:49:42 +02:00
if ( notify ) {
2015-01-23 20:25:14 +01:00
attacker . msg ( TL . PLAYER_PVP_LOGIN , Conf . noPVPDamageToOthersForXSecondsAfterLogin ) ;
2014-07-01 22:10:18 +02:00
}
return false ;
2014-04-04 20:55:21 +02:00
}
2014-10-19 07:37:25 +02:00
Faction locFaction = Board . getInstance ( ) . getFactionAt ( new FLocation ( attacker ) ) ;
2014-04-04 20:55:21 +02:00
// so we know from above that the defender isn't in a safezone... what about the attacker, sneaky dog that he might be?
if ( locFaction . noPvPInTerritory ( ) ) {
2014-07-01 21:49:42 +02:00
if ( notify ) {
2015-01-27 16:46:56 +01:00
attacker . msg ( TL . PLAYER_CANTHURT , ( locFaction . isSafeZone ( ) ? TL . REGION_SAFEZONE . toString ( ) : TL . REGION_PEACEFUL . toString ( ) ) ) ;
2014-07-01 22:10:18 +02:00
}
return false ;
2014-04-04 20:55:21 +02:00
}
2014-07-01 22:10:18 +02:00
if ( locFaction . isWarZone ( ) & & Conf . warZoneFriendlyFire ) {
return true ;
}
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
if ( Conf . worldsIgnorePvP . contains ( defenderLoc . getWorld ( ) . getName ( ) ) ) {
return true ;
}
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
Faction defendFaction = defender . getFaction ( ) ;
Faction attackFaction = attacker . getFaction ( ) ;
2014-04-04 20:55:21 +02:00
2015-09-08 18:46:48 +02:00
if ( attackFaction . isWilderness ( ) & & Conf . disablePVPForFactionlessPlayers ) {
2014-07-01 22:10:18 +02:00
if ( notify ) {
2015-01-23 20:25:14 +01:00
attacker . msg ( TL . PLAYER_PVP_REQUIREFACTION ) ;
2014-07-01 22:10:18 +02:00
}
return false ;
2015-09-08 18:46:48 +02:00
} else if ( defendFaction . isWilderness ( ) ) {
2014-04-04 20:55:21 +02:00
if ( defLocFaction = = attackFaction & & Conf . enablePVPAgainstFactionlessInAttackersLand ) {
// Allow PVP vs. Factionless in attacker's faction territory
return true ;
2014-07-01 21:52:40 +02:00
} else if ( Conf . disablePVPForFactionlessPlayers ) {
2014-07-01 22:10:18 +02:00
if ( notify ) {
2015-01-23 20:25:14 +01:00
attacker . msg ( TL . PLAYER_PVP_FACTIONLESS ) ;
2014-07-01 22:10:18 +02:00
}
2014-04-04 20:55:21 +02:00
return false ;
}
}
if ( defendFaction . isPeaceful ( ) ) {
2014-07-01 22:10:18 +02:00
if ( notify ) {
2015-01-23 20:25:14 +01:00
attacker . msg ( TL . PLAYER_PVP_PEACEFUL ) ;
2014-07-01 22:10:18 +02:00
}
return false ;
2014-07-01 21:52:40 +02:00
} else if ( attackFaction . isPeaceful ( ) ) {
2014-07-01 22:10:18 +02:00
if ( notify ) {
2015-01-23 20:25:14 +01:00
attacker . msg ( TL . PLAYER_PVP_PEACEFUL ) ;
2014-07-01 22:10:18 +02:00
}
2014-04-04 20:55:21 +02:00
return false ;
}
Relation relation = defendFaction . getRelationTo ( attackFaction ) ;
// You can not hurt neutral factions
if ( Conf . disablePVPBetweenNeutralFactions & & relation . isNeutral ( ) ) {
2014-07-01 22:10:18 +02:00
if ( notify ) {
2015-01-23 20:25:14 +01:00
attacker . msg ( TL . PLAYER_PVP_NEUTRAL ) ;
2014-07-01 22:10:18 +02:00
}
return false ;
2014-04-04 20:55:21 +02:00
}
// Players without faction may be hurt anywhere
2014-07-01 22:10:18 +02:00
if ( ! defender . hasFaction ( ) ) {
return true ;
}
2014-04-04 20:55:21 +02:00
// You can never hurt faction members or allies
if ( relation . isMember ( ) | | relation . isAlly ( ) ) {
2014-07-01 22:10:18 +02:00
if ( notify ) {
2015-01-23 20:25:14 +01:00
attacker . msg ( TL . PLAYER_PVP_CANTHURT , defender . describeTo ( attacker ) ) ;
2014-07-01 22:10:18 +02:00
}
return false ;
2014-04-04 20:55:21 +02:00
}
boolean ownTerritory = defender . isInOwnTerritory ( ) ;
// You can not hurt neutrals in their own territory.
if ( ownTerritory & & relation . isNeutral ( ) ) {
if ( notify ) {
2015-01-23 20:25:14 +01:00
attacker . msg ( TL . PLAYER_PVP_NEUTRALFAIL , defender . describeTo ( attacker ) ) ;
defender . msg ( TL . PLAYER_PVP_TRIED , attacker . describeTo ( defender , true ) ) ;
2014-07-01 22:10:18 +02:00
}
return false ;
2014-04-04 20:55:21 +02:00
}
// Damage will be dealt. However check if the damage should be reduced.
2014-07-09 20:20:38 +02:00
/ *
2014-04-04 20:55:21 +02:00
if ( damage > 0 . 0 & & ownTerritory & & Conf . territoryShieldFactor > 0 ) {
2014-07-06 06:52:07 +02:00
double newDamage = Math . ceil ( damage * ( 1D - Conf . territoryShieldFactor ) ) ;
2014-07-01 22:10:18 +02:00
sub . setDamage ( newDamage ) ;
2014-04-04 20:55:21 +02:00
// Send message
if ( notify ) {
String perc = MessageFormat . format ( " {0,number,#%} " , ( Conf . territoryShieldFactor ) ) ; // TODO does this display correctly??
defender . msg ( " <i>Enemy damage reduced by <rose>%s<i>. " , perc ) ;
}
2014-07-08 20:54:22 +02:00
} * /
2014-04-04 20:55:21 +02:00
return true ;
}
2015-02-23 20:33:23 +01:00
@EventHandler ( priority = EventPriority . NORMAL , ignoreCancelled = true )
2014-04-04 20:55:21 +02:00
public void onCreatureSpawn ( CreatureSpawnEvent event ) {
2015-02-23 20:33:23 +01:00
if ( event . getLocation ( ) = = null ) {
2014-04-04 20:55:21 +02:00
return ;
}
2014-10-19 07:37:25 +02:00
if ( Conf . safeZoneNerfedCreatureTypes . contains ( event . getEntityType ( ) ) & & Board . getInstance ( ) . getFactionAt ( new FLocation ( event . getLocation ( ) ) ) . noMonstersInTerritory ( ) ) {
2014-04-04 20:55:21 +02:00
event . setCancelled ( true ) ;
}
}
2015-02-23 20:33:23 +01:00
@EventHandler ( priority = EventPriority . NORMAL , ignoreCancelled = true )
2014-04-04 20:55:21 +02:00
public void onEntityTarget ( EntityTargetEvent event ) {
// if there is a target
2014-07-01 22:10:18 +02:00
Entity target = event . getTarget ( ) ;
if ( target = = null ) {
2014-04-04 20:55:21 +02:00
return ;
}
// We are interested in blocking targeting for certain mobs:
if ( ! Conf . safeZoneNerfedCreatureTypes . contains ( MiscUtil . creatureTypeFromEntity ( event . getEntity ( ) ) ) ) {
return ;
}
// in case the target is in a safe zone.
2014-10-19 07:37:25 +02:00
if ( Board . getInstance ( ) . getFactionAt ( new FLocation ( target . getLocation ( ) ) ) . noMonstersInTerritory ( ) ) {
2014-04-04 20:55:21 +02:00
event . setCancelled ( true ) ;
}
}
2015-02-23 20:33:23 +01:00
@EventHandler ( priority = EventPriority . NORMAL , ignoreCancelled = true )
2014-04-04 20:55:21 +02:00
public void onPaintingBreak ( HangingBreakEvent event ) {
2014-07-01 22:10:18 +02:00
if ( event . getCause ( ) = = RemoveCause . EXPLOSION ) {
Location loc = event . getEntity ( ) . getLocation ( ) ;
2014-10-19 07:37:25 +02:00
Faction faction = Board . getInstance ( ) . getFactionAt ( new FLocation ( loc ) ) ;
2014-04-04 20:55:21 +02:00
if ( faction . noExplosionsInTerritory ( ) ) {
// faction is peaceful and has explosions set to disabled
2014-07-01 22:10:18 +02:00
event . setCancelled ( true ) ;
return ;
2014-04-04 20:55:21 +02:00
}
boolean online = faction . hasPlayersOnline ( ) ;
2015-09-08 18:46:48 +02:00
if ( ( faction . isWilderness ( ) & & ! Conf . worldsNoWildernessProtection . contains ( loc . getWorld ( ) . getName ( ) ) & & ( Conf . wildernessBlockCreepers | | Conf . wildernessBlockFireballs | | Conf . wildernessBlockTNT ) ) | |
2018-01-05 02:40:27 +01:00
( faction . isNormal ( ) & & ( online ? ( Conf . territoryBlockCreepers | | Conf . territoryBlockFireballs | | Conf . territoryBlockTNT ) : ( Conf . territoryBlockCreepersWhenOffline | | Conf . territoryBlockFireballsWhenOffline | | Conf . territoryBlockTNTWhenOffline ) ) ) | |
( faction . isWarZone ( ) & & ( Conf . warZoneBlockCreepers | | Conf . warZoneBlockFireballs | | Conf . warZoneBlockTNT ) ) | |
faction . isSafeZone ( ) ) {
2014-04-04 20:55:21 +02:00
// explosion which needs prevention
event . setCancelled ( true ) ;
}
}
if ( ! ( event instanceof HangingBreakByEntityEvent ) ) {
return ;
}
2014-07-01 22:10:18 +02:00
Entity breaker = ( ( HangingBreakByEntityEvent ) event ) . getRemover ( ) ;
if ( ! ( breaker instanceof Player ) ) {
2014-04-04 20:55:21 +02:00
return ;
}
if ( ! FactionsBlockListener . playerCanBuildDestroyBlock ( ( Player ) breaker , event . getEntity ( ) . getLocation ( ) , " remove paintings " , false ) ) {
event . setCancelled ( true ) ;
}
}
2015-02-23 20:33:23 +01:00
@EventHandler ( priority = EventPriority . NORMAL , ignoreCancelled = true )
2014-04-04 20:55:21 +02:00
public void onPaintingPlace ( HangingPlaceEvent event ) {
if ( ! FactionsBlockListener . playerCanBuildDestroyBlock ( event . getPlayer ( ) , event . getBlock ( ) . getLocation ( ) , " place paintings " , false ) ) {
event . setCancelled ( true ) ;
}
}
2015-02-23 20:33:23 +01:00
@EventHandler ( priority = EventPriority . NORMAL , ignoreCancelled = true )
2014-04-04 20:55:21 +02:00
public void onEntityChangeBlock ( EntityChangeBlockEvent event ) {
Entity entity = event . getEntity ( ) ;
// for now, only interested in Enderman and Wither boss tomfoolery
2014-07-01 22:10:18 +02:00
if ( ! ( entity instanceof Enderman ) & & ! ( entity instanceof Wither ) ) {
return ;
}
2014-04-04 20:55:21 +02:00
Location loc = event . getBlock ( ) . getLocation ( ) ;
if ( entity instanceof Enderman ) {
2014-07-01 22:10:18 +02:00
if ( stopEndermanBlockManipulation ( loc ) ) {
event . setCancelled ( true ) ;
}
2014-07-01 21:52:40 +02:00
} else if ( entity instanceof Wither ) {
2014-10-19 07:37:25 +02:00
Faction faction = Board . getInstance ( ) . getFactionAt ( new FLocation ( loc ) ) ;
2014-04-04 20:55:21 +02:00
// it's a bit crude just using fireball protection, but I'd rather not add in a whole new set of xxxBlockWitherExplosion or whatever
2015-09-08 18:46:48 +02:00
if ( ( faction . isWilderness ( ) & & Conf . wildernessBlockFireballs & & ! Conf . worldsNoWildernessProtection . contains ( loc . getWorld ( ) . getName ( ) ) ) | |
2018-01-05 02:40:27 +01:00
( faction . isNormal ( ) & & ( faction . hasPlayersOnline ( ) ? Conf . territoryBlockFireballs : Conf . territoryBlockFireballsWhenOffline ) ) | |
( faction . isWarZone ( ) & & Conf . warZoneBlockFireballs ) | |
faction . isSafeZone ( ) ) {
2014-07-01 22:10:18 +02:00
event . setCancelled ( true ) ;
}
2014-04-04 20:55:21 +02:00
}
}
2015-01-05 16:58:32 +01:00
@EventHandler
public void onTravel ( PlayerPortalEvent event ) {
if ( ! P . p . getConfig ( ) . getBoolean ( " portals.limit " , false ) ) {
return ; // Don't do anything if they don't want us to.
}
TravelAgent agent = event . getPortalTravelAgent ( ) ;
// If they aren't able to find a portal, it'll try to create one.
if ( event . useTravelAgent ( ) & & agent . getCanCreatePortal ( ) & & agent . findPortal ( event . getTo ( ) ) = = null ) {
FLocation loc = new FLocation ( event . getTo ( ) ) ;
Faction faction = Board . getInstance ( ) . getFactionAt ( loc ) ;
2015-09-08 18:46:48 +02:00
if ( faction . isWilderness ( ) ) {
2015-01-05 16:58:32 +01:00
return ; // We don't care about wilderness.
} else if ( ! faction . isNormal ( ) & & ! event . getPlayer ( ) . isOp ( ) ) {
// Don't let non ops make portals in safezone or warzone.
event . setCancelled ( true ) ;
return ;
}
FPlayer fp = FPlayers . getInstance ( ) . getByPlayer ( event . getPlayer ( ) ) ;
String mininumRelation = P . p . getConfig ( ) . getString ( " portals.minimum-relation " , " MEMBER " ) ; // Defaults to Neutral if typed wrong.
if ( ! fp . getFaction ( ) . getRelationTo ( faction ) . isAtLeast ( Relation . fromString ( mininumRelation ) ) ) {
event . setCancelled ( true ) ;
}
}
}
2014-04-04 20:55:21 +02:00
private boolean stopEndermanBlockManipulation ( Location loc ) {
if ( loc = = null ) {
return false ;
}
// quick check to see if all Enderman deny options are enabled; if so, no need to check location
2014-07-01 21:52:40 +02:00
if ( Conf . wildernessDenyEndermanBlocks & &
2018-01-05 02:40:27 +01:00
Conf . territoryDenyEndermanBlocks & &
Conf . territoryDenyEndermanBlocksWhenOffline & &
Conf . safeZoneDenyEndermanBlocks & &
Conf . warZoneDenyEndermanBlocks ) {
2014-04-04 20:55:21 +02:00
return true ;
}
2014-07-01 22:10:18 +02:00
FLocation fLoc = new FLocation ( loc ) ;
2014-10-19 07:37:25 +02:00
Faction claimFaction = Board . getInstance ( ) . getFactionAt ( fLoc ) ;
2014-04-04 20:55:21 +02:00
2015-09-08 18:46:48 +02:00
if ( claimFaction . isWilderness ( ) ) {
2014-04-04 20:55:21 +02:00
return Conf . wildernessDenyEndermanBlocks ;
2014-07-01 21:52:40 +02:00
} else if ( claimFaction . isNormal ( ) ) {
2014-04-04 20:55:21 +02:00
return claimFaction . hasPlayersOnline ( ) ? Conf . territoryDenyEndermanBlocks : Conf . territoryDenyEndermanBlocksWhenOffline ;
2014-07-01 21:52:40 +02:00
} else if ( claimFaction . isSafeZone ( ) ) {
2014-04-04 20:55:21 +02:00
return Conf . safeZoneDenyEndermanBlocks ;
2014-07-01 21:52:40 +02:00
} else if ( claimFaction . isWarZone ( ) ) {
2014-04-04 20:55:21 +02:00
return Conf . warZoneDenyEndermanBlocks ;
}
return false ;
}
2011-02-06 13:36:11 +01:00
}