From fbe69b61a9f5acab3e6e09084448d5ecff201e1f Mon Sep 17 00:00:00 2001 From: Brokkonaut Date: Thu, 21 Jun 2018 03:59:11 +0200 Subject: [PATCH] Add EntityKnockbackByEntityEvent (#1162) This event is called when an entity receives knockback by another entity. The knockback can be modified in the event. If the event is cancelled the entity is not knocked back. --- .../Add-EntityKnockbackByEntityEvent.patch | 80 +++++++++++++++++++ ...plement-EntityKnockbackByEntityEvent.patch | 44 ++++++++++ 2 files changed, 124 insertions(+) create mode 100644 Spigot-API-Patches/Add-EntityKnockbackByEntityEvent.patch create mode 100644 Spigot-Server-Patches/Implement-EntityKnockbackByEntityEvent.patch diff --git a/Spigot-API-Patches/Add-EntityKnockbackByEntityEvent.patch b/Spigot-API-Patches/Add-EntityKnockbackByEntityEvent.patch new file mode 100644 index 0000000000..3a178049e2 --- /dev/null +++ b/Spigot-API-Patches/Add-EntityKnockbackByEntityEvent.patch @@ -0,0 +1,80 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Brokkonaut +Date: Mon, 18 Jun 2018 15:40:39 +0200 +Subject: [PATCH] Add EntityKnockbackByEntityEvent + + +diff --git a/src/main/java/com/destroystokyo/paper/event/entity/EntityKnockbackByEntityEvent.java b/src/main/java/com/destroystokyo/paper/event/entity/EntityKnockbackByEntityEvent.java +new file mode 100644 +index 00000000..99f7ef70 +--- /dev/null ++++ b/src/main/java/com/destroystokyo/paper/event/entity/EntityKnockbackByEntityEvent.java +@@ -0,0 +0,0 @@ ++package com.destroystokyo.paper.event.entity; ++ ++import org.bukkit.entity.Entity; ++import org.bukkit.entity.LivingEntity; ++import org.bukkit.event.Cancellable; ++import org.bukkit.event.HandlerList; ++import org.bukkit.event.entity.EntityEvent; ++import org.bukkit.util.Vector; ++ ++/** ++ * Fired when an Entity is knocked back by the hit of another Entity. The acceleration ++ * vector can be modified. If this event is cancelled, the entity is not knocked back. ++ * ++ */ ++public class EntityKnockbackByEntityEvent extends EntityEvent implements Cancellable { ++ private static final HandlerList handlers = new HandlerList(); ++ ++ private final Entity hitBy; ++ private final float knockbackStrength; ++ private final Vector acceleration; ++ private boolean cancelled = false; ++ ++ public EntityKnockbackByEntityEvent(LivingEntity entity, Entity hitBy, float knockbackStrength, Vector acceleration) { ++ super(entity); ++ this.hitBy = hitBy; ++ this.knockbackStrength = knockbackStrength; ++ this.acceleration = acceleration; ++ } ++ ++ public HandlerList getHandlers() { ++ return handlers; ++ } ++ ++ public static HandlerList getHandlerList() { ++ return handlers; ++ } ++ ++ @Override ++ public boolean isCancelled() { ++ return cancelled; ++ } ++ ++ @Override ++ public void setCancelled(boolean cancel) { ++ cancelled = cancel; ++ } ++ ++ @Override ++ public LivingEntity getEntity() { ++ return (LivingEntity) super.getEntity(); ++ } ++ ++ /** ++ * Returns the original knockback strength. ++ */ ++ public float getKnockbackStrength() { ++ return knockbackStrength; ++ } ++ ++ public Entity getHitBy() { ++ return hitBy; ++ } ++ ++ public Vector getAcceleration() { ++ return acceleration; ++ } ++} +-- \ No newline at end of file diff --git a/Spigot-Server-Patches/Implement-EntityKnockbackByEntityEvent.patch b/Spigot-Server-Patches/Implement-EntityKnockbackByEntityEvent.patch new file mode 100644 index 0000000000..8656aab515 --- /dev/null +++ b/Spigot-Server-Patches/Implement-EntityKnockbackByEntityEvent.patch @@ -0,0 +1,44 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Brokkonaut +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/server/EntityLiving.java b/src/main/java/net/minecraft/server/EntityLiving.java +index 65bc19b17..156bf8ee0 100644 +--- a/src/main/java/net/minecraft/server/EntityLiving.java ++++ b/src/main/java/net/minecraft/server/EntityLiving.java +@@ -0,0 +0,0 @@ public abstract class EntityLiving extends Entity { + this.impulse = true; + float f1 = MathHelper.sqrt(d0 * d0 + d1 * d1); + ++ // Paper start - preserve old velocity ++ double oldMotX = this.motX; ++ double oldMotY = this.motY; ++ double oldMotZ = this.motZ; ++ // Paper end ++ + this.motX /= 2.0D; + this.motZ /= 2.0D; + this.motX -= d0 / (double) f1 * (double) f; +@@ -0,0 +0,0 @@ public abstract class EntityLiving extends Entity { + } + } + ++ // Paper start - call EntityKnockbackByEntityEvent ++ org.bukkit.util.Vector delta = new org.bukkit.util.Vector(this.motX - oldMotX, this.motY - oldMotY, this.motZ - oldMotZ); ++ // Restore old velocity to be able to access it in the event ++ this.motX = oldMotX; ++ this.motY = oldMotY; ++ this.motZ = oldMotZ; ++ if (entity == null || new com.destroystokyo.paper.event.entity.EntityKnockbackByEntityEvent((LivingEntity) getBukkitEntity(), entity.getBukkitEntity(), f, delta).callEvent()) { ++ this.motX += delta.getX(); ++ this.motY += delta.getY(); ++ this.motZ += delta.getZ(); ++ } ++ // Paper end + } + } + +-- \ No newline at end of file