Solved Canceling Death Event

Discussion in 'Plugin Development' started by bobthefish, Jan 6, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    bobthefish

    Hello,

    in a plugin that I am working on i require a player to not die when they are supposed to (when they would have normally i need to do something else). I know i need to use damage listeners to do so, but with the method e.getDamage(); I notice that it returns raw damage and does not take into account any armour that the player is wearing. I was wondering how I might modify it so that it takes into account the armour that the player is wearing before it "kills" them. here is my method so far:
    Code:
        @EventHandler
        public void PlayerLastDamage(EntityDamageByEntityEvent e) {
            if(e.getEntity() instanceof Player) {
                Player p = (Player) e.getEntity();
                if(!plugin.pm.isDead(p)){
                    if((((Damageable) p).getHealth() - e.getDamage()) <= 0) {
                        ((Player)e.getEntity()).damage(((Damageable) p).getHealth() - 1);
                        die(p);
                        p.sendMessage(ChatColor.RED + ((Player) e.getDamager()).getName() + ChatColor.BLUE + " killed you!");
                        p.sendMessage(ChatColor.BOLD + "Respawning in 5 seconds...");
                        ((Player) e.getDamager()).sendMessage(ChatColor.BLUE + "You killed " + ChatColor.GREEN + p.getName() + ChatColor.BLUE + "!");
                    }
                }
            }
        }
    Thanks!
     
  2. Offline

    sirrus86

    Try e.getFinalDamage() instead of e.getDamage(), this is the total amount of damage caused after stuff like armor is taken into consideration.
     
    bobthefish likes this.
  3. Offline

    bobthefish

    THANK YOU SO MUCH! xD i was looking everywhere for this, dont know why i didnt try the bukkit docs.... oh well thanks so much
     
Thread Status:
Not open for further replies.

Share This Page