Added McMMOPlayerVampirismEvent

Expands API possibilities regarding death penalty features.
This commit is contained in:
TfT_02 2014-03-16 20:59:03 +01:00
parent 80d358d1fd
commit 809779e508
5 changed files with 84 additions and 22 deletions

View File

@ -5,11 +5,20 @@ import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerEvent;
import com.gmail.nossr50.datatypes.skills.SkillType;
public class McMMOPlayerDeathPenaltyEvent extends PlayerEvent implements Cancellable { public class McMMOPlayerDeathPenaltyEvent extends PlayerEvent implements Cancellable {
private SkillType skill;
private boolean cancelled; private boolean cancelled;
@Deprecated
public McMMOPlayerDeathPenaltyEvent(Player player) { public McMMOPlayerDeathPenaltyEvent(Player player) {
super(player); super(player);
}
public McMMOPlayerDeathPenaltyEvent(Player player, SkillType skill) {
super(player);
this.skill = skill;
this.cancelled = false; this.cancelled = false;
} }
@ -35,4 +44,8 @@ public class McMMOPlayerDeathPenaltyEvent extends PlayerEvent implements Cancell
public static HandlerList getHandlerList() { public static HandlerList getHandlerList() {
return handlers; return handlers;
} }
public SkillType getSkill() {
return skill;
}
} }

View File

@ -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;
}
}

View File

@ -140,9 +140,8 @@ public class PlayerListener implements Listener {
Player killer = killedPlayer.getKiller(); Player killer = killedPlayer.getKiller();
if (statLossEnabled || (killer != null && vampirismEnabled)) { if (statLossEnabled || (killer != null && vampirismEnabled)) {
if (EventUtils.callDeathPenaltyEvent(killedPlayer).isCancelled()) { if (EventUtils.callDeathPenaltyEvent(killedPlayer, null).isCancelled()) {
return; return;
} }

View File

@ -28,6 +28,7 @@ import com.gmail.nossr50.events.fake.FakeBlockDamageEvent;
import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent; import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent;
import com.gmail.nossr50.events.fake.FakePlayerFishEvent; import com.gmail.nossr50.events.fake.FakePlayerFishEvent;
import com.gmail.nossr50.events.hardcore.McMMOPlayerDeathPenaltyEvent; 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.McMMOPartyLevelUpEvent;
import com.gmail.nossr50.events.party.McMMOPartyTeleportEvent; import com.gmail.nossr50.events.party.McMMOPartyTeleportEvent;
import com.gmail.nossr50.events.party.McMMOPartyXpGainEvent; import com.gmail.nossr50.events.party.McMMOPartyXpGainEvent;
@ -163,6 +164,38 @@ public class EventUtils {
return !isCancelled; 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) { public static McMMOPlayerAbilityDeactivateEvent callAbilityDeactivateEvent(Player player, AbilityType ability) {
McMMOPlayerAbilityDeactivateEvent event = new McMMOPlayerAbilityDeactivateEvent(player, SkillType.byAbility(ability)); McMMOPlayerAbilityDeactivateEvent event = new McMMOPlayerAbilityDeactivateEvent(player, SkillType.byAbility(ability));
mcMMO.p.getServer().getPluginManager().callEvent(event); mcMMO.p.getServer().getPluginManager().callEvent(event);
@ -191,8 +224,8 @@ public class EventUtils {
return event; return event;
} }
public static McMMOPlayerDeathPenaltyEvent callDeathPenaltyEvent(Player player) { public static McMMOPlayerDeathPenaltyEvent callDeathPenaltyEvent(Player player, SkillType skill) {
McMMOPlayerDeathPenaltyEvent event = new McMMOPlayerDeathPenaltyEvent(player); McMMOPlayerDeathPenaltyEvent event = new McMMOPlayerDeathPenaltyEvent(player, skill);
mcMMO.p.getServer().getPluginManager().callEvent(event); mcMMO.p.getServer().getPluginManager().callEvent(event);
return event; return event;

View File

@ -3,10 +3,8 @@ package com.gmail.nossr50.util;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.nossr50.config.Config; 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.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.datatypes.skills.XPGainReason;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
@ -57,8 +55,7 @@ public final class HardcoreManager {
double vampirismStatLeechPercentage = Config.getInstance().getHardcoreVampirismStatLeechPercentage(); double vampirismStatLeechPercentage = Config.getInstance().getHardcoreVampirismStatLeechPercentage();
int levelThreshold = Config.getInstance().getHardcoreVampirismLevelThreshold(); int levelThreshold = Config.getInstance().getHardcoreVampirismLevelThreshold();
McMMOPlayer killerPlayer = UserManager.getPlayer(killer); PlayerProfile killerProfile = UserManager.getPlayer(killer).getProfile();
PlayerProfile killerProfile = killerPlayer.getProfile();
PlayerProfile victimProfile = UserManager.getPlayer(victim).getProfile(); PlayerProfile victimProfile = UserManager.getPlayer(victim).getProfile();
int totalLevelsStolen = 0; int totalLevelsStolen = 0;
@ -80,20 +77,8 @@ public final class HardcoreManager {
int levelsStolen = (int) statsStolen; int levelsStolen = (int) statsStolen;
int xpStolen = (int) Math.floor(victimSkillXpLevel * (statsStolen - levelsStolen)); int xpStolen = (int) Math.floor(victimSkillXpLevel * (statsStolen - levelsStolen));
totalLevelsStolen += levelsStolen; if (EventUtils.handleVampirismEvent(killer, victim, skillType, levelsStolen, xpStolen)) {
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);
} }
} }