Navigation Menu

Skip to content

Commit

Permalink
Capped bleeding ticks for monsters, moved players bleeding to BleedTimer
Browse files Browse the repository at this point in the history
  • Loading branch information
bm01 committed Apr 28, 2012
1 parent b46997b commit 9e98351
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 182 deletions.
27 changes: 0 additions & 27 deletions src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java
Expand Up @@ -19,8 +19,6 @@

public class PlayerProfile {

final static int MAX_BLEED_TICKS = 10;

/* HUD */
private HUDType hud;
private int xpbarinc = 0;
Expand Down Expand Up @@ -52,7 +50,6 @@ public class PlayerProfile {
/* mySQL STUFF */
private int lastlogin = 0;
private int userid = 0;
private int bleedticks = 0;

HashMap<SkillType, Integer> skills = new HashMap<SkillType, Integer>(); //Skills and Levels
HashMap<SkillType, Integer> skillsXp = new HashMap<SkillType, Integer>(); //Skills and XP
Expand Down Expand Up @@ -608,30 +605,6 @@ public void togglePartyChat() {
partyChatMode = !partyChatMode;
}

/*
* Bleed Stuff
*/

public void decreaseBleedTicks() {
bleedticks--;
}

public int getBleedTicks() {
return bleedticks;
}

public void resetBleedTicks() {
bleedticks = 0;
}

public void addBleedTicks(int newvalue){
bleedticks += newvalue;

if (bleedticks > MAX_BLEED_TICKS) {
bleedticks = MAX_BLEED_TICKS;
}
}

/*
* Exploit Prevention
*/
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/gmail/nossr50/listeners/EntityListener.java
Expand Up @@ -143,17 +143,13 @@ else if (cause == DamageCause.BLOCK_EXPLOSION && Permissions.getInstance().demol
*/
@EventHandler (priority = EventPriority.MONITOR)
public void onEntityDeath(EntityDeathEvent event) {
LivingEntity x = event.getEntity();
x.setFireTicks(0);
LivingEntity entity = event.getEntity();
entity.setFireTicks(0);

/* Remove bleed track */
BleedTimer.remove(x);
BleedTimer.remove(entity);

Archery.arrowRetrievalCheck(x, plugin);

if (x instanceof Player) {
Users.getProfile((Player)x).resetBleedTicks();
}
Archery.arrowRetrievalCheck(entity, plugin);
}

/**
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/gmail/nossr50/listeners/PlayerListener.java
Expand Up @@ -27,6 +27,7 @@
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.commands.general.XprateCommand;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.runnables.BleedTimer;
import com.gmail.nossr50.runnables.RemoveProfileFromMemoryTask;
import com.gmail.nossr50.spout.SpoutStuff;
import com.gmail.nossr50.datatypes.AbilityType;
Expand All @@ -43,7 +44,6 @@
import com.gmail.nossr50.skills.Skills;
import com.gmail.nossr50.skills.Taming;
import com.gmail.nossr50.util.BlockChecks;
import com.gmail.nossr50.util.Combat;
import com.gmail.nossr50.util.Item;
import com.gmail.nossr50.util.ItemChecks;
import com.gmail.nossr50.util.Permissions;
Expand Down Expand Up @@ -140,7 +140,6 @@ public void onPlayerLogin(PlayerLoginEvent event) {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
PlayerProfile PP = Users.getProfile(player);

/* GARBAGE COLLECTION */

Expand All @@ -150,9 +149,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
}

//Bleed it out
if(PP.getBleedTicks() > 0) {
Combat.dealDamage(player, PP.getBleedTicks() * 2);
}
BleedTimer.bleedOut(player);

//Schedule PlayerProfile removal 2 minutes after quitting
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new RemoveProfileFromMemoryTask(player.getName()), 2400);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/gmail/nossr50/mcMMO.java
Expand Up @@ -114,7 +114,7 @@ public void onEnable() {
//Regen & Cooldown timer (Runs every second)
scheduler.scheduleSyncRepeatingTask(this, new SkillMonitor(this), 0, 20);
//Bleed timer (Runs every two seconds)
scheduler.scheduleSyncRepeatingTask(this, new BleedTimer(this), 0, 40);
scheduler.scheduleSyncRepeatingTask(this, new BleedTimer(), 0, 40);

registerCommands();

Expand Down
136 changes: 88 additions & 48 deletions src/main/java/com/gmail/nossr50/runnables/BleedTimer.java
@@ -1,45 +1,51 @@
package com.gmail.nossr50.runnables;

import java.util.HashSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.Combat;
import com.gmail.nossr50.util.Users;

public class BleedTimer 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 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>();

private static boolean lock = false;

public BleedTimer(final mcMMO plugin) {
this.plugin = plugin;
}

@Override
public void run() {
updateBleedList();
bleedSimulate();
}

// Player bleed simulation
for (Player player : plugin.getServer().getOnlinePlayers()) {
if (player == null) {
continue;
}
private void bleedSimulate() {
lock = true;

for (Entry<LivingEntity, Integer> entry : bleedList.entrySet()) {
LivingEntity entity = entry.getKey();

PlayerProfile PP = Users.getProfile(player);
if (PP == null) {
continue;
if (entry.getValue() <= 0 || entity.isDead() || entity == null) {
remove(entity);
break;
}

if (PP.getBleedTicks() >= 1) {
// Player bleed simulation
if (entity instanceof Player) {
Player player = (Player) entity;

if (!player.isOnline()) {
continue;
}

//Never kill with Bleeding
if (player.getHealth() - 2 < 0) {
Expand All @@ -51,29 +57,16 @@ public void run() {
Combat.dealDamage(player, 2);
}

PP.decreaseBleedTicks();
entry.setValue(entry.getValue() - 1);

if (PP.getBleedTicks() == 0) {
if (entry.getValue() <= 0) {
player.sendMessage(LocaleLoader.getString("Swords.Combat.Bleeding.Stopped"));
}
}
}

// Non-player bleed simulation
bleedSimulate();
}

private void bleedSimulate() {
lock = true;

// Bleed monsters/animals
for (LivingEntity entity : bleedList) {
if ((entity == null || entity.isDead())) {
remove(entity);
continue;
}
// Bleed monsters/animals
else {
Combat.dealDamage(entity, 2);
entry.setValue(entry.getValue() - 1);
}
}

Expand All @@ -83,17 +76,29 @@ private void bleedSimulate() {

private void updateBleedList() {
if (lock) {
plugin.getLogger().warning("mcBleedTimer attempted to update the bleedList but the list was locked!");
mcMMO.p.getLogger().warning("mcBleedTimer attempted to update the bleedList but the list was locked!");
}
else {
bleedList.removeAll(bleedRemoveList);
bleedList.keySet().removeAll(bleedRemoveList);
bleedRemoveList.clear();

bleedList.addAll(bleedAddList);
bleedList.putAll(bleedAddList);
bleedAddList.clear();
}
}

/**
* Instantly Bleed out a LivingEntity
*
* @param entity LivingEntity to bleed out
*/
public static void bleedOut(LivingEntity entity) {
if (bleedList.containsKey(entity)) {
Combat.dealDamage(entity, bleedList.get(entity) * 2);
bleedList.remove(entity);
}
}

/**
* Remove a LivingEntity from the bleedList if it is in it
*
Expand All @@ -106,7 +111,7 @@ public static void remove(LivingEntity entity) {
}
}
else {
if (bleedList.contains(entity)) {
if (bleedList.containsKey(entity)) {
bleedList.remove(entity);
}
}
Expand All @@ -117,15 +122,50 @@ public static void remove(LivingEntity entity) {
*
* @param entity LivingEntity to add
*/
public static void add(LivingEntity entity) {
public static void add(LivingEntity entity, int ticks) {
int newTicks = ticks;

if (lock) {
if (!bleedAddList.contains(entity)) {
bleedAddList.add(entity);
if (bleedAddList.containsKey(entity)) {
newTicks += bleedAddList.get(entity);

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);
}
}
else {
if (!bleedList.contains(entity)){
bleedList.add(entity);
if (bleedList.containsKey(entity)) {
newTicks += bleedList.get(entity);

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);
}
}
}
Expand All @@ -137,6 +177,6 @@ public static void add(LivingEntity entity) {
* @return true if in the list, false if not
*/
public static boolean contains(LivingEntity entity) {
return (bleedList.contains(entity) || bleedAddList.contains(entity));
return (bleedList.containsKey(entity) || bleedAddList.containsKey(entity));
}
}
18 changes: 6 additions & 12 deletions src/main/java/com/gmail/nossr50/skills/Swords.java
Expand Up @@ -57,22 +57,16 @@ public static void bleedCheck(Player attacker, LivingEntity entity, mcMMO plugin
int skillCheck = Misc.skillCheck(skillLevel, MAX_BONUS_LEVEL);

if (random.nextInt(1000) <= skillCheck && !entity.isDead()) {
if (entity instanceof Player) {
Player target = (Player) entity;
int bleedTicks;
int bleedTicks = 0;

if (skillLevel >= 750) {
bleedTicks = 3;
}
else {
bleedTicks = 2;
}

Users.getProfile(target).addBleedTicks(bleedTicks);
if (skillLevel >= 750) {
bleedTicks = 3;
}
else {
BleedTimer.add(entity);
bleedTicks = 2;
}

BleedTimer.add(entity, bleedTicks);
attacker.sendMessage(LocaleLoader.getString("Swords.Combat.Bleeding"));
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/com/gmail/nossr50/skills/Taming.java
Expand Up @@ -92,15 +92,11 @@ public static void gore(PlayerProfile PPo, EntityDamageEvent event, Player maste
event.setDamage(event.getDamage() * GORE_MULTIPLIER);

if (entity instanceof Player) {
Player target = (Player) entity;

target.sendMessage(LocaleLoader.getString("Combat.StruckByGore"));
Users.getProfile(target).addBleedTicks(2);
}
else {
BleedTimer.add((LivingEntity) entity);
((Player) entity).sendMessage(LocaleLoader.getString("Combat.StruckByGore"));
}

BleedTimer.add((LivingEntity) entity, 2);

master.sendMessage(LocaleLoader.getString("Combat.Gore"));
}
}
Expand Down

0 comments on commit 9e98351

Please sign in to comment.