Move bleeding to mcBleedTimer

Put all the logic handling adding/removing/contains there and encapsulate our List
Additionally, should prevent a ConcurrentModificationException by locking, but I'm not 100% on the contiains not throing such an exception.
This commit is contained in:
NuclearW 2012-03-18 16:59:35 -04:00
parent b10f599a87
commit 8f2c424657
9 changed files with 182 additions and 84 deletions

View File

@ -23,6 +23,7 @@ import com.gmail.nossr50.events.FakeEntityDamageEvent;
import com.gmail.nossr50.locale.mcLocale;
import com.gmail.nossr50.party.Party;
import com.gmail.nossr50.runnables.GainXp;
import com.gmail.nossr50.runnables.mcBleedTimer;
import com.gmail.nossr50.skills.Acrobatics;
import com.gmail.nossr50.skills.Archery;
import com.gmail.nossr50.skills.Axes;
@ -59,7 +60,7 @@ public class Combat {
combatAbilityChecks(attacker);
if (ItemChecks.isSword(itemInHand) && mcPermissions.getInstance().swords(attacker)) {
if (!plugin.misc.bleedTracker.contains(target)) {
if (!mcBleedTimer.contains(target)) {
Swords.bleedCheck(attacker, target, plugin);
}
@ -360,8 +361,8 @@ public class Combat {
else {
LivingEntity livingEntity = (LivingEntity) entity;
if (type.equals(SkillType.SWORDS) && !plugin.misc.bleedTracker.contains(entity)) {
plugin.misc.bleedQue.add(livingEntity);
if (type.equals(SkillType.SWORDS)) {
mcBleedTimer.add(livingEntity);
}
dealDamage(livingEntity, damageAmount, attacker);

View File

@ -1,25 +1,16 @@
package com.gmail.nossr50.config;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO;
public class Misc {
public HashMap<Entity, Integer> arrowTracker = new HashMap<Entity, Integer>();
public ArrayList<LivingEntity> bleedTracker = new ArrayList<LivingEntity>();
public HashMap<Integer, Player> tntTracker = new HashMap<Integer, Player>();
mcMMO plugin;
/* BLEED QUE STUFF */
public HashSet<LivingEntity> bleedQue = new HashSet<LivingEntity>();
public HashSet<LivingEntity> bleedRemovalQue = new HashSet<LivingEntity>();
public Misc(mcMMO mcMMO) {
this.plugin = mcMMO;
}

View File

@ -33,6 +33,7 @@ import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.events.FakeEntityDamageByEntityEvent;
import com.gmail.nossr50.events.FakeEntityDamageEvent;
import com.gmail.nossr50.party.Party;
import com.gmail.nossr50.runnables.mcBleedTimer;
import com.gmail.nossr50.skills.Acrobatics;
import com.gmail.nossr50.skills.Archery;
import com.gmail.nossr50.skills.BlastMining;
@ -146,9 +147,7 @@ public class mcEntityListener implements Listener {
x.setFireTicks(0);
/* Remove bleed track */
if(plugin.misc.bleedTracker.contains(x)) {
plugin.misc.bleedRemovalQue.add(x);
}
mcBleedTimer.remove(x);
Archery.arrowRetrievalCheck(x, plugin);

View File

@ -291,29 +291,4 @@ public class m {
return skillLevel;
}
}
/**
* Simulate a bleed.
*
* @param plugin mcMMO plugin instance
*/
public static void bleedSimulate(mcMMO plugin) {
/* Set up the tracker */
plugin.misc.bleedTracker.addAll(plugin.misc.bleedQue);
plugin.misc.bleedQue.clear();
plugin.misc.bleedTracker.removeAll(plugin.misc.bleedRemovalQue);
plugin.misc.bleedRemovalQue.clear();
/* Bleed monsters/animals */
for (LivingEntity entity : plugin.misc.bleedTracker) {
if ((entity == null || entity.isDead())) {
plugin.misc.bleedRemovalQue.add(entity);
continue;
}
else {
Combat.dealDamage(entity, 2);
}
}
}
}

View File

@ -35,6 +35,7 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
@ -51,9 +52,6 @@ public class mcMMO extends JavaPlugin {
private final mcBlockListener blockListener = new mcBlockListener(this);
private final mcEntityListener entityListener = new mcEntityListener(this);
private Runnable mcMMO_Timer = new mcTimer(this); //BLEED AND REGENERATION
private Runnable mcMMO_SaveTimer = new mcSaveTimer(this); //Periodic saving of Player Data
//Alias - Command
public HashMap<String, String> aliasMap = new HashMap<String, String>();
@ -138,11 +136,14 @@ public class mcMMO extends JavaPlugin {
System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
//Periodic save timer (Saves every 10 minutes)
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, mcMMO_SaveTimer, 0, LoadProperties.saveInterval * 1200);
BukkitScheduler scheduler = getServer().getScheduler();
//Bleed & Regen timer (Runs every 20 seconds)
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, mcMMO_Timer, 0, 20);
//Periodic save timer (Saves every 10 minutes)
scheduler.scheduleSyncRepeatingTask(this, new mcSaveTimer(this), 0, LoadProperties.saveInterval * 1200);
//Regen & Cooldown timer (Runs every second)
scheduler.scheduleSyncRepeatingTask(this, new mcTimer(this), 0, 20);
//Bleed timer (Runs every two seconds)
scheduler.scheduleSyncRepeatingTask(this, new mcBleedTimer(this), 0, 40);
registerCommands();

View File

@ -0,0 +1,164 @@
package com.gmail.nossr50.runnables;
import java.util.HashSet;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import com.gmail.nossr50.Combat;
import com.gmail.nossr50.Users;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.locale.mcLocale;
public class mcBleedTimer implements Runnable {
private final mcMMO plugin;
private static HashSet<LivingEntity> bleedList = new HashSet<LivingEntity>();
private static HashSet<LivingEntity> bleedAddList = new HashSet<LivingEntity>();
private static HashSet<LivingEntity> bleedRemoveList = new HashSet<LivingEntity>();
private static boolean lock = false;
public mcBleedTimer(final mcMMO plugin) {
this.plugin = plugin;
}
@Override
public void run() {
// Update bleedList with bleedRemoveList and bleedAddList
updateBleedList();
// Player bleed simulation
for (Player player : plugin.getServer().getOnlinePlayers())
{
if (player == null) continue;
PlayerProfile PP = Users.getProfile(player);
if (PP == null) continue;
if (PP.getBleedTicks() >= 1)
{
//Never kill with Bleeding
if (player.getHealth() - 2 < 0)
{
if (player.getHealth() - 1 > 0)
{
Combat.dealDamage(player, 1);
}
}
else
{
Combat.dealDamage(player, 2);
}
PP.decreaseBleedTicks();
if (PP.getBleedTicks() == 0)
{
player.sendMessage(mcLocale.getString("Swords.StoppedBleeding"));
}
}
}
// Non-player bleed simulation
bleedSimulate();
}
private void bleedSimulate() {
// Lock list for looping
lock = true;
// Bleed monsters/animals
for (LivingEntity entity : bleedList)
{
if ((entity == null || entity.isDead()))
{
remove(entity);
continue;
}
else
{
Combat.dealDamage(entity, 2);
}
}
// Unlock list now that we are done
lock = false;
}
private void updateBleedList() {
if (lock)
{
// We can't do anything when locked.
plugin.getLogger().warning("mcBleedTimer attempted to update the bleedList but the list was locked!");
}
else
{
// Remove
bleedList.removeAll(bleedRemoveList);
bleedRemoveList.clear();
// Add
bleedList.addAll(bleedAddList);
bleedAddList.clear();
}
}
/**
* Remove a LivingEntity from the bleedList if it is in it
*
* @param entity LivingEntity to remove
*/
public static void remove(LivingEntity entity) {
if (lock)
{
// Cannot remove when locked, put into bleedRemoveList
if (!bleedRemoveList.contains(entity))
{
bleedRemoveList.add(entity);
}
}
else
{
// Remove as normal
if (bleedList.contains(entity))
{
bleedList.remove(entity);
}
}
}
/**
* Add a LivingEntity to the bleedList if it is not in it
*
* @param entity LivingEntity to add
*/
public static void add(LivingEntity entity) {
if (lock)
{
// Cannot add when locked, put into bleedAddList
if (!bleedAddList.contains(entity))
{
bleedAddList.add(entity);
}
}
else
{
// Add as normal
if (!bleedList.contains(entity))
{
bleedList.add(entity);
}
}
}
/**
* 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.contains(entity) || bleedAddList.contains(entity));
}
}

View File

@ -2,20 +2,16 @@ package com.gmail.nossr50.runnables;
import org.bukkit.entity.*;
import com.gmail.nossr50.Combat;
import com.gmail.nossr50.Users;
import com.gmail.nossr50.m;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.AbilityType;
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.locale.mcLocale;
import com.gmail.nossr50.skills.Skills;
public class mcTimer implements Runnable
{
private final mcMMO plugin;
int thecount = 1;
public mcTimer(final mcMMO plugin)
{
@ -56,37 +52,6 @@ public class mcTimer implements Runnable
Skills.watchCooldown(player, PP, curTime, AbilityType.BERSERK);
Skills.watchCooldown(player, PP, curTime, AbilityType.TREE_FELLER);
Skills.watchCooldown(player, PP, curTime, AbilityType.BLAST_MINING);
/*
* PLAYER BLEED MONITORING
*/
if(thecount % 2 == 0 && PP.getBleedTicks() >= 1)
{
//Never kill with Bleeding
if(player.getHealth() - 2 < 0)
{
if(player.getHealth() - 1 > 0)
Combat.dealDamage(player, 1);
} else
Combat.dealDamage(player, 2);
PP.decreaseBleedTicks();
if(PP.getBleedTicks() == 0)
player.sendMessage(mcLocale.getString("Swords.StoppedBleeding"));
}
/*
* NON-PLAYER BLEED MONITORING
*/
if(thecount % 2 == 0)
m.bleedSimulate(plugin);
//SETUP FOR HP REGEN/BLEED
thecount++;
if(thecount >= 81)
thecount = 1;
}
}
}

View File

@ -17,6 +17,7 @@ import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.locale.mcLocale;
import com.gmail.nossr50.party.Party;
import com.gmail.nossr50.runnables.mcBleedTimer;
public class Swords {
@ -66,7 +67,7 @@ public class Swords {
Users.getProfile(target).addBleedTicks(bleedTicks);
}
else {
plugin.misc.bleedQue.add(entity);
mcBleedTimer.add(entity);
}
attacker.sendMessage(mcLocale.getString("Swords.EnemyBleeding"));
}

View File

@ -21,6 +21,7 @@ import com.gmail.nossr50.config.LoadProperties;
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.locale.mcLocale;
import com.gmail.nossr50.runnables.mcBleedTimer;
public class Taming {
@ -90,7 +91,7 @@ public class Taming {
Users.getProfile(target).setBleedTicks(2);
}
else {
plugin.misc.bleedQue.add((LivingEntity) entity);
mcBleedTimer.add((LivingEntity) entity);
}
master.sendMessage(mcLocale.getString("Combat.Gore"));