From 48d3280e5ef18970ffddb60951cee7ed97991266 Mon Sep 17 00:00:00 2001 From: TfT_02 Date: Fri, 22 Aug 2014 13:27:05 -0700 Subject: [PATCH] Make sure that player has McMMOPlayer object before calling API --- .../com/gmail/nossr50/api/ExperienceAPI.java | 48 +++++++++++-------- .../McMMOPlayerNotFoundException.java | 11 +++++ 2 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/api/exceptions/McMMOPlayerNotFoundException.java diff --git a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java index 4e05a0136..ec2492605 100644 --- a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java +++ b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java @@ -5,14 +5,16 @@ import java.util.UUID; import org.bukkit.entity.Player; -import com.gmail.nossr50.api.exceptions.InvalidFormulaTypeException; -import com.gmail.nossr50.datatypes.experience.FormulaType; import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.api.exceptions.InvalidFormulaTypeException; import com.gmail.nossr50.api.exceptions.InvalidPlayerException; import com.gmail.nossr50.api.exceptions.InvalidSkillException; import com.gmail.nossr50.api.exceptions.InvalidXPGainReasonException; +import com.gmail.nossr50.api.exceptions.McMMOPlayerNotFoundException; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.experience.ExperienceConfig; +import com.gmail.nossr50.datatypes.experience.FormulaType; +import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.XPGainReason; @@ -105,11 +107,11 @@ public final class ExperienceAPI { */ public static void addRawXP(Player player, String skillType, float XP, String xpGainReason, boolean isUnshared) { if (isUnshared) { - UserManager.getPlayer(player).beginUnsharedXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); + getPlayer(player).beginUnsharedXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); return; } - UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); + getPlayer(player).applyXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); } /** @@ -191,7 +193,7 @@ public final class ExperienceAPI { * @throws InvalidXPGainReasonException if the given xpGainReason is not valid */ public static void addMultipliedXP(Player player, String skillType, int XP, String xpGainReason) { - UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason)); + getPlayer(player).applyXpGain(getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason)); } /** @@ -262,11 +264,11 @@ public final class ExperienceAPI { SkillType skill = getSkillType(skillType); if (isUnshared) { - UserManager.getPlayer(player).beginUnsharedXpGain(skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason)); + getPlayer(player).beginUnsharedXpGain(skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason)); return; } - UserManager.getPlayer(player).applyXpGain(skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason)); + getPlayer(player).applyXpGain(skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason)); } /** @@ -340,11 +342,11 @@ public final class ExperienceAPI { */ public static void addXP(Player player, String skillType, int XP, String xpGainReason, boolean isUnshared) { if (isUnshared) { - UserManager.getPlayer(player).beginUnsharedXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); + getPlayer(player).beginUnsharedXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); return; } - UserManager.getPlayer(player).beginXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); + getPlayer(player).beginXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); } /** @@ -360,7 +362,7 @@ public final class ExperienceAPI { * @throws UnsupportedOperationException if the given skill is a child skill */ public static int getXP(Player player, String skillType) { - return UserManager.getPlayer(player).getSkillXpLevel(getNonChildSkillType(skillType)); + return getPlayer(player).getSkillXpLevel(getNonChildSkillType(skillType)); } /** @@ -411,7 +413,7 @@ public final class ExperienceAPI { * @throws UnsupportedOperationException if the given skill is a child skill */ public static float getXPRaw(Player player, String skillType) { - return UserManager.getPlayer(player).getSkillXpLevelRaw(getNonChildSkillType(skillType)); + return getPlayer(player).getSkillXpLevelRaw(getNonChildSkillType(skillType)); } /** @@ -462,7 +464,7 @@ public final class ExperienceAPI { * @throws UnsupportedOperationException if the given skill is a child skill */ public static int getXPToNextLevel(Player player, String skillType) { - return UserManager.getPlayer(player).getXpToLevel(getNonChildSkillType(skillType)); + return getPlayer(player).getXpToLevel(getNonChildSkillType(skillType)); } /** @@ -515,7 +517,7 @@ public final class ExperienceAPI { public static int getXPRemaining(Player player, String skillType) { SkillType skill = getNonChildSkillType(skillType); - PlayerProfile profile = UserManager.getPlayer(player).getProfile(); + PlayerProfile profile = getPlayer(player).getProfile(); return profile.getXpToLevel(skill) - profile.getSkillXpLevel(skill); } @@ -573,7 +575,7 @@ public final class ExperienceAPI { * @throws InvalidSkillException if the given skill is not valid */ public static void addLevel(Player player, String skillType, int levels) { - UserManager.getPlayer(player).addLevels(getSkillType(skillType), levels); + getPlayer(player).addLevels(getSkillType(skillType), levels); } /** @@ -651,7 +653,7 @@ public final class ExperienceAPI { * @throws InvalidSkillException if the given skill is not valid */ public static int getLevel(Player player, String skillType) { - return UserManager.getPlayer(player).getSkillLevel(getSkillType(skillType)); + return getPlayer(player).getSkillLevel(getSkillType(skillType)); } /** @@ -696,7 +698,7 @@ public final class ExperienceAPI { * @return the power level of the player */ public static int getPowerLevel(Player player) { - return UserManager.getPlayer(player).getPowerLevel(); + return getPlayer(player).getPowerLevel(); } /** @@ -847,7 +849,7 @@ public final class ExperienceAPI { * @throws InvalidSkillException if the given skill is not valid */ public static void setLevel(Player player, String skillType, int skillLevel) { - UserManager.getPlayer(player).modifySkill(getSkillType(skillType), skillLevel); + getPlayer(player).modifySkill(getSkillType(skillType), skillLevel); } /** @@ -896,7 +898,7 @@ public final class ExperienceAPI { * @throws UnsupportedOperationException if the given skill is a child skill */ public static void setXP(Player player, String skillType, int newValue) { - UserManager.getPlayer(player).setSkillXpLevel(getNonChildSkillType(skillType), newValue); + getPlayer(player).setSkillXpLevel(getNonChildSkillType(skillType), newValue); } /** @@ -947,7 +949,7 @@ public final class ExperienceAPI { * @throws UnsupportedOperationException if the given skill is a child skill */ public static void removeXP(Player player, String skillType, int xp) { - UserManager.getPlayer(player).removeXp(getNonChildSkillType(skillType), xp); + getPlayer(player).removeXp(getNonChildSkillType(skillType), xp); } /** @@ -1089,4 +1091,12 @@ public final class ExperienceAPI { return formulaType; } + + private static McMMOPlayer getPlayer(Player player) throws McMMOPlayerNotFoundException { + if (!UserManager.hasPlayerDataKey(player)) { + throw new McMMOPlayerNotFoundException(player); + } + + return UserManager.getPlayer(player); + } } diff --git a/src/main/java/com/gmail/nossr50/api/exceptions/McMMOPlayerNotFoundException.java b/src/main/java/com/gmail/nossr50/api/exceptions/McMMOPlayerNotFoundException.java new file mode 100644 index 000000000..d087631cd --- /dev/null +++ b/src/main/java/com/gmail/nossr50/api/exceptions/McMMOPlayerNotFoundException.java @@ -0,0 +1,11 @@ +package com.gmail.nossr50.api.exceptions; + +import org.bukkit.entity.Player; + +public class McMMOPlayerNotFoundException extends RuntimeException { + private static final long serialVersionUID = 761917904993202836L; + + public McMMOPlayerNotFoundException(Player player) { + super("McMMOPlayer object was not found for: " + player.getName() + " " + player.getUniqueId()); + } +}