Stopping Land Damage while Still causing Player/Mob Damage

Discussion in 'Plugin Development' started by iKeirNez, Jan 26, 2012.

Thread Status:
Not open for further replies.
  1. How would I apply:

    Code:
    event.blockList.clear()
    to

    Code:
    world.createExplosion(loc, (plugin.getConfig().getInt("Options.Explosion.Radius")));
    I basically want mobs and players to still get damage from the explosion but I don't want it doing any block damage.

    My first thought was creating and EntityExplodeEvent and doing:

    Code:
    if (event instanceof plugin)
    But you can't do that, so what other ways could I do this without getting nearby entities and manually giving them damage?

    Keir
     
  2. I wrote a world repair with this approach:

    - Let the explosion happen
    - keep the information of each block in explosion's blocklist
    - after a short while, i take all the information and "repair" the exploded blocks

    You still take damage, the blocks are destroyed, but get repaired after.
     
  3. That sounds like a great idea but for the moment I just want to be able to cancel it breaking blocks.

    Keir
     
  4. Offline

    nisovin

    I believe a plugin-caused explosion will still cause an EntityExplodeEvent. The entity in the event will be null.
     
  5. Would it be possible to change the entity id of an explosion I am creating. So that I could do something like this

    if(event instanceof LockedChestSurprise){

    //Run code here!

    }

    LockedChestSurprise is the name of my plugin btw.

    Keir
     
  6. Offline

    nisovin

    Not really. But you could save the location of the explosion, then in EntityExplodeEvent check if the explosion location matches your saved location.
     
  7. Ok, i'll try that thanks!

    You might wanna make it so that sand and gravel get replaced from the bottom up, otherwise a sand block could be placed, It falls and then where it fell another sand block is placed, causing it to ruin the landscape.

    Keir

    How would I store the location and then retrieve it from another file?

    Keir

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  8. Offline

    boardinggamer

    just do the onEntityExplode event and cancel the event. it will still blow up but not do any damage to blocks
     
  9. Offline

    nisovin

    Huh? When you call createExplosion() you pass in a location. Just store that location in a variable so you can refer to it in onEntityExplode.
     
  10. Ok, i'll try and figure this one out.

    Keir

    But I want it to do mob and player damage without doing getnearbyentities. Also that would disable all explosions.

    Keir

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  11. Offline

    Technius

    I looked at NMS files. createExplosion throws no event. Spawn a TNT, add it to a list, see it if is on the list onEntityExplode, and clear the blocklist if it is.
     
  12. Offline

    boardinggamer

    Code:java
    1. @EventHandler
    2. public void onEntityExplode(EntityExplodeEvent event){
    3. Entity entity = event.getEntity();
    4. if (entity instanceof TNTPrimed){
    5. event.setCancelled(true);
    6. }
    7. }


    it will still hurt mobs and players but not break blocks. or if you want it on a creeper explosion just change "TNTPrimed" to "Creeper"
     
  13. Offline

    nisovin

    Look closer. createExplosion creates an instance of nms.Explosion, which always calls the event.
     
  14. Offline

    Technius

    Oh... just saw it. The entity in createExplosion is null.
     
  15. Yes but I only want it to do this in my Plugin, e.g. I want normal TNT and creepers to still do their damage.

    But what if another Plugin was also creating an explosion, it would also stop the block damage which I do not want. I want it to only cancel block damage with explosions created with my Plugin.

    Any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  16. Offline

    Technius

    Try this:
    Code:java
    1. public static boolean blockdamage = false;
    2. @EventHandler
    3. public void onEntityExplode(EntityExplodeEvent event)
    4. {
    5. if(blockdamage && event.getEntity() == null)event.blockList().clear();
    6. }

    When you want to cause your explosion do this:
    Code:java
    1. YourClass.blockdamage = true;//change "YourClass" to your listener
    2. world.createExplosion(loc, (plugin.getConfig().getInt("Options.Explosion.Radius")));
    3. YourClass.blockdamage = false;
     
  17. Offline

    Tj3

    The issue with Technius's code is that it cancels any plugin explosions' damage to blocks. For example, if you have some kind of exploding arrows, that won't work to destroy blocks anymore. I'd add a Set<Location>, and before you create the explosion, add the location to the set. Only clear the blocklist if the location of the explosion is in the set, and then remove it from the set.
     
  18. Offline

    nisovin

    Not really. It will only cancel other explosions if they happen in the exact same tick, which is very unlikely and possibly not even possible.

    Edit: In fact, I'm fairly confident it's not possible, since the event will get called exactly between the times the variable gets set and reset. No other code will get an opportunity to run.
     
  19. Wow thanks! This seems to work! This is much appreciated. Never thought of this method before.

    Keir
     
  20. Offline

    tincopper2

    I think he wants createExplosion event not TNTPrimed.class
     
  21. Offline

    Firefly

    Here's what I used; seems to work fine.

    Code:
    event.setCancelled(true);
    someEntity.getWorld().createExplosion(tnt.getLocation(), 0.0F);
    Still does damage to nearby mobs/players.
     
  22. Thanks for your recent replies but I posted this back when I barely knew anything about bukkit. Just thought i would say this to prevent further bumping :p

    EDIT: Yes that means you don't have to reply :)
     
    Firefly likes this.
Thread Status:
Not open for further replies.

Share This Page