Browse Source

ExperienceAPI methods will now throw InvalidSkillException if the skill
name passed in is invalid

GJ 12 years ago
parent
commit
7525de1a72

+ 1 - 0
Changelog.txt

@@ -16,6 +16,7 @@ Version 1.4.06-dev
  = Fixed bug where /party chat was not working
  ! Changed Chimaera Wing's recipe result to use the ingredient Material
  ! Players will no longer pickup items to their hotbar while using Unarmed
+ ! ExperienceAPI methods will now throw InvalidSkillException if the skill name passed in is invalid.
 
 Version 1.4.05
  + Added option to allow refreshing of chunks after block-breaking abilities. (Disabled by default)

+ 210 - 47
src/main/java/com/gmail/nossr50/api/ExperienceAPI.java

@@ -21,9 +21,17 @@ public final class ExperienceAPI {
      * @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
      */
-    public static void addRawXP(Player player, String skillType, int XP) {
-        UserManager.getPlayer(player).applyXpGain(SkillType.getSkill(skillType), XP);
+    public static void addRawXP(Player player, String skillType, int XP) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        UserManager.getPlayer(player).applyXpGain(skill, XP);
     }
 
     /**
@@ -35,10 +43,17 @@ public final class ExperienceAPI {
      * @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(String playerName, String skillType, int XP) {
-        addOfflineXP(playerName, skillType, XP);
+    public static void addRawXPOffline(String playerName, String skillType, int XP) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        addOfflineXP(playerName, skill, XP);
     }
 
     /**
@@ -49,9 +64,17 @@ public final class ExperienceAPI {
      * @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
      */
-    public static void addMultipliedXP(Player player, String skillType, int XP) {
-        UserManager.getPlayer(player).applyXpGain(SkillType.getSkill(skillType), (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
+    public static void addMultipliedXP(Player player, String skillType, int XP) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        UserManager.getPlayer(player).applyXpGain(skill, (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
     }
 
     /**
@@ -63,10 +86,17 @@ public final class ExperienceAPI {
      * @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 addMultipliedXPOffline(String playerName, String skillType, int XP) {
-        addOfflineXP(playerName, skillType, (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
+    public static void addMultipliedXPOffline(String playerName, String skillType, int XP) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        addOfflineXP(playerName, skill, (int) (XP * Config.getInstance().getExperienceGainsGlobalMultiplier()));
     }
 
     /**
@@ -77,10 +107,16 @@ public final class ExperienceAPI {
      * @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
      */
-    public static void addModifiedXP(Player player, String skillType, int XP) {
+    public static void addModifiedXP(Player player, String skillType, int XP) throws InvalidSkillException {
         SkillType skill = SkillType.getSkill(skillType);
 
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
         UserManager.getPlayer(player).applyXpGain(skill, (int) (XP  / skill.getXpModifier() * Config.getInstance().getExperienceGainsGlobalMultiplier()));
     }
 
@@ -93,10 +129,17 @@ public final class ExperienceAPI {
      * @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 addModifiedXPOffline(String playerName, String skillType, int XP) {
-        addOfflineXP(playerName, skillType, (int) (XP / SkillType.getSkill(skillType).getXpModifier() * Config.getInstance().getExperienceGainsGlobalMultiplier()));
+    public static void addModifiedXPOffline(String playerName, String skillType, int XP) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        addOfflineXP(playerName, skill, (int) (XP / skill.getXpModifier() * Config.getInstance().getExperienceGainsGlobalMultiplier()));
     }
 
     /**
@@ -107,9 +150,17 @@ public final class ExperienceAPI {
      * @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
      */
-    public static void addXP(Player player, String skillType, int XP) {
-        UserManager.getPlayer(player).beginXpGain(SkillType.getSkill(skillType), XP);
+    public static void addXP(Player player, String skillType, int XP) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        UserManager.getPlayer(player).beginXpGain(skill, XP);
     }
 
     /**
@@ -120,9 +171,17 @@ public final class ExperienceAPI {
      * @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
      */
-    public static int getXP(Player player, String skillType) {
-        return UserManager.getPlayer(player).getProfile().getSkillXpLevel(SkillType.getSkill(skillType));
+    public static int getXP(Player player, String skillType) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        return UserManager.getPlayer(player).getProfile().getSkillXpLevel(skill);
     }
 
     /**
@@ -133,10 +192,18 @@ public final class ExperienceAPI {
      * @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
      */
-    public static int getOfflineXP(String playerName, String skillType) {
-        return getOfflineProfile(playerName).getSkillXpLevel(SkillType.getSkill(skillType));
+    public static int getOfflineXP(String playerName, String skillType) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        return getOfflineProfile(playerName).getSkillXpLevel(skill);
     }
 
     /**
@@ -147,9 +214,17 @@ public final class ExperienceAPI {
      * @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 left before leveling up a specifc skill
+     *
+     * @throws InvalidSkillException if the given skill is not valid
      */
-    public static int getXPToNextLevel(Player player, String skillType) {
-        return UserManager.getPlayer(player).getProfile().getXpToLevel(SkillType.getSkill(skillType));
+    public static int getXPToNextLevel(Player player, String skillType) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        return UserManager.getPlayer(player).getProfile().getXpToLevel(skill);
     }
 
     /**
@@ -160,10 +235,18 @@ public final class ExperienceAPI {
      * @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
      */
-    public static int getOfflineXPToNextLevel(String playerName, String skillType) {
-        return getOfflineProfile(playerName).getXpToLevel(SkillType.getSkill(skillType));
+    public static int getOfflineXPToNextLevel(String playerName, String skillType) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        return getOfflineProfile(playerName).getXpToLevel(skill);
     }
 
     /**
@@ -174,9 +257,17 @@ public final class ExperienceAPI {
      * @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) {
-        UserManager.getPlayer(player).getProfile().addLevels(SkillType.getSkill(skillType), levels);
+    public static void addLevel(Player player, String skillType, int levels) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        UserManager.getPlayer(player).getProfile().addLevels(skill, levels);
     }
 
     /**
@@ -188,13 +279,17 @@ public final class ExperienceAPI {
      * @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(String playerName, String skillType, int levels) {
+    public static void addLevelOffline(String playerName, String skillType, int levels) throws InvalidSkillException {
         PlayerProfile profile = getOfflineProfile(playerName);
-
         SkillType skill = SkillType.getSkill(skillType);
 
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
         if (skill.isChildSkill()) {
             Set<SkillType> parentSkills = FamilyTree.getParents(skill);
 
@@ -218,9 +313,17 @@ public final class ExperienceAPI {
      * @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 UserManager.getPlayer(player).getProfile().getSkillLevel(SkillType.getSkill(skillType));
+    public static int getLevel(Player player, String skillType) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        return UserManager.getPlayer(player).getProfile().getSkillLevel(skill);
     }
 
     /**
@@ -231,10 +334,18 @@ public final class ExperienceAPI {
      * @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
      */
-    public static int getLevelOffline(String playerName, String skillType) {
-        return getOfflineProfile(playerName).getSkillLevel(SkillType.getSkill(skillType));
+    public static int getLevelOffline(String playerName, String skillType) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        return getOfflineProfile(playerName).getSkillLevel(skill);
     }
 
     /**
@@ -256,6 +367,7 @@ public final class ExperienceAPI {
      *
      * @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
      */
     public static int getPowerLevelOffline(String playerName) {
@@ -280,9 +392,17 @@ public final class ExperienceAPI {
      *
      * @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 Config.getInstance().getLevelCap(SkillType.getSkill(skillType));
+    public static int getLevelCap(String skillType) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        return Config.getInstance().getLevelCap(skill);
     }
 
     /**
@@ -290,7 +410,7 @@ public final class ExperienceAPI {
      * </br>
      * This function is designed for API usage.
      *
-     * @return the power level cap of a given skill
+     * @return the overall power level cap
      */
     public static int getPowerLevelCap() {
         return Config.getInstance().getPowerLevelCap();
@@ -304,9 +424,17 @@ public final class ExperienceAPI {
      * @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) {
-        UserManager.getPlayer(player).getProfile().modifySkill(SkillType.getSkill(skillType), skillLevel);
+    public static void setLevel(Player player, String skillType, int skillLevel) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        UserManager.getPlayer(player).getProfile().modifySkill(skill, skillLevel);
     }
 
     /**
@@ -318,10 +446,17 @@ public final class ExperienceAPI {
      * @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(String playerName, String skillType, int skillLevel) {
-        getOfflineProfile(playerName).modifySkill(SkillType.getSkill(skillType), skillLevel);
+    public static void setLevelOffline(String playerName, String skillType, int skillLevel) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        getOfflineProfile(playerName).modifySkill(skill, skillLevel);
     }
 
     /**
@@ -332,9 +467,17 @@ public final class ExperienceAPI {
      * @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
      */
-    public static void setXP(Player player, String skillType, int newValue) {
-        UserManager.getPlayer(player).getProfile().setSkillXpLevel(SkillType.getSkill(skillType), newValue);
+    public static void setXP(Player player, String skillType, int newValue) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        UserManager.getPlayer(player).getProfile().setSkillXpLevel(skill, newValue);
     }
 
     /**
@@ -346,10 +489,17 @@ public final class ExperienceAPI {
      * @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
      */
-    public static void setXPOffline(String playerName, String skillType, int newValue) {
-        getOfflineProfile(playerName).setSkillXpLevel(SkillType.getSkill(skillType), newValue);
+    public static void setXPOffline(String playerName, String skillType, int newValue) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        getOfflineProfile(playerName).setSkillXpLevel(skill, newValue);
     }
 
     /**
@@ -360,9 +510,17 @@ public final class ExperienceAPI {
      * @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
      */
-    public static void removeXP(Player player, String skillType, int xp) {
-        UserManager.getPlayer(player).getProfile().removeXp(SkillType.getSkill(skillType), xp);
+    public static void removeXP(Player player, String skillType, int xp) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        UserManager.getPlayer(player).getProfile().removeXp(skill, xp);
     }
 
     /**
@@ -374,10 +532,17 @@ public final class ExperienceAPI {
      * @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
      */
-    public static void removeXPOffline(String playerName, String skillType, int xp) {
-        getOfflineProfile(playerName).removeXp(SkillType.getSkill(skillType), xp);
+    public static void removeXPOffline(String playerName, String skillType, int xp) throws InvalidSkillException {
+        SkillType skill = SkillType.getSkill(skillType);
+
+        if (skill == null) {
+            throw new InvalidSkillException();
+        }
+
+        getOfflineProfile(playerName).removeXp(skill, xp);
     }
 
     /**
@@ -387,11 +552,9 @@ public final class ExperienceAPI {
      * @param skillType The skill to check
      * @param XP The amount of XP to award.
      */
-    private static void addOfflineXP(String playerName, String skillType, int XP) {
+    private static void addOfflineXP(String playerName, SkillType skill, int XP) {
         PlayerProfile profile = getOfflineProfile(playerName);
 
-        SkillType skill = SkillType.getSkill(skillType);
-
         if (skill.isChildSkill()) {
             Set<SkillType> parentSkills = FamilyTree.getParents(skill);
 

+ 9 - 0
src/main/java/com/gmail/nossr50/api/InvalidSkillException.java

@@ -0,0 +1,9 @@
+package com.gmail.nossr50.api;
+
+public class InvalidSkillException extends Exception {
+    private static final long serialVersionUID = 942705284195791157L;
+
+    public InvalidSkillException() {
+        super("That is not a valid skill.");
+    }
+}