Solved Detect if Entity is damaged with Bed Explosion

Discussion in 'Spigot Plugin Development' started by apatuka, Jun 30, 2020.

  1. Hello,

    I have this code:

    Code (Java):
        @EventHandler
        public void onExplosion(EntityDamageByEntityEvent e) {
            if (e.getCause().equals(EntityDamageEvent.DamageCause.BLOCK_EXPLOSION) || e.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)) {

                if (e.getEntity() instanceof EnderDragon && e.getEntity().getLastDamageCause() instanceof Player) {
                    Entity damager = e.getDamager();
                    Player player = (Player) damager;

                    EnderDragon dragon = (EnderDragon) e.getEntity();
                    World world = player.getWorld();
                    Location location = player.getLocation();
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 180, 1));

                }
            }
        }
    What i want to acomplish is, if the Entity on this case EnderDragon gets damaged with explosion caused by BED using DamageCause.ENTITY_EXPLOSION i want to detect the damager, but whenever i try to explode beds it damages the EnderDragon as expected but it doesn't execute the "player.addPotionEffect".

    What could be possibly wrong ?
    Thank you !

    EDIT: The console doesn't spam any kind of warnings/errors.
     
  2. EntityDamageByEntityEvent is called when an entity is damaged by an entity.
    The bed explosion damage is from the bed, and the bed is not an entity, so the event will never be called.
    You can use EntityDamageEvent or EntityDamageByBlockEnevt then it should works.

    btw, this from your codes will never return true because "Player" is not a type of "DamageCause".
    Code (Java):
    e.getEntity().getLastDamageCause() instanceof Player
     
    • Agree Agree x 1
  3. getLastDamageCause() retrieves the last EntityDamageEvent of an entity.
     
    • Agree Agree x 1
  4. getDamager() retrieves the block, how can i get the player who exploded the block ?

    Update: Will try getDamager().getLocation() and then check players around block to trigger.
     
    #4 apatuka, Jun 30, 2020
    Last edited: Jun 30, 2020
  5. I am not sure if this is the best way. Maybe you can listen to PlayerInteractEvent and check if it is right click bed block, if so, save the block and the player to a hashmap, then in the damage event you can use the block to get the player who click the bed from the hashmap.
     
    • Like Like x 1
  6. Because the e.getDamager() returns null, i ended used e.getEntity().getLocation() to get players nearby the entity to do the stuff. For now this works well for me, thank you so much !

    I still need to learn to code JAVA to get a better understand of the Javadocs
     
    • Like Like x 1