use entity type vs. instanceof where possible

This commit is contained in:
RoboMWM 2016-10-23 05:44:46 -07:00
parent 2f186bdf88
commit e565b433be
2 changed files with 17 additions and 16 deletions

View File

@ -95,10 +95,12 @@ public class EntityEventHandler implements Listener
{ {
//convenience reference for the singleton datastore //convenience reference for the singleton datastore
private DataStore dataStore; private DataStore dataStore;
GriefPrevention instance;
public EntityEventHandler(DataStore dataStore) public EntityEventHandler(DataStore dataStore, GriefPrevention plugin)
{ {
this.dataStore = dataStore; this.dataStore = dataStore;
instance = plugin;
} }
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
@ -173,9 +175,8 @@ public class EntityEventHandler implements Listener
else else
{ {
List<MetadataValue> values = entity.getMetadata("GP_FALLINGBLOCK"); List<MetadataValue> values = entity.getMetadata("GP_FALLINGBLOCK");
//If entity fell through an end portal, allow it to form, as the event is erroneously fired twice in this scenario.
if (entity.hasMetadata("GP_FELLTHROUGHPORTAL")) return;
//if we're not sure where this entity came from (maybe another plugin didn't follow the standard?), allow the block to form //if we're not sure where this entity came from (maybe another plugin didn't follow the standard?), allow the block to form
//Or if entity fell through an end portal, allow it to form, as the event is erroneously fired twice in this scenario.
if(values.size() < 1) return; if(values.size() < 1) return;
Location originalLocation = (Location)(values.get(0).value()); Location originalLocation = (Location)(values.get(0).value());
@ -213,7 +214,7 @@ public class EntityEventHandler implements Listener
{ {
if (event.getEntityType() != EntityType.FALLING_BLOCK) if (event.getEntityType() != EntityType.FALLING_BLOCK)
return; return;
event.getEntity().setMetadata("GP_FELLTHROUGHPORTAL", new FixedMetadataValue(GriefPrevention.instance, true)); event.getEntity().removeMetadata("GP_FALLINGBLOCK", instance);
} }
//don't allow zombies to break down doors //don't allow zombies to break down doors
@ -268,7 +269,7 @@ public class EntityEventHandler implements Listener
if(!GriefPrevention.instance.claimsEnabledForWorld(world)) return; if(!GriefPrevention.instance.claimsEnabledForWorld(world)) return;
//FEATURE: explosions don't destroy surface blocks by default //FEATURE: explosions don't destroy surface blocks by default
boolean isCreeper = (entity != null && entity instanceof Creeper); boolean isCreeper = (entity != null && entity.getType() == EntityType.CREEPER);
boolean applySurfaceRules = world.getEnvironment() == Environment.NORMAL && ((isCreeper && GriefPrevention.instance.config_blockSurfaceCreeperExplosions) || (!isCreeper && GriefPrevention.instance.config_blockSurfaceOtherExplosions)); boolean applySurfaceRules = world.getEnvironment() == Environment.NORMAL && ((isCreeper && GriefPrevention.instance.config_blockSurfaceCreeperExplosions) || (!isCreeper && GriefPrevention.instance.config_blockSurfaceOtherExplosions));
@ -459,7 +460,7 @@ public class EntityEventHandler implements Listener
//FEATURE: when a player is involved in a siege (attacker or defender role) //FEATURE: when a player is involved in a siege (attacker or defender role)
//his death will end the siege //his death will end the siege
if(!(entity instanceof Player)) return; //only tracking players if(entity.getType() != EntityType.PLAYER) return; //only tracking players
Player player = (Player)entity; Player player = (Player)entity;
PlayerData playerData = this.dataStore.getPlayerData(player.getUniqueId()); PlayerData playerData = this.dataStore.getPlayerData(player.getUniqueId());
@ -510,7 +511,7 @@ public class EntityEventHandler implements Listener
//FEATURE: endermen don't steal claimed blocks //FEATURE: endermen don't steal claimed blocks
//if its an enderman //if its an enderman
if(event.getEntity() instanceof Enderman) if(event.getEntity().getType() == EntityType.ENDERMAN)
{ {
//and the block is claimed //and the block is claimed
if(this.dataStore.getClaimAt(event.getBlock().getLocation(), false, null) != null) if(this.dataStore.getClaimAt(event.getBlock().getLocation(), false, null) != null)
@ -550,7 +551,7 @@ public class EntityEventHandler implements Listener
Entity remover = entityEvent.getRemover(); Entity remover = entityEvent.getRemover();
//again, making sure the breaker is a player //again, making sure the breaker is a player
if(!(remover instanceof Player)) if(remover.getType() != EntityType.PLAYER)
{ {
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -686,7 +687,7 @@ public class EntityEventHandler implements Listener
if(damageSource != null) if(damageSource != null)
{ {
if(damageSource instanceof Player) if(damageSource.getType() == EntityType.PLAYER)
{ {
attacker = (Player)damageSource; attacker = (Player)damageSource;
} }
@ -735,7 +736,7 @@ public class EntityEventHandler implements Listener
} }
//if the attacker is a player and defender is a player (pvp combat) //if the attacker is a player and defender is a player (pvp combat)
if(attacker != null && event.getEntity() instanceof Player && GriefPrevention.instance.pvpRulesApply(attacker.getWorld())) if(attacker != null && event.getEntityType() == EntityType.PLAYER && GriefPrevention.instance.pvpRulesApply(attacker.getWorld()))
{ {
//FEATURE: prevent pvp in the first minute after spawn, and prevent pvp when one or both players have no inventory //FEATURE: prevent pvp in the first minute after spawn, and prevent pvp when one or both players have no inventory
@ -879,7 +880,7 @@ public class EntityEventHandler implements Listener
if(attacker == null) if(attacker == null)
{ {
//exception case //exception case
if(event.getEntity() instanceof Villager && damageSource != null && damageSource instanceof Zombie) if(event.getEntityType() == EntityType.VILLAGER && damageSource != null && damageSource.getType() == EntityType.ZOMBIE)
{ {
return; return;
} }
@ -982,7 +983,7 @@ public class EntityEventHandler implements Listener
if(attacker == null) if(attacker == null)
{ {
//exception case //exception case
if(event.getEntity() instanceof Villager && damageSource != null && damageSource instanceof Zombie) if(event.getEntityType() == EntityType.VILLAGER && damageSource != null && damageSource.getType() == EntityType.ZOMBIE)
{ {
return; return;
} }
@ -1059,7 +1060,7 @@ public class EntityEventHandler implements Listener
if(damageSource != null) if(damageSource != null)
{ {
if(damageSource instanceof Player) if(damageSource.getType() == EntityType.PLAYER)
{ {
attacker = (Player)damageSource; attacker = (Player)damageSource;
} }
@ -1225,7 +1226,7 @@ public class EntityEventHandler implements Listener
if(effected == thrower) continue; if(effected == thrower) continue;
//always impact non players //always impact non players
if(!(effected instanceof Player)) continue; if(effected.getType() != EntityType.PLAYER) continue;
//otherwise if in no-pvp zone, stop effect //otherwise if in no-pvp zone, stop effect
//FEATURE: prevent players from engaging in PvP combat inside land claims (when it's disabled) //FEATURE: prevent players from engaging in PvP combat inside land claims (when it's disabled)

View File

@ -342,7 +342,7 @@ public class GriefPrevention extends JavaPlugin
pluginManager.registerEvents(blockEventHandler, this); pluginManager.registerEvents(blockEventHandler, this);
//entity events //entity events
EntityEventHandler entityEventHandler = new EntityEventHandler(this.dataStore); EntityEventHandler entityEventHandler = new EntityEventHandler(this.dataStore, this);
pluginManager.registerEvents(entityEventHandler, this); pluginManager.registerEvents(entityEventHandler, this);
//if economy is enabled //if economy is enabled
@ -979,7 +979,7 @@ public class GriefPrevention extends JavaPlugin
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
Player player = null; Player player = null;
if (sender instanceof Player) if (sender instanceof Player)
{ {
player = (Player) sender; player = (Player) sender;
} }