Solved Issue with EntityDamageEvent

Discussion in 'Spigot Plugin Help' started by diamante_0018, Jan 24, 2020.

  1. So I have this little problem with the EntityDamageEvent. I simply want to set the damage received by the player to 0.
    It seems that the solution suggested by other threads is to cancel the event.
    Well, that doesn't seem to work for me. Down below you can find my code.
    As you can see it is supposed to work only if inside the armour[0] there is a pair of diamond boots.
    After some testing, it seems that indeed the boots data is stored inside index 0.
    The event is triggered correctly because the code that is omitted after the cancelled event works, just the part where I want to cancel the damage doesn't work.
    Code (Java):
    @EventHandler
        public void onFallDamage(EntityDamageEvent e) {
            if (e.getCause() == DamageCause.FALL && e.getEntityType() == EntityType.PLAYER) {
                Player player = (Player) e.getEntity();
                ItemStack[] armor = player.getInventory().getArmorContents();
       
                if (armor[0] != null && armor[0].getType() == Material.DIAMOND_BOOTS) {
                     double damageToDeliver = e.getDamage();
                     e.setCancelled(true);
    //Other code after the damage is supposed to be canceled.
        }
           }
               }
     
    I'm using 1.15.2 and yes I have registered the event inside the onLoad function() etc..
    One thing that might be different is that I'm using Java SDK 13.0.2. I know that a lot of people are still on 1.8 but I made sure that I'm both the server and the plugins are built on the latest version. (I don't know if that catches anything and my client also uses the latest SE)
     
    #1 diamante_0018, Jan 24, 2020
    Last edited: Jan 26, 2020
  2. #2 tappestry, Jan 25, 2020
    Last edited: Jan 25, 2020
  3. Thanks for the help, after a lot of testing I realised that the problem was not the .setCancelled but rather something that I did later.
    Code (Java):
    Collection<Entity> nearbyEntites = e.getEntity().getLocation().getWorld().getNearbyEntities(e.getEntity().getLocation(), 4, 2, 4);
    This line of code is supposed to get all the entities near the player after he has taken fall damage. I didn't realize that .getNearbyEntities also included the player itself! Simply put, after that line of code I ran a for each loop and damaged every single entity inside the Collection including the player. That's why I thought that the .setCancelled didn't work. My bad.