Clean up the Monsters flag logic.

This commit is contained in:
Olof Larsson 2013-04-18 16:11:45 +02:00
parent 86b143573d
commit 3ac432ac3d
3 changed files with 49 additions and 41 deletions

View File

@ -7,6 +7,7 @@ import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Enderman; import org.bukkit.entity.Enderman;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Wither; import org.bukkit.entity.Wither;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
@ -22,10 +23,12 @@ import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockSpreadEvent; import org.bukkit.event.block.BlockSpreadEvent;
import org.bukkit.event.block.BlockIgniteEvent.IgniteCause; import org.bukkit.event.block.BlockIgniteEvent.IgniteCause;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent; import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent; import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.hanging.HangingBreakByEntityEvent; import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.hanging.HangingBreakEvent; import org.bukkit.event.hanging.HangingBreakEvent;
import org.bukkit.event.hanging.HangingPlaceEvent; import org.bukkit.event.hanging.HangingPlaceEvent;
@ -38,6 +41,7 @@ import org.bukkit.event.server.PluginEnableEvent;
import com.massivecraft.factions.BoardColl; import com.massivecraft.factions.BoardColl;
import com.massivecraft.factions.ConfServer; import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.FFlag; import com.massivecraft.factions.FFlag;
import com.massivecraft.factions.FPerm; import com.massivecraft.factions.FPerm;
import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayer;
@ -65,6 +69,47 @@ public class FactionsListenerMain implements Listener
Bukkit.getPluginManager().registerEvents(this, Factions.get()); Bukkit.getPluginManager().registerEvents(this, Factions.get());
} }
// -------------------------------------------- //
// FLAG: MONSTERS
// -------------------------------------------- //
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void blockMonsters(CreatureSpawnEvent event)
{
// If a monster is spawning ...
if ( ! Const.ENTITY_TYPES_MONSTERS.contains(event.getEntityType())) return;
// ... at a place where monsters are forbidden ...
PS ps = PS.valueOf(event.getLocation());
Faction faction = BoardColl.get().getFactionAt(ps);
if (faction.getFlag(FFlag.MONSTERS)) return;
// ... block the spawn.
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void blockMonsters(EntityTargetEvent event)
{
// If a monster is targeting something ...
if ( ! Const.ENTITY_TYPES_MONSTERS.contains(event.getEntityType())) return;
// ... at a place where monsters are forbidden ...
PS ps = PS.valueOf(event.getTarget());
Faction faction = BoardColl.get().getFactionAt(ps);
if (faction.getFlag(FFlag.MONSTERS)) return;
// ... then if ghast target nothing ...
if (event.getEntityType() == EntityType.GHAST)
{
event.setTarget(null);
return;
}
// ... otherwise simply cancel.
event.setCancelled(true);
}
// -------------------------------------------- // // -------------------------------------------- //
// FLAG: EXPLOSIONS // FLAG: EXPLOSIONS
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -15,11 +15,9 @@ import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityCombustByEntityEvent; import org.bukkit.event.entity.EntityCombustByEntityEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.entity.PotionSplashEvent; import org.bukkit.event.entity.PotionSplashEvent;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
@ -27,7 +25,6 @@ import org.bukkit.potion.PotionEffectType;
import com.massivecraft.factions.BoardColl; import com.massivecraft.factions.BoardColl;
import com.massivecraft.factions.ConfServer; import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.FFlag; import com.massivecraft.factions.FFlag;
import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayerColl; import com.massivecraft.factions.FPlayerColl;
@ -110,8 +107,9 @@ public class TodoFactionsEntityListener implements Listener
{ {
EntityDamageByEntityEvent sub = new EntityDamageByEntityEvent(event.getCombuster(), event.getEntity(), EntityDamageEvent.DamageCause.FIRE, 0); EntityDamageByEntityEvent sub = new EntityDamageByEntityEvent(event.getCombuster(), event.getEntity(), EntityDamageEvent.DamageCause.FIRE, 0);
if ( ! this.canDamagerHurtDamagee(sub, false)) if ( ! this.canDamagerHurtDamagee(sub, false))
{
event.setCancelled(true); event.setCancelled(true);
sub = null; }
} }
private static final Set<PotionEffectType> badPotionEffects = new LinkedHashSet<PotionEffectType>(Arrays.asList( private static final Set<PotionEffectType> badPotionEffects = new LinkedHashSet<PotionEffectType>(Arrays.asList(
@ -276,38 +274,6 @@ public class TodoFactionsEntityListener implements Listener
return true; return true;
} }
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onCreatureSpawn(CreatureSpawnEvent event)
{
if (event.getLocation() == null) return;
PS ps = PS.valueOf(event.getLocation());
Faction faction = BoardColl.get().getFactionAt(ps);
if (faction.getFlag(FFlag.MONSTERS)) return;
if ( ! Const.ENTITY_TYPES_MONSTERS.contains(event.getEntityType())) return;
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onEntityTarget(EntityTargetEvent event)
{
// if there is a target
Entity target = event.getTarget();
if (target == null) return;
// We are interested in blocking targeting for certain mobs:
if ( ! Const.ENTITY_TYPES_MONSTERS.contains(event.getEntity().getType())) return;
PS ps = PS.valueOf(target.getLocation());
Faction faction = BoardColl.get().getFactionAt(ps);
if (faction.getFlag(FFlag.MONSTERS)) return;
event.setCancelled(true);
}
} }

View File

@ -267,10 +267,7 @@ public class TodoFactionsPlayerListener implements Listener
public void onPlayerKick(PlayerKickEvent event) public void onPlayerKick(PlayerKickEvent event)
{ {
FPlayer badGuy = FPlayerColl.get().get(event.getPlayer()); FPlayer badGuy = FPlayerColl.get().get(event.getPlayer());
if (badGuy == null) if (badGuy == null) return;
{
return;
}
SpoutFeatures.playerDisconnect(badGuy); SpoutFeatures.playerDisconnect(badGuy);