Save and load profiles using UUIDs

fall back on usernames if neccesary
This commit is contained in:
TfT_02 2014-07-12 14:54:22 +02:00
parent e619e01c23
commit 792f21bc20
5 changed files with 37 additions and 26 deletions

View File

@ -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.
*

View File

@ -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) {

View File

@ -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

View File

@ -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);
}
/*

View File

@ -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);
}
}