Refactor EntityDamageEvents. Adds BUKKIT-1944 & BUKKIT-3684

By: feildmaster <admin@feildmaster.com>
This commit is contained in:
CraftBukkit/Spigot 2013-04-09 22:25:06 -05:00
parent 76976e4797
commit 868456b95f
2 changed files with 84 additions and 11 deletions

View file

@ -39,6 +39,7 @@ import org.bukkit.craftbukkit.entity.CraftLivingEntity;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.inventory.CraftInventoryCrafting;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.util.CraftDamageSource;
import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Creeper;
@ -64,6 +65,9 @@ import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.inventory.InventoryView;
public class CraftEventFactory {
public static final DamageSource MELTING = CraftDamageSource.copyOf(DamageSource.BURN);
public static final DamageSource POISON = CraftDamageSource.copyOf(DamageSource.MAGIC);
// helper methods
private static boolean canBuild(CraftWorld world, Player player, int x, int z) {
WorldServer worldServer = world.getHandle();
@ -379,21 +383,58 @@ public class CraftEventFactory {
}
public static EntityDamageEvent handleEntityDamageEvent(Entity entity, DamageSource source, int damage) {
Entity damager = source.getEntity();
DamageCause cause = DamageCause.ENTITY_ATTACK;
if (source instanceof EntityDamageSource) {
Entity damager = source.getEntity();
DamageCause cause = DamageCause.ENTITY_ATTACK;
if (source instanceof EntityDamageSourceIndirect) {
damager = ((EntityDamageSourceIndirect) source).getProximateDamageSource();
if (damager.getBukkitEntity() instanceof ThrownPotion) {
cause = DamageCause.MAGIC;
} else if (damager.getBukkitEntity() instanceof Projectile) {
cause = DamageCause.PROJECTILE;
if (source instanceof EntityDamageSourceIndirect) {
damager = ((EntityDamageSourceIndirect) source).getProximateDamageSource();
if (damager.getBukkitEntity() instanceof ThrownPotion) {
cause = DamageCause.MAGIC;
} else if (damager.getBukkitEntity() instanceof Projectile) {
cause = DamageCause.PROJECTILE;
}
} else if ("thorns".equals(source.translationIndex)) {
cause = DamageCause.THORNS;
}
} else if ("thorns".equals(source.translationIndex)) {
cause = DamageCause.THORNS;
return callEntityDamageEvent(damager, entity, cause, damage);
} else if (source == DamageSource.OUT_OF_WORLD) {
EntityDamageEvent event = callEvent(new EntityDamageByBlockEvent(null, entity.getBukkitEntity(), DamageCause.VOID, damage));
if (!event.isCancelled()) {
event.getEntity().setLastDamageCause(event);
}
return event;
}
return callEntityDamageEvent(damager, entity, cause, damage);
DamageCause cause = null;
if (source == DamageSource.FIRE) {
cause = DamageCause.FIRE;
} else if (source == DamageSource.STARVE) {
cause = DamageCause.STARVATION;
} else if (source == DamageSource.WITHER) {
cause = DamageCause.WITHER;
} else if (source == DamageSource.STUCK) {
cause = DamageCause.SUFFOCATION;
} else if (source == DamageSource.DROWN) {
cause = DamageCause.DROWNING;
} else if (source == DamageSource.BURN) {
cause = DamageCause.FIRE_TICK;
} else if (source == MELTING) {
cause = DamageCause.MELTING;
} else if (source == POISON) {
cause = DamageCause.POISON;
} else if (source == DamageSource.MAGIC) {
cause = DamageCause.MAGIC;
}
if (cause != null) {
return callEntityDamageEvent(null, entity, cause, damage);
}
// If an event was called earlier, we return null.
// EG: Cactus, Lava, EntityEnderPearl "fall", FallingSand
return null;
}
// Non-Living Entities such as EntityEnderCrystal need to call this
@ -401,6 +442,7 @@ public class CraftEventFactory {
if (!(source instanceof EntityDamageSource)) {
return false;
}
// We don't need to check for null, since EntityDamageSource will always return an event
EntityDamageEvent event = handleEntityDamageEvent(entity, source, damage);
return event.isCancelled() || event.getDamage() == 0;
}

View file

@ -0,0 +1,31 @@
package org.bukkit.craftbukkit.util;
import net.minecraft.server.DamageSource;
// Util class to create custom DamageSources.
public final class CraftDamageSource extends DamageSource {
public static DamageSource copyOf(final DamageSource original) {
CraftDamageSource newSource = new CraftDamageSource(original.translationIndex);
// Check ignoresArmor
if (original.ignoresArmor()) {
newSource.j();
}
// Check magic
if (original.q()) {
newSource.r();
}
// Check fire
if (original.c()) {
newSource.l();
}
return newSource;
}
private CraftDamageSource(String identifier) {
super(identifier);
}
}