Stop player damage from explosion

Discussion in 'Plugin Development' started by Molten, May 18, 2014.

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

    Molten

    I have a custom explosion I made with my plugin, I want it to still do the damage to blocks but not to the player that made it happen.

    For example, the player left clicks a block, it explodes, the blocks get exploded but the player isn't hurt.

    I have looked around but everything I have found is to stop block damage and keep player damage, I want the opposite.

    Here is my explosion being set:

    Code:java
    1. exploded = true;
    2. Location location = e.getClickedBlock().getLocation();
    3. World world = location.getWorld();
    4. world.createExplosion(location, 3f);


    I am not exactly sure how to do this but I think I have to use an explosion listener of some kind, that is the reasoning behind the exploded being set to true. After the player doesn't take any damage then the exploded is set to false again so that way it only takes no damage from this explosion and not normal ones like a creeper or tnt.

    Edit: I have a temporary fix to where right before the explosion happens, I get the players health, set the players health to max, then after the player is damaged I set it back to their original health. This is a very bad work around that I do not want to keep so I am hoping someone can help me :)
     
  2. Offline

    Senmori

    Listen to EntityDamageEvent and watch for DamageCause.
     
  3. Offline

    teej107

    Here is some code that I did for fall damage. It'll help.
    Code:java
    1. @EventHandler
    2. public void onEntityDamageEvent(EntityDamageEvent event)
    3. {
    4. if(event.getCause().equals(DamageCause.FALL))
    5. {
    6. Entity entity = event.getEntity();
    7. for(Entity ent : entity.getNearbyEntities(0.3, 0.3, 0.3))
    8. {
    9. if(ent instanceof LivingEntity)
    10. {
    11. ((LivingEntity) ent).damage(event.getDamage(), entity);
    12. }
    13. }
    14. }
    15. }

    I'm pretty sure there is a DamageCause for explosions.

    Epixpenguin IF your comment was to me, I took that code straight from one of my plugins. Didn't change it at all for that post.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page