From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Brokkonaut Date: Mon, 18 Jun 2018 15:40:39 +0200 Subject: [PATCH] Add EntityKnockbackByEntityEvent and EntityPushedByEntityAttackEvent Co-authored-by: aerulion 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 0000000000000000000000000000000000000000..e0ba692c9b107f2b042a9c06549185e1c4777e27 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/event/entity/EntityKnockbackByEntityEvent.java @@ -0,0 +1,48 @@ +package com.destroystokyo.paper.event.entity; + +import io.papermc.paper.event.entity.EntityPushedByEntityAttackEvent; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.util.Vector; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +/** + * 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 EntityPushedByEntityAttackEvent { + + private final float knockbackStrength; + + @ApiStatus.Internal + public EntityKnockbackByEntityEvent(@NotNull LivingEntity entity, @NotNull Entity hitBy, float knockbackStrength, @NotNull Vector acceleration) { + super(entity, hitBy, acceleration); + this.knockbackStrength = knockbackStrength; + } + + /** + * @return the entity which was knocked back + */ + @NotNull + @Override + public LivingEntity getEntity() { + return (LivingEntity) super.getEntity(); + } + + /** + * @return the original knockback strength. + */ + public float getKnockbackStrength() { + return this.knockbackStrength; + } + + /** + * @return the Entity which hit + */ + @NotNull + public Entity getHitBy() { + return super.getPushedBy(); + } + +} diff --git a/src/main/java/io/papermc/paper/event/entity/EntityPushedByEntityAttackEvent.java b/src/main/java/io/papermc/paper/event/entity/EntityPushedByEntityAttackEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..404bec776244fd776566c81f671f1009830c6d6e --- /dev/null +++ b/src/main/java/io/papermc/paper/event/entity/EntityPushedByEntityAttackEvent.java @@ -0,0 +1,82 @@ +package io.papermc.paper.event.entity; + +import org.bukkit.entity.Entity; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.entity.EntityEvent; +import org.bukkit.util.Vector; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +/** + * Fired when an entity is pushed by another entity's attack. The acceleration vector can be + * modified. If this event is cancelled, the entity will not get pushed. + *

+ * Note: Some entities might trigger this multiple times on the same entity + * as multiple acceleration calculations are done. + */ +public class EntityPushedByEntityAttackEvent extends EntityEvent implements Cancellable { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private final @NotNull Entity pushedBy; + private @NotNull Vector acceleration; + private boolean cancelled; + + @ApiStatus.Internal + public EntityPushedByEntityAttackEvent(@NotNull Entity entity, @NotNull Entity pushedBy, @NotNull Vector acceleration) { + super(entity); + this.pushedBy = pushedBy; + this.acceleration = acceleration; + } + + /** + * Gets the entity which pushed the affected entity. + * + * @return the pushing entity + */ + @NotNull + public Entity getPushedBy() { + return this.pushedBy; + } + + /** + * Gets the acceleration that will be applied to the affected entity. + * + * @return the acceleration vector + */ + @NotNull + public Vector getAcceleration() { + return this.acceleration; // TODO Clone in 1.21 to not instantly break what was technically already modifiable + } + + /** + * Sets the relative acceleration that will be applied to the affected entity. + * + * @param acceleration the new acceleration vector + */ + public void setAcceleration(final @NotNull Vector acceleration) { + this.acceleration = acceleration.clone(); + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + @NotNull + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } +} diff --git a/src/main/java/org/bukkit/event/entity/EntityKnockbackByEntityEvent.java b/src/main/java/org/bukkit/event/entity/EntityKnockbackByEntityEvent.java index 3f17290c0863cc1d452bb50c524c18b6ab255d70..bd44bc5ed9e20148f9b2ab3d2049187280f3eb18 100644 --- a/src/main/java/org/bukkit/event/entity/EntityKnockbackByEntityEvent.java +++ b/src/main/java/org/bukkit/event/entity/EntityKnockbackByEntityEvent.java @@ -7,7 +7,10 @@ import org.jetbrains.annotations.NotNull; /** * Called when an entity receives knockback from another entity. + * + * @deprecated use {@link com.destroystokyo.paper.event.entity.EntityKnockbackByEntityEvent} */ +@Deprecated(forRemoval = true) // Paper public class EntityKnockbackByEntityEvent extends EntityKnockbackEvent { private final Entity source; diff --git a/src/main/java/org/bukkit/event/entity/EntityKnockbackEvent.java b/src/main/java/org/bukkit/event/entity/EntityKnockbackEvent.java index 9355efbbd4625e34d6c9d26bcbd02272202dec79..fe3374fbbfef728358e4a15bbf2deb238a1e0bfd 100644 --- a/src/main/java/org/bukkit/event/entity/EntityKnockbackEvent.java +++ b/src/main/java/org/bukkit/event/entity/EntityKnockbackEvent.java @@ -11,7 +11,10 @@ import org.jetbrains.annotations.NotNull; /** * Called when a living entity receives knockback. + * + * @deprecated use {@link com.destroystokyo.paper.event.entity.EntityKnockbackByEntityEvent} or {@link io.papermc.paper.event.entity.EntityPushedByEntityAttackEvent} */ +@Deprecated(forRemoval = true) // Paper public class EntityKnockbackEvent extends EntityEvent implements Cancellable { private static final HandlerList handlers = new HandlerList();