mcMMO/src/main/java/com/gmail/nossr50/skills/runnables/BleedTimer.java

110 lines
3.3 KiB
Java
Raw Normal View History

package com.gmail.nossr50.skills.runnables;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.skills.utilities.CombatTools;
2013-02-20 16:40:47 +01:00
import com.gmail.nossr50.util.ParticleEffectUtils;
2012-04-27 11:47:11 +02:00
public class BleedTimer implements Runnable {
private final static int MAX_BLEED_TICKS = 10;
private static Map<LivingEntity, Integer> bleedList = new HashMap<LivingEntity, Integer>();
@Override
public void run() {
for (Iterator<Entry<LivingEntity, Integer>> bleedIterator = bleedList.entrySet().iterator(); bleedIterator.hasNext();) {
Entry<LivingEntity, Integer> entry = bleedIterator.next();
LivingEntity entity = entry.getKey();
2013-02-26 16:13:40 +01:00
if (entry.getValue() <= 0 || !entity.isValid()) {
bleedIterator.remove();
continue;
2012-03-21 03:51:02 +01:00
}
2013-02-27 13:49:56 +01:00
int damage = 0;
if (entity instanceof Player) {
2013-02-27 13:49:56 +01:00
damage = 1;
Player player = (Player) entity;
if (!player.isOnline()) {
continue;
}
// Never kill with Bleeding
2013-02-27 13:49:56 +01:00
if (player.getHealth() - damage > 0) {
CombatTools.dealDamage(player, damage);
ParticleEffectUtils.playBleedEffect(entity);
}
2013-02-27 13:49:56 +01:00
entry.setValue(entry.getValue() - damage);
2012-04-01 06:11:57 +02:00
if (entry.getValue() <= 0) {
2012-04-27 11:47:11 +02:00
player.sendMessage(LocaleLoader.getString("Swords.Combat.Bleeding.Stopped"));
}
}
2012-03-21 03:51:02 +01:00
else {
2013-02-27 13:49:56 +01:00
damage = 2;
// 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();
}
CombatTools.dealDamage(entity, damage);
2013-02-26 16:13:40 +01:00
ParticleEffectUtils.playBleedEffect(entity);
}
}
}
/**
* Instantly Bleed out a LivingEntity
2012-06-05 15:57:10 +02:00
*
* @param entity LivingEntity to bleed out
*/
public static void bleedOut(LivingEntity entity) {
if (bleedList.containsKey(entity)) {
CombatTools.dealDamage(entity, bleedList.get(entity) * 2);
bleedList.remove(entity);
2012-12-24 22:56:25 +01:00
}
}
/**
* Remove a LivingEntity from the bleedList if it is in it
2012-06-05 15:57:10 +02:00
*
* @param entity LivingEntity to remove
*/
public static void remove(LivingEntity entity) {
if (bleedList.containsKey(entity)) {
bleedList.remove(entity);
}
}
/**
2012-03-21 03:51:02 +01:00
* Add a LivingEntity to the bleedList if it is not in it.
*
* @param entity LivingEntity to add
2012-05-01 01:32:50 +02:00
* @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));
}
2012-03-21 03:51:02 +01:00
else {
bleedList.put(entity, Math.min(newTicks, MAX_BLEED_TICKS));
}
}
}