diff --git a/src/main/java/com/gmail/nossr50/database/DatabaseManager.java b/src/main/java/com/gmail/nossr50/database/DatabaseManager.java index 261d0a412..8b4b23928 100644 --- a/src/main/java/com/gmail/nossr50/database/DatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/DatabaseManager.java @@ -95,6 +95,18 @@ public interface DatabaseManager { */ public PlayerProfile loadPlayerProfile(UUID uuid, boolean createNew); + /** + * Load a player from the database. Attempt to use uuid, fall back on playername + * + * @param playerName The name of the player to load 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(String playerName, 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 c3d191ddc..95819f9d8 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java @@ -210,6 +210,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager { public boolean saveUser(PlayerProfile profile) { String playerName = profile.getPlayerName(); + UUID uuid = profile.getUniqueId(); BufferedReader in = null; FileWriter out = null; @@ -224,8 +225,9 @@ public final class FlatfileDatabaseManager implements DatabaseManager { // While not at the end of the file while ((line = in.readLine()) != null) { - // Read the line in and copy it to the output it's not the player we want to edit - if (!line.split(":")[0].equalsIgnoreCase(playerName)) { + // Read the line in and copy it to the output if it's not the player we want to edit + String[] character = line.split(":"); + if (!character[41].equalsIgnoreCase(uuid.toString()) && !character[0].equalsIgnoreCase(playerName)) { writer.append(line).append("\r\n"); } else { @@ -272,8 +274,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager { writer.append(mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString()).append(":"); writer.append(profile.getSkillLevel(SkillType.ALCHEMY)).append(":"); writer.append(profile.getSkillXpLevel(SkillType.ALCHEMY)).append(":"); - UUID uuid = profile.getUniqueId(); - writer.append(uuid == null ? "" : uuid.toString()).append(":"); + writer.append(uuid.toString()).append(":"); writer.append("\r\n"); } } @@ -389,7 +390,11 @@ public final class FlatfileDatabaseManager implements DatabaseManager { return loadPlayerProfile("", uuid.toString(), create); } - public PlayerProfile loadPlayerProfile(String playerName, String uuid, boolean create) { + public PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean create) { + return loadPlayerProfile(playerName, uuid.toString(), create); + } + + private PlayerProfile loadPlayerProfile(String playerName, String uuid, boolean create) { BufferedReader in = null; String usersFilePath = mcMMO.getUsersFilePath(); @@ -403,7 +408,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager { // Find if the line contains the player we want. String[] character = line.split(":"); - if (!character[41].equalsIgnoreCase(uuid) || !character[0].equalsIgnoreCase(playerName)) { + if (!character[41].equalsIgnoreCase(uuid) && !character[0].equalsIgnoreCase(playerName)) { continue; } @@ -443,7 +448,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager { return new PlayerProfile(playerName); } - return new PlayerProfile(UUID.fromString(uuid)); + return new PlayerProfile(playerName, UUID.fromString(uuid)); } public void convertUsers(DatabaseManager destination) { diff --git a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java index 1c7302135..3df35c64d 100644 --- a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java @@ -360,6 +360,10 @@ public final class SQLDatabaseManager implements DatabaseManager { return loadPlayerProfile("", uuid.toString(), create, true); } + public PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean create) { + return loadPlayerProfile(playerName, 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 038e50917..148c802d7 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -95,15 +95,16 @@ public class McMMOPlayer { public McMMOPlayer(Player player) { String playerName = player.getName(); + UUID uuid = player.getUniqueId(); this.player = player; playerMetadata = new FixedMetadataValue(mcMMO.p, playerName); - profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, true); + profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, uuid, true); party = PartyManager.getPlayerParty(playerName); ptpRecord = new PartyTeleportRecord(); - if (!player.getUniqueId().equals(profile.getUniqueId())) { - profile.setUniqueId(player.getUniqueId()); + if (profile.getUniqueId() == null) { + profile.setUniqueId(uuid); } /* diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java b/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java index d3434545a..c7b95e2a7 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java @@ -34,23 +34,12 @@ public class PlayerProfile { @Deprecated public PlayerProfile(String playerName) { - this.playerName = playerName; - - mobHealthbarType = Config.getInstance().getMobHealthbarDefault(); - - for (AbilityType abilityType : AbilityType.values()) { - abilityDATS.put(abilityType, 0); - } - - for (SkillType skillType : SkillType.NON_CHILD_SKILLS) { - skills.put(skillType, 0); - skillsXp.put(skillType, 0F); - } + this(playerName, null); } - public PlayerProfile(UUID uuid) { + public PlayerProfile(String playerName, UUID uuid) { this.uuid = uuid; - this.playerName = null; + this.playerName = playerName; mobHealthbarType = Config.getInstance().getMobHealthbarDefault(); @@ -71,7 +60,7 @@ public class PlayerProfile { } public PlayerProfile(String playerName, UUID uuid, boolean isLoaded) { - this(uuid); + this(playerName, uuid); this.loaded = isLoaded; } @@ -101,7 +90,7 @@ public class PlayerProfile { changed = !mcMMO.getDatabaseManager().saveUser(profileCopy); if (changed) { - mcMMO.p.getLogger().warning("PlayerProfile for " + playerName + " failed to save"); + mcMMO.p.getLogger().warning("PlayerProfile saving failed for player: " + playerName + " " + uuid); } }