Reduce code duplication in ExperienceAPI

This commit is contained in:
Kane York 2013-05-30 12:18:13 -05:00 committed by GJ
parent 0ba4bc25c7
commit 385f8ca0b3

View File

@ -15,6 +15,37 @@ 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>
@ -27,13 +58,7 @@ 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);
} }
/** /**
@ -49,13 +74,7 @@ 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);
} }
/** /**
@ -70,13 +89,7 @@ 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()));
} }
/** /**
@ -92,13 +105,7 @@ 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()));
} }
/** /**
@ -113,11 +120,7 @@ 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()));
} }
@ -135,17 +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, child skills,
* and party sharing.
* </br> * </br>
* This function is designed for API usage. * This function is designed for API usage.
* *
@ -156,13 +156,7 @@ 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);
} }
/** /**
@ -178,17 +172,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 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);
} }
/** /**
@ -205,17 +189,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 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);
} }
/** /**
@ -231,17 +205,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 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);
} }
/** /**
@ -258,17 +222,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 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);
} }
/** /**
@ -284,17 +238,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 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);
} }
/** /**
@ -311,17 +255,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 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);
} }
/** /**
@ -337,15 +271,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();
@ -366,15 +292,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);
@ -393,13 +311,7 @@ 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);
} }
/** /**
@ -416,11 +328,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);
@ -449,13 +357,7 @@ 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);
} }
/** /**
@ -471,13 +373,7 @@ 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);
} }
/** /**
@ -524,13 +420,7 @@ 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);
} }
/** /**
@ -556,13 +446,7 @@ 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);
} }
/** /**
@ -578,13 +462,7 @@ 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);
} }
/** /**
@ -600,17 +478,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 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);
} }
/** /**
@ -627,17 +495,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 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);
} }
/** /**
@ -653,17 +511,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 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);
} }
/** /**
@ -680,26 +528,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 +560,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;
}
} }