BleedTimerTask.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package com.gmail.nossr50.runnables.skills;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6. import com.gmail.nossr50.datatypes.interactions.NotificationType;
  7. import com.gmail.nossr50.util.player.NotificationManager;
  8. import org.bukkit.entity.LivingEntity;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.scheduler.BukkitRunnable;
  11. import com.gmail.nossr50.config.AdvancedConfig;
  12. import com.gmail.nossr50.locale.LocaleLoader;
  13. import com.gmail.nossr50.util.skills.CombatUtils;
  14. import com.gmail.nossr50.util.skills.ParticleEffectUtils;
  15. public class BleedTimerTask extends BukkitRunnable {
  16. private final static int MAX_BLEED_TICKS = 10;
  17. private static Map<LivingEntity, Integer> bleedList = new HashMap<LivingEntity, Integer>();
  18. @Override
  19. public void run() {
  20. for (Iterator<Entry<LivingEntity, Integer>> bleedIterator = bleedList.entrySet().iterator(); bleedIterator.hasNext(); ) {
  21. Entry<LivingEntity, Integer> entry = bleedIterator.next();
  22. LivingEntity entity = entry.getKey();
  23. if (entry.getValue() <= 0 || !entity.isValid()) {
  24. bleedIterator.remove();
  25. continue;
  26. }
  27. double damage;
  28. if (entity instanceof Player) {
  29. damage = AdvancedConfig.getInstance().getBleedDamagePlayer();
  30. Player player = (Player) entity;
  31. if (!player.isOnline()) {
  32. continue;
  33. }
  34. // Never kill with Bleeding
  35. if (player.getHealth() - damage > 0) {
  36. CombatUtils.dealDamage(player, damage);
  37. ParticleEffectUtils.playBleedEffect(entity);
  38. }
  39. entry.setValue(entry.getValue() - 1);
  40. if (entry.getValue() <= 0) {
  41. NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Swords.Combat.Bleeding.Stopped");
  42. }
  43. }
  44. else {
  45. damage = AdvancedConfig.getInstance().getBleedDamageMobs();
  46. // Anticipate the entity's death to prevent CME because of our EntityDeathEvent listener
  47. if (entity.getHealth() - damage > 0) {
  48. entry.setValue(entry.getValue() - 1);
  49. }
  50. else {
  51. bleedIterator.remove();
  52. }
  53. CombatUtils.dealDamage(entity, damage);
  54. ParticleEffectUtils.playBleedEffect(entity);
  55. }
  56. }
  57. }
  58. /**
  59. * Instantly Bleed out a LivingEntity
  60. *
  61. * @param entity LivingEntity to bleed out
  62. */
  63. public static void bleedOut(LivingEntity entity) {
  64. if (bleedList.containsKey(entity)) {
  65. CombatUtils.dealDamage(entity, bleedList.get(entity) * 2);
  66. bleedList.remove(entity);
  67. }
  68. }
  69. /**
  70. * Remove a LivingEntity from the bleedList if it is in it
  71. *
  72. * @param entity LivingEntity to remove
  73. */
  74. public static void remove(LivingEntity entity) {
  75. if (bleedList.containsKey(entity)) {
  76. bleedList.remove(entity);
  77. }
  78. }
  79. /**
  80. * Add a LivingEntity to the bleedList if it is not in it.
  81. *
  82. * @param entity LivingEntity to add
  83. * @param ticks Number of bleeding ticks
  84. */
  85. public static void add(LivingEntity entity, int ticks) {
  86. int newTicks = ticks;
  87. if (bleedList.containsKey(entity)) {
  88. newTicks += bleedList.get(entity);
  89. bleedList.put(entity, Math.min(newTicks, MAX_BLEED_TICKS));
  90. }
  91. else {
  92. bleedList.put(entity, Math.min(newTicks, MAX_BLEED_TICKS));
  93. }
  94. }
  95. public static boolean isBleeding(LivingEntity entity) {
  96. return bleedList.containsKey(entity);
  97. }
  98. }