From d0a1d44fa2c8de74af9857c9045f32262bbff4da Mon Sep 17 00:00:00 2001 From: GJ Date: Mon, 21 Oct 2013 09:34:16 -0400 Subject: [PATCH] Add new McMMOPlayerXpLossEvent --- .../nossr50/datatypes/player/McMMOPlayer.java | 2 +- .../experience/McMMOPlayerXpChangeEvent.java | 11 ++++++++ .../experience/McMMOPlayerXpGainEvent.java | 4 +-- .../experience/McMMOPlayerXpLossEvent.java | 28 +++++++++++++++++++ .../com/gmail/nossr50/util/EventUtils.java | 9 ++++-- .../gmail/nossr50/util/HardcoreManager.java | 7 ++--- 6 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerXpChangeEvent.java create mode 100644 src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerXpLossEvent.java diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java index 90f82e46c..b62f1b4cc 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -528,7 +528,7 @@ public class McMMOPlayer { return; } - if (!EventUtils.handleXpGainEvent(player, skillType, xp)) { + if (!EventUtils.handleXpChangeEvent(player, skillType, xp, true)) { return; } diff --git a/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerXpChangeEvent.java b/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerXpChangeEvent.java new file mode 100644 index 000000000..f01c495e0 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerXpChangeEvent.java @@ -0,0 +1,11 @@ +package com.gmail.nossr50.events.experience; + +import org.bukkit.entity.Player; + +import com.gmail.nossr50.datatypes.skills.SkillType; + +public abstract class McMMOPlayerXpChangeEvent extends McMMOPlayerExperienceEvent { + public McMMOPlayerXpChangeEvent(Player player, SkillType skill) { + super(player, skill); + } +} diff --git a/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerXpGainEvent.java b/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerXpGainEvent.java index 6364a823f..58db0f271 100644 --- a/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerXpGainEvent.java +++ b/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerXpGainEvent.java @@ -7,7 +7,7 @@ import com.gmail.nossr50.datatypes.skills.SkillType; /** * Called when a player gains XP in a skill */ -public class McMMOPlayerXpGainEvent extends McMMOPlayerExperienceEvent { +public class McMMOPlayerXpGainEvent extends McMMOPlayerXpChangeEvent { private float xpGained; public McMMOPlayerXpGainEvent(Player player, SkillType skill, float xpGained) { @@ -31,7 +31,7 @@ public class McMMOPlayerXpGainEvent extends McMMOPlayerExperienceEvent { } /** - * @param xpGained int amount of experience gained in this event + * @param xpGained float amount of experience gained in this event */ public void setRawXpGained(float xpGained) { this.xpGained = xpGained; diff --git a/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerXpLossEvent.java b/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerXpLossEvent.java new file mode 100644 index 000000000..3b6d3668b --- /dev/null +++ b/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerXpLossEvent.java @@ -0,0 +1,28 @@ +package com.gmail.nossr50.events.experience; + +import org.bukkit.entity.Player; + +import com.gmail.nossr50.datatypes.skills.SkillType; + +public class McMMOPlayerXpLossEvent extends McMMOPlayerXpChangeEvent { + private float xpLost; + + public McMMOPlayerXpLossEvent(Player player, SkillType skill, float xpLost) { + super(player, skill); + this.xpLost = xpLost; + } + + /** + * @return The amount of experience lost in this event + */ + public float getRawXpLost() { + return xpLost; + } + + /** + * @param xpLost amount of experience lost in this event + */ + public void setRawXpLost(float xpLost) { + this.xpLost = xpLost; + } +} diff --git a/src/main/java/com/gmail/nossr50/util/EventUtils.java b/src/main/java/com/gmail/nossr50/util/EventUtils.java index b7426418e..e3bdbf49d 100644 --- a/src/main/java/com/gmail/nossr50/util/EventUtils.java +++ b/src/main/java/com/gmail/nossr50/util/EventUtils.java @@ -18,7 +18,9 @@ import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.events.experience.McMMOPlayerLevelChangeEvent; import com.gmail.nossr50.events.experience.McMMOPlayerLevelDownEvent; import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent; +import com.gmail.nossr50.events.experience.McMMOPlayerXpChangeEvent; import com.gmail.nossr50.events.experience.McMMOPlayerXpGainEvent; +import com.gmail.nossr50.events.experience.McMMOPlayerXpLossEvent; import com.gmail.nossr50.events.fake.FakeBlockBreakEvent; import com.gmail.nossr50.events.fake.FakeBlockDamageEvent; import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent; @@ -108,14 +110,15 @@ public class EventUtils { mcMMOPlayer.getPartyTeleportRecord().actualizeLastUse(); } - public static boolean handleXpGainEvent(Player player, SkillType skill, float xpGained) { - McMMOPlayerXpGainEvent event = new McMMOPlayerXpGainEvent(player, skill, xpGained); + public static boolean handleXpChangeEvent(Player player, SkillType skill, float xpChanged, boolean isXpGain) { + McMMOPlayerXpChangeEvent event = isXpGain ? new McMMOPlayerXpGainEvent(player, skill, xpChanged) : new McMMOPlayerXpLossEvent(player, skill, xpChanged); mcMMO.p.getServer().getPluginManager().callEvent(event); boolean isCancelled = event.isCancelled(); if (!isCancelled) { - UserManager.getPlayer(player).addXp(skill, event.getRawXpGained()); + PlayerProfile profile = UserManager.getPlayer(player).getProfile(); + profile.setSkillXpLevel(skill, profile.getSkillXpLevelRaw(skill) + (isXpGain ? ((McMMOPlayerXpGainEvent) event).getRawXpGained() : -((McMMOPlayerXpLossEvent) event).getRawXpLost())); } return !isCancelled; diff --git a/src/main/java/com/gmail/nossr50/util/HardcoreManager.java b/src/main/java/com/gmail/nossr50/util/HardcoreManager.java index 3ccd209bb..41a5e7430 100644 --- a/src/main/java/com/gmail/nossr50/util/HardcoreManager.java +++ b/src/main/java/com/gmail/nossr50/util/HardcoreManager.java @@ -77,13 +77,12 @@ public final class HardcoreManager { EventUtils.handleLevelChangeEvent(killer, skillType, levelsStolen, killerXpRemoved, true); killerProfile.addXp(skillType, xpStolen); - EventUtils.handleXpGainEvent(killer, skillType, xpStolen); + EventUtils.handleXpChangeEvent(killer, skillType, xpStolen, true); float victimXpRemoved = killerProfile.getSkillXpLevelRaw(skillType); victimProfile.modifySkill(skillType, victimSkillLevel - levelsStolen); - victimProfile.removeXp(skillType, xpStolen); - - EventUtils.handleLevelChangeEvent(victim, skillType, levelsStolen, victimXpRemoved + xpStolen, false); + EventUtils.handleLevelChangeEvent(victim, skillType, levelsStolen, victimXpRemoved, false); + EventUtils.handleXpChangeEvent(killer, skillType, xpStolen, false); } if (totalLevelsStolen > 0) {