Explosion Selective Knockback

Discussion in 'Spigot Plugin Development' started by R3dThunderr, Apr 28, 2017.

  1. I summon an explosion in a world at a player's location. The knockback from the explosion should not affect the player or anyome on the arraylist "noKnock".

    I know how to create the explosion, I just don't know how to smoothly disable knockback for certain people only.
     
  2. Try to use the EntityDamageEvent and check if the last damage cause was an explosion, then set the damaged entity's velocity to 0
     
  3. Wouldn't that make them stop moving? If someone is running and in noKnock I want the explosion to have no effect on them.
     
  4. Hmm yea checked that quickly, but i wrote a working function, would be similar to this:

    Code (Text):


    private List<Player> damagedByExplosion = new ArrayList<Player>();

    @EventHandler
    public void velocityExplosion(PlayerVelocityEvent e){
      Player player = e.getPlayer();
        if(damagedByExplosion.contains(player)){
            e.setVelocity(new Vector(0,0,0));
            damagedByExplosion.remove(player);
        }
    }


    @EventHandler
    public void takenDamage(EntityDamageEvent e){
        if(e.getEntity() instanceof Player){
            Player player = (Player)e.getEntity();
            if(e.getCause() == EntityDamageEvent.DamageCause.BLOCK_EXPLOSION || e.getCause() == EntityDamageEvent.DamageCause.ENTITY_EXPLOSION){
                damagedByExplosion.add(player);
            }
        }
    }
     
     
  5. Correct me if I am wrong, but you're firing an event when the player's velocity changes, then checking the source.
    Cancel from there?
     
  6. yea, the code above checks the damage cause and adds the player to a list just to make sure he he got damaged and since the knockback of the Explosion fires a velocity change the PlayerVelocityEvent is beeing fired, then its beeing checked if the player is in the list and the velocity would be stopped.

    I tried the same with using only the velocity event and the last damage cause of the player but that won't work/would stop the velocity all the time until a New damage cause is beeing called