BleedTimer.java 5.2 KB

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