BleedTimer.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package com.gmail.nossr50.skills.runnables;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import org.bukkit.Effect;
  8. import org.bukkit.Material;
  9. import org.bukkit.entity.LivingEntity;
  10. import org.bukkit.entity.Player;
  11. import com.gmail.nossr50.mcMMO;
  12. import com.gmail.nossr50.locale.LocaleLoader;
  13. import com.gmail.nossr50.skills.utilities.CombatTools;
  14. import com.gmail.nossr50.util.ParticleEffectUtils;
  15. public class BleedTimer implements Runnable {
  16. private final static int MAX_BLEED_TICKS = 10;
  17. private static Map<LivingEntity, Integer> bleedList = new HashMap<LivingEntity, Integer>();
  18. private static Map<LivingEntity, Integer> bleedAddList = new HashMap<LivingEntity, Integer>();
  19. private static List<LivingEntity> bleedRemoveList = new ArrayList<LivingEntity>();
  20. private static boolean lock = false;
  21. @Override
  22. public void run() {
  23. updateBleedList();
  24. bleedSimulate();
  25. }
  26. private void bleedSimulate() {
  27. lock = true;
  28. for (Entry<LivingEntity, Integer> entry : bleedList.entrySet()) {
  29. LivingEntity entity = entry.getKey();
  30. if (entry.getValue() <= 0 || entity.isDead()) {
  31. remove(entity);
  32. break;
  33. }
  34. // Player bleed simulation
  35. if (entity instanceof Player) {
  36. Player player = (Player) entity;
  37. if (!player.isOnline()) {
  38. continue;
  39. }
  40. //Never kill with Bleeding
  41. if (player.getHealth() - 1 > 0) {
  42. CombatTools.dealDamage(player, 1);
  43. ParticleEffectUtils.playBleedEffect(player);
  44. }
  45. entry.setValue(entry.getValue() - 1);
  46. if (entry.getValue() <= 0) {
  47. player.sendMessage(LocaleLoader.getString("Swords.Combat.Bleeding.Stopped"));
  48. }
  49. }
  50. // Bleed monsters/animals
  51. else {
  52. CombatTools.dealDamage(entity, 2);
  53. entry.setValue(entry.getValue() - 1);
  54. entity.getWorld().playEffect(entity.getEyeLocation(), Effect.STEP_SOUND, Material.REDSTONE_WIRE);
  55. }
  56. }
  57. // Unlock list now that we are done
  58. lock = false;
  59. }
  60. private void updateBleedList() {
  61. if (lock) {
  62. mcMMO.p.getLogger().warning("mcBleedTimer attempted to update the bleedList but the list was locked!");
  63. }
  64. else {
  65. bleedList.keySet().removeAll(bleedRemoveList);
  66. bleedRemoveList.clear();
  67. bleedList.putAll(bleedAddList);
  68. bleedAddList.clear();
  69. }
  70. }
  71. /**
  72. * Instantly Bleed out a LivingEntity
  73. *
  74. * @param entity LivingEntity to bleed out
  75. */
  76. public static void bleedOut(LivingEntity entity) {
  77. if (bleedList.containsKey(entity)) {
  78. CombatTools.dealDamage(entity, bleedList.get(entity) * 2);
  79. bleedList.remove(entity);
  80. }
  81. }
  82. /**
  83. * Remove a LivingEntity from the bleedList if it is in it
  84. *
  85. * @param entity LivingEntity to remove
  86. */
  87. public static void remove(LivingEntity entity) {
  88. if (lock) {
  89. if (!bleedRemoveList.contains(entity)) {
  90. bleedRemoveList.add(entity);
  91. }
  92. }
  93. else {
  94. if (bleedList.containsKey(entity)) {
  95. bleedList.remove(entity);
  96. }
  97. }
  98. }
  99. /**
  100. * Add a LivingEntity to the bleedList if it is not in it.
  101. *
  102. * @param entity LivingEntity to add
  103. * @param ticks Number of bleeding ticks
  104. */
  105. public static void add(LivingEntity entity, int ticks) {
  106. int newTicks = ticks;
  107. if (lock) {
  108. if (bleedAddList.containsKey(entity)) {
  109. newTicks += bleedAddList.get(entity);
  110. if (newTicks > MAX_BLEED_TICKS) {
  111. newTicks = MAX_BLEED_TICKS;
  112. }
  113. bleedAddList.put(entity, newTicks);
  114. }
  115. else {
  116. if (newTicks > MAX_BLEED_TICKS) {
  117. newTicks = MAX_BLEED_TICKS;
  118. }
  119. bleedAddList.put(entity, newTicks);
  120. }
  121. }
  122. else {
  123. if (bleedList.containsKey(entity)) {
  124. newTicks += bleedList.get(entity);
  125. if (newTicks > MAX_BLEED_TICKS) {
  126. newTicks = MAX_BLEED_TICKS;
  127. }
  128. bleedList.put(entity, newTicks);
  129. // Need to find a better way to ensure that the entity stays in bleedList
  130. // when some ticks are added but already marked for removal.
  131. // Suggestion: Why not use Iterator.remove() and drop the lock boolean?
  132. if (bleedRemoveList.contains(entity)) {
  133. bleedRemoveList.remove(entity);
  134. }
  135. }
  136. else {
  137. if (newTicks > MAX_BLEED_TICKS) {
  138. newTicks = MAX_BLEED_TICKS;
  139. }
  140. bleedList.put(entity, newTicks);
  141. }
  142. }
  143. }
  144. /**
  145. * Check to see if a LivingEntity is in the bleedList
  146. *
  147. * @param entity LivingEntity to check if in the bleedList
  148. * @return true if in the list, false if not
  149. */
  150. public static boolean contains(LivingEntity entity) {
  151. return (bleedList.containsKey(entity) || bleedAddList.containsKey(entity));
  152. }
  153. }