Search in sources :

Example 1 with DisallowedPVPEvent

use of com.sk89q.worldguard.bukkit.protection.events.DisallowedPVPEvent in project WorldGuard by EngineHub.

the class RegionProtectionListener method onDamageEntity.

@EventHandler(ignoreCancelled = true)
public void onDamageEntity(DamageEntityEvent event) {
    // Don't care about events that have been pre-allowed
    if (event.getResult() == Result.ALLOW)
        return;
    // Region support disabled
    if (!isRegionSupportEnabled(event.getWorld()))
        return;
    // Whitelist check is below
    com.sk89q.worldedit.util.Location target = BukkitAdapter.adapt(event.getTarget());
    RegionAssociable associable = createRegionAssociable(event.getCause());
    RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
    Player playerAttacker = event.getCause().getFirstPlayer();
    boolean canDamage;
    String what;
    // Block PvP like normal even if the player has an override permission
    // because (1) this is a frequent source of confusion and
    // (2) some users want to block PvP even with the bypass permission
    boolean pvp = event.getEntity() instanceof Player && playerAttacker != null && !playerAttacker.equals(event.getEntity());
    if (isWhitelisted(event.getCause(), event.getWorld(), pvp)) {
        return;
    }
    /* Hostile / ambient mob override */
    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";
    /* Paintings, item frames, etc. */
    } else if (Entities.isConsideredBuildingIfUsed(event.getEntity())) {
        canDamage = query.testBuild(target, associable, combine(event));
        what = "change that";
    /* PVP */
    } else if (pvp) {
        LocalPlayer localAttacker = WorldGuardPlugin.inst().wrapPlayer(playerAttacker);
        Player defender = (Player) event.getEntity();
        // if defender is an NPC
        if (Entities.isNPC(defender)) {
            return;
        }
        canDamage = query.testBuild(target, associable, combine(event, Flags.PVP)) && query.queryState(localAttacker.getLocation(), localAttacker, combine(event, Flags.PVP)) != State.DENY && query.queryState(target, localAttacker, combine(event, Flags.PVP)) != State.DENY;
        // Fire the disallow PVP event
        if (!canDamage && Events.fireAndTestCancel(new DisallowedPVPEvent(playerAttacker, defender, event.getOriginalEvent()))) {
            canDamage = true;
        }
        what = "PvP";
    /* Player damage not caused  by another player */
    } else if (event.getEntity() instanceof Player) {
        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, Flags.DAMAGE_ANIMALS));
        what = "harm that";
    /* Everything else */
    } else {
        canDamage = query.testBuild(target, associable, combine(event, Flags.INTERACT));
        what = "hit that";
    }
    if (!canDamage) {
        tellErrorMessage(event, event.getCause(), event.getTarget(), what);
        event.setCancelled(true);
    }
}
Also used : RegionAssociable(com.sk89q.worldguard.protection.association.RegionAssociable) Player(org.bukkit.entity.Player) LocalPlayer(com.sk89q.worldguard.LocalPlayer) LocalPlayer(com.sk89q.worldguard.LocalPlayer) RegionQuery(com.sk89q.worldguard.protection.regions.RegionQuery) DisallowedPVPEvent(com.sk89q.worldguard.bukkit.protection.events.DisallowedPVPEvent) EventHandler(org.bukkit.event.EventHandler)

Aggregations

LocalPlayer (com.sk89q.worldguard.LocalPlayer)1 DisallowedPVPEvent (com.sk89q.worldguard.bukkit.protection.events.DisallowedPVPEvent)1 RegionAssociable (com.sk89q.worldguard.protection.association.RegionAssociable)1 RegionQuery (com.sk89q.worldguard.protection.regions.RegionQuery)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1