Reduce code duplication in ExperienceAPI

This commit is contained in:
Kane York 2013-05-30 12:18:13 -05:00
parent 0ba4bc25c7
commit c750ca89b5

View File

@ -15,9 +15,40 @@ import com.gmail.nossr50.util.player.UserManager;
public final class ExperienceAPI { public final class ExperienceAPI {
private 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 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).)
* <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) {
SkillType skill = SkillType.getSkill(skillType);
if (skill == null) return false;
return !skill.isChildSkill();
}
/** /**
* Adds raw XP to the player. * Adds raw XP to the player.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to add XP to * @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 * @throws InvalidSkillException if the given skill is not valid
*/ */
public static void addRawXP(Player player, String skillType, int XP) { public static void addRawXP(Player player, String skillType, int XP) {
SkillType skill = SkillType.getSkill(skillType); UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), XP);
if (skill == null) {
throw new InvalidSkillException();
}
UserManager.getPlayer(player).applyXpGain(skill, XP);
} }
/** /**
* Adds raw XP to an offline player. * Adds raw XP to an offline player.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to add XP to * @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 * @throws InvalidPlayerException if the given player does not exist in the database
*/ */
public static void addRawXPOffline(String playerName, String skillType, int XP) { public static void addRawXPOffline(String playerName, String skillType, int XP) {
SkillType skill = SkillType.getSkill(skillType); addOfflineXP(playerName, getSkillType(skillType), XP);
if (skill == null) {
throw new InvalidSkillException();
}
addOfflineXP(playerName, skill, XP);
} }
/** /**
* Adds XP to the player, calculates for XP Rate only. * Adds XP to the player, calculates for XP Rate only.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to add XP to * @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 * @throws InvalidSkillException if the given skill is not valid
*/ */
public static void addMultipliedXP(Player player, String skillType, int XP) { public static void addMultipliedXP(Player player, String skillType, int XP) {
SkillType skill = SkillType.getSkill(skillType); UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
if (skill == null) {
throw new InvalidSkillException();
}
UserManager.getPlayer(player).applyXpGain(skill, (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
} }
/** /**
* Adds XP to an offline player, calculates for XP Rate only. * Adds XP to an offline player, calculates for XP Rate only.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to add XP to * @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 * @throws InvalidPlayerException if the given player does not exist in the database
*/ */
public static void addMultipliedXPOffline(String playerName, String skillType, int XP) { public static void addMultipliedXPOffline(String playerName, String skillType, int XP) {
SkillType skill = SkillType.getSkill(skillType); addOfflineXP(playerName, getSkillType(skillType), (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
if (skill == null) {
throw new InvalidSkillException();
}
addOfflineXP(playerName, skill, (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
} }
/** /**
* Adds XP to the player, calculates for XP Rate and skill modifier. * Adds XP to the player, calculates for XP Rate and skill modifier.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to add XP to * @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 * @throws InvalidSkillException if the given skill is not valid
*/ */
public static void addModifiedXP(Player player, String skillType, int XP) { public static void addModifiedXP(Player player, String skillType, int XP) {
SkillType skill = SkillType.getSkill(skillType); SkillType skill = getSkillType(skillType);
if (skill == null) {
throw new InvalidSkillException();
}
UserManager.getPlayer(player).applyXpGain(skill, (int) (XP / skill.getXpModifier() * Config.getInstance().getExperienceGainsGlobalMultiplier())); 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. * Adds XP to an offline player, calculates for XP Rate and skill modifier.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to add XP to * @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 * @throws InvalidPlayerException if the given player does not exist in the database
*/ */
public static void addModifiedXPOffline(String playerName, String skillType, int XP) { public static void addModifiedXPOffline(String playerName, String skillType, int XP) {
SkillType skill = SkillType.getSkill(skillType); SkillType skill = getSkillType(skillType);
if (skill == null) {
throw new InvalidSkillException();
}
addOfflineXP(playerName, skill, (int) (XP / skill.getXpModifier() * Config.getInstance().getExperienceGainsGlobalMultiplier())); 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.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to add XP to * @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 * @throws InvalidSkillException if the given skill is not valid
*/ */
public static void addXP(Player player, String skillType, int XP) { public static void addXP(Player player, String skillType, int XP) {
SkillType skill = SkillType.getSkill(skillType); UserManager.getPlayer(player).beginXpGain(getSkillType(skillType), XP);
if (skill == null) {
throw new InvalidSkillException();
}
UserManager.getPlayer(player).beginXpGain(skill, XP);
} }
/** /**
* Get the amount of XP a player has in a specific skill. * Get the amount of XP a player has in a specific skill.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to get XP for * @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 * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
public static int getXP(Player player, String skillType) { public static int getXP(Player player, String skillType) {
SkillType skill = SkillType.getSkill(skillType); return UserManager.getPlayer(player).getProfile().getSkillXpLevel(getNonChildSkillType(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);
} }
/** /**
* Get the amount of XP an offline player has in a specific skill. * Get the amount of XP an offline player has in a specific skill.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to get XP for * @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 * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
public static int getOfflineXP(String playerName, String skillType) { public static int getOfflineXP(String playerName, String skillType) {
SkillType skill = SkillType.getSkill(skillType); return getOfflineProfile(playerName).getSkillXpLevel(getNonChildSkillType(skillType));
if (skill == null) {
throw new InvalidSkillException();
}
if (skill.isChildSkill()) {
throw new UnsupportedOperationException("Child skills do not have XP");
}
return getOfflineProfile(playerName).getSkillXpLevel(skill);
} }
/** /**
* Get the raw amount of XP a player has in a specific skill. * Get the raw amount of XP a player has in a specific skill.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to get XP for * @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 * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
public static float getXPRaw(Player player, String skillType) { public static float getXPRaw(Player player, String skillType) {
SkillType skill = SkillType.getSkill(skillType); return UserManager.getPlayer(player).getProfile().getSkillXpLevelRaw(getNonChildSkillType(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);
} }
/** /**
* Get the raw amount of XP an offline player has in a specific skill. * Get the raw amount of XP an offline player has in a specific skill.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to get XP for * @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 * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
public static float getOfflineXPRaw(String playerName, String skillType) { public static float getOfflineXPRaw(String playerName, String skillType) {
SkillType skill = SkillType.getSkill(skillType); return getOfflineProfile(playerName).getSkillXpLevelRaw(getNonChildSkillType(skillType));
if (skill == null) {
throw new InvalidSkillException();
}
if (skill.isChildSkill()) {
throw new UnsupportedOperationException("Child skills do not have XP");
}
return getOfflineProfile(playerName).getSkillXpLevelRaw(skill);
} }
/** /**
* Get the total amount of XP needed to reach the next level. * Get the total amount of XP needed to reach the next level.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to get the XP amount for * @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 * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
public static int getXPToNextLevel(Player player, String skillType) { public static int getXPToNextLevel(Player player, String skillType) {
SkillType skill = SkillType.getSkill(skillType); return UserManager.getPlayer(player).getProfile().getXpToLevel(getNonChildSkillType(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);
} }
/** /**
* Get the total amount of XP an offline player needs to reach the next level. * Get the total amount of XP an offline player needs to reach the next level.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to get XP for * @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 * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
public static int getOfflineXPToNextLevel(String playerName, String skillType) { public static int getOfflineXPToNextLevel(String playerName, String skillType) {
SkillType skill = SkillType.getSkill(skillType); return getOfflineProfile(playerName).getXpToLevel(getNonChildSkillType(skillType));
if (skill == null) {
throw new InvalidSkillException();
}
if (skill.isChildSkill()) {
throw new UnsupportedOperationException("Child skills do not have XP");
}
return getOfflineProfile(playerName).getXpToLevel(skill);
} }
/** /**
* Get the amount of XP remaining until the next level. * Get the amount of XP remaining until the next level.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to get the XP amount for * @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 * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
public static int getXPRemaining(Player player, String skillType) { public static int getXPRemaining(Player player, String skillType) {
SkillType skill = SkillType.getSkill(skillType); SkillType skill = getNonChildSkillType(skillType);
if (skill == null) {
throw new InvalidSkillException();
}
if (skill.isChildSkill()) {
throw new UnsupportedOperationException("Child skills do not have XP");
}
PlayerProfile profile = UserManager.getPlayer(player).getProfile(); 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. * Get the amount of XP an offline player has left before leveling up.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to get XP for * @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 * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
public static int getOfflineXPRemaining(String playerName, String skillType) { public static int getOfflineXPRemaining(String playerName, String skillType) {
SkillType skill = SkillType.getSkill(skillType); SkillType skill = getNonChildSkillType(skillType);
if (skill == null) {
throw new InvalidSkillException();
}
if (skill.isChildSkill()) {
throw new UnsupportedOperationException("Child skills do not have XP");
}
PlayerProfile profile = getOfflineProfile(playerName); PlayerProfile profile = getOfflineProfile(playerName);
@ -383,7 +300,7 @@ public final class ExperienceAPI {
/** /**
* Add levels to a skill. * Add levels to a skill.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to add levels to * @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 * @throws InvalidSkillException if the given skill is not valid
*/ */
public static void addLevel(Player player, String skillType, int levels) { public static void addLevel(Player player, String skillType, int levels) {
SkillType skill = SkillType.getSkill(skillType); UserManager.getPlayer(player).getProfile().addLevels(getSkillType(skillType), levels);
if (skill == null) {
throw new InvalidSkillException();
}
UserManager.getPlayer(player).getProfile().addLevels(skill, levels);
} }
/** /**
* Add levels to a skill for an offline player. * Add levels to a skill for an offline player.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to add levels to * @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) { public static void addLevelOffline(String playerName, String skillType, int levels) {
PlayerProfile profile = getOfflineProfile(playerName); PlayerProfile profile = getOfflineProfile(playerName);
SkillType skill = SkillType.getSkill(skillType); SkillType skill = getSkillType(skillType);
if (skill == null) {
throw new InvalidSkillException();
}
if (skill.isChildSkill()) { if (skill.isChildSkill()) {
Set<SkillType> parentSkills = FamilyTree.getParents(skill); Set<SkillType> parentSkills = FamilyTree.getParents(skill);
@ -439,7 +346,7 @@ public final class ExperienceAPI {
/** /**
* Get the level a player has in a specific skill. * Get the level a player has in a specific skill.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to get the level for * @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 * @throws InvalidSkillException if the given skill is not valid
*/ */
public static int getLevel(Player player, String skillType) { public static int getLevel(Player player, String skillType) {
SkillType skill = SkillType.getSkill(skillType); return UserManager.getPlayer(player).getProfile().getSkillLevel(getSkillType(skillType));
if (skill == null) {
throw new InvalidSkillException();
}
return UserManager.getPlayer(player).getProfile().getSkillLevel(skill);
} }
/** /**
* Get the level an offline player has in a specific skill. * Get the level an offline player has in a specific skill.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to get the level for * @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 * @throws InvalidPlayerException if the given player does not exist in the database
*/ */
public static int getLevelOffline(String playerName, String skillType) { public static int getLevelOffline(String playerName, String skillType) {
SkillType skill = SkillType.getSkill(skillType); return getOfflineProfile(playerName).getSkillLevel(getSkillType(skillType));
if (skill == null) {
throw new InvalidSkillException();
}
return getOfflineProfile(playerName).getSkillLevel(skill);
} }
/** /**
* Gets the power level of a player. * Gets the power level of a player.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to get the power level for * @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. * Gets the power level of an offline player.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to get the power level for * @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. * Get the level cap of a specific skill.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param skillType The skill to get the level cap for * @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 * @throws InvalidSkillException if the given skill is not valid
*/ */
public static int getLevelCap(String skillType) { public static int getLevelCap(String skillType) {
SkillType skill = SkillType.getSkill(skillType); return Config.getInstance().getLevelCap(getSkillType(skillType));
if (skill == null) {
throw new InvalidSkillException();
}
return Config.getInstance().getLevelCap(skill);
} }
/** /**
* Get the power level cap. * Get the power level cap.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @return the overall power level cap * @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. * Sets the level of a player in a specific skill type.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to set the level of * @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 * @throws InvalidSkillException if the given skill is not valid
*/ */
public static void setLevel(Player player, String skillType, int skillLevel) { public static void setLevel(Player player, String skillType, int skillLevel) {
SkillType skill = SkillType.getSkill(skillType); UserManager.getPlayer(player).getProfile().modifySkill(getSkillType(skillType), skillLevel);
if (skill == null) {
throw new InvalidSkillException();
}
UserManager.getPlayer(player).getProfile().modifySkill(skill, skillLevel);
} }
/** /**
* Sets the level of an offline player in a specific skill type. * Sets the level of an offline player in a specific skill type.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to set the level of * @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 * @throws InvalidPlayerException if the given player does not exist in the database
*/ */
public static void setLevelOffline(String playerName, String skillType, int skillLevel) { public static void setLevelOffline(String playerName, String skillType, int skillLevel) {
SkillType skill = SkillType.getSkill(skillType); getOfflineProfile(playerName).modifySkill(getSkillType(skillType), skillLevel);
if (skill == null) {
throw new InvalidSkillException();
}
getOfflineProfile(playerName).modifySkill(skill, skillLevel);
} }
/** /**
* Sets the XP of a player in a specific skill type. * Sets the XP of a player in a specific skill type.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to set the XP of * @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 * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
public static void setXP(Player player, String skillType, int newValue) { public static void setXP(Player player, String skillType, int newValue) {
SkillType skill = SkillType.getSkill(skillType); UserManager.getPlayer(player).getProfile().setSkillXpLevel(getNonChildSkillType(skillType), newValue);
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);
} }
/** /**
* Sets the XP of an offline player in a specific skill type. * Sets the XP of an offline player in a specific skill type.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to set the XP of * @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 * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
public static void setXPOffline(String playerName, String skillType, int newValue) { public static void setXPOffline(String playerName, String skillType, int newValue) {
SkillType skill = SkillType.getSkill(skillType); getOfflineProfile(playerName).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
if (skill == null) {
throw new InvalidSkillException();
}
if (skill.isChildSkill()) {
throw new UnsupportedOperationException("Child skills do not have XP");
}
getOfflineProfile(playerName).setSkillXpLevel(skill, newValue);
} }
/** /**
* Removes XP from a player in a specific skill type. * Removes XP from a player in a specific skill type.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player The player to change the XP of * @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 * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
public static void removeXP(Player player, String skillType, int xp) { public static void removeXP(Player player, String skillType, int xp) {
SkillType skill = SkillType.getSkill(skillType); UserManager.getPlayer(player).getProfile().removeXp(getNonChildSkillType(skillType), xp);
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);
} }
/** /**
* Removes XP from an offline player in a specific skill type. * Removes XP from an offline player in a specific skill type.
* </br> * <br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param playerName The player to change the XP of * @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 * @throws UnsupportedOperationException if the given skill is a child skill
*/ */
public static void removeXPOffline(String playerName, String skillType, int xp) { public static void removeXPOffline(String playerName, String skillType, int xp) {
SkillType skill = SkillType.getSkill(skillType); getOfflineProfile(playerName).removeXp(getNonChildSkillType(skillType), xp);
if (skill == null) {
throw new InvalidSkillException();
}
if (skill.isChildSkill()) {
throw new UnsupportedOperationException("Child skills do not have XP");
}
getOfflineProfile(playerName).removeXp(skill, xp);
} }
/** // Utility methods follow.
* 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.
*/
private static void addOfflineXP(String playerName, SkillType skill, int XP) { private static void addOfflineXP(String playerName, SkillType skill, int XP) {
PlayerProfile profile = getOfflineProfile(playerName); PlayerProfile profile = getOfflineProfile(playerName);
@ -727,4 +559,24 @@ public final class ExperienceAPI {
return profile; 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;
}
} }