diff options
author | feildmaster <admin@feildmaster.com> | 2013-04-09 22:25:06 -0500 |
---|---|---|
committer | feildmaster <admin@feildmaster.com> | 2013-04-10 21:33:33 -0500 |
commit | 77d1524b3d11f2580cf5792a79cd4c72385fdee9 (patch) | |
tree | 0a8a817a20cde1904bd1894705d28434ad524350 /src/main/java/org | |
parent | 535a85a5b0a25fdbce3a5ae8440d2c60f0fae6ee (diff) | |
download | craftbukkit-77d1524b3d11f2580cf5792a79cd4c72385fdee9.tar craftbukkit-77d1524b3d11f2580cf5792a79cd4c72385fdee9.tar.gz craftbukkit-77d1524b3d11f2580cf5792a79cd4c72385fdee9.tar.lz craftbukkit-77d1524b3d11f2580cf5792a79cd4c72385fdee9.tar.xz craftbukkit-77d1524b3d11f2580cf5792a79cd4c72385fdee9.zip |
Refactor EntityDamageEvents. Adds BUKKIT-1944 & BUKKIT-3684
Diffstat (limited to 'src/main/java/org')
-rw-r--r-- | src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java | 66 | ||||
-rw-r--r-- | src/main/java/org/bukkit/craftbukkit/util/CraftDamageSource.java | 31 |
2 files changed, 85 insertions, 12 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index f65b7f85..07970c00 100644 --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -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 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 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; + } + } 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); } - } else if ("thorns".equals(source.translationIndex)) { - cause = DamageCause.THORNS; + return event; + } + + 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); } - return callEntityDamageEvent(damager, 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; } diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftDamageSource.java b/src/main/java/org/bukkit/craftbukkit/util/CraftDamageSource.java new file mode 100644 index 00000000..3374fe2a --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftDamageSource.java @@ -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); + } +} |