Solved Remove knockback on damage

Discussion in 'Spigot Plugin Development' started by BemLo, Oct 8, 2018.

  1. Hi! As I said in title, i want remove knockback when entity got damaged.
    I have this code now:

    Code (Java):
            @EventHandler
            public void Damage(EntityDamageByEntityEvent e) {
                    Entity le = e.getEntity();
                    if(le.getType() != EntityType.PLAYER) {
                        le.setVelocity(new Vector());
                    }
            }
    Tried a lot of things from google, but nothing helped
     
  2. Have your tried delaying when you set the velocity by a tick
     
  3. Try using
    Code (Java):

        final Vector vec = new Vector();
        p.setVelocity(vec);

                  new BukkitRunnable() {
                     public void run() {
                         p.setVelocity(vec);
                     }
                   }.runTaskLater(yourMainClass, 1l);
     
     
  4. Better use Lambda if it's going to be for a simple method.
     
    • Agree Agree x 1
  5. Indeed lambda expression could be used:
    Code (Java):
    Bukkit.getScheduler().runTaskLater(yourMainClass, () -> p.setVelocity(vec), 1l);
    However keep in mind, refering to OP as well, the piece of code earlier was found on google after a 2 min search. (@OP) If it actually worked and this has solved your issue, which I'm not entirely sure if it will, you surely did not search more than the first link after typing your issue once.

    EDIT: I'd like to mention that bukkitrunnables do not support lamda expressions but they can be self cancelled where as a scheduler supports lamda expressions but can't be self cancelled. First one returns a BukkitTask object where as the latter returns an int which can be used to cancel it using Bukkit#getScheduler#cancelTask(taskId) where taskId is retrieved from the task itself as I said earlier, it returns an int.
     
    #5 Shin1gamiX, Oct 8, 2018
    Last edited: Oct 8, 2018
  6. Thank you all very much! It works!