2013-01-30 17:53:51 +01:00
|
|
|
package com.gmail.nossr50.skills.runnables;
|
2012-03-18 21:59:35 +01:00
|
|
|
|
2012-04-28 06:14:19 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2012-03-18 21:59:35 +01:00
|
|
|
|
2013-02-04 17:40:09 +01:00
|
|
|
import org.bukkit.Effect;
|
|
|
|
import org.bukkit.Material;
|
2012-03-18 21:59:35 +01:00
|
|
|
import org.bukkit.entity.LivingEntity;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2012-06-07 00:02:22 +02:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
2012-04-27 11:47:11 +02:00
|
|
|
import com.gmail.nossr50.locale.LocaleLoader;
|
2013-01-30 17:53:51 +01:00
|
|
|
import com.gmail.nossr50.skills.utilities.CombatTools;
|
2012-03-18 21:59:35 +01:00
|
|
|
|
2012-04-27 11:47:11 +02:00
|
|
|
public class BleedTimer implements Runnable {
|
2012-04-28 06:14:19 +02:00
|
|
|
private final static int MAX_BLEED_TICKS = 10;
|
|
|
|
private static Map<LivingEntity, Integer> bleedList = new HashMap<LivingEntity, Integer>();
|
|
|
|
private static Map<LivingEntity, Integer> bleedAddList = new HashMap<LivingEntity, Integer>();
|
|
|
|
private static List<LivingEntity> bleedRemoveList = new ArrayList<LivingEntity>();
|
2012-03-18 21:59:35 +01:00
|
|
|
private static boolean lock = false;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
updateBleedList();
|
2012-04-28 06:14:19 +02:00
|
|
|
bleedSimulate();
|
|
|
|
}
|
2012-03-18 21:59:35 +01:00
|
|
|
|
2012-04-28 06:14:19 +02:00
|
|
|
private void bleedSimulate() {
|
|
|
|
lock = true;
|
|
|
|
|
|
|
|
for (Entry<LivingEntity, Integer> entry : bleedList.entrySet()) {
|
|
|
|
LivingEntity entity = entry.getKey();
|
2012-03-18 21:59:35 +01:00
|
|
|
|
2012-07-03 03:52:12 +02:00
|
|
|
if (entry.getValue() <= 0 || entity.isDead()) {
|
2012-04-28 06:14:19 +02:00
|
|
|
remove(entity);
|
|
|
|
break;
|
2012-03-21 03:51:02 +01:00
|
|
|
}
|
|
|
|
|
2012-04-28 06:14:19 +02:00
|
|
|
// Player bleed simulation
|
|
|
|
if (entity instanceof Player) {
|
|
|
|
Player player = (Player) entity;
|
|
|
|
|
|
|
|
if (!player.isOnline()) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-18 21:59:35 +01:00
|
|
|
|
|
|
|
//Never kill with Bleeding
|
2012-05-01 23:49:11 +02:00
|
|
|
if (player.getHealth() - 1 > 0) {
|
2013-01-30 17:53:51 +01:00
|
|
|
CombatTools.dealDamage(player, 1);
|
2013-02-04 17:40:09 +01:00
|
|
|
player.getWorld().playEffect(player.getEyeLocation(), Effect.STEP_SOUND, Material.REDSTONE_WIRE);
|
2012-03-18 21:59:35 +01:00
|
|
|
}
|
|
|
|
|
2012-04-28 06:14:19 +02:00
|
|
|
entry.setValue(entry.getValue() - 1);
|
2012-04-01 06:11:57 +02:00
|
|
|
|
2012-04-28 06:14:19 +02:00
|
|
|
if (entry.getValue() <= 0) {
|
2012-04-27 11:47:11 +02:00
|
|
|
player.sendMessage(LocaleLoader.getString("Swords.Combat.Bleeding.Stopped"));
|
2012-03-18 21:59:35 +01:00
|
|
|
}
|
|
|
|
}
|
2012-04-28 06:14:19 +02:00
|
|
|
// Bleed monsters/animals
|
2012-03-21 03:51:02 +01:00
|
|
|
else {
|
2013-01-30 17:53:51 +01:00
|
|
|
CombatTools.dealDamage(entity, 2);
|
2012-04-28 06:14:19 +02:00
|
|
|
entry.setValue(entry.getValue() - 1);
|
2013-02-04 17:40:09 +01:00
|
|
|
entity.getWorld().playEffect(entity.getEyeLocation(), Effect.STEP_SOUND, Material.REDSTONE_WIRE);
|
2012-03-18 21:59:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock list now that we are done
|
|
|
|
lock = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateBleedList() {
|
2012-03-21 03:51:02 +01:00
|
|
|
if (lock) {
|
2012-06-07 00:02:22 +02:00
|
|
|
mcMMO.p.getLogger().warning("mcBleedTimer attempted to update the bleedList but the list was locked!");
|
2012-03-18 21:59:35 +01:00
|
|
|
}
|
2012-03-21 03:51:02 +01:00
|
|
|
else {
|
2012-04-28 06:14:19 +02:00
|
|
|
bleedList.keySet().removeAll(bleedRemoveList);
|
2012-03-18 21:59:35 +01:00
|
|
|
bleedRemoveList.clear();
|
2012-03-21 03:51:02 +01:00
|
|
|
|
2012-04-28 06:14:19 +02:00
|
|
|
bleedList.putAll(bleedAddList);
|
2012-03-18 21:59:35 +01:00
|
|
|
bleedAddList.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-28 06:14:19 +02:00
|
|
|
/**
|
|
|
|
* Instantly Bleed out a LivingEntity
|
2012-06-05 15:57:10 +02:00
|
|
|
*
|
2012-04-28 06:14:19 +02:00
|
|
|
* @param entity LivingEntity to bleed out
|
|
|
|
*/
|
|
|
|
public static void bleedOut(LivingEntity entity) {
|
|
|
|
if (bleedList.containsKey(entity)) {
|
2013-01-30 17:53:51 +01:00
|
|
|
CombatTools.dealDamage(entity, bleedList.get(entity) * 2);
|
2012-04-28 06:14:19 +02:00
|
|
|
bleedList.remove(entity);
|
2012-12-24 22:56:25 +01:00
|
|
|
}
|
2012-04-28 06:14:19 +02:00
|
|
|
}
|
|
|
|
|
2012-03-18 21:59:35 +01:00
|
|
|
/**
|
|
|
|
* Remove a LivingEntity from the bleedList if it is in it
|
2012-06-05 15:57:10 +02:00
|
|
|
*
|
2012-03-18 21:59:35 +01:00
|
|
|
* @param entity LivingEntity to remove
|
|
|
|
*/
|
|
|
|
public static void remove(LivingEntity entity) {
|
2012-03-21 03:51:02 +01:00
|
|
|
if (lock) {
|
|
|
|
if (!bleedRemoveList.contains(entity)) {
|
2012-03-18 21:59:35 +01:00
|
|
|
bleedRemoveList.add(entity);
|
|
|
|
}
|
|
|
|
}
|
2012-03-21 03:51:02 +01:00
|
|
|
else {
|
2012-04-28 06:14:19 +02:00
|
|
|
if (bleedList.containsKey(entity)) {
|
2012-03-18 21:59:35 +01:00
|
|
|
bleedList.remove(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-21 03:51:02 +01:00
|
|
|
* Add a LivingEntity to the bleedList if it is not in it.
|
|
|
|
*
|
2012-03-18 21:59:35 +01:00
|
|
|
* @param entity LivingEntity to add
|
2012-05-01 01:32:50 +02:00
|
|
|
* @param ticks Number of bleeding ticks
|
2012-03-18 21:59:35 +01:00
|
|
|
*/
|
2012-04-28 06:14:19 +02:00
|
|
|
public static void add(LivingEntity entity, int ticks) {
|
|
|
|
int newTicks = ticks;
|
|
|
|
|
2012-03-21 03:51:02 +01:00
|
|
|
if (lock) {
|
2012-04-28 06:14:19 +02:00
|
|
|
if (bleedAddList.containsKey(entity)) {
|
|
|
|
newTicks += bleedAddList.get(entity);
|
2012-05-01 01:32:50 +02:00
|
|
|
|
2012-04-28 06:14:19 +02:00
|
|
|
if (newTicks > MAX_BLEED_TICKS) {
|
|
|
|
newTicks = MAX_BLEED_TICKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bleedAddList.put(entity, newTicks);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (newTicks > MAX_BLEED_TICKS) {
|
|
|
|
newTicks = MAX_BLEED_TICKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bleedAddList.put(entity, newTicks);
|
2012-03-18 21:59:35 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-21 03:51:02 +01:00
|
|
|
else {
|
2012-04-28 06:14:19 +02:00
|
|
|
if (bleedList.containsKey(entity)) {
|
|
|
|
newTicks += bleedList.get(entity);
|
2012-05-01 01:32:50 +02:00
|
|
|
|
2012-04-28 06:14:19 +02:00
|
|
|
if (newTicks > MAX_BLEED_TICKS) {
|
|
|
|
newTicks = MAX_BLEED_TICKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bleedList.put(entity, newTicks);
|
|
|
|
|
|
|
|
// Need to find a better way to ensure that the entity stays in bleedList
|
|
|
|
// when some ticks are added but already marked for removal.
|
|
|
|
// Suggestion: Why not use Iterator.remove() and drop the lock boolean?
|
|
|
|
if (bleedRemoveList.contains(entity)) {
|
|
|
|
bleedRemoveList.remove(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (newTicks > MAX_BLEED_TICKS) {
|
|
|
|
newTicks = MAX_BLEED_TICKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bleedList.put(entity, newTicks);
|
2012-03-18 21:59:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check to see if a LivingEntity is in the bleedList
|
2012-06-05 15:57:10 +02:00
|
|
|
*
|
2012-03-18 21:59:35 +01:00
|
|
|
* @param entity LivingEntity to check if in the bleedList
|
|
|
|
* @return true if in the list, false if not
|
|
|
|
*/
|
|
|
|
public static boolean contains(LivingEntity entity) {
|
2012-04-28 06:14:19 +02:00
|
|
|
return (bleedList.containsKey(entity) || bleedAddList.containsKey(entity));
|
2012-03-18 21:59:35 +01:00
|
|
|
}
|
|
|
|
}
|