mirror of
				https://github.com/mcMMO-Dev/mcMMO.git
				synced 2025-11-04 02:53:43 +01:00 
			
		
		
		
	Added API to XP events to get XP gain reason
This commit is contained in:
		@@ -7,10 +7,12 @@ import org.bukkit.entity.Player;
 | 
			
		||||
import com.gmail.nossr50.mcMMO;
 | 
			
		||||
import com.gmail.nossr50.api.exceptions.InvalidPlayerException;
 | 
			
		||||
import com.gmail.nossr50.api.exceptions.InvalidSkillException;
 | 
			
		||||
import com.gmail.nossr50.api.exceptions.InvalidXPGainReasonException;
 | 
			
		||||
import com.gmail.nossr50.config.Config;
 | 
			
		||||
import com.gmail.nossr50.config.experience.ExperienceConfig;
 | 
			
		||||
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.skills.child.FamilyTree;
 | 
			
		||||
import com.gmail.nossr50.util.player.UserManager;
 | 
			
		||||
 | 
			
		||||
@@ -62,8 +64,26 @@ public final class ExperienceAPI {
 | 
			
		||||
     *
 | 
			
		||||
     * @throws InvalidSkillException if the given skill is not valid
 | 
			
		||||
     */
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    public static void addRawXP(Player player, String skillType, float XP) {
 | 
			
		||||
        UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), XP);
 | 
			
		||||
        addRawXP(player, skillType, XP, "UNKNOWN");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Adds raw XP to the player.
 | 
			
		||||
     * </br>
 | 
			
		||||
     * This function is designed for API usage.
 | 
			
		||||
     *
 | 
			
		||||
     * @param player The player to add XP to
 | 
			
		||||
     * @param skillType The skill to add XP to
 | 
			
		||||
     * @param XP The amount of XP to add
 | 
			
		||||
     * @param xpGainReason The reason to gain XP
 | 
			
		||||
     *
 | 
			
		||||
     * @throws InvalidSkillException if the given skill is not valid
 | 
			
		||||
     * @throws InvalidXPGainReasonException if the given xpGainReason is not valid
 | 
			
		||||
     */
 | 
			
		||||
    public static void addRawXP(Player player, String skillType, float XP, String xpGainReason) {
 | 
			
		||||
        UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Deprecated
 | 
			
		||||
@@ -98,8 +118,26 @@ public final class ExperienceAPI {
 | 
			
		||||
     *
 | 
			
		||||
     * @throws InvalidSkillException if the given skill is not valid
 | 
			
		||||
     */
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    public static void addMultipliedXP(Player player, String skillType, int XP) {
 | 
			
		||||
        UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()));
 | 
			
		||||
        addMultipliedXP(player, skillType, XP, "UNKNOWN");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Adds XP to the player, calculates for XP Rate only.
 | 
			
		||||
     * </br>
 | 
			
		||||
     * This function is designed for API usage.
 | 
			
		||||
     *
 | 
			
		||||
     * @param player The player to add XP to
 | 
			
		||||
     * @param skillType The skill to add XP to
 | 
			
		||||
     * @param XP The amount of XP to add
 | 
			
		||||
     * @param xpGainReason The reason to gain XP
 | 
			
		||||
     *
 | 
			
		||||
     * @throws InvalidSkillException if the given skill is not valid
 | 
			
		||||
     * @throws InvalidXPGainReasonException if the given xpGainReason is not valid
 | 
			
		||||
     */
 | 
			
		||||
    public static void addMultipliedXP(Player player, String skillType, int XP, String xpGainReason) {
 | 
			
		||||
        UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -129,10 +167,28 @@ public final class ExperienceAPI {
 | 
			
		||||
     *
 | 
			
		||||
     * @throws InvalidSkillException if the given skill is not valid
 | 
			
		||||
     */
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    public static void addModifiedXP(Player player, String skillType, int XP) {
 | 
			
		||||
        addModifiedXP(player, skillType, XP, "UNKNOWN");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Adds XP to the player, calculates for XP Rate and skill modifier.
 | 
			
		||||
     * </br>
 | 
			
		||||
     * This function is designed for API usage.
 | 
			
		||||
     *
 | 
			
		||||
     * @param player The player to add XP to
 | 
			
		||||
     * @param skillType The skill to add XP to
 | 
			
		||||
     * @param XP The amount of XP to add
 | 
			
		||||
     * @param xpGainReason The reason to gain XP
 | 
			
		||||
     *
 | 
			
		||||
     * @throws InvalidSkillException if the given skill is not valid
 | 
			
		||||
     * @throws InvalidXPGainReasonException if the given xpGainReason is not valid
 | 
			
		||||
     */
 | 
			
		||||
    public static void addModifiedXP(Player player, String skillType, int XP, String xpGainReason) {
 | 
			
		||||
        SkillType skill = getSkillType(skillType);
 | 
			
		||||
 | 
			
		||||
        UserManager.getPlayer(player).applyXpGain(skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()));
 | 
			
		||||
        UserManager.getPlayer(player).applyXpGain(skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -165,8 +221,27 @@ public final class ExperienceAPI {
 | 
			
		||||
     *
 | 
			
		||||
     * @throws InvalidSkillException if the given skill is not valid
 | 
			
		||||
     */
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    public static void addXP(Player player, String skillType, int XP) {
 | 
			
		||||
        UserManager.getPlayer(player).beginXpGain(getSkillType(skillType), XP);
 | 
			
		||||
        addXP(player, skillType, XP, "UNKNOWN");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Adds XP to the player, calculates for XP Rate, skill modifiers, perks, child skills,
 | 
			
		||||
     * and party sharing.
 | 
			
		||||
     * </br>
 | 
			
		||||
     * This function is designed for API usage.
 | 
			
		||||
     *
 | 
			
		||||
     * @param player The player to add XP to
 | 
			
		||||
     * @param skillType The skill to add XP to
 | 
			
		||||
     * @param XP The amount of XP to add
 | 
			
		||||
     * @param xpGainReason The reason to gain XP
 | 
			
		||||
     *
 | 
			
		||||
     * @throws InvalidSkillException if the given skill is not valid
 | 
			
		||||
     * @throws InvalidXPGainReasonException if the given xpGainReason is not valid
 | 
			
		||||
     */
 | 
			
		||||
    public static void addXP(Player player, String skillType, int XP, String xpGainReason) {
 | 
			
		||||
        UserManager.getPlayer(player).beginXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -613,4 +688,14 @@ public final class ExperienceAPI {
 | 
			
		||||
 | 
			
		||||
        return skill;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static XPGainReason getXPGainReason(String reason) throws InvalidXPGainReasonException {
 | 
			
		||||
        XPGainReason xpGainReason = XPGainReason.getXPGainReason(reason);
 | 
			
		||||
 | 
			
		||||
        if (xpGainReason == null) {
 | 
			
		||||
            throw new InvalidXPGainReasonException();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return xpGainReason;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user