mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-01 16:19:53 +01:00
Use an iterator rather than that stupid lock boolean.
This commit is contained in:
parent
38cd395171
commit
ae1eda915b
@ -1,15 +1,13 @@
|
|||||||
package com.gmail.nossr50.skills.runnables;
|
package com.gmail.nossr50.skills.runnables;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import org.bukkit.entity.LivingEntity;
|
import org.bukkit.entity.LivingEntity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import com.gmail.nossr50.mcMMO;
|
|
||||||
import com.gmail.nossr50.locale.LocaleLoader;
|
import com.gmail.nossr50.locale.LocaleLoader;
|
||||||
import com.gmail.nossr50.skills.utilities.CombatTools;
|
import com.gmail.nossr50.skills.utilities.CombatTools;
|
||||||
import com.gmail.nossr50.util.ParticleEffectUtils;
|
import com.gmail.nossr50.util.ParticleEffectUtils;
|
||||||
@ -17,28 +15,18 @@ import com.gmail.nossr50.util.ParticleEffectUtils;
|
|||||||
public class BleedTimer implements Runnable {
|
public class BleedTimer implements Runnable {
|
||||||
private final static int MAX_BLEED_TICKS = 10;
|
private final static int MAX_BLEED_TICKS = 10;
|
||||||
private static Map<LivingEntity, Integer> bleedList = new HashMap<LivingEntity, Integer>();
|
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>();
|
|
||||||
private static boolean lock = false;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
updateBleedList();
|
for (Iterator<Entry<LivingEntity, Integer>> bleedIterator = bleedList.entrySet().iterator(); bleedIterator.hasNext();) {
|
||||||
bleedSimulate();
|
Entry<LivingEntity, Integer> entry = bleedIterator.next();
|
||||||
}
|
|
||||||
|
|
||||||
private void bleedSimulate() {
|
|
||||||
lock = true;
|
|
||||||
|
|
||||||
for (Entry<LivingEntity, Integer> entry : bleedList.entrySet()) {
|
|
||||||
LivingEntity entity = entry.getKey();
|
LivingEntity entity = entry.getKey();
|
||||||
|
|
||||||
if (entry.getValue() <= 0 || !entity.isValid()) {
|
if (entry.getValue() <= 0 || !entity.isValid()) {
|
||||||
remove(entity);
|
bleedIterator.remove();
|
||||||
break;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Player bleed simulation
|
|
||||||
if (entity instanceof Player) {
|
if (entity instanceof Player) {
|
||||||
Player player = (Player) entity;
|
Player player = (Player) entity;
|
||||||
|
|
||||||
@ -49,7 +37,7 @@ public class BleedTimer implements Runnable {
|
|||||||
// Never kill with Bleeding
|
// Never kill with Bleeding
|
||||||
if (player.getHealth() - 1 > 0) {
|
if (player.getHealth() - 1 > 0) {
|
||||||
CombatTools.dealDamage(player, 1);
|
CombatTools.dealDamage(player, 1);
|
||||||
ParticleEffectUtils.playBleedEffect(player);
|
ParticleEffectUtils.playBleedEffect(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
entry.setValue(entry.getValue() - 1);
|
entry.setValue(entry.getValue() - 1);
|
||||||
@ -58,29 +46,12 @@ public class BleedTimer implements Runnable {
|
|||||||
player.sendMessage(LocaleLoader.getString("Swords.Combat.Bleeding.Stopped"));
|
player.sendMessage(LocaleLoader.getString("Swords.Combat.Bleeding.Stopped"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Bleed monsters/animals
|
|
||||||
else {
|
else {
|
||||||
CombatTools.dealDamage(entity, 2);
|
CombatTools.dealDamage(entity, 2);
|
||||||
entry.setValue(entry.getValue() - 1);
|
|
||||||
ParticleEffectUtils.playBleedEffect(entity);
|
ParticleEffectUtils.playBleedEffect(entity);
|
||||||
|
entry.setValue(entry.getValue() - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unlock list now that we are done
|
|
||||||
lock = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateBleedList() {
|
|
||||||
if (lock) {
|
|
||||||
mcMMO.p.getLogger().warning("mcBleedTimer attempted to update the bleedList but the list was locked!");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
bleedList.keySet().removeAll(bleedRemoveList);
|
|
||||||
bleedRemoveList.clear();
|
|
||||||
|
|
||||||
bleedList.putAll(bleedAddList);
|
|
||||||
bleedAddList.clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -101,17 +72,10 @@ public class BleedTimer implements Runnable {
|
|||||||
* @param entity LivingEntity to remove
|
* @param entity LivingEntity to remove
|
||||||
*/
|
*/
|
||||||
public static void remove(LivingEntity entity) {
|
public static void remove(LivingEntity entity) {
|
||||||
if (lock) {
|
|
||||||
if (!bleedRemoveList.contains(entity)) {
|
|
||||||
bleedRemoveList.add(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (bleedList.containsKey(entity)) {
|
if (bleedList.containsKey(entity)) {
|
||||||
bleedList.remove(entity);
|
bleedList.remove(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a LivingEntity to the bleedList if it is not in it.
|
* Add a LivingEntity to the bleedList if it is not in it.
|
||||||
@ -122,41 +86,12 @@ public class BleedTimer implements Runnable {
|
|||||||
public static void add(LivingEntity entity, int ticks) {
|
public static void add(LivingEntity entity, int ticks) {
|
||||||
int newTicks = ticks;
|
int newTicks = ticks;
|
||||||
|
|
||||||
if (lock) {
|
|
||||||
if (bleedAddList.containsKey(entity)) {
|
|
||||||
newTicks += bleedAddList.get(entity);
|
|
||||||
bleedAddList.put(entity, Math.min(newTicks, MAX_BLEED_TICKS));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
bleedAddList.put(entity, Math.min(newTicks, MAX_BLEED_TICKS));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (bleedList.containsKey(entity)) {
|
if (bleedList.containsKey(entity)) {
|
||||||
newTicks += bleedList.get(entity);
|
newTicks += bleedList.get(entity);
|
||||||
bleedList.put(entity, Math.min(newTicks, MAX_BLEED_TICKS));
|
bleedList.put(entity, Math.min(newTicks, MAX_BLEED_TICKS));
|
||||||
|
|
||||||
// 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?
|
|
||||||
// TODO: Actually implement this suggestion?
|
|
||||||
if (bleedRemoveList.contains(entity)) {
|
|
||||||
bleedRemoveList.remove(entity);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
bleedList.put(entity, Math.min(newTicks, MAX_BLEED_TICKS));
|
bleedList.put(entity, Math.min(newTicks, MAX_BLEED_TICKS));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check to see if a LivingEntity is in the bleedList
|
|
||||||
*
|
|
||||||
* @param entity LivingEntity to check if in the bleedList
|
|
||||||
* @return true if in the list, false if not
|
|
||||||
*/
|
|
||||||
public static boolean contains(LivingEntity entity) {
|
|
||||||
return (bleedList.containsKey(entity) || bleedAddList.containsKey(entity));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user