Cancelling DamageFromTNT

Discussion in 'Plugin Development' started by Smeary_Subset, May 5, 2022.

  1. Offline

    Smeary_Subset

    I am trying to cancel the damage from a PrimedTNT's explosion when the damage is to the person who primed it. In other words, players cannot be damaged by the TNT they prime. For some reason, however, the damage event I coded below is not being cancelled despite both 'if' statements returning true. Any ideas?

    Code:
        @EventHandler
        private void onDamageSelfByTNT(EntityDamageByEntityEvent event) {
            new DamageSelfByTNTHandler(event).run();
        }
       
        static class DamageSelfByTNTHandler {
            EntityDamageByEntityEvent event;
            DamageSelfByTNTHandler(EntityDamageByEntityEvent event) {
                this.event = event;
            }
            void run() {
                if(damagerIsTNT() && damagedEntityPrimedTNT())
                    event.setCancelled(true);
            }
            boolean damagerIsTNT() {
                return event.getDamager() instanceof TNTPrimed;
            }
            boolean damagedEntityPrimedTNT() {
                Bukkit.broadcastMessage(Objects.equals(((TNTPrimed) event.getDamager()).getSource(), event.getEntity()) + "");
                return Objects.equals(((TNTPrimed) event.getDamager()).getSource(), event.getEntity());
            }
        }
     
  2. Offline

    Shqep

    @Smeary_Subset
    You sure it's not something else uncancelling the event?
    I made a quick short test plugin:

    Code:Java
    1.  
    2. private void onDamage(final EntityDamageByEntityEvent event) {
    3. event.setCancelled(event.getDamager() instanceof TNTPrimed && ((TNTPrimed) event.getDamager()).getSource().getUniqueId().equals(event.getEntity().getUniqueId()));
    4. }
    5.  


    It worked fine. I don't know if passing into static classes makes it not work somehow. Would be very weird if if that's the case.
     
  3. Offline

    Strahan

    getSource() is nullable, you shouldn't chain it like that.

    OP, are you sure the event is registered?
     

Share This Page