Skip to content

Commit

Permalink
Separate vehicles and animals from INTERACT flag.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wizjany committed May 28, 2015
1 parent a277e69 commit cf1716f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
Expand Up @@ -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)
Expand Down
Expand Up @@ -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";

Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -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));
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/sk89q/worldguard/bukkit/util/Entities.java
Expand Up @@ -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.
*
Expand Down
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit cf1716f

Please sign in to comment.