From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Brokkonaut <hannos17@gmx.de>
Date: Mon, 18 Jun 2018 15:46:23 +0200
Subject: [PATCH] Implement EntityKnockbackByEntityEvent

This event is called when an entity receives knockback by another entity.

diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
index af2e81137d9c686f8d94a1d0d7241619fa1f352c..04489a915d11ba970a5188a5a913432ab4ef9faa 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -1336,7 +1336,7 @@ public abstract class LivingEntity extends Entity {
                     }
 
                     this.hurtDir = (float) (Mth.atan2(d1, d0) * 57.2957763671875D - (double) this.yRot);
-                    this.knockback(0.4F, d0, d1);
+                    this.doKnockback(0.4F, d0, d1, entity1); // Paper
                 } else {
                     this.hurtDir = (float) ((int) (Math.random() * 2.0D) * 180);
                 }
@@ -1384,7 +1384,7 @@ public abstract class LivingEntity extends Entity {
     }
 
     protected void blockedByShield(LivingEntity target) {
-        target.knockback(0.5F, target.getX() - this.getX(), target.getZ() - this.getZ());
+        target.doKnockback(0.5F, target.getX() - this.getX(), target.getZ() - this.getZ(), this); // Paper
     }
 
     private boolean checkTotemDeathProtection(DamageSource source) {
@@ -1627,6 +1627,11 @@ public abstract class LivingEntity extends Entity {
     }
 
     public void knockback(float f, double d0, double d1) {
+        // Paper start - add knockbacking entity parameter
+        this.doKnockback(f, d0, d1, null);
+    }
+    public void doKnockback(float f, double d0, double d1, Entity knockingBackEntity) {
+        // Paper end - add knockbacking entity parameter
         f = (float) ((double) f * (1.0D - this.getAttributeValue(Attributes.KNOCKBACK_RESISTANCE)));
         if (f > 0.0F) {
             this.hasImpulse = true;
@@ -1634,6 +1639,16 @@ public abstract class LivingEntity extends Entity {
             Vec3 vec3d1 = (new Vec3(d0, 0.0D, d1)).normalize().scale((double) f);
 
             this.setDeltaMovement(vec3d.x / 2.0D - vec3d1.x, this.onGround ? Math.min(0.4D, vec3d.y / 2.0D + (double) f) : vec3d.y, vec3d.z / 2.0D - vec3d1.z);
+
+            // Paper start - call EntityKnockbackByEntityEvent
+            Vec3 currentMot = this.getDeltaMovement();
+            org.bukkit.util.Vector delta = new org.bukkit.util.Vector(currentMot.x - vec3d.x, currentMot.y - vec3d.y, currentMot.z - vec3d.z);
+            // Restore old velocity to be able to access it in the event
+            this.setDeltaMovement(vec3d);
+            if (knockingBackEntity == null || new com.destroystokyo.paper.event.entity.EntityKnockbackByEntityEvent((org.bukkit.entity.LivingEntity) getBukkitEntity(), knockingBackEntity.getBukkitEntity(), f, delta).callEvent()) {
+                this.setDeltaMovement(vec3d.x + delta.getX(), vec3d.y + delta.getY(), vec3d.z + delta.getZ());
+            }
+            // Paper end
         }
     }
 
diff --git a/src/main/java/net/minecraft/world/entity/Mob.java b/src/main/java/net/minecraft/world/entity/Mob.java
index eb35c69bb777ba8d83b2304ff9f862512643e745..f3690ea49cf90c816b8b3554b47d6f2d9dfbe016 100644
--- a/src/main/java/net/minecraft/world/entity/Mob.java
+++ b/src/main/java/net/minecraft/world/entity/Mob.java
@@ -1566,7 +1566,7 @@ public abstract class Mob extends LivingEntity {
 
         if (flag) {
             if (f1 > 0.0F && target instanceof LivingEntity) {
-                ((LivingEntity) target).knockback(f1 * 0.5F, (double) Mth.sin(this.yRot * 0.017453292F), (double) (-Mth.cos(this.yRot * 0.017453292F)));
+                ((LivingEntity) target).doKnockback(f1 * 0.5F, (double) Mth.sin(this.yRot * 0.017453292F), (double) (-Mth.cos(this.yRot * 0.017453292F)), this); // Paper
                 this.setDeltaMovement(this.getDeltaMovement().multiply(0.6D, 1.0D, 0.6D));
             }
 
diff --git a/src/main/java/net/minecraft/world/entity/player/Player.java b/src/main/java/net/minecraft/world/entity/player/Player.java
index b88d8f2c377322290002e84e217f3f2f6e4e71e8..5e6a86b0b8999a5c47d2f9bdd9857ab5f0772033 100644
--- a/src/main/java/net/minecraft/world/entity/player/Player.java
+++ b/src/main/java/net/minecraft/world/entity/player/Player.java
@@ -1174,7 +1174,7 @@ public abstract class Player extends LivingEntity {
                     if (flag5) {
                         if (i > 0) {
                             if (target instanceof LivingEntity) {
-                                ((LivingEntity) target).knockback((float) i * 0.5F, (double) Mth.sin(this.yRot * 0.017453292F), (double) (-Mth.cos(this.yRot * 0.017453292F)));
+                                ((LivingEntity) target).doKnockback((float) i * 0.5F, (double) Mth.sin(this.yRot * 0.017453292F), (double) (-Mth.cos(this.yRot * 0.017453292F)), this); // Paper
                             } else {
                                 target.push((double) (-Mth.sin(this.yRot * 0.017453292F) * (float) i * 0.5F), 0.1D, (double) (Mth.cos(this.yRot * 0.017453292F) * (float) i * 0.5F));
                             }
@@ -1198,7 +1198,7 @@ public abstract class Player extends LivingEntity {
                                 if (entityliving != this && entityliving != target && !this.isAlliedTo(entityliving) && (!(entityliving instanceof ArmorStand) || !((ArmorStand) entityliving).isMarker()) && this.distanceToSqr((Entity) entityliving) < 9.0D) {
                                     // CraftBukkit start - Only apply knockback if the damage hits
                                     if (entityliving.hurt(DamageSource.playerAttack(this).sweep(), f4)) {
-                                    entityliving.knockback(0.4F, (double) Mth.sin(this.yRot * 0.017453292F), (double) (-Mth.cos(this.yRot * 0.017453292F)));
+                                    entityliving.doKnockback(0.4F, (double) Mth.sin(this.yRot * 0.017453292F), (double) (-Mth.cos(this.yRot * 0.017453292F)), this); // Paper
                                     }
                                     // CraftBukkit end
                                 }