Entity damage hooks

By: angelsl <angelsl@rpm>
This commit is contained in:
Bukkit/Spigot 2011-01-03 16:11:36 +08:00
parent 05b2a299c0
commit b2817db6b7
6 changed files with 196 additions and 2 deletions

View file

@ -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

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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) {
}
}

View file

@ -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;