1.14.4 Canceling explosion damage to entities

Discussion in 'Spigot Plugin Development' started by DavidMa, Feb 10, 2020.

  1. Hi.
    I am trying to create a plugin that overrides the default TNT explosion behavior and use a less expensive approach to minimize TNT's impact on performance.
    Code (Java):
    @EventHandler(priority = EventPriority.LOWEST)
        public void onExplosion(EntityExplodeEvent event) {
            if (event.getEntityType() == EntityType.PRIMED_TNT) {
                event.setCancelled(true);
               
                // Values obtained from config are replaced by constants for posting.
                int radius = 3;
                boolean shouldDrop = true;
                int resistanceThreshold = 500;
             
                Location location = event.getLocation().clone().subtract(radius, radius, radius);
                if (event.getLocation().getBlock().isLiquid()) return;
             
                int diameter = radius * 2 + 1;
                for (int x = 0; x < diameter; x++) {
                    for (int y = 0; y < diameter; y++) {
                        for (int z = 0; z < diameter; z++) {
                            Block block = location.getBlock();
                            if (location.getBlock().getType().getBlastResistance() < resistanceThreshold) {
                                if (shouldDrop) {
                                    block.breakNaturally();
                                } else {
                                    block.setType(Material.AIR);
                                }
                            }
                            location.add(0, 0, 1);
                        }
                        location.add(0, 1, -diameter);
                    }
                    location.add(1, -diameter, 0);
                }
            }
        }
    The block breaking part is functioning as expected. However, despite the canceling of the EntityExplodeEvent, the player still takes explosion damage when standing next to an exploding TNT (as did other entities).
    This is not desired, as when an explosion damage entities, it will have to iterates through all entities in loaded chunks (at least in vanilla Minecraft), which will cause an overhead that I am trying to negate. How would I cancel the explosion's damage to entities?
     
  2. FrostedSnowman

    Resource Staff

    You're going to need to listen for entity damage. The same block destruction logic seems to be different from when the entity is calculated to take damage.
     
    • Like Like x 1
  3. Thanks for the reply.
    However, I am trying to cancel the entire "find nearby entities and damage them" part since the goal of this plugin is to minimize the TNT's impact on performance (TNT explosion not dealing damage to entities is only a side effect). Due to this, listening for entity damage and canceling them won't achieve the goal as the iteration through all entities is still performed.
    Is there an event corresponding to the part where the explosion finds nearby entities and damage them?