Returning entity from EntityDamageEvent

Discussion in 'Plugin Development' started by Lightcaster5, Dec 28, 2019.

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

    Lightcaster5

    I am trying to create a minigame but don't want players to see the "Respawn" screen so I am using a EntityDamageEvent like this:
    Code:
    @EventHandler
        public void entityDamage(EntityDamageEvent event) {
            if (event.getEntity() instanceof Player) {
                Player player = ((Player) event.getEntity()).getPlayer();
                if (((Damageable) event.getEntity()).getHealth() - event.getFinalDamage() <= 0) {
                    if (event.getCause() == DamageCause.ENTITY_ATTACK
                            && player.getLastDamageCause().getEntity() instanceof Player) {
                        Player attacker = player.getKiller();
                        player.getInventory().clear();
                        player.setHealth(20D);
                        player.setFoodLevel(20);
                        player.setFireTicks(0);
                        for (PotionEffect effect : player.getActivePotionEffects()) {
                            player.removePotionEffect(effect.getType());
                        }
                        player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 60, 50, true));
    The problem is, when a player is killed by a mob it still returns as a player type. Any suggestions?
     
  2. Offline

    Zombie_Striker

    @Lightcaster5
    Try debugging what the last damage cause is. Print out the EntityType, and check to make sure that player was not damaged by any other player before being killed.

    Also, getKiller may return null if you make sure the player does not die, so you may want to check that.
     
  3. Offline

    Lightcaster5

    I already have it printing the player that died and the last entity that attacked it but unless it is something like a creeper explosion, it returns as a player. Check the if statement, if the damage to the player results in health lower than 0, then it will do what I want, if not, it doesn't do anything... that is working fine it's just the EntityType is not returning anything other than "PLAYER"
     
  4. Offline

    Zombie_Striker

    @Lightcaster5
    For the player type that returns for the last damage caused, which player is specified? Is it just some other player that is online, or does it reference the player that has died?
     
  5. Offline

    Lightcaster5

    It is referencing the player who has "died" (im using quotes because yes it would be a death but the player never really dies.)
     
  6. Offline

    Zombie_Striker

    @Lightcaster5
    Just noticed you are using "EntityDamageEvent". This is good if you are detecting damage from causes other than entities (like falling, drowning, void, ect.)

    For what you want, I recommend using EntityDamageByEntityEvent. This has a method called getDamager() which returns the entity instance that damaged the player.
     
  7. Offline

    Lightcaster5

    Thanks so much for the help, I got everything working properly. For anyone who struggled with a problem similar to mine, here you go
    Code:
        @EventHandler
        public void entityDamagebyEntity(EntityDamageByEntityEvent event) {
            if (event.getEntity() instanceof Player) {
                Player player = (Player) event.getEntity();
                if (player.getHealth() - event.getFinalDamage() <= 0) {
                    if (event.getDamager() instanceof Player) {
                        // Respawn Stuff
                        event.setCancelled(true);
                    } else {
                        // Respawn Stuff
                        event.setCancelled(true);
                    }
                }
            }
        }
     
  8. Offline

    Sw_aG

    FWI, there's a new Immidiate Respawn gamerule
     
Thread Status:
Not open for further replies.

Share This Page