From cf1716ff372003a8ba658fd2451839b9d23c1894 Mon Sep 17 00:00:00 2001 From: wizjany Date: Thu, 28 May 2015 17:03:58 -0400 Subject: [PATCH] Separate vehicles and animals from INTERACT flag. Destroying vehicles now checks only the vehicle-destroy flag. Damaging animals checks the new damage-animals flag. These are both allowed for members by default, like other build-dependent flags. Fixes WORLDGUARD-3427 and WORLDGUARD-3358. --- .../bukkit/listener/EventAbstractionListener.java | 2 +- .../bukkit/listener/RegionProtectionListener.java | 11 +++++++++-- .../com/sk89q/worldguard/bukkit/util/Entities.java | 11 +++++++++++ .../worldguard/protection/flags/DefaultFlag.java | 3 ++- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java b/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java index bd9fcc61..58a8f3ae 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java @@ -693,7 +693,7 @@ public void onPlayerDropItem(PlayerDropItemEvent event) { @EventHandler(ignoreCancelled = true) public void onVehicleDamage(VehicleDamageEvent event) { Entity attacker = event.getAttacker(); - Events.fireToCancel(event, new DestroyEntityEvent(event, create(attacker), event.getVehicle())); + Events.fireToCancel(event, new DamageEntityEvent(event, create(attacker), event.getVehicle())); } @EventHandler(ignoreCancelled = true) diff --git a/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java b/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java index 97c83122..ba2db033 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java @@ -390,7 +390,8 @@ public void onUseEntity(UseEntityEvent event) { String what; /* Hostile / ambient mob override */ - if (Entities.isHostile(event.getEntity()) || Entities.isAmbient(event.getEntity()) || Entities.isNPC(event.getEntity())) { + if (Entities.isHostile(event.getEntity()) || Entities.isAmbient(event.getEntity()) + || Entities.isNPC(event.getEntity()) || Entities.isVehicle(event.getEntity().getType())) { canUse = event.getRelevantFlags().isEmpty() || query.queryState(target, associable, combine(event)) != State.DENY; what = "use that"; @@ -439,7 +440,8 @@ public void onDamageEntity(DamageEntityEvent event) { } /* Hostile / ambient mob override */ - if (Entities.isHostile(event.getEntity()) || Entities.isAmbient(event.getEntity())) { + if (Entities.isHostile(event.getEntity()) || Entities.isAmbient(event.getEntity()) + || Entities.isVehicle(event.getEntity().getType())) { canDamage = event.getRelevantFlags().isEmpty() || query.queryState(target, associable, combine(event)) != State.DENY; what = "hit that"; @@ -467,6 +469,11 @@ public void onDamageEntity(DamageEntityEvent event) { canDamage = event.getRelevantFlags().isEmpty() || query.queryState(target, associable, combine(event)) != State.DENY; what = "damage that"; + /* damage to non-hostile mobs (e.g. animals) */ + } else if (Entities.isNonHostile(event.getEntity())) { + canDamage = query.testBuild(target, associable, combine(event, DefaultFlag.DAMAGE_ANIMALS)); + what = "harm that"; + /* Everything else */ } else { canDamage = query.testBuild(target, associable, combine(event, DefaultFlag.INTERACT)); diff --git a/src/main/java/com/sk89q/worldguard/bukkit/util/Entities.java b/src/main/java/com/sk89q/worldguard/bukkit/util/Entities.java index 3aa6d56c..74fa82a4 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/util/Entities.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/util/Entities.java @@ -133,6 +133,17 @@ public static boolean isHostile(Entity entity) { || entity instanceof EnderDragon; } + /** + * Test whether an entity is a non-hostile creature. + * + * @param entity + * @return true if non-hostile + */ + public static boolean isNonHostile(Entity entity) { + return !isHostile(entity) + && (entity instanceof Creature || entity instanceof WaterMob); + } + /** * Test whether an entity is ambient. * diff --git a/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java b/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java index 265fd8d5..9aad43f4 100644 --- a/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java +++ b/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java @@ -49,6 +49,7 @@ public final class DefaultFlag { public static final StateFlag BLOCK_PLACE = new StateFlag("block-place", false); public static final StateFlag USE = new StateFlag("use", false); public static final StateFlag INTERACT = new StateFlag("interact", false); + public static final StateFlag DAMAGE_ANIMALS = new StateFlag("damage-animals", false); public static final StateFlag PVP = new StateFlag("pvp", false); public static final StateFlag SLEEP = new StateFlag("sleep", false); public static final StateFlag TNT = new StateFlag("tnt", false); @@ -140,7 +141,7 @@ public final class DefaultFlag { public static final Flag[] flagsList = new Flag[] { PASSTHROUGH, BUILD, CONSTRUCT, BLOCK_BREAK, BLOCK_PLACE, PVP, CHEST_ACCESS, PISTONS, - TNT, LIGHTER, RIDE, USE, INTERACT, PLACE_VEHICLE, DESTROY_VEHICLE, SLEEP, + TNT, LIGHTER, RIDE, USE, INTERACT, PLACE_VEHICLE, DESTROY_VEHICLE, DAMAGE_ANIMALS, SLEEP, MOB_DAMAGE, MOB_SPAWNING, DENY_SPAWN, INVINCIBILITY, EXP_DROPS, CREEPER_EXPLOSION, OTHER_EXPLOSION, ENDERDRAGON_BLOCK_DAMAGE, GHAST_FIREBALL, ENDER_BUILD, DENY_MESSAGE, ENTRY_DENY_MESSAGE, EXIT_DENY_MESSAGE, EXIT_OVERRIDE, EXIT_VIA_TELEPORT,