2011-07-18 22:06:02 +02:00
package com.massivecraft.factions.listeners ;
2011-02-06 13:36:11 +01:00
2011-03-07 04:13:27 +01:00
import java.text.MessageFormat ;
2011-02-06 13:36:11 +01:00
import org.bukkit.ChatColor ;
2011-03-22 17:20:21 +01:00
import org.bukkit.entity.Creeper ;
2011-02-06 13:36:11 +01:00
import org.bukkit.entity.Entity ;
2011-03-18 00:02:29 +01:00
import org.bukkit.entity.Fireball ;
2011-02-06 13:36:11 +01:00
import org.bukkit.entity.Player ;
2011-03-23 17:39:56 +01:00
import org.bukkit.event.entity.CreatureSpawnEvent ;
2011-02-06 13:36:11 +01:00
import org.bukkit.event.entity.EntityDamageByEntityEvent ;
2011-02-13 09:08:20 +01:00
import org.bukkit.event.entity.EntityDamageByProjectileEvent ;
2011-02-13 17:04:06 +01:00
import org.bukkit.event.entity.EntityDamageEvent ;
2011-02-06 13:36:11 +01:00
import org.bukkit.event.entity.EntityDeathEvent ;
2011-03-06 21:13:48 +01:00
import org.bukkit.event.entity.EntityExplodeEvent ;
2011-02-06 13:36:11 +01:00
import org.bukkit.event.entity.EntityListener ;
2011-03-23 17:39:56 +01:00
import org.bukkit.event.entity.EntityTargetEvent ;
2011-04-28 22:45:43 +02:00
import org.bukkit.event.painting.PaintingBreakByEntityEvent ;
import org.bukkit.event.painting.PaintingBreakEvent ;
import org.bukkit.event.painting.PaintingPlaceEvent ;
2011-07-18 22:06:02 +02:00
import com.massivecraft.factions.Board ;
import com.massivecraft.factions.Conf ;
import com.massivecraft.factions.FLocation ;
import com.massivecraft.factions.FPlayer ;
import com.massivecraft.factions.Faction ;
import com.massivecraft.factions.Factions ;
import com.massivecraft.factions.struct.Relation ;
import com.massivecraft.factions.util.EntityUtil ;
2011-02-06 13:36:11 +01:00
public class FactionsEntityListener extends EntityListener {
@Override
public void onEntityDeath ( EntityDeathEvent event ) {
Entity entity = event . getEntity ( ) ;
if ( ! ( entity instanceof Player ) ) {
return ;
}
2011-05-29 23:28:29 +02:00
2011-02-06 13:36:11 +01:00
Player player = ( Player ) entity ;
2011-03-23 17:39:56 +01:00
FPlayer fplayer = FPlayer . get ( player ) ;
2011-05-29 23:28:29 +02:00
Faction faction = Board . getFactionAt ( new FLocation ( player . getLocation ( ) ) ) ;
if ( faction . isWarZone ( ) ) { // war zones always override worldsNoPowerLoss either way, thus this layout
if ( ! Conf . warZonePowerLoss ) {
fplayer . sendMessage ( " You didn't lose any power since you were in a war zone. " ) ;
return ;
}
if ( Conf . worldsNoPowerLoss . contains ( player . getWorld ( ) . getName ( ) ) ) {
fplayer . sendMessage ( " The world you are in has power loss normally disabled, but you still lost power since you were in a war zone. " ) ;
}
2011-06-02 01:32:09 +02:00
} else if ( faction . isNone ( ) & & ! Conf . wildernessPowerLoss ) {
fplayer . sendMessage ( " You didn't lose any power since you were in the wilderness. " ) ;
return ;
2011-05-29 23:28:29 +02:00
} else if ( Conf . worldsNoPowerLoss . contains ( player . getWorld ( ) . getName ( ) ) ) {
fplayer . sendMessage ( " You didn't lose any power due to the world you died in. " ) ;
return ;
}
2011-03-23 17:39:56 +01:00
fplayer . onDeath ( ) ;
fplayer . sendMessage ( " Your power is now " + fplayer . getPowerRounded ( ) + " / " + fplayer . getPowerMaxRounded ( ) ) ;
2011-02-06 13:36:11 +01:00
}
2011-02-13 17:04:06 +01:00
2011-02-06 13:36:11 +01:00
/ * *
* 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 .
* /
@Override
2011-02-13 17:04:06 +01:00
public void onEntityDamage ( EntityDamageEvent event ) {
2011-02-13 09:33:42 +01:00
if ( event . isCancelled ( ) ) {
2011-03-22 17:20:21 +01:00
return ;
2011-02-06 13:36:11 +01:00
}
2011-02-13 17:04:06 +01:00
if ( event instanceof EntityDamageByEntityEvent ) {
2011-07-29 13:39:20 +02:00
EntityDamageByEntityEvent sub = ( EntityDamageByEntityEvent ) event ;
if ( ! this . canDamagerHurtDamagee ( sub ) ) {
2011-02-13 17:04:06 +01:00
event . setCancelled ( true ) ;
}
2011-07-29 13:39:20 +02:00
} else if ( event instanceof EntityDamageByProjectileEvent ) {
EntityDamageByProjectileEvent sub = ( EntityDamageByProjectileEvent ) event ;
if ( ! this . canDamagerHurtDamagee ( sub ) ) {
2011-02-13 17:04:06 +01:00
event . setCancelled ( true ) ;
}
2011-07-29 13:39:20 +02:00
} else if ( Conf . safeZonePreventAllDamageToPlayers & & isPlayerInSafeZone ( event . getEntity ( ) ) ) {
// Players can not take any damage in a Safe Zone
event . setCancelled ( true ) ;
}
2011-02-13 09:33:42 +01:00
}
2011-03-22 17:20:21 +01:00
2011-03-06 21:13:48 +01:00
@Override
public void onEntityExplode ( EntityExplodeEvent event )
{
2011-03-22 17:20:21 +01:00
if ( event . isCancelled ( ) ) {
return ;
2011-03-06 21:13:48 +01:00
}
2011-03-22 17:20:21 +01:00
2011-03-23 17:39:56 +01:00
Faction faction = Board . getFactionAt ( new FLocation ( event . getLocation ( ) ) ) ;
2011-06-21 07:20:36 +02:00
boolean online = faction . hasPlayersOnline ( ) ;
2011-03-23 17:39:56 +01:00
2011-05-29 23:28:29 +02:00
if ( event . getEntity ( ) instanceof Creeper & & (
2011-06-02 01:32:09 +02:00
( faction . isNone ( ) & & Conf . wildernessBlockCreepers ) | |
2011-06-11 01:47:39 +02:00
( faction . isNormal ( ) & & online & & Conf . territoryBlockCreepers ) | |
( faction . isNormal ( ) & & ! online & & Conf . territoryBlockCreepersWhenOffline ) | |
2011-05-29 23:28:29 +02:00
( faction . isWarZone ( ) & & Conf . warZoneBlockCreepers ) | |
faction . isSafeZone ( )
) ) {
// creeper which needs prevention
2011-03-22 17:20:21 +01:00
event . setCancelled ( true ) ;
2011-05-29 23:28:29 +02:00
} else if ( event . getEntity ( ) instanceof Fireball & & (
2011-06-02 01:32:09 +02:00
( faction . isNone ( ) & & Conf . wildernessBlockFireballs ) | |
2011-06-11 01:47:39 +02:00
( faction . isNormal ( ) & & online & & Conf . territoryBlockFireballs ) | |
( faction . isNormal ( ) & & ! online & & Conf . territoryBlockFireballsWhenOffline ) | |
2011-05-29 23:28:29 +02:00
( faction . isWarZone ( ) & & Conf . warZoneBlockFireballs ) | |
faction . isSafeZone ( )
) ) {
// ghast fireball which needs prevention
2011-03-22 17:20:21 +01:00
event . setCancelled ( true ) ;
2011-05-29 23:28:29 +02:00
} else if (
2011-06-02 01:32:09 +02:00
( faction . isNone ( ) & & Conf . wildernessBlockTNT ) | |
2011-06-11 01:47:39 +02:00
( faction . isNormal ( ) & & online & & Conf . territoryBlockTNT ) | |
( faction . isNormal ( ) & & ! online & & Conf . territoryBlockTNTWhenOffline ) | |
2011-05-29 23:28:29 +02:00
( faction . isWarZone ( ) & & Conf . warZoneBlockTNT ) | |
( faction . isSafeZone ( ) & & Conf . safeZoneBlockTNT )
) {
// we'll assume it's TNT, which needs prevention
New boolean config options territoryBlockTNT (default false), safeZoneDenyBuild (default true), safeZoneDenyUseage (default true), safeZoneBlockTNT (default true). territoryBlockTNT prevents TNT explosions inside faction territory, which defaults to false (same as before). It prevents TNT cannons from being a viable tactic, and I think for most people it would be an undesirable option. safeZoneBlockTNT prevents TNT explosions inside safe zone areas, which defaults to true (new behavior). I think most server operators would prefer safe zones to be safe from TNT, from TNT cannons or otherwise. safeZoneDenyBuild prevents players from building inside safe zone areas, which defaults to true (same as before). This option is added for server admins who, for whatever reason, want to let their players build and break inside safe zone areas. safeZoneDenyUseage prevents players from using items in the territoryDenyUseageMaterials list, which defaults to true (new behavior). This will (with the default list) prevent players from dumping/using buckets inside safe zone areas.
2011-04-04 13:31:48 +02:00
event . setCancelled ( true ) ;
2011-03-18 00:02:29 +01:00
}
2011-03-06 21:13:48 +01:00
}
2011-07-29 13:39:20 +02:00
public boolean isPlayerInSafeZone ( Entity damagee ) {
if ( ! ( damagee instanceof Player ) ) {
return false ;
}
if ( Board . getFactionAt ( new FLocation ( damagee . getLocation ( ) ) ) . isSafeZone ( ) ) {
return true ;
}
return false ;
}
2011-03-07 04:13:27 +01:00
public boolean canDamagerHurtDamagee ( EntityDamageByEntityEvent sub ) {
Entity damager = sub . getDamager ( ) ;
Entity damagee = sub . getEntity ( ) ;
int damage = sub . getDamage ( ) ;
2011-02-06 13:36:11 +01:00
2011-05-12 07:52:17 +02:00
if ( ! ( damagee instanceof Player ) ) {
2011-02-13 09:33:42 +01:00
return true ;
}
2011-02-06 13:36:11 +01:00
2011-03-18 17:33:23 +01:00
FPlayer defender = FPlayer . get ( ( Player ) damagee ) ;
2011-03-23 17:39:56 +01:00
2011-05-12 07:52:17 +02:00
if ( defender = = null | | defender . getPlayer ( ) = = null ) {
return true ;
}
2011-03-23 17:39:56 +01:00
// Players can not take attack damage in a SafeZone
if ( Board . getFactionAt ( new FLocation ( defender ) ) . isSafeZone ( ) ) {
if ( damager instanceof Player ) {
FPlayer attacker = FPlayer . get ( ( Player ) damager ) ;
2011-05-12 07:52:17 +02:00
attacker . sendMessage ( " You can't hurt other players in a SafeZone. " ) ;
2011-06-03 20:02:24 +02:00
// defender.sendMessage(attacker.getNameAndRelevant(defender)+Conf.colorSystem+" tried to hurt you.");
2011-03-23 17:39:56 +01:00
}
return false ;
}
if ( ! ( damager instanceof Player ) ) {
return true ;
}
2011-03-18 17:33:23 +01:00
FPlayer attacker = FPlayer . get ( ( Player ) damager ) ;
2011-05-12 07:52:17 +02:00
if ( attacker = = null | | attacker . getPlayer ( ) = = null ) {
return true ;
}
2011-06-23 03:10:42 +02:00
if ( attacker . hasLoginPvpDisabled ( ) ) {
attacker . sendMessage ( " You can't hurt other players for " + Conf . noPVPDamageToOthersForXSecondsAfterLogin + " seconds after logging in. " ) ;
return false ;
}
2011-06-10 14:38:14 +02:00
Faction locFaction = Board . getFactionAt ( new FLocation ( attacker ) ) ;
2011-06-08 23:22:19 +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?
2011-06-10 14:38:14 +02:00
if ( locFaction . isSafeZone ( ) ) {
2011-06-08 23:22:19 +02:00
attacker . sendMessage ( " You can't hurt other players while you are in a SafeZone. " ) ;
return false ;
}
2011-06-10 14:38:14 +02:00
else if ( locFaction . isWarZone ( ) & & Conf . warZoneFriendlyFire ) {
return true ;
}
2011-06-08 23:22:19 +02:00
2011-08-02 01:28:34 +02:00
if ( ! attacker . hasFaction ( ) & & Conf . disablePVPForFactionlessPlayers ) {
attacker . sendMessage ( " You can't hurt other players until you join a faction. " ) ;
return false ;
}
else if ( ! defender . hasFaction ( ) & & Conf . disablePVPForFactionlessPlayers ) {
attacker . sendMessage ( " You can't hurt players who are not currently in a faction. " ) ;
return false ;
}
2011-02-06 13:36:11 +01:00
Relation relation = defender . getRelation ( attacker ) ;
2011-07-28 16:04:11 +02:00
// You can not hurt neutral factions
2011-07-29 12:12:14 +02:00
if ( Conf . disablePVPBetweenNeutralFactions & & relation . isNeutral ( ) ) {
attacker . sendMessage ( " You can't hurt neutral factions " ) ;
return false ;
2011-07-28 16:04:11 +02:00
}
2011-02-06 13:36:11 +01:00
// Players without faction may be hurt anywhere
2011-07-29 12:12:14 +02:00
if ( ! defender . hasFaction ( ) ) {
2011-02-13 09:33:42 +01:00
return true ;
2011-02-06 13:36:11 +01:00
}
// You can never hurt faction members or allies
2011-07-09 08:36:18 +02:00
if ( relation . isMember ( ) | | relation . isAlly ( ) ) {
2011-02-12 18:05:05 +01:00
attacker . sendMessage ( Conf . colorSystem + " You can't hurt " + defender . getNameAndRelevant ( attacker ) ) ;
2011-02-13 09:33:42 +01:00
return false ;
2011-02-06 13:36:11 +01:00
}
// You can not hurt neutrals in their own territory.
2011-07-09 08:36:18 +02:00
if ( relation . isNeutral ( ) & & defender . isInOwnTerritory ( ) ) {
2011-02-13 17:02:51 +01:00
attacker . sendMessage ( Conf . colorSystem + " You can't hurt " + relation . getColor ( ) + defender . getNameAndRelevant ( attacker ) + Conf . colorSystem + " in their own territory. " ) ;
2011-02-12 18:05:05 +01:00
defender . sendMessage ( attacker . getNameAndRelevant ( defender ) + Conf . colorSystem + " tried to hurt you. " ) ;
2011-02-13 09:33:42 +01:00
return false ;
2011-02-06 13:36:11 +01:00
}
// Damage will be dealt. However check if the damage should be reduced.
2011-02-13 09:33:42 +01:00
if ( defender . isInOwnTerritory ( ) & & Conf . territoryShieldFactor > 0 ) {
2011-03-23 17:39:56 +01:00
int newDamage = ( int ) Math . ceil ( damage * ( 1D - Conf . territoryShieldFactor ) ) ;
2011-03-07 04:13:27 +01:00
sub . setDamage ( newDamage ) ;
2011-02-06 13:36:11 +01:00
// Send message
2011-03-23 17:39:56 +01:00
String perc = MessageFormat . format ( " {0,number,#%} " , ( Conf . territoryShieldFactor ) ) ; // TODO does this display correctly??
defender . sendMessage ( " Enemy damage reduced by " + ChatColor . RED + perc + Conf . colorSystem + " . " ) ;
2011-02-06 13:36:11 +01:00
}
2011-02-13 09:33:42 +01:00
return true ;
2011-02-06 13:36:11 +01:00
}
2011-03-23 17:39:56 +01:00
public void onCreatureSpawn ( CreatureSpawnEvent event ) {
if ( event . isCancelled ( ) ) {
return ;
}
if ( Conf . safeZoneNerfedCreatureTypes . contains ( event . getCreatureType ( ) ) & & Board . getFactionAt ( new FLocation ( event . getLocation ( ) ) ) . isSafeZone ( ) ) {
event . setCancelled ( true ) ;
}
}
@Override
public void onEntityTarget ( EntityTargetEvent event ) {
if ( event . isCancelled ( ) ) {
return ;
}
// if there is a target
Entity target = event . getTarget ( ) ;
if ( target = = null ) {
return ;
}
// We are interested in blocking targeting for certain mobs:
if ( ! Conf . safeZoneNerfedCreatureTypes . contains ( EntityUtil . creatureTypeFromEntity ( event . getEntity ( ) ) ) ) {
return ;
}
// in case the target is in a safe zone.
if ( Board . getFactionAt ( new FLocation ( target . getLocation ( ) ) ) . isSafeZone ( ) ) {
event . setCancelled ( true ) ;
}
}
2011-04-28 22:45:43 +02:00
@Override
public void onPaintingBreak ( PaintingBreakEvent event )
{
if ( event . isCancelled ( ) ) {
return ;
}
if ( ! ( event instanceof PaintingBreakByEntityEvent ) ) {
return ;
}
Entity breaker = ( ( PaintingBreakByEntityEvent ) event ) . getRemover ( ) ;
if ( ! ( breaker instanceof Player ) ) {
return ;
}
FLocation loc = new FLocation ( event . getPainting ( ) . getLocation ( ) ) ;
if ( ! this . playerCanDoPaintings ( ( Player ) breaker , loc , " remove " ) ) {
event . setCancelled ( true ) ;
}
}
@Override
public void onPaintingPlace ( PaintingPlaceEvent event )
{
if ( event . isCancelled ( ) ) {
return ;
}
if ( ! this . playerCanDoPaintings ( event . getPlayer ( ) , new FLocation ( event . getBlock ( ) ) , " place " ) ) {
event . setCancelled ( true ) ;
}
}
public boolean playerCanDoPaintings ( Player player , FLocation loc , String action ) {
if ( Conf . adminBypassPlayers . contains ( player . getName ( ) ) ) {
return true ;
}
Faction otherFaction = Board . getFactionAt ( loc ) ;
2011-06-02 01:32:09 +02:00
FPlayer me = FPlayer . get ( player ) ;
2011-04-28 22:45:43 +02:00
if ( otherFaction . isNone ( ) ) {
2011-06-02 06:01:51 +02:00
if ( ! Conf . wildernessDenyBuild | | Factions . hasPermAdminBypass ( player ) ) {
2011-06-02 01:32:09 +02:00
return true ; // This is not faction territory. Use whatever you like here.
}
me . sendMessage ( " You can't " + action + " paintings in the wilderness. " ) ;
return false ;
2011-04-28 22:45:43 +02:00
}
if ( otherFaction . isSafeZone ( ) ) {
if ( Factions . hasPermManageSafeZone ( player ) | | ! Conf . safeZoneDenyBuild ) {
return true ;
}
me . sendMessage ( " You can't " + action + " paintings in a safe zone. " ) ;
return false ;
}
2011-05-29 23:28:29 +02:00
else if ( otherFaction . isWarZone ( ) ) {
if ( Factions . hasPermManageWarZone ( player ) | | ! Conf . warZoneDenyBuild ) {
return true ;
}
me . sendMessage ( " You can't " + action + " paintings in a war zone. " ) ;
return false ;
}
2011-04-28 22:45:43 +02:00
Faction myFaction = me . getFaction ( ) ;
2011-07-09 08:36:18 +02:00
boolean areEnemies = myFaction . getRelation ( otherFaction ) . isEnemy ( ) ;
2011-04-28 22:45:43 +02:00
// Cancel if we are not in our own territory
2011-06-10 21:14:02 +02:00
if ( myFaction ! = otherFaction ) {
2011-06-21 07:20:36 +02:00
boolean online = otherFaction . hasPlayersOnline ( ) ;
2011-07-09 08:36:18 +02:00
if (
( online & & ( areEnemies ? Conf . territoryEnemyDenyBuild : Conf . territoryDenyBuild ) )
| | ( ! online & & ( areEnemies ? Conf . territoryEnemyDenyBuildWhenOffline : Conf . territoryDenyBuildWhenOffline ) )
) {
2011-06-10 21:14:02 +02:00
me . sendMessage ( " You can't " + action + " paintings in the territory of " + otherFaction . getTag ( myFaction ) ) ;
return false ;
}
2011-04-28 22:45:43 +02:00
}
Faction admins can now mark already claimed areas as owned by specific faction members. Ownership can include multiple members. New command /f owner *[player name], to set/remove ownership. This command is only available to the faction admin and optionally the faction moderators. If no player name is specified, it will either set ownership to the player running the command (if no owner is currently set) or completely clear ownership of the territory. New command /f ownerlist, to view a list of owners for the current area. Only works inside your own faction's territory. New conf.json options "ownedAreasEnabled", "ownedAreasModeratorsCanSet", "ownedAreaModeratorsBypass", "ownedAreaDenyBuild", "ownedAreaProtectMaterials", and "ownedAreaDenyUseage" (all defaulting to true) to determine whether faction moderators can set or bypass ownership (faction admin always can), and what sort of protection these owned areas have against normal members of the faction (members other than the owner(s), faction admin, and probably faction moderators). New conf.json option "ownedAreasLimitPerFaction" to limit how many owned areas can be set. New permission node "factions.ownershipBypass" which allows a player to bypass ownership protection, but only within the person's own faction.
various little tweaks and improvements to other code
moderate speed boost to FLocation code
made commandDisable permissions work for any command alias of a command, instead of just the first one
2011-07-31 03:17:00 +02:00
// Also cancel if player doesn't have ownership rights for this claim
else if (
Conf . ownedAreasEnabled
& & Conf . ownedAreaDenyBuild
& & ! myFaction . playerHasOwnershipRights ( me , loc )
& & ! Factions . hasPermOwnershipBypass ( player )
) {
me . sendMessage ( " You can't " + action + " paintings in this territory, it is owned by: " + myFaction . getOwnerListString ( loc ) ) ;
return false ;
}
2011-04-28 22:45:43 +02:00
return true ;
}
2011-02-06 13:36:11 +01:00
}