From 67a4b6c7c136a394580f4b6c6f5b1887875fd97a Mon Sep 17 00:00:00 2001 From: Shane Freeder Date: Sun, 25 Oct 2020 14:43:33 +0000 Subject: [PATCH] Experience PreGain event --- .../nossr50/datatypes/player/McMMOPlayer.java | 7 +++ .../experience/McMMOPlayerPreXpGainEvent.java | 50 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerPreXpGainEvent.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 4d94f0d4b..cc5aebefe 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -17,6 +17,7 @@ import com.gmail.nossr50.datatypes.skills.PrimarySkillType; import com.gmail.nossr50.datatypes.skills.SubSkillType; import com.gmail.nossr50.datatypes.skills.SuperAbilityType; import com.gmail.nossr50.datatypes.skills.ToolType; +import com.gmail.nossr50.events.experience.McMMOPlayerPreXpGainEvent; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.party.PartyManager; @@ -56,6 +57,8 @@ import com.gmail.nossr50.util.sounds.SoundManager; import com.gmail.nossr50.util.sounds.SoundType; import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; +import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.block.Block; @@ -601,6 +604,10 @@ public class McMMOPlayer implements Identified { return; } + final McMMOPlayerPreXpGainEvent mcMMOPlayerPreXpGainEvent = new McMMOPlayerPreXpGainEvent(player, primarySkillType, xp, xpGainReason); + Bukkit.getPluginManager().callEvent(mcMMOPlayerPreXpGainEvent); + xp = mcMMOPlayerPreXpGainEvent.getXpGained(); + if (primarySkillType.isChildSkill()) { Set parentSkills = FamilyTree.getParents(primarySkillType); diff --git a/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerPreXpGainEvent.java b/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerPreXpGainEvent.java new file mode 100644 index 000000000..849d7ab48 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerPreXpGainEvent.java @@ -0,0 +1,50 @@ +package com.gmail.nossr50.events.experience; + +import com.gmail.nossr50.datatypes.experience.XPGainReason; +import com.gmail.nossr50.datatypes.skills.PrimarySkillType; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +/** + * Called when a player gains XP in a skill + */ +public class McMMOPlayerPreXpGainEvent extends McMMOPlayerExperienceEvent { + private float xpGained; + + @Deprecated + public McMMOPlayerPreXpGainEvent(Player player, PrimarySkillType skill, float xpGained) { + super(player, skill, XPGainReason.UNKNOWN); + this.xpGained = xpGained; + } + + public McMMOPlayerPreXpGainEvent(Player player, PrimarySkillType skill, float xpGained, XPGainReason xpGainReason) { + super(player, skill, xpGainReason); + this.xpGained = xpGained; + } + + /** + * @return int amount of experience gained in this event + */ + public int getXpGained() { + return (int) xpGained; + } + + /** + * @param xpGained int amount of experience gained in this event + */ + public void setXpGained(int xpGained) { + this.xpGained = xpGained; + } + + private static final HandlerList handlers = new HandlerList(); + + @Override + public @NotNull HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +}