From b2817db6b757b3fac1643afe565451677d2deb25 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Mon, 3 Jan 2011 16:11:36 +0800 Subject: [PATCH] Entity damage hooks By: angelsl --- .../src/main/java/org/bukkit/event/Event.java | 4 +- .../entity/EntityDamagedByBlockEvent.java | 67 +++++++++++++++++++ .../entity/EntityDamagedByEntityEvent.java | 67 +++++++++++++++++++ .../org/bukkit/event/entity/EntityEvent.java | 26 +++++++ .../bukkit/event/entity/EntityListener.java | 17 +++++ .../bukkit/plugin/java/JavaPluginLoader.java | 17 +++++ 6 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 paper-api/src/main/java/org/bukkit/event/entity/EntityDamagedByBlockEvent.java create mode 100644 paper-api/src/main/java/org/bukkit/event/entity/EntityDamagedByEntityEvent.java create mode 100644 paper-api/src/main/java/org/bukkit/event/entity/EntityEvent.java create mode 100644 paper-api/src/main/java/org/bukkit/event/entity/EntityListener.java 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 812e007df3..c6be80956e 100644 --- a/paper-api/src/main/java/org/bukkit/event/Event.java +++ b/paper-api/src/main/java/org/bukkit/event/Event.java @@ -318,12 +318,12 @@ public abstract class Event { * Called when a LivingEntity is damaged by the environment (for example, * falling or lava) */ - ENTITY_DAMAGED_ENVIRONMENT (Category.LIVING_ENTITY), + ENTITY_DAMAGEDBY_BLOCK (Category.LIVING_ENTITY), /** * Called when a LivingEntity is damaged by another LivingEntity */ - ENTITY_DAMAGED (Category.LIVING_ENTITY), + ENTITY_DAMAGEDBY_ENTITY (Category.LIVING_ENTITY), /** * Called when a LivingEntity dies diff --git a/paper-api/src/main/java/org/bukkit/event/entity/EntityDamagedByBlockEvent.java b/paper-api/src/main/java/org/bukkit/event/entity/EntityDamagedByBlockEvent.java new file mode 100644 index 0000000000..28362c9292 --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/event/entity/EntityDamagedByBlockEvent.java @@ -0,0 +1,67 @@ +package org.bukkit.event.entity; + +import org.bukkit.Block; +import org.bukkit.Entity; +import org.bukkit.LivingEntity; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; + +/** + * Stores details for damage events where the damager is a block + */ +public class EntityDamagedByBlockEvent extends EntityEvent implements Cancellable { + + private Block damager; + private int damage; + private boolean cancelled; + + public EntityDamagedByBlockEvent(Block damager, LivingEntity damagee, int damage) + { + super(Event.Type.ENTITY_DAMAGEDBY_BLOCK, damagee); + this.damager = damager; + } + + /** + * Gets the cancellation state of this event. A cancelled event will not + * be executed in the server, but will still pass to other plugins + * + * If a damage event is cancelled, the damage will not be deducted from the player's health. + * This will not fire an event. + * + * @return true if this event is cancelled + */ + public boolean isCancelled() { + return cancelled; + } + + /** + * Sets the cancellation state of this event. A cancelled event will not + * be executed in the server, but will still pass to other plugins + * + * If a damage event is cancelled, the damage will not be deducted from the player's health. + * This will not fire an event. + * + * @param cancel true if you wish to cancel this event + */ + public void setCancelled(boolean cancel) { + cancelled = cancel; + } + + /** + * Returns the block that damaged the player. + * @return Block that damaged the player + */ + public Block getDamager() + { + return damager; + } + + /** + * Gets the amount of damage caused by the Block + * @return The amount of damage caused by the Block + */ + public int getDamage() + { + return damage; + } +} diff --git a/paper-api/src/main/java/org/bukkit/event/entity/EntityDamagedByEntityEvent.java b/paper-api/src/main/java/org/bukkit/event/entity/EntityDamagedByEntityEvent.java new file mode 100644 index 0000000000..b3cccd5b79 --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/event/entity/EntityDamagedByEntityEvent.java @@ -0,0 +1,67 @@ +package org.bukkit.event.entity; + +import org.bukkit.Block; +import org.bukkit.Entity; +import org.bukkit.LivingEntity; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; + +/** + * Stores details for damage events where the damager is another entity + */ +public class EntityDamagedByEntityEvent extends EntityEvent implements Cancellable { + + private LivingEntity damager; + private int damage; + private boolean cancelled; + + public EntityDamagedByEntityEvent(LivingEntity damager, LivingEntity damagee, int damage) + { + super(Event.Type.ENTITY_DAMAGEDBY_ENTITY, damagee); + this.damager = damager; + } + + /** + * Gets the cancellation state of this event. A cancelled event will not + * be executed in the server, but will still pass to other plugins + * + * If a damage event is cancelled, the damage will not be deducted from the player's health. + * This will not fire an event. + * + * @return true if this event is cancelled + */ + public boolean isCancelled() { + return cancelled; + } + + /** + * Sets the cancellation state of this event. A cancelled event will not + * be executed in the server, but will still pass to other plugins + * + * If a damage event is cancelled, the damage will not be deducted from the player's health. + * This will not fire an event. + * + * @param cancel true if you wish to cancel this event + */ + public void setCancelled(boolean cancel) { + cancelled = cancel; + } + + /** + * Returns the entity that damaged the player. + * @return LivingEntity that damaged the player + */ + public LivingEntity getDamager() + { + return damager; + } + + /** + * Gets the amount of damage caused by the Block + * @return The amount of damage caused by the Block + */ + public int getDamage() + { + return damage; + } +} diff --git a/paper-api/src/main/java/org/bukkit/event/entity/EntityEvent.java b/paper-api/src/main/java/org/bukkit/event/entity/EntityEvent.java new file mode 100644 index 0000000000..9638d4a3af --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/event/entity/EntityEvent.java @@ -0,0 +1,26 @@ +package org.bukkit.event.entity; + +import org.bukkit.LivingEntity; +import org.bukkit.event.Event; + +/** + * Represents an LivingEntity-related event + */ +public class EntityEvent extends Event { + protected LivingEntity entity; + + public EntityEvent(final Event.Type type, final LivingEntity what) + { + super(type); + entity = what; + } + + /** + * Returns the LivingEntity involved in this event + * @return LivingEntity who is involved in this event + */ + public final LivingEntity getEntity() + { + return entity; + } +} 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 new file mode 100644 index 0000000000..60808f5260 --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/event/entity/EntityListener.java @@ -0,0 +1,17 @@ +package org.bukkit.event.entity; + +import org.bukkit.event.Listener; + +/** + * Handles all events fired in relation to entities + */ +public class EntityListener implements Listener { + public EntityListener() { + } + + public void onEntityDamagedByBlock(EntityDamagedByBlockEvent event) { + } + + public void onEntityDamagedByEntity(EntityDamagedByEntityEvent event) { + } +} 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 436476169c..ba220c3e64 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 @@ -16,6 +16,9 @@ import org.bukkit.event.CustomEventListener; import org.bukkit.event.Event; import org.bukkit.event.Listener; import org.bukkit.event.block.*; +import org.bukkit.event.entity.EntityDamagedByBlockEvent; +import org.bukkit.event.entity.EntityDamagedByEntityEvent; +import org.bukkit.event.entity.EntityListener; import org.bukkit.event.player.*; import org.bukkit.event.server.PluginEvent; import org.bukkit.event.server.ServerListener; @@ -133,6 +136,20 @@ public final class JavaPluginLoader implements PluginLoader { trueListener.onPluginDisabled((PluginEvent)event); break; } + } else if(listener instanceof EntityListener) { + EntityListener trueListener = (EntityListener) listener; + switch(event.getType()) + { + case ENTITY_DAMAGEDBY_BLOCK: + trueListener.onEntityDamagedByBlock((EntityDamagedByBlockEvent)event); + break; + case ENTITY_DAMAGEDBY_ENTITY: + trueListener.onEntityDamagedByEntity((EntityDamagedByEntityEvent)event); + break; + case ENTITY_DEATH: + // TODO: ENTITY_DEATH hook + break; + } } else if (listener instanceof VehicleListener) { VehicleListener trueListener = (VehicleListener)listener;