diff --git a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java
index 8217584b9..ec4ea18d9 100644
--- a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java
+++ b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java
@@ -15,9 +15,40 @@ import com.gmail.nossr50.util.player.UserManager;
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.
+ *
+ * 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 SkillType.getSkill(skillType) != null;
+ }
+
+ /**
+ * 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).)
+ *
+ * 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) {
+ SkillType skill = SkillType.getSkill(skillType);
+
+ if (skill == null) return false;
+
+ return !skill.isChildSkill();
+ }
+
/**
* Adds raw XP to the player.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to add XP to
@@ -27,18 +58,12 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid
*/
public static void addRawXP(Player player, String skillType, int XP) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- UserManager.getPlayer(player).applyXpGain(skill, XP);
+ UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), XP);
}
/**
* Adds raw XP to an offline player.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to add XP to
@@ -49,18 +74,12 @@ public final class ExperienceAPI {
* @throws InvalidPlayerException if the given player does not exist in the database
*/
public static void addRawXPOffline(String playerName, String skillType, int XP) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- addOfflineXP(playerName, skill, XP);
+ addOfflineXP(playerName, getSkillType(skillType), XP);
}
/**
* Adds XP to the player, calculates for XP Rate only.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to add XP to
@@ -70,18 +89,12 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid
*/
public static void addMultipliedXP(Player player, String skillType, int XP) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- UserManager.getPlayer(player).applyXpGain(skill, (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
+ UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
}
/**
* Adds XP to an offline player, calculates for XP Rate only.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to add XP to
@@ -92,18 +105,12 @@ public final class ExperienceAPI {
* @throws InvalidPlayerException if the given player does not exist in the database
*/
public static void addMultipliedXPOffline(String playerName, String skillType, int XP) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- addOfflineXP(playerName, skill, (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
+ addOfflineXP(playerName, getSkillType(skillType), (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
}
/**
* Adds XP to the player, calculates for XP Rate and skill modifier.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to add XP to
@@ -113,18 +120,14 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid
*/
public static void addModifiedXP(Player player, String skillType, int XP) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
+ SkillType skill = getSkillType(skillType);
UserManager.getPlayer(player).applyXpGain(skill, (int) (XP / skill.getXpModifier() * Config.getInstance().getExperienceGainsGlobalMultiplier()));
}
/**
* Adds XP to an offline player, calculates for XP Rate and skill modifier.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to add XP to
@@ -135,18 +138,14 @@ public final class ExperienceAPI {
* @throws InvalidPlayerException if the given player does not exist in the database
*/
public static void addModifiedXPOffline(String playerName, String skillType, int XP) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
+ SkillType skill = getSkillType(skillType);
addOfflineXP(playerName, skill, (int) (XP / skill.getXpModifier() * Config.getInstance().getExperienceGainsGlobalMultiplier()));
}
/**
- * Adds XP to the player, calculates for XP Rate, skill modifiers and perks. May be shared with the party.
- *
+ * Adds XP to the player, calculates for XP Rate, skill modifiers, perks, and child skills. May be shared with the party.
+ *
* This function is designed for API usage.
*
* @param player The player to add XP to
@@ -156,18 +155,12 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid
*/
public static void addXP(Player player, String skillType, int XP) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- UserManager.getPlayer(player).beginXpGain(skill, XP);
+ UserManager.getPlayer(player).beginXpGain(getSkillType(skillType), XP);
}
/**
* Get the amount of XP a player has in a specific skill.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to get XP for
@@ -178,22 +171,12 @@ public final class ExperienceAPI {
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static int getXP(Player player, String skillType) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
-
- return UserManager.getPlayer(player).getProfile().getSkillXpLevel(skill);
+ return UserManager.getPlayer(player).getProfile().getSkillXpLevel(getNonChildSkillType(skillType));
}
/**
* Get the amount of XP an offline player has in a specific skill.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to get XP for
@@ -205,22 +188,12 @@ public final class ExperienceAPI {
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static int getOfflineXP(String playerName, String skillType) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
-
- return getOfflineProfile(playerName).getSkillXpLevel(skill);
+ return getOfflineProfile(playerName).getSkillXpLevel(getNonChildSkillType(skillType));
}
/**
* Get the raw amount of XP a player has in a specific skill.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to get XP for
@@ -231,22 +204,12 @@ public final class ExperienceAPI {
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static float getXPRaw(Player player, String skillType) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
-
- return UserManager.getPlayer(player).getProfile().getSkillXpLevelRaw(skill);
+ return UserManager.getPlayer(player).getProfile().getSkillXpLevelRaw(getNonChildSkillType(skillType));
}
/**
* Get the raw amount of XP an offline player has in a specific skill.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to get XP for
@@ -258,22 +221,12 @@ public final class ExperienceAPI {
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static float getOfflineXPRaw(String playerName, String skillType) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
-
- return getOfflineProfile(playerName).getSkillXpLevelRaw(skill);
+ return getOfflineProfile(playerName).getSkillXpLevelRaw(getNonChildSkillType(skillType));
}
/**
* Get the total amount of XP needed to reach the next level.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to get the XP amount for
@@ -284,22 +237,12 @@ public final class ExperienceAPI {
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static int getXPToNextLevel(Player player, String skillType) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
-
- return UserManager.getPlayer(player).getProfile().getXpToLevel(skill);
+ return UserManager.getPlayer(player).getProfile().getXpToLevel(getNonChildSkillType(skillType));
}
/**
* Get the total amount of XP an offline player needs to reach the next level.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to get XP for
@@ -311,22 +254,12 @@ public final class ExperienceAPI {
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static int getOfflineXPToNextLevel(String playerName, String skillType) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
-
- return getOfflineProfile(playerName).getXpToLevel(skill);
+ return getOfflineProfile(playerName).getXpToLevel(getNonChildSkillType(skillType));
}
/**
* Get the amount of XP remaining until the next level.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to get the XP amount for
@@ -337,15 +270,7 @@ public final class ExperienceAPI {
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static int getXPRemaining(Player player, String skillType) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
+ SkillType skill = getNonChildSkillType(skillType);
PlayerProfile profile = UserManager.getPlayer(player).getProfile();
@@ -354,7 +279,7 @@ public final class ExperienceAPI {
/**
* Get the amount of XP an offline player has left before leveling up.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to get XP for
@@ -366,15 +291,7 @@ public final class ExperienceAPI {
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static int getOfflineXPRemaining(String playerName, String skillType) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
+ SkillType skill = getNonChildSkillType(skillType);
PlayerProfile profile = getOfflineProfile(playerName);
@@ -383,7 +300,7 @@ public final class ExperienceAPI {
/**
* Add levels to a skill.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to add levels to
@@ -393,18 +310,12 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid
*/
public static void addLevel(Player player, String skillType, int levels) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- UserManager.getPlayer(player).getProfile().addLevels(skill, levels);
+ UserManager.getPlayer(player).getProfile().addLevels(getSkillType(skillType), levels);
}
/**
* Add levels to a skill for an offline player.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to add levels to
@@ -416,11 +327,7 @@ public final class ExperienceAPI {
*/
public static void addLevelOffline(String playerName, String skillType, int levels) {
PlayerProfile profile = getOfflineProfile(playerName);
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
+ SkillType skill = getSkillType(skillType);
if (skill.isChildSkill()) {
Set parentSkills = FamilyTree.getParents(skill);
@@ -439,7 +346,7 @@ public final class ExperienceAPI {
/**
* Get the level a player has in a specific skill.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to get the level for
@@ -449,18 +356,12 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid
*/
public static int getLevel(Player player, String skillType) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- return UserManager.getPlayer(player).getProfile().getSkillLevel(skill);
+ return UserManager.getPlayer(player).getProfile().getSkillLevel(getSkillType(skillType));
}
/**
* Get the level an offline player has in a specific skill.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to get the level for
@@ -471,18 +372,12 @@ public final class ExperienceAPI {
* @throws InvalidPlayerException if the given player does not exist in the database
*/
public static int getLevelOffline(String playerName, String skillType) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- return getOfflineProfile(playerName).getSkillLevel(skill);
+ return getOfflineProfile(playerName).getSkillLevel(getSkillType(skillType));
}
/**
* Gets the power level of a player.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to get the power level for
@@ -494,7 +389,7 @@ public final class ExperienceAPI {
/**
* Gets the power level of an offline player.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to get the power level for
@@ -515,7 +410,7 @@ public final class ExperienceAPI {
/**
* Get the level cap of a specific skill.
- *
+ *
* This function is designed for API usage.
*
* @param skillType The skill to get the level cap for
@@ -524,18 +419,12 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid
*/
public static int getLevelCap(String skillType) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- return Config.getInstance().getLevelCap(skill);
+ return Config.getInstance().getLevelCap(getSkillType(skillType));
}
/**
* Get the power level cap.
- *
+ *
* This function is designed for API usage.
*
* @return the overall power level cap
@@ -546,7 +435,7 @@ public final class ExperienceAPI {
/**
* Sets the level of a player in a specific skill type.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to set the level of
@@ -556,18 +445,12 @@ public final class ExperienceAPI {
* @throws InvalidSkillException if the given skill is not valid
*/
public static void setLevel(Player player, String skillType, int skillLevel) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- UserManager.getPlayer(player).getProfile().modifySkill(skill, skillLevel);
+ UserManager.getPlayer(player).getProfile().modifySkill(getSkillType(skillType), skillLevel);
}
/**
* Sets the level of an offline player in a specific skill type.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to set the level of
@@ -578,18 +461,12 @@ public final class ExperienceAPI {
* @throws InvalidPlayerException if the given player does not exist in the database
*/
public static void setLevelOffline(String playerName, String skillType, int skillLevel) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- getOfflineProfile(playerName).modifySkill(skill, skillLevel);
+ getOfflineProfile(playerName).modifySkill(getSkillType(skillType), skillLevel);
}
/**
* Sets the XP of a player in a specific skill type.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to set the XP of
@@ -600,22 +477,12 @@ public final class ExperienceAPI {
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static void setXP(Player player, String skillType, int newValue) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
-
- UserManager.getPlayer(player).getProfile().setSkillXpLevel(skill, newValue);
+ UserManager.getPlayer(player).getProfile().setSkillXpLevel(getNonChildSkillType(skillType), newValue);
}
/**
* Sets the XP of an offline player in a specific skill type.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to set the XP of
@@ -627,22 +494,12 @@ public final class ExperienceAPI {
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static void setXPOffline(String playerName, String skillType, int newValue) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
-
- getOfflineProfile(playerName).setSkillXpLevel(skill, newValue);
+ getOfflineProfile(playerName).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
}
/**
* Removes XP from a player in a specific skill type.
- *
+ *
* This function is designed for API usage.
*
* @param player The player to change the XP of
@@ -653,22 +510,12 @@ public final class ExperienceAPI {
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static void removeXP(Player player, String skillType, int xp) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
-
- UserManager.getPlayer(player).getProfile().removeXp(skill, xp);
+ UserManager.getPlayer(player).getProfile().removeXp(getNonChildSkillType(skillType), xp);
}
/**
* Removes XP from an offline player in a specific skill type.
- *
+ *
* This function is designed for API usage.
*
* @param playerName The player to change the XP of
@@ -680,26 +527,11 @@ public final class ExperienceAPI {
* @throws UnsupportedOperationException if the given skill is a child skill
*/
public static void removeXPOffline(String playerName, String skillType, int xp) {
- SkillType skill = SkillType.getSkill(skillType);
-
- if (skill == null) {
- throw new InvalidSkillException();
- }
-
- if (skill.isChildSkill()) {
- throw new UnsupportedOperationException("Child skills do not have XP");
- }
-
- getOfflineProfile(playerName).removeXp(skill, xp);
+ getOfflineProfile(playerName).removeXp(getNonChildSkillType(skillType), xp);
}
- /**
- * Add XP to an offline player.
- *
- * @param playerName The player to check
- * @param skillType The skill to check
- * @param XP The amount of XP to award.
- */
+ // Utility methods follow.
+
private static void addOfflineXP(String playerName, SkillType skill, int XP) {
PlayerProfile profile = getOfflineProfile(playerName);
@@ -727,4 +559,24 @@ public final class ExperienceAPI {
return profile;
}
+
+ private static SkillType getSkillType(String skillType) throws InvalidSkillException {
+ SkillType skill = SkillType.getSkill(skillType);
+
+ if (skill == null) {
+ throw new InvalidSkillException();
+ }
+
+ return skill;
+ }
+
+ private static SkillType getNonChildSkillType(String skillType) throws InvalidSkillException, UnsupportedOperationException {
+ SkillType skill = getSkillType(skillType);
+
+ if (skill.isChildSkill()) {
+ throw new UnsupportedOperationException("Child skills do not have XP");
+ }
+
+ return skill;
+ }
}