Make McMMOPlayerXPGainEvent cancellable.

This commit is contained in:
GJ 2013-02-15 12:39:55 -05:00
parent 2d0cf6413e
commit 0701169a7e
4 changed files with 31 additions and 4 deletions

View File

@ -96,8 +96,14 @@ public class McMMOPlayer {
* @param xp Experience amount to add * @param xp Experience amount to add
*/ */
public void applyXpGain(SkillType skillType, int xp) { public void applyXpGain(SkillType skillType, int xp) {
mcMMO.p.getServer().getPluginManager().callEvent(new McMMOPlayerXpGainEvent(player, skillType, xp)); McMMOPlayerXpGainEvent event = new McMMOPlayerXpGainEvent(player, skillType, xp);
profile.setSkillXpLevel(skillType, profile.getSkillXpLevel(skillType) + xp); mcMMO.p.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
profile.setSkillXpLevel(skillType, profile.getSkillXpLevel(skillType) + event.getXpGained());
SpoutHud spoutHud = profile.getSpoutHud(); SpoutHud spoutHud = profile.getSpoutHud();

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.events.experience; package com.gmail.nossr50.events.experience;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
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;
@ -10,7 +11,8 @@ import com.gmail.nossr50.util.Users;
/** /**
* Generic event for mcMMO experience events. * Generic event for mcMMO experience events.
*/ */
public abstract class McMMOPlayerExperienceEvent extends PlayerEvent { public abstract class McMMOPlayerExperienceEvent extends PlayerEvent implements Cancellable {
private boolean cancelled;
protected SkillType skill; protected SkillType skill;
protected int skillLevel; protected int skillLevel;
@ -45,4 +47,15 @@ public abstract class McMMOPlayerExperienceEvent extends PlayerEvent {
public static HandlerList getHandlerList() { public static HandlerList getHandlerList() {
return handlers; return handlers;
} }
/** Following are required for Cancellable **/
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
} }

View File

@ -21,4 +21,11 @@ public class McMMOPlayerXpGainEvent extends McMMOPlayerExperienceEvent {
public int getXpGained() { public int getXpGained() {
return xpGained; return xpGained;
} }
/**
* @param xpGained int amount of experience gained in this event
*/
public void setXpGained(int xpGained) {
this.xpGained = xpGained;
}
} }

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.listeners; package com.gmail.nossr50.listeners;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
@ -15,7 +16,7 @@ public class SelfListener implements Listener {
* *
* @param event The event to watch * @param event The event to watch
*/ */
@EventHandler @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerXpGain(McMMOPlayerXpGainEvent event) { public void onPlayerXpGain(McMMOPlayerXpGainEvent event) {
int xp = event.getXpGained(); int xp = event.getXpGained();