Advertisement
Guest User

EntityDamageEvent

a guest
Sep 30th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. package org.bukkit.event.entity;
  2.  
  3. import com.google.common.base.Function;
  4. import com.google.common.base.Functions;
  5. import com.google.common.collect.ImmutableMap;
  6. import java.util.EnumMap;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import org.apache.commons.lang.Validate;
  10. import org.bukkit.entity.Entity;
  11. import org.bukkit.event.Cancellable;
  12. import org.bukkit.event.HandlerList;
  13. import org.bukkit.util.NumberConversions;
  14.  
  15. public class EntityDamageEvent extends EntityEvent
  16. implements Cancellable
  17. {
  18. private static final HandlerList handlers = new HandlerList();
  19. private static final DamageModifier[] MODIFIERS = DamageModifier.values();
  20. private static final Function<? super Double, Double> ZERO = Functions.constant(Double.valueOf(-0.0D));
  21. private final Map<DamageModifier, Double> modifiers;
  22. private final Map<DamageModifier, ? extends Function<? super Double, Double>> modifierFunctions;
  23. private final Map<DamageModifier, Double> originals;
  24. private boolean cancelled;
  25. private final DamageCause cause;
  26.  
  27. @Deprecated
  28. public EntityDamageEvent(Entity damagee, DamageCause cause, int damage)
  29. {
  30. this(damagee, cause, damage);
  31. }
  32.  
  33. @Deprecated
  34. public EntityDamageEvent(Entity damagee, DamageCause cause, double damage) {
  35. this(damagee, cause, new EnumMap(ImmutableMap.of(DamageModifier.BASE, Double.valueOf(damage))), new EnumMap(ImmutableMap.of(DamageModifier.BASE, ZERO)));
  36. }
  37.  
  38. public EntityDamageEvent(Entity damagee, DamageCause cause, Map<DamageModifier, Double> modifiers, Map<DamageModifier, ? extends Function<? super Double, Double>> modifierFunctions) {
  39. super(damagee);
  40. Validate.isTrue(modifiers.containsKey(DamageModifier.BASE), "BASE DamageModifier missing");
  41. Validate.isTrue(!modifiers.containsKey(null), "Cannot have null DamageModifier");
  42. Validate.noNullElements(modifiers.values(), "Cannot have null modifier values");
  43. Validate.isTrue(modifiers.keySet().equals(modifierFunctions.keySet()), "Must have a modifier function for each DamageModifier");
  44. Validate.noNullElements(modifierFunctions.values(), "Cannot have null modifier function");
  45. this.originals = new EnumMap(modifiers);
  46. this.cause = cause;
  47. this.modifiers = modifiers;
  48. this.modifierFunctions = modifierFunctions;
  49. }
  50.  
  51. public boolean isCancelled() {
  52. return this.cancelled;
  53. }
  54.  
  55. public void setCancelled(boolean cancel) {
  56. this.cancelled = cancel;
  57. }
  58.  
  59. public double getOriginalDamage(DamageModifier type)
  60. throws IllegalArgumentException
  61. {
  62. Double damage = (Double)this.originals.get(type);
  63. if (damage != null) {
  64. return damage.doubleValue();
  65. }
  66. if (type == null) {
  67. throw new IllegalArgumentException("Cannot have null DamageModifier");
  68. }
  69. return 0.0D;
  70. }
  71.  
  72. public void setDamage(DamageModifier type, double damage)
  73. throws IllegalArgumentException, UnsupportedOperationException
  74. {
  75. if (!this.modifiers.containsKey(type)) {
  76. throw (type == null ? new IllegalArgumentException("Cannot have null DamageModifier") : new UnsupportedOperationException(type + " is not applicable to " + getEntity()));
  77. }
  78. this.modifiers.put(type, Double.valueOf(damage));
  79. }
  80.  
  81. public double getDamage(DamageModifier type)
  82. throws IllegalArgumentException
  83. {
  84. Validate.notNull(type, "Cannot have null DamageModifier");
  85. Double damage = (Double)this.modifiers.get(type);
  86. return damage == null ? 0.0D : damage.doubleValue();
  87. }
  88.  
  89. public boolean isApplicable(DamageModifier type)
  90. throws IllegalArgumentException
  91. {
  92. Validate.notNull(type, "Cannot have null DamageModifier");
  93. return this.modifiers.containsKey(type);
  94. }
  95.  
  96. public double getDamage()
  97. {
  98. return getDamage(DamageModifier.BASE);
  99. }
  100.  
  101. public final double getFinalDamage()
  102. {
  103. double damage = 0.0D;
  104. for (DamageModifier modifier : MODIFIERS) {
  105. damage += getDamage(modifier);
  106. }
  107. return damage;
  108. }
  109.  
  110. @Deprecated
  111. public int getDamage()
  112. {
  113. return NumberConversions.ceil(getDamage());
  114. }
  115.  
  116. public void setDamage(double damage)
  117. {
  118. double remaining = damage;
  119. double oldRemaining = getDamage(DamageModifier.BASE);
  120. for (DamageModifier modifier : MODIFIERS) {
  121. if (isApplicable(modifier))
  122. {
  123. Function modifierFunction = (Function)this.modifierFunctions.get(modifier);
  124. double newVanilla = ((Double)modifierFunction.apply(Double.valueOf(remaining))).doubleValue();
  125. double oldVanilla = ((Double)modifierFunction.apply(Double.valueOf(oldRemaining))).doubleValue();
  126. double difference = oldVanilla - newVanilla;
  127.  
  128. double old = getDamage(modifier);
  129. if (old > 0.0D)
  130. setDamage(modifier, Math.max(0.0D, old - difference));
  131. else {
  132. setDamage(modifier, Math.min(0.0D, old - difference));
  133. }
  134. remaining += newVanilla;
  135. oldRemaining += oldVanilla;
  136. }
  137. }
  138. setDamage(DamageModifier.BASE, damage);
  139. }
  140.  
  141. @Deprecated
  142. public void setDamage(int damage)
  143. {
  144. setDamage(damage);
  145. }
  146.  
  147. public DamageCause getCause()
  148. {
  149. return this.cause;
  150. }
  151.  
  152. public HandlerList getHandlers()
  153. {
  154. return handlers;
  155. }
  156.  
  157. public static HandlerList getHandlerList() {
  158. return handlers;
  159. }
  160.  
  161. public static enum DamageCause
  162. {
  163. CONTACT,
  164.  
  165. ENTITY_ATTACK,
  166.  
  167. PROJECTILE,
  168.  
  169. SUFFOCATION,
  170.  
  171. FALL,
  172.  
  173. FIRE,
  174.  
  175. FIRE_TICK,
  176.  
  177. MELTING,
  178.  
  179. LAVA,
  180.  
  181. DROWNING,
  182.  
  183. BLOCK_EXPLOSION,
  184.  
  185. ENTITY_EXPLOSION,
  186.  
  187. VOID,
  188.  
  189. LIGHTNING,
  190.  
  191. SUICIDE,
  192.  
  193. STARVATION,
  194.  
  195. POISON,
  196.  
  197. MAGIC,
  198.  
  199. WITHER,
  200.  
  201. FALLING_BLOCK,
  202.  
  203. THORNS,
  204.  
  205. CUSTOM;
  206. }
  207.  
  208. public static enum DamageModifier
  209. {
  210. BASE,
  211.  
  212. HARD_HAT,
  213.  
  214. BLOCKING,
  215.  
  216. ARMOR,
  217.  
  218. RESISTANCE,
  219.  
  220. MAGIC,
  221.  
  222. ABSORPTION;
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement