From dc21e18cc28fa31e6d88e7aa8a7ad01cac359d9a Mon Sep 17 00:00:00 2001 From: TfT_02 Date: Tue, 8 Jul 2014 14:46:43 +0200 Subject: [PATCH] Deprecate methods in ExperienceAPI --- .../com/gmail/nossr50/api/ExperienceAPI.java | 288 +++++++++++++++++- .../database/ConvertDatabaseCommand.java | 2 +- .../nossr50/database/DatabaseManager.java | 15 + .../database/FlatfileDatabaseManager.java | 4 + .../nossr50/database/SQLDatabaseManager.java | 4 + .../nossr50/datatypes/player/McMMOPlayer.java | 4 +- 6 files changed, 312 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java index 3d85b2253..aef29050b 100644 --- a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java +++ b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java @@ -1,6 +1,7 @@ package com.gmail.nossr50.api; import java.util.Set; +import java.util.UUID; import org.bukkit.entity.Player; @@ -86,6 +87,14 @@ public final class ExperienceAPI { UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason)); } + /** + * Adds raw XP to an offline player. + *
+ * This function is designed for API usage. + * + * @deprecated We're using float for our XP values now + * replaced by {@link #addRawXPOffline(String playerName, String skillType, float XP)} + */ @Deprecated public static void addRawXPOffline(String playerName, String skillType, int XP) { addRawXPOffline(playerName, skillType, (float) XP); @@ -96,6 +105,9 @@ public final class ExperienceAPI { *
* This function is designed for API usage. * + * @deprecated We're using uuids to get an offline player + * replaced by {@link #addRawXPOffline(UUID uuid, String skillType, float XP)} + * * @param playerName The player to add XP to * @param skillType The skill to add XP to * @param XP The amount of XP to add @@ -103,10 +115,27 @@ public final class ExperienceAPI { * @throws InvalidSkillException if the given skill is not valid * @throws InvalidPlayerException if the given player does not exist in the database */ + @Deprecated public static void addRawXPOffline(String playerName, String skillType, float XP) { addOfflineXP(playerName, getSkillType(skillType), (int) Math.floor(XP)); } + /** + * Adds raw XP to an offline player. + *
+ * This function is designed for API usage. + * + * @param uuid The UUID of 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 + * @throws InvalidPlayerException if the given player does not exist in the database + */ + public static void addRawXPOffline(UUID uuid, String skillType, float XP) { + addOfflineXP(uuid, getSkillType(skillType), (int) Math.floor(XP)); + } + /** * Adds XP to the player, calculates for XP Rate only. *
@@ -152,6 +181,7 @@ public final class ExperienceAPI { * @throws InvalidSkillException if the given skill is not valid * @throws InvalidPlayerException if the given player does not exist in the database */ + @Deprecated public static void addMultipliedXPOffline(String playerName, String skillType, int XP) { addOfflineXP(playerName, getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier())); } @@ -203,6 +233,7 @@ public final class ExperienceAPI { * @throws InvalidSkillException if the given skill is not valid * @throws InvalidPlayerException if the given player does not exist in the database */ + @Deprecated public static void addModifiedXPOffline(String playerName, String skillType, int XP) { SkillType skill = getSkillType(skillType); @@ -273,10 +304,28 @@ public final class ExperienceAPI { * @throws InvalidPlayerException if the given player does not exist in the database * @throws UnsupportedOperationException if the given skill is a child skill */ + @Deprecated public static int getOfflineXP(String playerName, String skillType) { return getOfflineProfile(playerName).getSkillXpLevel(getNonChildSkillType(skillType)); } + /** + * Get the amount of XP an offline player has in a specific skill. + *
+ * This function is designed for API usage. + * + * @param uuid 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 + * @throws UnsupportedOperationException if the given skill is a child skill + */ + public static int getOfflineXP(UUID uuid, String skillType) { + return getOfflineProfile(uuid).getSkillXpLevel(getNonChildSkillType(skillType)); + } + /** * Get the raw amount of XP a player has in a specific skill. *
@@ -306,10 +355,28 @@ public final class ExperienceAPI { * @throws InvalidPlayerException if the given player does not exist in the database * @throws UnsupportedOperationException if the given skill is a child skill */ + @Deprecated public static float getOfflineXPRaw(String playerName, String skillType) { return getOfflineProfile(playerName).getSkillXpLevelRaw(getNonChildSkillType(skillType)); } + /** + * Get the raw amount of XP an offline player has in a specific skill. + *
+ * This function is designed for API usage. + * + * @param uuid 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 + * @throws UnsupportedOperationException if the given skill is a child skill + */ + public static float getOfflineXPRaw(UUID uuid, String skillType) { + return getOfflineProfile(uuid).getSkillXpLevelRaw(getNonChildSkillType(skillType)); + } + /** * Get the total amount of XP needed to reach the next level. *
@@ -339,10 +406,28 @@ public final class ExperienceAPI { * @throws InvalidPlayerException if the given player does not exist in the database * @throws UnsupportedOperationException if the given skill is a child skill */ + @Deprecated public static int getOfflineXPToNextLevel(String playerName, String skillType) { return getOfflineProfile(playerName).getXpToLevel(getNonChildSkillType(skillType)); } + /** + * Get the total amount of XP an offline player needs to reach the next level. + *
+ * This function is designed for API usage. + * + * @param uuid The player to get XP for + * @param skillType The skill to get XP for + * @return the total amount of XP needed to reach the next level + * + * @throws InvalidSkillException if the given skill is not valid + * @throws InvalidPlayerException if the given player does not exist in the database + * @throws UnsupportedOperationException if the given skill is a child skill + */ + public static int getOfflineXPToNextLevel(UUID uuid, String skillType) { + return getOfflineProfile(uuid).getXpToLevel(getNonChildSkillType(skillType)); + } + /** * Get the amount of XP remaining until the next level. *
@@ -376,14 +461,34 @@ public final class ExperienceAPI { * @throws InvalidPlayerException if the given player does not exist in the database * @throws UnsupportedOperationException if the given skill is a child skill */ + @Deprecated public static int getOfflineXPRemaining(String playerName, String skillType) { SkillType skill = getNonChildSkillType(skillType); - PlayerProfile profile = getOfflineProfile(playerName); return profile.getXpToLevel(skill) - profile.getSkillXpLevel(skill); } + /** + * Get the amount of XP an offline player has left before leveling up. + *
+ * This function is designed for API usage. + * + * @param uuid The player to get XP for + * @param skillType The skill to get XP for + * @return the amount of XP needed to reach the next level + * + * @throws InvalidSkillException if the given skill is not valid + * @throws InvalidPlayerException if the given player does not exist in the database + * @throws UnsupportedOperationException if the given skill is a child skill + */ + public static float getOfflineXPRemaining(UUID uuid, String skillType) { + SkillType skill = getNonChildSkillType(skillType); + PlayerProfile profile = getOfflineProfile(uuid); + + return profile.getXpToLevel(skill) - profile.getSkillXpLevelRaw(skill); + } + /** * Add levels to a skill. *
@@ -411,6 +516,7 @@ public final class ExperienceAPI { * @throws InvalidSkillException if the given skill is not valid * @throws InvalidPlayerException if the given player does not exist in the database */ + @Deprecated public static void addLevelOffline(String playerName, String skillType, int levels) { PlayerProfile profile = getOfflineProfile(playerName); SkillType skill = getSkillType(skillType); @@ -430,6 +536,37 @@ public final class ExperienceAPI { profile.scheduleAsyncSave(); } + /** + * Add levels to a skill for an offline player. + *
+ * This function is designed for API usage. + * + * @param uuid 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 + * @throws InvalidPlayerException if the given player does not exist in the database + */ + public static void addLevelOffline(UUID uuid, String skillType, int levels) { + PlayerProfile profile = getOfflineProfile(uuid); + SkillType skill = getSkillType(skillType); + + if (skill.isChildSkill()) { + Set parentSkills = FamilyTree.getParents(skill); + + for (SkillType parentSkill : parentSkills) { + profile.addLevels(parentSkill, (levels / parentSkills.size())); + } + + profile.scheduleAsyncSave(); + return; + } + + profile.addLevels(skill, levels); + profile.scheduleAsyncSave(); + } + /** * Get the level a player has in a specific skill. *
@@ -457,10 +594,27 @@ public final class ExperienceAPI { * @throws InvalidSkillException if the given skill is not valid * @throws InvalidPlayerException if the given player does not exist in the database */ + @Deprecated public static int getLevelOffline(String playerName, String skillType) { return getOfflineProfile(playerName).getSkillLevel(getSkillType(skillType)); } + /** + * Get the level an offline player has in a specific skill. + *
+ * This function is designed for API usage. + * + * @param uuid 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(UUID uuid, String skillType) { + return getOfflineProfile(uuid).getSkillLevel(getSkillType(skillType)); + } + /** * Gets the power level of a player. *
@@ -483,6 +637,7 @@ public final class ExperienceAPI { * * @throws InvalidPlayerException if the given player does not exist in the database */ + @Deprecated public static int getPowerLevelOffline(String playerName) { int powerLevel = 0; PlayerProfile profile = getOfflineProfile(playerName); @@ -494,6 +649,27 @@ public final class ExperienceAPI { return powerLevel; } + /** + * Gets the power level of an offline player. + *
+ * This function is designed for API usage. + * + * @param uuid 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(UUID uuid) { + int powerLevel = 0; + PlayerProfile profile = getOfflineProfile(uuid); + + for (SkillType type : SkillType.NON_CHILD_SKILLS) { + powerLevel += profile.getSkillLevel(type); + } + + return powerLevel; + } + /** * Get the level cap of a specific skill. *
@@ -533,10 +709,28 @@ public final class ExperienceAPI { * * @return the position on the leaderboard */ + @Deprecated public static int getPlayerRankSkill(String playerName, String skillType) { return mcMMO.getDatabaseManager().readRank(getOfflineProfile(playerName).getPlayerName()).get(getNonChildSkillType(skillType)); } + /** + * Get the position on the leaderboard of a player. + *
+ * This function is designed for API usage. + * + * @param uuid The name of the player to check + * @param skillType The skill to check + * + * @throws InvalidSkillException if the given skill is not valid + * @throws InvalidPlayerException if the given player does not exist in the database + * @throws UnsupportedOperationException if the given skill is a child skill + * + * @return the position on the leaderboard + */ + public static int getPlayerRankSkill(UUID uuid, String skillType) { + return mcMMO.getDatabaseManager().readRank(getOfflineProfile(uuid).getPlayerName()).get(getNonChildSkillType(skillType)); + } /** * Get the position on the power level leaderboard of a player. @@ -549,10 +743,26 @@ public final class ExperienceAPI { * * @return the position on the power level leaderboard */ + @Deprecated public static int getPlayerRankOverall(String playerName) { return mcMMO.getDatabaseManager().readRank(getOfflineProfile(playerName).getPlayerName()).get(null); } + /** + * Get the position on the power level leaderboard of a player. + *
+ * This function is designed for API usage. + * + * @param uuid The name of the player to check + * + * @throws InvalidPlayerException if the given player does not exist in the database + * + * @return the position on the power level leaderboard + */ + public static int getPlayerRankOverall(UUID uuid) { + return mcMMO.getDatabaseManager().readRank(getOfflineProfile(uuid).getPlayerName()).get(null); + } + /** * Sets the level of a player in a specific skill type. *
@@ -580,10 +790,27 @@ public final class ExperienceAPI { * @throws InvalidSkillException if the given skill is not valid * @throws InvalidPlayerException if the given player does not exist in the database */ + @Deprecated public static void setLevelOffline(String playerName, String skillType, int skillLevel) { getOfflineProfile(playerName).modifySkill(getSkillType(skillType), skillLevel); } + /** + * Sets the level of an offline player in a specific skill type. + *
+ * This function is designed for API usage. + * + * @param uuid 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 + * @throws InvalidPlayerException if the given player does not exist in the database + */ + public static void setLevelOffline(UUID uuid, String skillType, int skillLevel) { + getOfflineProfile(uuid).modifySkill(getSkillType(skillType), skillLevel); + } + /** * Sets the XP of a player in a specific skill type. *
@@ -613,10 +840,28 @@ public final class ExperienceAPI { * @throws InvalidPlayerException if the given player does not exist in the database * @throws UnsupportedOperationException if the given skill is a child skill */ + @Deprecated public static void setXPOffline(String playerName, String skillType, int newValue) { getOfflineProfile(playerName).setSkillXpLevel(getNonChildSkillType(skillType), newValue); } + /** + * Sets the XP of an offline player in a specific skill type. + *
+ * This function is designed for API usage. + * + * @param uuid 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 + * @throws InvalidPlayerException if the given player does not exist in the database + * @throws UnsupportedOperationException if the given skill is a child skill + */ + public static void setXPOffline(UUID uuid, String skillType, int newValue) { + getOfflineProfile(uuid).setSkillXpLevel(getNonChildSkillType(skillType), newValue); + } + /** * Removes XP from a player in a specific skill type. *
@@ -646,12 +891,37 @@ public final class ExperienceAPI { * @throws InvalidPlayerException if the given player does not exist in the database * @throws UnsupportedOperationException if the given skill is a child skill */ + @Deprecated public static void removeXPOffline(String playerName, String skillType, int xp) { getOfflineProfile(playerName).removeXp(getNonChildSkillType(skillType), xp); } - // Utility methods follow. + /** + * Removes XP from an offline player in a specific skill type. + *
+ * This function is designed for API usage. + * + * @param uuid 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 + * @throws InvalidPlayerException if the given player does not exist in the database + * @throws UnsupportedOperationException if the given skill is a child skill + */ + public static void removeXPOffline(UUID uuid, String skillType, int xp) { + getOfflineProfile(uuid).removeXp(getNonChildSkillType(skillType), xp); + } + // Utility methods follow. + private static void addOfflineXP(UUID playerUniqueId, SkillType skill, int XP) { + PlayerProfile profile = getOfflineProfile(playerUniqueId); + + profile.addXp(skill, XP); + profile.save(); + } + + @Deprecated private static void addOfflineXP(String playerName, SkillType skill, int XP) { PlayerProfile profile = getOfflineProfile(playerName); @@ -659,8 +929,20 @@ public final class ExperienceAPI { profile.scheduleAsyncSave(); } + private static PlayerProfile getOfflineProfile(UUID uuid) { + PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(uuid, false); + + if (!profile.isLoaded()) { + throw new InvalidPlayerException(); + } + + return profile; + } + + @Deprecated private static PlayerProfile getOfflineProfile(String playerName) { - PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, false); + UUID uuid = mcMMO.p.getServer().getOfflinePlayer(playerName).getUniqueId(); + PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(uuid, false); if (!profile.isLoaded()) { throw new InvalidPlayerException(); diff --git a/src/main/java/com/gmail/nossr50/commands/database/ConvertDatabaseCommand.java b/src/main/java/com/gmail/nossr50/commands/database/ConvertDatabaseCommand.java index 2bcea962c..7f85a8977 100644 --- a/src/main/java/com/gmail/nossr50/commands/database/ConvertDatabaseCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/database/ConvertDatabaseCommand.java @@ -55,7 +55,7 @@ public class ConvertDatabaseCommand implements CommandExecutor { UserManager.clearAll(); for (Player player : mcMMO.p.getServer().getOnlinePlayers()) { - PlayerProfile profile = oldDatabase.loadPlayerProfile(player.getName(), false); + PlayerProfile profile = oldDatabase.loadPlayerProfile(player.getUniqueId(), false); if (profile.isLoaded()) { mcMMO.getDatabaseManager().saveUser(profile); diff --git a/src/main/java/com/gmail/nossr50/database/DatabaseManager.java b/src/main/java/com/gmail/nossr50/database/DatabaseManager.java index 021f0d0aa..cbe63d41e 100644 --- a/src/main/java/com/gmail/nossr50/database/DatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/DatabaseManager.java @@ -2,6 +2,7 @@ package com.gmail.nossr50.database; import java.util.List; import java.util.Map; +import java.util.UUID; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.database.DatabaseType; @@ -72,14 +73,28 @@ public interface DatabaseManager { /** * Load a player from the database. * + * @deprecated replaced by {@link #loadPlayerProfile(UUID uuid, boolean createNew)} + * * @param playerName The name of the player to load from the database * @param createNew Whether to create a new record if the player is not * found * @return The player's data, or an unloaded PlayerProfile if not found * and createNew is false */ + @Deprecated public PlayerProfile loadPlayerProfile(String playerName, boolean createNew); + /** + * Load a player from the database. + * + * @param uuid The uuid of the player to load from the database + * @param createNew Whether to create a new record if the player is not + * found + * @return The player's data, or an unloaded PlayerProfile if not found + * and createNew is false + */ + public PlayerProfile loadPlayerProfile(UUID uuid, boolean createNew); + /** * Get all users currently stored in the database. * diff --git a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java index ba3561402..f185214a4 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java @@ -385,6 +385,10 @@ public final class FlatfileDatabaseManager implements DatabaseManager { return loadPlayerProfile(playerName, "", create); } + public PlayerProfile loadPlayerProfile(UUID uuid, boolean create) { + return loadPlayerProfile("", uuid.toString(), create); + } + public PlayerProfile loadPlayerProfile(String playerName, String uuid, boolean create) { BufferedReader in = null; String usersFilePath = mcMMO.getUsersFilePath(); diff --git a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java index 8315184f0..8ecd42f4c 100644 --- a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java @@ -356,6 +356,10 @@ public final class SQLDatabaseManager implements DatabaseManager { return loadPlayerProfile(playerName, "", create, true); } + public PlayerProfile loadPlayerProfile(UUID uuid, boolean create) { + return loadPlayerProfile("", uuid.toString(), create, true); + } + private PlayerProfile loadPlayerProfile(String playerName, String uuid, boolean create, boolean retry) { if (!checkConnected()) { return new PlayerProfile(playerName, false); // return fake profile if not connected 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 e9dce3e6e..038e50917 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -3,6 +3,7 @@ package com.gmail.nossr50.datatypes.player; import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.UUID; import org.bukkit.GameMode; import org.bukkit.Location; @@ -138,6 +139,7 @@ public class McMMOPlayer { private class RetryProfileLoadingTask extends BukkitRunnable { private static final int MAX_TRIES = 5; private final String playerName = McMMOPlayer.this.player.getName(); + private final UUID uniqueId = McMMOPlayer.this.player.getUniqueId(); private int attempt = 0; // WARNING: ASYNC TASK @@ -158,7 +160,7 @@ public class McMMOPlayer { // Increment attempt counter and try attempt++; - PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, true); + PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(uniqueId, true); // If successful, schedule the apply if (profile.isLoaded()) { new ApplySuccessfulProfile(profile).runTask(mcMMO.p);