mcBleedTimer.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.gmail.nossr50.runnables;
  2. import java.util.HashSet;
  3. import org.bukkit.entity.LivingEntity;
  4. import org.bukkit.entity.Player;
  5. import com.gmail.nossr50.Combat;
  6. import com.gmail.nossr50.Users;
  7. import com.gmail.nossr50.mcMMO;
  8. import com.gmail.nossr50.datatypes.PlayerProfile;
  9. import com.gmail.nossr50.locale.mcLocale;
  10. public class mcBleedTimer implements Runnable {
  11. private final mcMMO plugin;
  12. private static HashSet<LivingEntity> bleedList = new HashSet<LivingEntity>();
  13. private static HashSet<LivingEntity> bleedAddList = new HashSet<LivingEntity>();
  14. private static HashSet<LivingEntity> bleedRemoveList = new HashSet<LivingEntity>();
  15. private static boolean lock = false;
  16. public mcBleedTimer(final mcMMO plugin) {
  17. this.plugin = plugin;
  18. }
  19. @Override
  20. public void run() {
  21. updateBleedList();
  22. // Player bleed simulation
  23. for (Player player : plugin.getServer().getOnlinePlayers()) {
  24. if (player == null) {
  25. continue;
  26. }
  27. PlayerProfile PP = Users.getProfile(player);
  28. if (PP == null) {
  29. continue;
  30. }
  31. if (PP.getBleedTicks() >= 1) {
  32. //Never kill with Bleeding
  33. if (player.getHealth() - 2 < 0) {
  34. if (player.getHealth() - 1 > 0) {
  35. Combat.dealDamage(player, 1);
  36. }
  37. }
  38. else {
  39. Combat.dealDamage(player, 2);
  40. }
  41. PP.decreaseBleedTicks();
  42. if (PP.getBleedTicks() == 0) {
  43. player.sendMessage(mcLocale.getString("Swords.StoppedBleeding"));
  44. }
  45. }
  46. }
  47. // Non-player bleed simulation
  48. bleedSimulate();
  49. }
  50. private void bleedSimulate() {
  51. lock = true;
  52. // Bleed monsters/animals
  53. for (LivingEntity entity : bleedList) {
  54. if ((entity == null || entity.isDead())) {
  55. remove(entity);
  56. continue;
  57. }
  58. else {
  59. Combat.dealDamage(entity, 2);
  60. }
  61. }
  62. // Unlock list now that we are done
  63. lock = false;
  64. }
  65. private void updateBleedList() {
  66. if (lock) {
  67. plugin.getLogger().warning("mcBleedTimer attempted to update the bleedList but the list was locked!");
  68. }
  69. else {
  70. bleedList.removeAll(bleedRemoveList);
  71. bleedRemoveList.clear();
  72. bleedList.addAll(bleedAddList);
  73. bleedAddList.clear();
  74. }
  75. }
  76. /**
  77. * Remove a LivingEntity from the bleedList if it is in it
  78. *
  79. * @param entity LivingEntity to remove
  80. */
  81. public static void remove(LivingEntity entity) {
  82. if (lock) {
  83. if (!bleedRemoveList.contains(entity)) {
  84. bleedRemoveList.add(entity);
  85. }
  86. }
  87. else {
  88. if (bleedList.contains(entity)) {
  89. bleedList.remove(entity);
  90. }
  91. }
  92. }
  93. /**
  94. * Add a LivingEntity to the bleedList if it is not in it.
  95. *
  96. * @param entity LivingEntity to add
  97. */
  98. public static void add(LivingEntity entity) {
  99. if (lock) {
  100. if (!bleedAddList.contains(entity)) {
  101. bleedAddList.add(entity);
  102. }
  103. }
  104. else {
  105. if (!bleedList.contains(entity)){
  106. bleedList.add(entity);
  107. }
  108. }
  109. }
  110. /**
  111. * Check to see if a LivingEntity is in the bleedList
  112. *
  113. * @param entity LivingEntity to check if in the bleedList
  114. * @return true if in the list, false if not
  115. */
  116. public static boolean contains(LivingEntity entity) {
  117. return (bleedList.contains(entity) || bleedAddList.contains(entity));
  118. }
  119. }