12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145 |
- package com.gmail.nossr50.api;
- import com.gmail.nossr50.api.exceptions.*;
- import com.gmail.nossr50.datatypes.experience.FormulaType;
- import com.gmail.nossr50.datatypes.experience.XPGainReason;
- import com.gmail.nossr50.datatypes.experience.XPGainSource;
- import com.gmail.nossr50.datatypes.player.McMMOPlayer;
- import com.gmail.nossr50.datatypes.player.PlayerProfile;
- import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
- import com.gmail.nossr50.mcMMO;
- import com.gmail.nossr50.skills.child.FamilyTree;
- import com.gmail.nossr50.util.player.UserManager;
- import org.bukkit.block.BlockState;
- import org.bukkit.entity.Player;
- import java.util.ArrayList;
- import java.util.Set;
- import java.util.UUID;
- public final class ExperienceAPI {
- private ExperienceAPI() {
- }
- /**
- * Returns whether given string is a valid type of skill suitable for the
- * other API calls in this class.
- * </br>
- * This function is designed for API usage.
- *
- * @param skillType A string that may or may not be a skill
- * @return true if this is a valid mcMMO skill
- */
- public static boolean isValidSkillType(String skillType) {
- return PrimarySkillType.getSkill(skillType) != null;
- }
- /**
- * Grabs the XP multiplier for a player for this specific skill
- * The multiplier will default to 1.0 and will be over written by any XP perks
- *
- * @param player target player
- * @param primarySkillType target skill
- * @return this players personal XP rate for target PrimarySkillType
- */
- public float getPlayersPersonalXPRate(McMMOPlayer player, PrimarySkillType primarySkillType) {
- //First check if the player has ANY of the custom perks
- return player.getPlayerSpecificXPMult(primarySkillType);
- }
- /**
- * Returns whether the given skill type string is both valid and not a
- * child skill. (Child skills have no XP of their own, and their level is
- * derived from the parent(s).)
- * </br>
- * This function is designed for API usage.
- *
- * @param skillType the skill to check
- * @return true if this is a valid, non-child mcMMO skill
- */
- public static boolean isNonChildSkill(String skillType) {
- PrimarySkillType skill = PrimarySkillType.getSkill(skillType);
- return skill != null && !skill.isChildSkill();
- }
- @Deprecated
- public static void addRawXP(Player player, String skillType, int XP) {
- addRawXP(player, skillType, (float) XP);
- }
- /**
- * 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
- * @throws InvalidSkillException if the given skill is not valid
- */
- @Deprecated
- public static void addRawXP(Player player, String skillType, float 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) {
- addRawXP(player, skillType, XP, xpGainReason, false);
- }
- /**
- * 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
- * @param isUnshared true if the XP cannot be shared with party members
- * @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, boolean isUnshared) {
- if (isUnshared) {
- getPlayer(player).beginUnsharedXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason), XPGainSource.CUSTOM);
- return;
- }
- getPlayer(player).applyXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason), XPGainSource.CUSTOM);
- }
- /**
- * Adds raw XP to an offline player.
- * </br>
- * This function is designed for API usage.
- *
- * @deprecated We're using float for our XP values now
- * replaced by {@link #addRawXPOffline(String playerName, String skillType, float XP)}
- */
- @Deprecated
- public static void addRawXPOffline(String playerName, String skillType, int XP) {
- addRawXPOffline(playerName, skillType, (float) XP);
- }
- /**
- * Adds raw XP to an offline player.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to add XP to
- * @param skillType The skill to add XP to
- * @param XP The amount of XP to add
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @deprecated We're using uuids to get an offline player
- * replaced by {@link #addRawXPOffline(UUID uuid, String skillType, float XP)}
- */
- @Deprecated
- public static void addRawXPOffline(String playerName, String skillType, float XP) {
- addOfflineXP(playerName, getSkillType(skillType), (int) Math.floor(XP));
- }
- /**
- * Adds raw XP to an offline player.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The UUID of player to add XP to
- * @param skillType The skill to add XP to
- * @param XP The amount of XP to add
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- public static void addRawXPOffline(UUID uuid, String skillType, float XP) {
- addOfflineXP(uuid, getSkillType(skillType), (int) Math.floor(XP));
- }
- /**
- * 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
- * @throws InvalidSkillException if the given skill is not valid
- */
- @Deprecated
- public static void addMultipliedXP(Player player, String skillType, int XP) {
- 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) {
- getPlayer(player).applyXpGain(getSkillType(skillType), (int) (XP * mcMMO.getDynamicSettingsManager().getExperienceMapManager().getGlobalXpMult()), getXPGainReason(xpGainReason), XPGainSource.CUSTOM);
- }
- /**
- * Adds XP to an offline player, calculates for XP Rate only.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to add XP to
- * @param skillType The skill to add XP to
- * @param XP The amount of XP to add
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- @Deprecated
- public static void addMultipliedXPOffline(String playerName, String skillType, int XP) {
- addOfflineXP(playerName, getSkillType(skillType), (int) (XP * mcMMO.getDynamicSettingsManager().getExperienceMapManager().getGlobalXpMult()));
- }
- /**
- * 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
- * @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) {
- addModifiedXP(player, skillType, XP, xpGainReason, false);
- }
- /**
- * 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
- * @param isUnshared true if the XP cannot be shared with party members
- * @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, boolean isUnshared) {
- PrimarySkillType skill = getSkillType(skillType);
- if (isUnshared) {
- getPlayer(player).beginUnsharedXpGain(skill, (int) (XP / skill.getXpModifier() * mcMMO.getDynamicSettingsManager().getExperienceMapManager().getGlobalXpMult()), getXPGainReason(xpGainReason), XPGainSource.CUSTOM);
- return;
- }
- getPlayer(player).applyXpGain(skill, (int) (XP / skill.getXpModifier() * mcMMO.getDynamicSettingsManager().getExperienceMapManager().getGlobalXpMult()), getXPGainReason(xpGainReason), XPGainSource.CUSTOM);
- }
- /**
- * Adds XP to an offline player, calculates for XP Rate and skill modifier.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to add XP to
- * @param skillType The skill to add XP to
- * @param XP The amount of XP to add
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- @Deprecated
- public static void addModifiedXPOffline(String playerName, String skillType, int XP) {
- PrimarySkillType skill = getSkillType(skillType);
- addOfflineXP(playerName, skill, (int) (XP / skill.getXpModifier() * mcMMO.getDynamicSettingsManager().getExperienceMapManager().getGlobalXpMult()));
- }
- /**
- * 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
- * @throws InvalidSkillException if the given skill is not valid
- */
- @Deprecated
- public static void addXP(Player player, String skillType, int 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) {
- addXP(player, skillType, XP, xpGainReason, false);
- }
- /**
- * 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
- * @param isUnshared true if the XP cannot be shared with party members
- * @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, boolean isUnshared) {
- if (isUnshared) {
- getPlayer(player).beginUnsharedXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason), XPGainSource.CUSTOM);
- return;
- }
- getPlayer(player).beginXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason), XPGainSource.CUSTOM);
- }
- /**
- * Get the amount of XP a player has in a specific skill.
- * </br>
- * This function is designed for API usage.
- *
- * @param player The player to get XP for
- * @param skillType The skill to get XP for
- * @return the amount of XP in a given skill
- * @throws InvalidSkillException if the given skill is not valid
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static int getXP(Player player, String skillType) {
- return getPlayer(player).getSkillXpLevel(getNonChildSkillType(skillType));
- }
- /**
- * Get the amount of XP an offline player has in a specific skill.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to get XP for
- * @param skillType The skill to get XP for
- * @return the amount of XP in a given skill
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- @Deprecated
- public static int getOfflineXP(String playerName, String skillType) {
- return getOfflineProfile(playerName).getSkillXpLevel(getNonChildSkillType(skillType));
- }
- /**
- * Get the amount of XP an offline player has in a specific skill.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The player to get XP for
- * @param skillType The skill to get XP for
- * @return the amount of XP in a given skill
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static int getOfflineXP(UUID uuid, String skillType) {
- return getOfflineProfile(uuid).getSkillXpLevel(getNonChildSkillType(skillType));
- }
- /**
- * Get the raw amount of XP a player has in a specific skill.
- * </br>
- * This function is designed for API usage.
- *
- * @param player The player to get XP for
- * @param skillType The skill to get XP for
- * @return the amount of XP in a given skill
- * @throws InvalidSkillException if the given skill is not valid
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static float getXPRaw(Player player, String skillType) {
- return getPlayer(player).getSkillXpLevelRaw(getNonChildSkillType(skillType));
- }
- /**
- * Get the raw amount of XP an offline player has in a specific skill.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to get XP for
- * @param skillType The skill to get XP for
- * @return the amount of XP in a given skill
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- @Deprecated
- public static float getOfflineXPRaw(String playerName, String skillType) {
- return getOfflineProfile(playerName).getSkillXpLevelRaw(getNonChildSkillType(skillType));
- }
- /**
- * Get the raw amount of XP an offline player has in a specific skill.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The player to get XP for
- * @param skillType The skill to get XP for
- * @return the amount of XP in a given skill
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static float getOfflineXPRaw(UUID uuid, String skillType) {
- return getOfflineProfile(uuid).getSkillXpLevelRaw(getNonChildSkillType(skillType));
- }
- /**
- * Get the total amount of XP needed to reach the next level.
- * </br>
- * This function is designed for API usage.
- *
- * @param player The player to get the XP amount for
- * @param skillType The skill to get the XP amount for
- * @return the total amount of XP needed to reach the next level
- * @throws InvalidSkillException if the given skill is not valid
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static int getXPToNextLevel(Player player, String skillType) {
- return getPlayer(player).getXpToLevel(getNonChildSkillType(skillType));
- }
- /**
- * Get the total amount of XP an offline player needs to reach the next level.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to get XP for
- * @param skillType The skill to get XP for
- * @return the total amount of XP needed to reach the next level
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- @Deprecated
- public static int getOfflineXPToNextLevel(String playerName, String skillType) {
- return getOfflineProfile(playerName).getXpToLevel(getNonChildSkillType(skillType));
- }
- /**
- * Get the total amount of XP an offline player needs to reach the next level.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The player to get XP for
- * @param skillType The skill to get XP for
- * @return the total amount of XP needed to reach the next level
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static int getOfflineXPToNextLevel(UUID uuid, String skillType) {
- return getOfflineProfile(uuid).getXpToLevel(getNonChildSkillType(skillType));
- }
- /**
- * Get the amount of XP remaining until the next level.
- * </br>
- * This function is designed for API usage.
- *
- * @param player The player to get the XP amount for
- * @param skillType The skill to get the XP amount for
- * @return the amount of XP remaining until the next level
- * @throws InvalidSkillException if the given skill is not valid
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static int getXPRemaining(Player player, String skillType) {
- PrimarySkillType skill = getNonChildSkillType(skillType);
- PlayerProfile profile = getPlayer(player).getProfile();
- return profile.getXpToLevel(skill) - profile.getSkillXpLevel(skill);
- }
- /**
- * Get the amount of XP an offline player has left before leveling up.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to get XP for
- * @param skillType The skill to get XP for
- * @return the amount of XP needed to reach the next level
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- @Deprecated
- public static int getOfflineXPRemaining(String playerName, String skillType) {
- PrimarySkillType skill = getNonChildSkillType(skillType);
- PlayerProfile profile = getOfflineProfile(playerName);
- return profile.getXpToLevel(skill) - profile.getSkillXpLevel(skill);
- }
- /**
- * Get the amount of XP an offline player has left before leveling up.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The player to get XP for
- * @param skillType The skill to get XP for
- * @return the amount of XP needed to reach the next level
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static float getOfflineXPRemaining(UUID uuid, String skillType) {
- PrimarySkillType skill = getNonChildSkillType(skillType);
- PlayerProfile profile = getOfflineProfile(uuid);
- return profile.getXpToLevel(skill) - profile.getSkillXpLevelRaw(skill);
- }
- /**
- * Add levels to a skill.
- * </br>
- * This function is designed for API usage.
- *
- * @param player The player to add levels to
- * @param skillType Type of skill to add levels to
- * @param levels Number of levels to add
- * @throws InvalidSkillException if the given skill is not valid
- */
- public static void addLevel(Player player, String skillType, int levels) {
- getPlayer(player).addLevels(getSkillType(skillType), levels);
- }
- /**
- * Add levels to a skill for an offline player.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to add levels to
- * @param skillType Type of skill to add levels to
- * @param levels Number of levels to add
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- @Deprecated
- public static void addLevelOffline(String playerName, String skillType, int levels) {
- PlayerProfile profile = getOfflineProfile(playerName);
- PrimarySkillType skill = getSkillType(skillType);
- if (skill.isChildSkill()) {
- Set<PrimarySkillType> parentSkills = FamilyTree.getParents(skill);
- for (PrimarySkillType parentSkill : parentSkills) {
- profile.addLevels(parentSkill, (levels / parentSkills.size()));
- }
- profile.scheduleAsyncSave();
- return;
- }
- profile.addLevels(skill, levels);
- profile.scheduleAsyncSave();
- }
- /**
- * Add levels to a skill for an offline player.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The player to add levels to
- * @param skillType Type of skill to add levels to
- * @param levels Number of levels to add
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- public static void addLevelOffline(UUID uuid, String skillType, int levels) {
- PlayerProfile profile = getOfflineProfile(uuid);
- PrimarySkillType skill = getSkillType(skillType);
- if (skill.isChildSkill()) {
- Set<PrimarySkillType> parentSkills = FamilyTree.getParents(skill);
- for (PrimarySkillType parentSkill : parentSkills) {
- profile.addLevels(parentSkill, (levels / parentSkills.size()));
- }
- profile.scheduleAsyncSave();
- return;
- }
- profile.addLevels(skill, levels);
- profile.scheduleAsyncSave();
- }
- /**
- * Get the level a player has in a specific skill.
- * </br>
- * This function is designed for API usage.
- *
- * @param player The player to get the level for
- * @param skillType The skill to get the level for
- * @return the level of a given skill
- * @throws InvalidSkillException if the given skill is not valid
- */
- public static int getLevel(Player player, String skillType) {
- return getPlayer(player).getSkillLevel(getSkillType(skillType));
- }
- /**
- * Get the level an offline player has in a specific skill.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to get the level for
- * @param skillType The skill to get the level for
- * @return the level of a given skill
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- @Deprecated
- public static int getLevelOffline(String playerName, String skillType) {
- return getOfflineProfile(playerName).getSkillLevel(getSkillType(skillType));
- }
- /**
- * Get the level an offline player has in a specific skill.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The player to get the level for
- * @param skillType The skill to get the level for
- * @return the level of a given skill
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- public static int getLevelOffline(UUID uuid, String skillType) {
- return getOfflineProfile(uuid).getSkillLevel(getSkillType(skillType));
- }
- /**
- * Gets the power level of a player.
- * </br>
- * This function is designed for API usage.
- *
- * @param player The player to get the power level for
- * @return the power level of the player
- */
- public static int getPowerLevel(Player player) {
- return getPlayer(player).getPowerLevel();
- }
- /**
- * Gets the power level of an offline player.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to get the power level for
- * @return the power level of the player
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- @Deprecated
- public static int getPowerLevelOffline(String playerName) {
- int powerLevel = 0;
- PlayerProfile profile = getOfflineProfile(playerName);
- for (PrimarySkillType type : PrimarySkillType.NON_CHILD_SKILLS) {
- powerLevel += profile.getSkillLevel(type);
- }
- return powerLevel;
- }
- /**
- * Gets the power level of an offline player.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The player to get the power level for
- * @return the power level of the player
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- public static int getPowerLevelOffline(UUID uuid) {
- int powerLevel = 0;
- PlayerProfile profile = getOfflineProfile(uuid);
- for (PrimarySkillType type : PrimarySkillType.NON_CHILD_SKILLS) {
- powerLevel += profile.getSkillLevel(type);
- }
- return powerLevel;
- }
- /**
- * Get the level cap of a specific skill.
- * </br>
- * This function is designed for API usage.
- *
- * @param skillType The skill to get the level cap for
- * @return the level cap of a given skill
- * @throws InvalidSkillException if the given skill is not valid
- */
- public static int getLevelCap(String skillType) {
- return mcMMO.getPlayerLevelingSettings().getLevelCap(getSkillType(skillType));
- }
- /**
- * Get the level cap of a specific skill.
- * </br>
- * This function is designed for API usage.
- *
- * @param skillType The skill to get the level cap for
- * @return the level cap of a given skill
- * @throws InvalidSkillException if the given skill is not valid
- */
- public static int getLevelCap(PrimarySkillType skillType) {
- return mcMMO.getPlayerLevelingSettings().getLevelCap(skillType);
- }
- /**
- * Checks whether or not a specific skill is level capped
- *
- * @param skillType target skill
- * @return true if the skill has a level cap
- */
- public static boolean isSkillLevelCapped(PrimarySkillType skillType) {
- return mcMMO.getPlayerLevelingSettings().isLevelCapEnabled(skillType);
- }
- /**
- * Get the power level cap.
- * </br>
- * This function is designed for API usage.
- *
- * @return the overall power level cap
- */
- public static int getPowerLevelCap() {
- return mcMMO.getPlayerLevelingSettings().getConfigSectionLevelCaps().getPowerLevel().getLevelCap();
- }
- /**
- * Get the position on the leaderboard of a player.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The name of the player to check
- * @param skillType The skill to check
- * @return the position on the leaderboard
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- @Deprecated
- public static int getPlayerRankSkill(String playerName, String skillType) {
- return mcMMO.getDatabaseManager().readRank(mcMMO.p.getServer().getOfflinePlayer(playerName).getName()).get(getNonChildSkillType(skillType));
- }
- /**
- * Get the position on the leaderboard of a player.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The name of the player to check
- * @param skillType The skill to check
- * @return the position on the leaderboard
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static int getPlayerRankSkill(UUID uuid, String skillType) {
- return mcMMO.getDatabaseManager().readRank(mcMMO.p.getServer().getOfflinePlayer(uuid).getName()).get(getNonChildSkillType(skillType));
- }
- /**
- * Get the position on the power level leaderboard of a player.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The name of the player to check
- * @return the position on the power level leaderboard
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- @Deprecated
- public static int getPlayerRankOverall(String playerName) {
- return mcMMO.getDatabaseManager().readRank(mcMMO.p.getServer().getOfflinePlayer(playerName).getName()).get(null);
- }
- /**
- * Get the position on the power level leaderboard of a player.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The name of the player to check
- * @return the position on the power level leaderboard
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- public static int getPlayerRankOverall(UUID uuid) {
- return mcMMO.getDatabaseManager().readRank(mcMMO.p.getServer().getOfflinePlayer(uuid).getName()).get(null);
- }
- /**
- * Sets the level of a player in a specific skill type.
- * </br>
- * This function is designed for API usage.
- *
- * @param player The player to set the level of
- * @param skillType The skill to set the level for
- * @param skillLevel The value to set the level to
- * @throws InvalidSkillException if the given skill is not valid
- */
- public static void setLevel(Player player, String skillType, int skillLevel) {
- getPlayer(player).modifySkill(getSkillType(skillType), skillLevel);
- }
- /**
- * Sets the level of an offline player in a specific skill type.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to set the level of
- * @param skillType The skill to set the level for
- * @param skillLevel The value to set the level to
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- @Deprecated
- public static void setLevelOffline(String playerName, String skillType, int skillLevel) {
- getOfflineProfile(playerName).modifySkill(getSkillType(skillType), skillLevel);
- }
- /**
- * Sets the level of an offline player in a specific skill type.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The player to set the level of
- * @param skillType The skill to set the level for
- * @param skillLevel The value to set the level to
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- */
- public static void setLevelOffline(UUID uuid, String skillType, int skillLevel) {
- getOfflineProfile(uuid).modifySkill(getSkillType(skillType), skillLevel);
- }
- /**
- * Sets the XP of a player in a specific skill type.
- * </br>
- * This function is designed for API usage.
- *
- * @param player The player to set the XP of
- * @param skillType The skill to set the XP for
- * @param newValue The value to set the XP to
- * @throws InvalidSkillException if the given skill is not valid
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static void setXP(Player player, String skillType, int newValue) {
- getPlayer(player).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
- }
- /**
- * Sets the XP of an offline player in a specific skill type.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to set the XP of
- * @param skillType The skill to set the XP for
- * @param newValue The value to set the XP to
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- @Deprecated
- public static void setXPOffline(String playerName, String skillType, int newValue) {
- getOfflineProfile(playerName).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
- }
- /**
- * Sets the XP of an offline player in a specific skill type.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The player to set the XP of
- * @param skillType The skill to set the XP for
- * @param newValue The value to set the XP to
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static void setXPOffline(UUID uuid, String skillType, int newValue) {
- getOfflineProfile(uuid).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
- }
- /**
- * Removes XP from a player in a specific skill type.
- * </br>
- * This function is designed for API usage.
- *
- * @param player The player to change the XP of
- * @param skillType The skill to change the XP for
- * @param xp The amount of XP to remove
- * @throws InvalidSkillException if the given skill is not valid
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static void removeXP(Player player, String skillType, int xp) {
- getPlayer(player).removeXp(getNonChildSkillType(skillType), xp);
- }
- /**
- * Removes XP from an offline player in a specific skill type.
- * </br>
- * This function is designed for API usage.
- *
- * @param playerName The player to change the XP of
- * @param skillType The skill to change the XP for
- * @param xp The amount of XP to remove
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- @Deprecated
- public static void removeXPOffline(String playerName, String skillType, int xp) {
- getOfflineProfile(playerName).removeXp(getNonChildSkillType(skillType), xp);
- }
- /**
- * Removes XP from an offline player in a specific skill type.
- * </br>
- * This function is designed for API usage.
- *
- * @param uuid The player to change the XP of
- * @param skillType The skill to change the XP for
- * @param xp The amount of XP to remove
- * @throws InvalidSkillException if the given skill is not valid
- * @throws InvalidPlayerException if the given player does not exist in the database
- * @throws UnsupportedOperationException if the given skill is a child skill
- */
- public static void removeXPOffline(UUID uuid, String skillType, int xp) {
- getOfflineProfile(uuid).removeXp(getNonChildSkillType(skillType), xp);
- }
- /**
- * Check how much XP is needed for a specific level with the selected level curve.
- * </br>
- * This function is designed for API usage.
- *
- * @param level The level to get the amount of XP for
- * @throws InvalidFormulaTypeException if the given formulaType is not valid
- */
- public static int getXpNeededToLevel(int level) {
- return mcMMO.getFormulaManager().getCachedXpToLevel(level, mcMMO.getConfigManager().getConfigLeveling().getFormulaType());
- }
- /**
- * Check how much XP is needed for a specific level with the provided level curve.
- * </br>
- * This function is designed for API usage.
- *
- * @param level The level to get the amount of XP for
- * @param formulaType The formula type to get the amount of XP for
- * @throws InvalidFormulaTypeException if the given formulaType is not valid
- */
- public static int getXpNeededToLevel(int level, String formulaType) {
- return mcMMO.getFormulaManager().getCachedXpToLevel(level, getFormulaType(formulaType));
- }
- /**
- * Will add the appropriate type of XP from the block to the player based on the material of the blocks given
- *
- * @param blockStates the blocks to reward XP for
- * @param mcMMOPlayer the target player
- */
- public static void addXpFromBlocks(ArrayList<BlockState> blockStates, McMMOPlayer mcMMOPlayer) {
- for (BlockState bs : blockStates) {
- for (PrimarySkillType skillType : PrimarySkillType.values()) {
- if (mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, bs.getType()) > 0) {
- mcMMOPlayer.applyXpGain(skillType, mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, bs.getType()), XPGainReason.PVE, XPGainSource.SELF);
- }
- }
- }
- }
- /**
- * Will add the appropriate type of XP from the block to the player based on the material of the blocks given if it matches the given skillType
- *
- * @param blockStates the blocks to reward XP for
- * @param mcMMOPlayer the target player
- * @param skillType target primary skill
- */
- public static void addXpFromBlocksBySkill(ArrayList<BlockState> blockStates, McMMOPlayer mcMMOPlayer, PrimarySkillType skillType) {
- for (BlockState bs : blockStates) {
- if (mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, bs.getType()) > 0) {
- mcMMOPlayer.applyXpGain(skillType, mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, bs.getType()), XPGainReason.PVE, XPGainSource.SELF);
- }
- }
- }
- /**
- * Will add the appropriate type of XP from the block to the player based on the material of the blocks given
- *
- * @param blockState The target blockstate
- * @param mcMMOPlayer The target player
- */
- public static void addXpFromBlock(BlockState blockState, McMMOPlayer mcMMOPlayer) {
- for (PrimarySkillType skillType : PrimarySkillType.values()) {
- if (mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, blockState.getType()) > 0) {
- mcMMOPlayer.applyXpGain(skillType, mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, blockState.getType()), XPGainReason.PVE, XPGainSource.SELF);
- }
- }
- }
- /**
- * Will add the appropriate type of XP from the block to the player based on the material of the blocks given if it matches the given skillType
- *
- * @param blockState The target blockstate
- * @param mcMMOPlayer The target player
- * @param skillType target primary skill
- */
- public static void addXpFromBlockBySkill(BlockState blockState, McMMOPlayer mcMMOPlayer, PrimarySkillType skillType) {
- if (mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, blockState.getType()) > 0) {
- mcMMOPlayer.applyXpGain(skillType, mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, blockState.getType()), XPGainReason.PVE, XPGainSource.SELF);
- }
- }
- // Utility methods follow.
- private static void addOfflineXP(UUID playerUniqueId, PrimarySkillType skill, int XP) {
- PlayerProfile profile = getOfflineProfile(playerUniqueId);
- profile.addXp(skill, XP);
- profile.save();
- }
- @Deprecated
- private static void addOfflineXP(String playerName, PrimarySkillType skill, int XP) {
- PlayerProfile profile = getOfflineProfile(playerName);
- profile.addXp(skill, XP);
- profile.scheduleAsyncSave();
- }
- private static PlayerProfile getOfflineProfile(UUID uuid) {
- PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(uuid);
- if (!profile.isLoaded()) {
- throw new InvalidPlayerException();
- }
- return profile;
- }
- @Deprecated
- private static PlayerProfile getOfflineProfile(String playerName) {
- UUID uuid = mcMMO.p.getServer().getOfflinePlayer(playerName).getUniqueId();
- PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(uuid);
- if (!profile.isLoaded()) {
- throw new InvalidPlayerException();
- }
- return profile;
- }
- private static PrimarySkillType getSkillType(String skillType) throws InvalidSkillException {
- PrimarySkillType skill = PrimarySkillType.getSkill(skillType);
- if (skill == null) {
- throw new InvalidSkillException(skillType);
- }
- return skill;
- }
- private static PrimarySkillType getNonChildSkillType(String skillType) throws InvalidSkillException, UnsupportedOperationException {
- PrimarySkillType skill = getSkillType(skillType);
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
- return skill;
- }
- private static XPGainReason getXPGainReason(String reason) throws InvalidXPGainReasonException {
- XPGainReason xpGainReason = XPGainReason.getXPGainReason(reason);
- if (xpGainReason == null) {
- throw new InvalidXPGainReasonException();
- }
- return xpGainReason;
- }
- private static FormulaType getFormulaType(String formula) throws InvalidFormulaTypeException {
- FormulaType formulaType = FormulaType.getFormulaType(formula);
- if (formulaType == null) {
- throw new InvalidFormulaTypeException();
- }
- return formulaType;
- }
- /**
- * @param player target player
- * @return McMMOPlayer for that player if the profile is loaded, otherwise null
- * @throws McMMOPlayerNotFoundException
- * @deprecated Use UserManager::getPlayer(Player player) instead
- */
- @Deprecated
- private static McMMOPlayer getPlayer(Player player) throws McMMOPlayerNotFoundException {
- if (!UserManager.hasPlayerDataKey(player)) {
- throw new McMMOPlayerNotFoundException(player);
- }
- return UserManager.getPlayer(player);
- }
- }
|