Solved How to set damage using createExplosion without changing explosion radius

Discussion in 'Plugin Development' started by Erimus, Jun 24, 2022.

  1. Offline

    Erimus

    Hi, I tried to make some kind of ability in explosions.
    But I wanted to increase explosion damage while maintaining explosion radius 3.

    So I found out using EntitiyDamageEvent.getCause and Damageble,
    I could increase explosion damage without changing radius.

    But that solution had problem. It works with all kinds of explosions, which I do not want.
    I only want to increase damage of explosion caused by the ability.

    How can I figure this out? Please help me out.

    Code:
    @EventHandler
        public void onClick(PlayerInteractEvent e){
            Player p = e.getPlayer();
            Action a = e.getAction();
            World w = p.getWorld();
    
            if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK){
                if (p.getItemInHand().getType().equals(Material.BLAZE_ROD)){
                    p.sendMessage("Power Activated");
                    Vector v = p.getLocation().getDirection();
                    p.setVelocity(new Vector(v.getX() * 6 , v.getY() + 8, v.getZ() * 6));
                    p.playSound(p.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 1.0f, 1.0f);
                }
            }
            if (a == Action.LEFT_CLICK_AIR || a == Action.LEFT_CLICK_BLOCK){
                if (p.getItemInHand().getType().equals(Material.BLAZE_ROD)){
                    p.sendMessage("Boom!");
                    Vector v = p.getLocation().getDirection();
    
                    for (int i = 5; i <= 100; i += 5){
                        Vector tmp = new Vector(v.getX()*i, v.getY()*i, v.getZ()*i);
                        w.createExplosion(p.getLocation().add(tmp), 3, false);
                    }
                }
            }
        }
    
        @EventHandler
        public void onExplosionDamge(EntityDamageEvent evt){
            Damageable e = (Damageable) evt.getEntity();
            if (evt.getCause() == BLOCK_EXPLOSION){
                e.damage(50);
            }
        }
     
  2. Offline

    gochi9

    When you create the explosion you can use the mothod getNearbyEntity(Location, x, y, z) from the World class to loop through the entities near the explosion's location, check if they are players and save their UUID's in a list. Then on the damage event check for the damage cause and if the player is in the list then change the damage and finnaly don't forget to remove the player from the list.
     
    Erimus likes this.
  3. Offline

    Strahan

    A slightly simpler approach would be to just store the location of special explosions in a map with an expiration, then when an entity is explosion damaged check if any map keys are within range of the entity location. If so, apply the extra damage. Then have a task timer runnable that starts at startup and iterates the map, checking if current time is less than the value. If so, continue. Then after that, you call iterator#remove().
     
    Erimus likes this.
  4. Offline

    Erimus

    Thanks a lot man. You're real hero
     
    Strahan likes this.

Share This Page