Util Check for attack protection

Discussion in 'Resources' started by 0ct0berBkkitPlgins, Aug 25, 2015.

Thread Status:
Not open for further replies.
  1. Checking for attack protection is simply seeing if Player A can attack Player B without being stopped by a plugin. This is extremely important for any plugin that will heal allies, inflict negative effects, or damage a player. Without it, PvP protection can be bypassed through your plugin- meaning Factions members can damage/kill each other, players can fight in no PvP regions, etc.

    So, here I present the code I made with partial help from @FisheyLP for anyone who needs to do this check. Use this boolean before you run anything that could possibly result in a protection bypass. The suppress warning is because the method I use is outdated and thus deprecated (I use it because the current method is complicated and not necessary for this purpose):

    Code:
    public static boolean attackable(LivingEntity attacker, LivingEntity defender) {
            @SuppressWarnings("deprecation")
            EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(attacker, defender, DamageCause.CUSTOM, 1.0D);
            Bukkit.getPluginManager().callEvent(e);
            return !e.isCancelled();
    }
    This was originally discussed and figured out on http://bukkit.org/threads/getting-if-player-can-attack-other-player.381057/#post-3206131, mainly due to the help of @FisheyLP.
     
    Last edited: Sep 18, 2015
  2. Offline

    meguy26

    @0ct0berBkkitPlgins
    This is a very nice and awesome solution, but maybe post it like this:
    Code:
    public static boolean attackable(LivingEntity attacker, LivingEntity defender) {
        @SuppressWarnings("deprecation")
        EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(attacker, defender, DamageCause.CUSTOM, 1.0D);
        Bukkit.getPluginManager().callEvent(e);
        return !e.isCancelled();
    }
    #CodeBlocksFTW
     
Thread Status:
Not open for further replies.

Share This Page