package com.gmail.nossr50.runnables.skills; import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.datatypes.interactions.NotificationType; import com.gmail.nossr50.util.player.NotificationManager; import com.gmail.nossr50.util.skills.CombatUtils; import com.gmail.nossr50.util.skills.ParticleEffectUtils; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; public class BleedTimerTask extends BukkitRunnable { private final static int MAX_BLEED_TICKS = 10; private static Map bleedList = new HashMap(); @Override public void run() { for (Iterator> bleedIterator = bleedList.entrySet().iterator(); bleedIterator.hasNext(); ) { Entry entry = bleedIterator.next(); LivingEntity entity = entry.getKey(); if (entry.getValue() <= 0 || !entity.isValid()) { bleedIterator.remove(); continue; } double damage; if (entity instanceof Player) { damage = AdvancedConfig.getInstance().getBleedDamagePlayer(); Player player = (Player) entity; if (!player.isOnline()) { continue; } // Never kill with Bleeding if (player.getHealth() - damage > 0) { CombatUtils.dealDamage(player, damage); ParticleEffectUtils.playBleedEffect(entity); } entry.setValue(entry.getValue() - 1); if (entry.getValue() <= 0) { NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Swords.Combat.Bleeding.Stopped"); } } else { damage = AdvancedConfig.getInstance().getBleedDamageMobs(); // Anticipate the entity's death to prevent CME because of our EntityDeathEvent listener if (entity.getHealth() - damage > 0) { entry.setValue(entry.getValue() - 1); } else { bleedIterator.remove(); } CombatUtils.dealDamage(entity, damage); ParticleEffectUtils.playBleedEffect(entity); } } } /** * Instantly Bleed out a LivingEntity * * @param entity LivingEntity to bleed out */ public static void bleedOut(LivingEntity entity) { if (bleedList.containsKey(entity)) { CombatUtils.dealDamage(entity, bleedList.get(entity) * 2); bleedList.remove(entity); } } /** * Remove a LivingEntity from the bleedList if it is in it * * @param entity LivingEntity to remove */ public static void remove(LivingEntity entity) { if (bleedList.containsKey(entity)) { bleedList.remove(entity); } } /** * Add a LivingEntity to the bleedList if it is not in it. * * @param entity LivingEntity to add * @param ticks Number of bleeding ticks */ public static void add(LivingEntity entity, int ticks) { int newTicks = ticks; if (bleedList.containsKey(entity)) { newTicks += bleedList.get(entity); bleedList.put(entity, Math.min(newTicks, MAX_BLEED_TICKS)); } else { bleedList.put(entity, Math.min(newTicks, MAX_BLEED_TICKS)); } } public static boolean isBleeding(LivingEntity entity) { return bleedList.containsKey(entity); } }