From 809779e50871c652089089e6fbbd00030ad9675b Mon Sep 17 00:00:00 2001 From: TfT_02 Date: Sun, 16 Mar 2014 20:59:03 +0100 Subject: [PATCH] Added McMMOPlayerVampirismEvent Expands API possibilities regarding death penalty features. --- .../McMMOPlayerDeathPenaltyEvent.java | 13 +++++++ .../McMMOPlayerVampirismPenaltyEvent.java | 32 ++++++++++++++++ .../nossr50/listeners/PlayerListener.java | 3 +- .../com/gmail/nossr50/util/EventUtils.java | 37 ++++++++++++++++++- .../gmail/nossr50/util/HardcoreManager.java | 21 ++--------- 5 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/events/hardcore/McMMOPlayerVampirismPenaltyEvent.java diff --git a/src/main/java/com/gmail/nossr50/events/hardcore/McMMOPlayerDeathPenaltyEvent.java b/src/main/java/com/gmail/nossr50/events/hardcore/McMMOPlayerDeathPenaltyEvent.java index 5b3b1838d..0543ab865 100644 --- a/src/main/java/com/gmail/nossr50/events/hardcore/McMMOPlayerDeathPenaltyEvent.java +++ b/src/main/java/com/gmail/nossr50/events/hardcore/McMMOPlayerDeathPenaltyEvent.java @@ -5,11 +5,20 @@ import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; import org.bukkit.event.player.PlayerEvent; +import com.gmail.nossr50.datatypes.skills.SkillType; + public class McMMOPlayerDeathPenaltyEvent extends PlayerEvent implements Cancellable { + private SkillType skill; private boolean cancelled; + @Deprecated public McMMOPlayerDeathPenaltyEvent(Player player) { super(player); + } + + public McMMOPlayerDeathPenaltyEvent(Player player, SkillType skill) { + super(player); + this.skill = skill; this.cancelled = false; } @@ -35,4 +44,8 @@ public class McMMOPlayerDeathPenaltyEvent extends PlayerEvent implements Cancell public static HandlerList getHandlerList() { return handlers; } + + public SkillType getSkill() { + return skill; + } } diff --git a/src/main/java/com/gmail/nossr50/events/hardcore/McMMOPlayerVampirismPenaltyEvent.java b/src/main/java/com/gmail/nossr50/events/hardcore/McMMOPlayerVampirismPenaltyEvent.java new file mode 100644 index 000000000..c9a554241 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/events/hardcore/McMMOPlayerVampirismPenaltyEvent.java @@ -0,0 +1,32 @@ +package com.gmail.nossr50.events.hardcore; + +import org.bukkit.entity.Player; + +import com.gmail.nossr50.datatypes.skills.SkillType; + +public class McMMOPlayerVampirismPenaltyEvent extends McMMOPlayerDeathPenaltyEvent { + private int levelChanged; + private float experienceChanged; + + public McMMOPlayerVampirismPenaltyEvent(Player player, SkillType skill, int levelChanged, float experienceChanged) { + super(player, skill); + this.levelChanged = levelChanged; + this.experienceChanged = experienceChanged; + } + + public int getLevelChanged() { + return levelChanged; + } + + public void setLevelChanged(int levelChanged) { + this.levelChanged = levelChanged; + } + + public float getExperienceChanged() { + return experienceChanged; + } + + public void setExperienceChanged(float experienceChanged) { + this.experienceChanged = experienceChanged; + } +} diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index 771fe4f87..d280075aa 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -140,9 +140,8 @@ public class PlayerListener implements Listener { Player killer = killedPlayer.getKiller(); - if (statLossEnabled || (killer != null && vampirismEnabled)) { - if (EventUtils.callDeathPenaltyEvent(killedPlayer).isCancelled()) { + if (EventUtils.callDeathPenaltyEvent(killedPlayer, null).isCancelled()) { return; } diff --git a/src/main/java/com/gmail/nossr50/util/EventUtils.java b/src/main/java/com/gmail/nossr50/util/EventUtils.java index 3f764d4e0..f6ed2818d 100644 --- a/src/main/java/com/gmail/nossr50/util/EventUtils.java +++ b/src/main/java/com/gmail/nossr50/util/EventUtils.java @@ -28,6 +28,7 @@ import com.gmail.nossr50.events.fake.FakeBlockDamageEvent; import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent; import com.gmail.nossr50.events.fake.FakePlayerFishEvent; import com.gmail.nossr50.events.hardcore.McMMOPlayerDeathPenaltyEvent; +import com.gmail.nossr50.events.hardcore.McMMOPlayerVampirismPenaltyEvent; import com.gmail.nossr50.events.party.McMMOPartyLevelUpEvent; import com.gmail.nossr50.events.party.McMMOPartyTeleportEvent; import com.gmail.nossr50.events.party.McMMOPartyXpGainEvent; @@ -163,6 +164,38 @@ public class EventUtils { return !isCancelled; } + public static boolean handleVampirismEvent(Player killer, Player victim, SkillType skillType, int levelsStolen, int xpStolen) { + McMMOPlayerVampirismPenaltyEvent eventKiller = new McMMOPlayerVampirismPenaltyEvent(killer, skillType, levelsStolen, xpStolen); + McMMOPlayerVampirismPenaltyEvent eventVictim = new McMMOPlayerVampirismPenaltyEvent(victim, skillType, -levelsStolen, -xpStolen); + mcMMO.p.getServer().getPluginManager().callEvent(eventKiller); + mcMMO.p.getServer().getPluginManager().callEvent(eventVictim); + + boolean isCancelled = eventKiller.isCancelled() || eventVictim.isCancelled(); + + if (!isCancelled) { + McMMOPlayer killerPlayer = UserManager.getPlayer(killer); + PlayerProfile victimProfile = UserManager.getPlayer(victim).getProfile(); + int victimSkillLevel = victimProfile.getSkillLevel(skillType); + + killerPlayer.addLevels(skillType, eventKiller.getLevelChanged()); + killerPlayer.beginUnsharedXpGain(skillType, eventKiller.getExperienceChanged(), XPGainReason.VAMPIRISM); + + // For victims McMMOPlayerVampirismPenaltyEvent is fired with negative levels changed and XP changed + victimProfile.modifySkill(skillType, victimSkillLevel + eventVictim.getLevelChanged()); + victimProfile.removeXp(skillType, (int) - eventVictim.getExperienceChanged()); + + if (victimProfile.getSkillXpLevel(skillType) < 0) { + victimProfile.setSkillXpLevel(skillType, 0); + } + + if (victimProfile.getSkillLevel(skillType) < 0) { + victimProfile.modifySkill(skillType, 0); + } + } + + return !isCancelled; + } + public static McMMOPlayerAbilityDeactivateEvent callAbilityDeactivateEvent(Player player, AbilityType ability) { McMMOPlayerAbilityDeactivateEvent event = new McMMOPlayerAbilityDeactivateEvent(player, SkillType.byAbility(ability)); mcMMO.p.getServer().getPluginManager().callEvent(event); @@ -191,8 +224,8 @@ public class EventUtils { return event; } - public static McMMOPlayerDeathPenaltyEvent callDeathPenaltyEvent(Player player) { - McMMOPlayerDeathPenaltyEvent event = new McMMOPlayerDeathPenaltyEvent(player); + public static McMMOPlayerDeathPenaltyEvent callDeathPenaltyEvent(Player player, SkillType skill) { + McMMOPlayerDeathPenaltyEvent event = new McMMOPlayerDeathPenaltyEvent(player, skill); mcMMO.p.getServer().getPluginManager().callEvent(event); return event; diff --git a/src/main/java/com/gmail/nossr50/util/HardcoreManager.java b/src/main/java/com/gmail/nossr50/util/HardcoreManager.java index 27aad1aa2..a97c275af 100644 --- a/src/main/java/com/gmail/nossr50/util/HardcoreManager.java +++ b/src/main/java/com/gmail/nossr50/util/HardcoreManager.java @@ -3,10 +3,8 @@ package com.gmail.nossr50.util; import org.bukkit.entity.Player; import com.gmail.nossr50.config.Config; -import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.datatypes.skills.XPGainReason; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.util.player.UserManager; @@ -57,8 +55,7 @@ public final class HardcoreManager { double vampirismStatLeechPercentage = Config.getInstance().getHardcoreVampirismStatLeechPercentage(); int levelThreshold = Config.getInstance().getHardcoreVampirismLevelThreshold(); - McMMOPlayer killerPlayer = UserManager.getPlayer(killer); - PlayerProfile killerProfile = killerPlayer.getProfile(); + PlayerProfile killerProfile = UserManager.getPlayer(killer).getProfile(); PlayerProfile victimProfile = UserManager.getPlayer(victim).getProfile(); int totalLevelsStolen = 0; @@ -80,20 +77,8 @@ public final class HardcoreManager { int levelsStolen = (int) statsStolen; int xpStolen = (int) Math.floor(victimSkillXpLevel * (statsStolen - levelsStolen)); - totalLevelsStolen += levelsStolen; - - killerPlayer.addLevels(skillType, levelsStolen); - killerPlayer.beginUnsharedXpGain(skillType, xpStolen, XPGainReason.VAMPIRISM); - - victimProfile.modifySkill(skillType, victimSkillLevel - levelsStolen); - victimProfile.removeXp(skillType, xpStolen); - - if (victimProfile.getSkillXpLevel(skillType) < 0) { - victimProfile.setSkillXpLevel(skillType, 0); - } - - if (victimProfile.getSkillLevel(skillType) < 0) { - victimProfile.modifySkill(skillType, 0); + if (EventUtils.handleVampirismEvent(killer, victim, skillType, levelsStolen, xpStolen)) { + totalLevelsStolen += levelsStolen; } }