How to properly cancel TNT explosions?

Discussion in 'Plugin Development' started by Etsijä, Sep 3, 2012.

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

    Etsijä

    I'm writing a plugin which is used to alter TNT behaviour into a "grenade" type, ie. where you throw or drop TNT and it explodes only from touching non-air blocks. But I'm at lost as to how to cancel TNT explosions to start with? Obviously, this does not seem to work:

    Code:
    public class eListener implements Listener {
      @EventHandler
      public void onIgniteTNT(BlockIgniteEvent event) {
     
        IgniteCause cause = event.getCause();
        // Listen only for the TNT ignited by the player
        if (!cause.equals(IgniteCause.FLINT_AND_STEEL)) return;
     
        Block block = event.getBlock();
        player = event.getPlayer();
       
        player.sendMessage(ChatColor.RED + "Trying to cancel ignition!");
        event.setCancelled(true);
      }
    }
    
    With the code, no message is sent to player and TNT explodes. So, quite obviously canceling BlockIgniteEvent is not enough.
     
  2. Offline

    calebbfmv

    :D Its not BlockIgniteEvent, as that doesn't work. It doesn't work because TNT, when ignited, becomes an entity, so to cancel it you have to do this.
    Code:
    @EventHandler
    public void onEntityExplode(EntityExplodeEvent event)
    //do whatever you need to do here :D
    
     
  3. Offline

    Phinary

  4. Offline

    calebbfmv

  5. Offline

    whitehooder

    To stop TNT explosions you should use the EntityExplodeEvent and check if the entity is a primed TNT (not a creeper) and cancel the event. Note: (Might help) The entity code for primed tnt is 20.
     
  6. Offline

    Etsijä

    Thank y'all, I'll look into this. Funny thing, though, that TNT doesn't respond to BlockIgniteEvent, although you ignite it with flint and tinder. Sounds a bit weird to me.
     
  7. Offline

    whitehooder

    It does, but you can't cancel the explosion from there, you can only cancel the igniting with the BlockIgniteEvent. Sounds good to me.
     
  8. Offline

    calebbfmv

    Yep because TNT "changes" block state from Block to entity you have to check that.
     
  9. Offline

    Etsijä

    I understand, but then, wouldn't it be better to remove responding to BlockIgniteEvent totally from TNT - since it doesn't really do anything? I mean, I do cancel its ignition in my listener, but still it ignites (and then explodes).
     
  10. Offline

    Zolli

    Try this.
    This code get all block damaged by TNT, and clear all damges. (Remove block from list). This code also damage the entitys.

    Code:
    @EventHandler(priority=EventPriority.NORMAL)
        public void explodeHeight(EntityExplodeEvent e) {
            if(e.getEntityType() == EntityType.PRIMED_TNT) {
                e.blockList().clear();
            }
        }
     
  11. Offline

    calebbfmv

    OR
    You could cancel the explosion
    Code:
    @EventHandler
    public void explodeEvent(EntityExplodeEvent event){
    event.setcancelled(true);
    
     
    Craftiii4 likes this.
  12. Offline

    Etsijä

    Yes, I was able to cancel the explosion now. It still damages entities and loses the TNT though, so the solution is not what I need yet. I would need a way to prolong the "countdown" time indefinitely (until the TNT hits something), any ideas? Tampering with TNTPrimed's setFuseTicks() method?
     
Thread Status:
Not open for further replies.

Share This Page