mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-07-15 20:14:42 +02:00
Add UUID support! - zreed, slipcor, t00thpick1
This commit updates our database managers to use UUIDs instead of usernames for persistent storage. Fixes #1979
This commit is contained in:
@ -14,6 +14,7 @@ import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
@ -26,6 +27,7 @@ import com.gmail.nossr50.datatypes.database.UpgradeType;
|
||||
import com.gmail.nossr50.datatypes.player.PlayerProfile;
|
||||
import com.gmail.nossr50.datatypes.skills.AbilityType;
|
||||
import com.gmail.nossr50.datatypes.skills.SkillType;
|
||||
import com.gmail.nossr50.runnables.database.UUIDUpdateAsyncTask;
|
||||
import com.gmail.nossr50.util.Misc;
|
||||
import com.gmail.nossr50.util.StringUtils;
|
||||
|
||||
@ -44,6 +46,10 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
usersFile = new File(mcMMO.getUsersFilePath());
|
||||
checkStructure();
|
||||
updateLeaderboards();
|
||||
|
||||
if (mcMMO.getUpgradeManager().shouldUpgrade(UpgradeType.ADD_UUIDS)) {
|
||||
new UUIDUpdateAsyncTask(mcMMO.p, getStoredUsers()).runTaskAsynchronously(mcMMO.p);
|
||||
}
|
||||
}
|
||||
|
||||
public void purgePowerlessUsers() {
|
||||
@ -209,6 +215,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;
|
||||
@ -223,8 +230,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 {
|
||||
@ -271,6 +279,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(":");
|
||||
writer.append(uuid.toString()).append(":");
|
||||
writer.append("\r\n");
|
||||
}
|
||||
}
|
||||
@ -313,7 +322,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
return skills;
|
||||
}
|
||||
|
||||
public void newUser(String playerName) {
|
||||
public void newUser(String playerName, String uuid) {
|
||||
BufferedWriter out = null;
|
||||
synchronized (fileWritingLock) {
|
||||
try {
|
||||
@ -362,7 +371,8 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
out.append(Config.getInstance().getMobHealthbarDefault().toString()).append(":"); // Mob Healthbar HUD
|
||||
out.append("0:"); // Alchemy
|
||||
out.append("0:"); // AlchemyXp
|
||||
|
||||
out.append(uuid).append(":"); // UUID
|
||||
|
||||
// Add more in the same format as the line above
|
||||
|
||||
out.newLine();
|
||||
@ -376,7 +386,20 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public PlayerProfile loadPlayerProfile(String playerName, boolean create) {
|
||||
return loadPlayerProfile(playerName, "", create);
|
||||
}
|
||||
|
||||
public PlayerProfile loadPlayerProfile(UUID uuid, boolean create) {
|
||||
return loadPlayerProfile("", uuid.toString(), 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();
|
||||
|
||||
@ -390,17 +413,28 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
// Find if the line contains the player we want.
|
||||
String[] character = line.split(":");
|
||||
|
||||
if (!character[0].equalsIgnoreCase(playerName)) {
|
||||
if (!character[41].equalsIgnoreCase(uuid) && !character[0].equalsIgnoreCase(playerName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Update playerName in database after name change
|
||||
if (!character[0].equalsIgnoreCase(playerName)) {
|
||||
mcMMO.p.debug("Name change detected: " + character[0] + " => " + playerName);
|
||||
character[0] = playerName;
|
||||
}
|
||||
|
||||
return loadFromLine(character);
|
||||
}
|
||||
|
||||
// Didn't find the player, create a new one
|
||||
if (create) {
|
||||
newUser(playerName);
|
||||
return new PlayerProfile(playerName, true);
|
||||
if (uuid.isEmpty()) {
|
||||
newUser(playerName, uuid);
|
||||
return new PlayerProfile(playerName, true);
|
||||
}
|
||||
|
||||
newUser(playerName, uuid);
|
||||
return new PlayerProfile(playerName, UUID.fromString(uuid), true);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
@ -421,7 +455,11 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
}
|
||||
|
||||
// Return unloaded profile
|
||||
return new PlayerProfile(playerName);
|
||||
if (uuid.isEmpty()) {
|
||||
return new PlayerProfile(playerName);
|
||||
}
|
||||
|
||||
return new PlayerProfile(playerName, UUID.fromString(uuid));
|
||||
}
|
||||
|
||||
public void convertUsers(DatabaseManager destination) {
|
||||
@ -458,6 +496,91 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean saveUserUUID(String userName, UUID uuid) {
|
||||
boolean worked = false;
|
||||
|
||||
BufferedReader in = null;
|
||||
FileWriter out = null;
|
||||
String usersFilePath = mcMMO.getUsersFilePath();
|
||||
|
||||
synchronized (fileWritingLock) {
|
||||
try {
|
||||
in = new BufferedReader(new FileReader(usersFilePath));
|
||||
StringBuilder writer = new StringBuilder();
|
||||
String line;
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
String[] character = line.split(":");
|
||||
if (!worked && character[0].equalsIgnoreCase(userName)) {
|
||||
if (character.length < 42) {
|
||||
mcMMO.p.getLogger().severe("Could not update UUID for " + userName + "!");
|
||||
mcMMO.p.getLogger().severe("Database entry is invalid.");
|
||||
break;
|
||||
}
|
||||
|
||||
line = line.replace(character[41], uuid.toString());
|
||||
worked = true;
|
||||
}
|
||||
|
||||
writer.append(line).append("\r\n");
|
||||
}
|
||||
|
||||
out = new FileWriter(usersFilePath); // Write out the new file
|
||||
out.write(writer.toString());
|
||||
}
|
||||
catch (Exception e) {
|
||||
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
|
||||
}
|
||||
finally {
|
||||
tryClose(in);
|
||||
tryClose(out);
|
||||
}
|
||||
}
|
||||
|
||||
return worked;
|
||||
}
|
||||
|
||||
public boolean saveUserUUIDs(Map<String, UUID> fetchedUUIDs) {
|
||||
BufferedReader in = null;
|
||||
FileWriter out = null;
|
||||
String usersFilePath = mcMMO.getUsersFilePath();
|
||||
|
||||
synchronized (fileWritingLock) {
|
||||
try {
|
||||
in = new BufferedReader(new FileReader(usersFilePath));
|
||||
StringBuilder writer = new StringBuilder();
|
||||
String line;
|
||||
|
||||
while (((line = in.readLine()) != null) && !fetchedUUIDs.isEmpty()) {
|
||||
String[] character = line.split(":");
|
||||
if (fetchedUUIDs.containsKey(character[0])) {
|
||||
if (character.length < 42) {
|
||||
mcMMO.p.getLogger().severe("Could not update UUID for " + character[0] + "!");
|
||||
mcMMO.p.getLogger().severe("Database entry is invalid.");
|
||||
return false;
|
||||
}
|
||||
|
||||
line = line.replace(character[41], fetchedUUIDs.remove(character[0]).toString());
|
||||
}
|
||||
|
||||
writer.append(line).append("\r\n");
|
||||
}
|
||||
|
||||
out = new FileWriter(usersFilePath); // Write out the new file
|
||||
out.write(writer.toString());
|
||||
}
|
||||
catch (Exception e) {
|
||||
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
|
||||
}
|
||||
finally {
|
||||
tryClose(in);
|
||||
tryClose(out);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<String> getStoredUsers() {
|
||||
ArrayList<String> users = new ArrayList<String>();
|
||||
BufferedReader in = null;
|
||||
@ -598,6 +721,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
in = new BufferedReader(new FileReader(usersFilePath));
|
||||
StringBuilder writer = new StringBuilder();
|
||||
String line;
|
||||
HashSet<String> usernames = new HashSet<String>();
|
||||
HashSet<String> players = new HashSet<String>();
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
@ -612,8 +736,13 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
}
|
||||
String[] character = line.split(":");
|
||||
|
||||
// Prevent the same username from being present multiple times
|
||||
if (!usernames.add(character[0])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Prevent the same player from being present multiple times
|
||||
if (!players.add(character[0])) {
|
||||
if (character.length == 42 && (!character[41].isEmpty() && !players.add(character[41]))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -645,7 +774,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
}
|
||||
|
||||
// If they're valid, rewrite them to the file.
|
||||
if (character.length == 41) {
|
||||
if (character.length == 42) {
|
||||
writer.append(line).append("\r\n");
|
||||
continue;
|
||||
}
|
||||
@ -699,16 +828,25 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
oldVersion = "1.4.08";
|
||||
}
|
||||
}
|
||||
if (character.length <= 41) {
|
||||
// Addition of UUIDs
|
||||
// Version 1.5.01
|
||||
// Add a space because otherwise it gets removed
|
||||
newLine.append(" :");
|
||||
if (oldVersion == null) {
|
||||
oldVersion = "1.5.01";
|
||||
}
|
||||
}
|
||||
|
||||
// Remove any blanks that shouldn't be there, and validate the other fields
|
||||
String[] newCharacter = newLine.toString().split(":");
|
||||
boolean corrupted = false;
|
||||
|
||||
for (int i = 0; i < newCharacter.length; i++) {
|
||||
if (newCharacter[i].isEmpty() && !(i == 2 || i == 3 || i == 23 || i == 33)) {
|
||||
if (newCharacter[i].isEmpty() && !(i == 2 || i == 3 || i == 23 || i == 33 || i == 41)) {
|
||||
corrupted = true;
|
||||
|
||||
if (newCharacter.length != 41) {
|
||||
if (newCharacter.length != 42) {
|
||||
newCharacter = (String[]) ArrayUtils.remove(newCharacter, i);
|
||||
}
|
||||
else {
|
||||
@ -729,7 +867,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
newCharacter[i] = Config.getInstance().getMobHealthbarDefault().toString();
|
||||
}
|
||||
|
||||
if (!StringUtils.isInt(newCharacter[i]) && !(i == 0 || i == 2 || i == 3 || i == 23 || i == 33 || i == 38)) {
|
||||
if (!StringUtils.isInt(newCharacter[i]) && !(i == 0 || i == 2 || i == 3 || i == 23 || i == 33 || i == 38 || i == 41)) {
|
||||
corrupted = true;
|
||||
newCharacter[i] = "0";
|
||||
}
|
||||
@ -740,7 +878,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
}
|
||||
|
||||
if (oldVersion != null) {
|
||||
mcMMO.p.debug("Updating database line for player " + character[0] + " from before version " + oldVersion);
|
||||
mcMMO.p.debug("Updating database line from before version " + oldVersion + " for player " + character[0]);
|
||||
}
|
||||
|
||||
if (corrupted || oldVersion != null) {
|
||||
@ -869,7 +1007,15 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
||||
mobHealthbarType = Config.getInstance().getMobHealthbarDefault();
|
||||
}
|
||||
|
||||
return new PlayerProfile(character[0], skills, skillsXp, skillsDATS, mobHealthbarType);
|
||||
UUID uuid;
|
||||
try {
|
||||
uuid = UUID.fromString(character[41]);
|
||||
}
|
||||
catch (Exception e) {
|
||||
uuid = null;
|
||||
}
|
||||
|
||||
return new PlayerProfile(character[0], uuid, skills, skillsXp, skillsDATS, mobHealthbarType);
|
||||
}
|
||||
|
||||
private Map<SkillType, Integer> getSkillMapFromLine(String[] character) {
|
||||
|
Reference in New Issue
Block a user