Solved EntityDamageByEntity getHealth weird glitch

Discussion in 'Spigot Plugin Development' started by notjacob1, Feb 4, 2020.

  1. So I made an EntityDamageByEntity listener to catch when a player dies and cancel that, but for some reason, it is killing them at a health higher than 0, even though I'm making sure its 0?

    Code (Text):

    @EventHandler
    public void on(EntityDamageByEntityEvent e) {
        if (e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
            Player p = (Player) e.getEntity();
            Player d = (Player) e.getDamager();
            PlayerCache pc = PlayerHandler.getFromPlayer(p);
            PlayerCache dc = PlayerHandler.getFromPlayer(d);
                if (pc.getInDuel() && dc.getInDuel() && pc.getInstance().getOther(p).equals(d)) {
                    if (p.getHealth() - p.getLastDamage() <= 0.0D) {
                        System.out.println("Player is DEAD " + p.getHealth()); //where i check with the console output
                        DuelResult r = new DuelResult(d, p, true, EnumDuelResult.RESOLVED);
                        e.setCancelled(true);
                        p.setHealth(20D);
                        p.setGameMode(GameMode.SPECTATOR);
                            for (ItemStack i : p.getInventory()) {
                                p.getWorld().dropItemNaturally(p.getLocation(), i);
                            }
                        p.getInventory().clear();
                        pc.getInstance().end(r);
                        p.setGameMode(GameMode.SURVIVAL);
                    }
              }
        }
    }

    [19:23:45 INFO]: Player is DEAD 7.199997425079346

     
    No console errors or anything
    Any ideas?
     
  2. p.getHealth() - p.getLastDamage() is <= 0, that doesnt mean p.getHealth() = 0. also idk if its different but im pretty sure you should be using e.getFinalDamage() not p.getLastDamage()
     
    • Winner Winner x 1
  3. Sorry I worded my problem wrong; The sword I was using does about 1.5 hp per hit and it was killing while the player still had 7 hp before hit
    your e.getFinalDamage() fixed that anyways though, so thanks!