1.15.2 Disable EntityDamageEvent but keep knockback

Discussion in 'Spigot Plugin Development' started by CodersCrux, Apr 1, 2020.

  1. So, I've been seeing a bunch of threads about remove knockback but leaving the damage, but I'm doing the opposite. I have custom health values which makes the health bar only display how much health I have relative to the custom health.
    My problem is, when I set the EntityDamageEvent to cancelled it also disables the knockback, and I do want to leave the knockback in. Now, I've checked and there's nothing in the event that tells you how much knockback the player must take, so, is there any way to do that?
    Now, I don't want a constant value, because tnt should give more knockback, etc.

    Here is my event code.
    Code (Java):
    @EventHandler
        public void OnPlayerDamage(EntityDamageEvent e) {
            int damage = (int)(e.getDamage() * 5);
            if (e.getEntityType() == EntityType.PLAYER) {
                Player p = (Player)e.getEntity();

                new SBPlayer(p).setCurrentHealth(new SBPlayer(p).getCurrentHealth() - damage);
                p.setHealth(new SBPlayer(p).getCurrentHealth() / (new SBPlayer(p).stats.getHealth() / 20.0));
                p.playSound(p.getLocation(), Sound.ENTITY_PLAYER_HURT, 10, 1);
            }
        }
     
  2. I'd go about this by just cancelling the event and envoking my own knockback by setting the hit entities velocity
     
  3. That's the thing tho, I would need to get all the default values for every single type of hit, while I'm sure there must be a way around it..
     
  4. Hmm, I'm not sure if there is a solid workaround, but maybe you could try just taking the damage and readd it to the entity so they don't take the damage. Or I would create velocities for every type of attack instead of every damageable meaning, projectiles might all knockback the same, punches from entities might all knockback the same except maybe an iron golem has more knockback when they attack something, etc.
     
  5. Okay I think what I decided to do to keep the knockback, is to start a delayed task 1 or 2 ticks after the event, and then set the currenthealth to the custom one, this way it'll be as barely noticeable as possible and might not even show it updating. Thanks for the help tho.