diff --git a/paper-api/src/main/java/org/bukkit/event/Event.java b/paper-api/src/main/java/org/bukkit/event/Event.java index 548ef0f0e7..1fb0966f2d 100644 --- a/paper-api/src/main/java/org/bukkit/event/Event.java +++ b/paper-api/src/main/java/org/bukkit/event/Event.java @@ -453,6 +453,13 @@ public abstract class Event { */ ENTITY_EXPLODE (Category.LIVING_ENTITY), + /** + * Called when an entity targets another entity + * + * @see org.bukkit.event.entity.EntityTargetEvent + */ + ENTITY_TARGET (Category.LIVING_ENTITY), + /** * VEHICLE EVENTS */ diff --git a/paper-api/src/main/java/org/bukkit/event/entity/EntityListener.java b/paper-api/src/main/java/org/bukkit/event/entity/EntityListener.java index 143beb05d1..10ceda1f4f 100644 --- a/paper-api/src/main/java/org/bukkit/event/entity/EntityListener.java +++ b/paper-api/src/main/java/org/bukkit/event/entity/EntityListener.java @@ -26,4 +26,7 @@ public class EntityListener implements Listener { public void onEntityExplode(EntityExplodeEvent event) { } + + public void onEntityTarget(EntityTargetEvent event) { + } } diff --git a/paper-api/src/main/java/org/bukkit/event/entity/EntityTargetEvent.java b/paper-api/src/main/java/org/bukkit/event/entity/EntityTargetEvent.java new file mode 100644 index 0000000000..d291b3373e --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/event/entity/EntityTargetEvent.java @@ -0,0 +1,99 @@ +package org.bukkit.event.entity; + +import org.bukkit.entity.Entity; +import org.bukkit.event.Cancellable; + +/** + * This event is fired when a mob (or any creature) targets another entity + * @author tkelly + */ +public class EntityTargetEvent extends EntityEvent implements Cancellable { + private boolean cancel; + private Entity target; + private TargetReason reason; + + public EntityTargetEvent(Entity entity, Entity target, TargetReason reason) { + super(Type.ENTITY_TARGET, entity); + this.target = target; + this.cancel = false; + this.reason = reason; + } + + public boolean isCancelled() { + return cancel; + } + + /** + * Cancel the change in target. The entity will remain with their original + * target, whether that is nothing or another entity. + * @param cancel + */ + public void setCancelled(boolean cancel) { + this.cancel = cancel; + } + + /** + * Returns the reason for the targeting + */ + public TargetReason getReason() { + return reason; + } + + /** + * Get the entity that this is target. + * This is possible to be null in the case that the event is called when + * the mob forgets its target. + */ + public Entity getTarget() { + return target; + } + + /** + * Set the entity that you want the mob to target instead. + * It is possible to be null, null will cause the entity to be + * target-less. + * + * This is different from cancelling the event. Cancelling the event + * will cause the entity to keep an original target, while setting to be + * null will cause the entity to be reset + * + * @param target The entity to target + */ + public void setTarget(Entity target) { + this.target = target; + } + + /** + * An enum to specify the reason for the targeting + */ + public enum TargetReason + { + /** + * When the entity's target has died, and so it no longer targets it + */ + TARGET_DIED, + /** + * When the entity doesn't have a target, so it attacks the nearest + * player + */ + CLOSEST_PLAYER, + /** + * When the target attacks the entity, so entity targets it + */ + TARGET_ATTACKED_ENTITY, + /** + * When the target attacks a fellow pig zombie, so the whole group + * will target him with this reason. + */ + PIG_ZOMBIE_TARGET, + /** + * When the target is forgotten for whatever reason. + * Currently only occurs in with spiders when there is a high brightness + */ + FORGOT_TARGET, + /** + * For custom calls to the event + */ + CUSTOM + } +} diff --git a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java index 3886027a2f..34e66cf719 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java @@ -23,6 +23,7 @@ import org.bukkit.event.entity.EntityDamageByProjectileEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityListener; +import org.bukkit.event.entity.EntityTargetEvent; import org.bukkit.event.player.*; import org.bukkit.event.server.PluginEvent; import org.bukkit.event.server.ServerListener; @@ -238,6 +239,9 @@ public final class JavaPluginLoader implements PluginLoader { case ENTITY_EXPLODE: trueListener.onEntityExplode((EntityExplodeEvent)event); break; + case ENTITY_TARGET: + trueListener.onEntityTarget((EntityTargetEvent)event); + break; } } else if (listener instanceof VehicleListener) { VehicleListener trueListener = (VehicleListener)listener;