diff --git a/Changelog.txt b/Changelog.txt index 7fd92c861..ce42d8b4a 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -8,6 +8,7 @@ Key: - Removal Version 1.4.04-dev + + Added functions to ExperienceAPI for use with offline players = Fixed bug where trying to activate a Chimaera Wing would require one item too much = Fixed bug where Treefeller would try to cut too many leaves and reach the threshold when it shouldn't = Fixed bug where Mining wasn't awarding double drops diff --git a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java index f535308f2..66a72771c 100644 --- a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java +++ b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java @@ -1,9 +1,13 @@ package com.gmail.nossr50.api; +import java.util.Set; + import org.bukkit.entity.Player; import com.gmail.nossr50.config.Config; +import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.skills.SkillType; +import com.gmail.nossr50.skills.child.FamilyTree; import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.skills.SkillUtils; @@ -38,6 +42,21 @@ public final class ExperienceAPI { UserManager.getPlayer(player).applyXpGain(SkillType.getSkill(skillType), XP); } + /** + * Adds raw XP to an offline player. + *
+ * This function is designed for API usage. + * + * @param playerName The player to add XP to + * @param skillType The skill to add XP to + * @param XP The amount of XP to add + * + * @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); + } + /** * Adds XP to the player, calculates for XP Rate only. *
@@ -66,6 +85,51 @@ public final class ExperienceAPI { UserManager.getPlayer(player).applyXpGain(SkillType.getSkill(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 + * @param skillType The skill to add XP to + * @param XP The amount of XP to add + * + * @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())); + } + + /** + * 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 + * @param skillType The skill to add XP to + * @param XP The amount of XP to add + */ + public static void addModifiedXP(Player player, String skillType, int XP) { + SkillType skill = SkillType.getSkill(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 + * @param skillType The skill to add XP to + * @param XP The amount of XP to add + * + * @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())); + } + /** * Adds XP to the player, calculates for XP Rate, skill modifiers and perks. May be shared with the party. *
@@ -122,6 +186,20 @@ public final class ExperienceAPI { return UserManager.getPlayer(player).getProfile().getSkillXpLevel(SkillType.getSkill(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 + * @param skillType The skill to get XP for + * @return the amount of XP in a given skill + * @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)); + } + /** * Get the amount of XP left before leveling up. *
@@ -150,6 +228,20 @@ public final class ExperienceAPI { return UserManager.getPlayer(player).getProfile().getXpToLevel(SkillType.getSkill(skillType)); } + /** + * 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 + * @param skillType The skill to get XP for + * @return the amount of XP in a given skill + * @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)); + } + /** * Add levels to a skill. *
@@ -197,6 +289,37 @@ public final class ExperienceAPI { UserManager.getPlayer(player).getProfile().addLevels(SkillType.getSkill(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 + * @param skillType Type of skill to add levels to + * @param levels Number of levels to add + * + * @throws InvalidPlayerException if the given player does not exist in the database + */ + public static void addLevelOffline(String playerName, String skillType, int levels) { + PlayerProfile profile = getOfflineProfile(playerName); + + SkillType skill = SkillType.getSkill(skillType); + + if (skill.isChildSkill()) { + Set parentSkills = FamilyTree.getParents(skill); + + for (SkillType parentSkill : parentSkills) { + profile.addLevels(parentSkill, (levels / parentSkills.size())); + } + + profile.save(); + return; + } + + profile.addLevels(skill, levels); + profile.save(); + } + /** * Get the level a player has in a specific skill. *
@@ -225,6 +348,20 @@ public final class ExperienceAPI { return UserManager.getPlayer(player).getProfile().getSkillLevel(SkillType.getSkill(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 + * @param skillType The skill to get the level for + * @return the level of a given skill + * @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)); + } + /** * Gets the power level of a player. *
@@ -237,6 +374,30 @@ public final class ExperienceAPI { return UserManager.getPlayer(player).getPowerLevel(); } + /** + * 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 + * @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) { + int powerLevel = 0; + PlayerProfile profile = getOfflineProfile(playerName); + + for (SkillType type : SkillType.values()) { + if (type.isChildSkill()) { + continue; + } + + powerLevel += profile.getSkillLevel(type); + } + + return powerLevel; + } + /** * Get the level cap of a specific skill. *
@@ -288,6 +449,21 @@ public final class ExperienceAPI { UserManager.getPlayer(player).getProfile().modifySkill(SkillType.getSkill(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 + * @param skillType The skill to set the level for + * @param skillLevel The value to set the level to + * + * @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); + } + /** * Sets the XP of a player in a specific skill type. *
@@ -316,6 +492,21 @@ public final class ExperienceAPI { UserManager.getPlayer(player).getProfile().setSkillXpLevel(SkillType.getSkill(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 + * @param skillType The skill to set the XP for + * @param newValue The value to set the XP to + * + * @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); + } + /** * Removes XP from a player in a specific skill type. *
@@ -344,6 +535,21 @@ public final class ExperienceAPI { UserManager.getPlayer(player).getProfile().removeXp(SkillType.getSkill(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 + * @param skillType The skill to change the XP for + * @param xp The amount of XP to remove + * + * @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); + } + /** * Check the XP of a player. This should be called after giving XP to process level-ups. * @@ -355,4 +561,41 @@ public final class ExperienceAPI { private static void checkXP(Player player, SkillType skillType) { SkillUtils.xpCheckSkill(skillType, player, UserManager.getProfile(player)); } + + /** + * 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, String skillType, int XP) { + PlayerProfile profile = getOfflineProfile(playerName); + + SkillType skill = SkillType.getSkill(skillType); + + if (skill.isChildSkill()) { + Set parentSkills = FamilyTree.getParents(skill); + + for (SkillType parentSkill : parentSkills) { + profile.setSkillXpLevel(parentSkill, profile.getSkillLevel(parentSkill) + (XP / parentSkills.size())); + } + + profile.save(); + return; + } + + profile.setSkillXpLevel(skill, profile.getSkillXpLevel(skill) + XP); + profile.save(); + } + + private static PlayerProfile getOfflineProfile(String playerName) { + PlayerProfile profile = new PlayerProfile(playerName, false); + + if (!profile.isLoaded()) { + throw new InvalidPlayerException(); + } + + return profile; + } } diff --git a/src/main/java/com/gmail/nossr50/api/InvalidPlayerException.java b/src/main/java/com/gmail/nossr50/api/InvalidPlayerException.java new file mode 100644 index 000000000..6d9b31f82 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/api/InvalidPlayerException.java @@ -0,0 +1,9 @@ +package com.gmail.nossr50.api; + +public class InvalidPlayerException extends RuntimeException { + private static final long serialVersionUID = 907213002618581385L; + + public InvalidPlayerException() { + super("That player does not exist in the database."); + } +} diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java index ad887efde..2385ccd12 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -450,9 +450,18 @@ public class McMMOPlayer { */ public void applyXpGain(SkillType skillType, int xp) { if (skillType.isChildSkill()) { + Set parentSkills = FamilyTree.getParents(skillType); + + for (SkillType parentSkill : parentSkills) { + if (Permissions.skillEnabled(player, parentSkill)) { + applyXpGain(parentSkill, xp / parentSkills.size()); + } + } + return; } + McMMOPlayerXpGainEvent event = new McMMOPlayerXpGainEvent(player, skillType, xp); mcMMO.p.getServer().getPluginManager().callEvent(event);