Stage 1 of IP -> UUID migration: changing player data to use UUIDs instead

This commit is contained in:
Video 2022-04-15 06:27:32 -06:00
parent b656925e4f
commit 98388d0d23
3 changed files with 58 additions and 89 deletions

View File

@ -3,24 +3,23 @@ package me.totalfreedom.totalfreedommod.player;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collections; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.shop.ShopItem; import me.totalfreedom.totalfreedommod.shop.ShopItem;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class PlayerData public class PlayerData
{ {
private UUID uuid;
private final List<String> ips = Lists.newArrayList(); private final List<String> ips = Lists.newArrayList();
private final List<String> notes = Lists.newArrayList(); private final List<String> notes = Lists.newArrayList();
private final List<String> backupCodes = Lists.newArrayList(); private final List<String> backupCodes = Lists.newArrayList();
private String name;
private String tag = null; private String tag = null;
private String discordID = null; private String discordID = null;
private Boolean masterBuilder = false; private Boolean masterBuilder = false;
@ -48,7 +47,7 @@ public class PlayerData
{ {
try try
{ {
name = resultSet.getString("username"); uuid = UUID.fromString(resultSet.getString("uuid"));
ips.clear(); ips.clear();
ips.addAll(FUtil.stringToList(resultSet.getString("ips"))); ips.addAll(FUtil.stringToList(resultSet.getString("ips")));
notes.clear(); notes.clear();
@ -88,14 +87,13 @@ public class PlayerData
public PlayerData(Player player) public PlayerData(Player player)
{ {
this.name = player.getName(); this.uuid = player.getUniqueId();
} }
@Override @Override
public String toString() public String toString()
{ {
return "Player: " + getName() + "\n" +
return "Player: " + name + "\n" +
"- IPs: " + StringUtils.join(ips, ", ") + "\n" + "- IPs: " + StringUtils.join(ips, ", ") + "\n" +
"- Discord ID: " + discordID + "\n" + "- Discord ID: " + discordID + "\n" +
"- Master Builder: " + masterBuilder + "\n" + "- Master Builder: " + masterBuilder + "\n" +
@ -232,7 +230,7 @@ public class PlayerData
{ {
return new HashMap<String, Object>() return new HashMap<String, Object>()
{{ {{
put("username", name); put("uuid", uuid.toString());
put("ips", FUtil.listToString(ips)); put("ips", FUtil.listToString(ips));
put("notes", FUtil.listToString(notes)); put("notes", FUtil.listToString(notes));
put("tag", tag); put("tag", tag);
@ -255,14 +253,14 @@ public class PlayerData
return displayDiscord; return displayDiscord;
} }
public String getName() public UUID getUuid()
{ {
return name; return uuid;
} }
public void setName(String name) public String getName()
{ {
this.name = name; return Bukkit.getOfflinePlayer(uuid).getName();
} }
public String getTag() public String getTag()

View File

@ -3,11 +3,8 @@ package me.totalfreedom.totalfreedommod.player;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.admin.Admin; import me.totalfreedom.totalfreedommod.admin.Admin;
import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.config.ConfigEntry;
@ -21,7 +18,7 @@ public class PlayerList extends FreedomService
{ {
public final Map<String, FPlayer> playerMap = Maps.newHashMap(); // ip,dataMap public final Map<String, FPlayer> playerMap = Maps.newHashMap(); // ip,dataMap
public final Map<String, PlayerData> dataMap = Maps.newHashMap(); // username, data public final Map<UUID, PlayerData> dataMap = Maps.newHashMap(); // uuid, data
@Override @Override
public void onStart() public void onStart()
@ -57,7 +54,7 @@ public class PlayerList extends FreedomService
while (resultSet.next()) while (resultSet.next())
{ {
PlayerData playerData = load(resultSet); PlayerData playerData = load(resultSet);
dataMap.put(playerData.getName(), playerData); dataMap.put(playerData.getUuid(), playerData);
} }
} }
catch (SQLException e) catch (SQLException e)
@ -122,9 +119,9 @@ public class PlayerList extends FreedomService
return tPlayer; return tPlayer;
} }
public PlayerData loadByName(String name) public PlayerData loadByUuid(UUID uuid)
{ {
return load(plugin.sql.getPlayerByName(name)); return load(plugin.sql.getPlayerByUuid(uuid));
} }
public PlayerData loadByIp(String ip) public PlayerData loadByIp(String ip)
@ -204,7 +201,7 @@ public class PlayerList extends FreedomService
{ {
try try
{ {
ResultSet currentSave = plugin.sql.getPlayerByName(player.getName()); ResultSet currentSave = plugin.sql.getPlayerByUuid(player.getUuid());
for (Map.Entry<String, Object> entry : player.toSQLStorable().entrySet()) for (Map.Entry<String, Object> entry : player.toSQLStorable().entrySet())
{ {
Object storedValue = plugin.sql.getValue(currentSave, entry.getKey(), entry.getValue()); Object storedValue = plugin.sql.getValue(currentSave, entry.getKey(), entry.getValue());
@ -223,69 +220,56 @@ public class PlayerList extends FreedomService
public PlayerData getData(Player player) public PlayerData getData(Player player)
{ {
// Check for existing data // Check for existing data
PlayerData playerData = dataMap.get(player.getName()); PlayerData playerData = dataMap.get(player.getUniqueId());
if (playerData != null) if (playerData != null)
{ {
return playerData; return playerData;
} }
// Load data // Load data
playerData = loadByName(player.getName()); playerData = loadByUuid(player.getUniqueId());
// Oh you don't have any data? Well now you do
if (playerData == null) if (playerData == null)
{ {
playerData = loadByIp(FUtil.getIp(player)); FLog.info("Creating new player data entry for " + player.getName());
if (playerData != null)
{
plugin.sql.updatePlayerName(playerData.getName(), player.getName());
playerData.setName(player.getName());
dataMap.put(player.getName(), playerData);
return playerData;
}
}
else
{
dataMap.put(player.getName(), playerData);
return playerData;
}
// Create new data if nonexistent
FLog.info("Creating new player verification entry for " + player.getName());
// Create new player
playerData = new PlayerData(player); playerData = new PlayerData(player);
playerData.addIp(FUtil.getIp(player)); playerData.addIp(FUtil.getIp(player));
}
// Store player // Store it in memory.
dataMap.put(player.getName(), playerData); dataMap.put(player.getUniqueId(), playerData);
// Save player // Send it to the SQL database.
plugin.sql.addPlayer(playerData); plugin.sql.addPlayer(playerData);
return playerData;
// Returns it
return playerData;
}
public PlayerData getData(UUID uuid)
{
PlayerData data = dataMap.get(uuid);
if (data == null)
{
data = loadByUuid(uuid);
}
return data;
} }
public PlayerData getData(String username) public PlayerData getData(String username)
{ {
// Check for existing data OfflinePlayer player = server.getPlayer(username);
PlayerData playerData = dataMap.get(username);
if (playerData != null) if (player == null)
{ {
return playerData; player = server.getOfflinePlayer(username);
} }
playerData = loadByName(username); return getData(player.getUniqueId());
if (playerData != null)
{
dataMap.put(username, playerData);
}
else
{
return null;
}
return playerData;
} }
public PlayerData getDataByIp(String ip) public PlayerData getDataByIp(String ip)
@ -294,7 +278,7 @@ public class PlayerList extends FreedomService
if (player != null) if (player != null)
{ {
dataMap.put(player.getName(), player); dataMap.put(player.getUuid(), player);
} }
return player; return player;
@ -305,7 +289,7 @@ public class PlayerList extends FreedomService
return playerMap; return playerMap;
} }
public Map<String, PlayerData> getDataMap() public Map<UUID, PlayerData> getDataMap()
{ {
return dataMap; return dataMap;
} }

View File

@ -7,6 +7,8 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.UUID;
import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.admin.Admin; import me.totalfreedom.totalfreedommod.admin.Admin;
import me.totalfreedom.totalfreedommod.banning.Ban; import me.totalfreedom.totalfreedommod.banning.Ban;
@ -93,7 +95,7 @@ public class SQLite extends FreedomService
{ {
try try
{ {
connection.createStatement().execute("CREATE TABLE `players` (`username` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `notes` VARCHAR, `tag` VARCHAR, `discord_id` VARCHAR, `backup_codes` VARCHAR, `master_builder` BOOLEAN NOT NULL,`verification` BOOLEAN NOT NULL, `ride_mode` VARCHAR NOT NULL, `coins` INT, `items` VARCHAR, `total_votes` INT NOT NULL, `display_discord` BOOLEAN NOT NULL, `login_message` VARCHAR, `inspect` BOOLEAN NOT NULL);"); connection.createStatement().execute("CREATE TABLE `players` (`uuid` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `notes` VARCHAR, `tag` VARCHAR, `discord_id` VARCHAR, `backup_codes` VARCHAR, `master_builder` BOOLEAN NOT NULL,`verification` BOOLEAN NOT NULL, `ride_mode` VARCHAR NOT NULL, `coins` INT, `items` VARCHAR, `total_votes` INT NOT NULL, `display_discord` BOOLEAN NOT NULL, `login_message` VARCHAR, `inspect` BOOLEAN NOT NULL);");
} }
catch (SQLException e) catch (SQLException e)
{ {
@ -151,7 +153,7 @@ public class SQLite extends FreedomService
try try
{ {
Object[] data = {key, player.getName()}; Object[] data = {key, player.getName()};
PreparedStatement statement = connection.prepareStatement(MessageFormat.format("UPDATE players SET {0}=? WHERE username=''{1}''", data)); PreparedStatement statement = connection.prepareStatement(MessageFormat.format("UPDATE players SET {0}=? WHERE uuid=''{1}''", data));
statement = setUnknownType(statement, 1, value); statement = setUnknownType(statement, 1, value);
statement.executeUpdate(); statement.executeUpdate();
@ -177,21 +179,6 @@ public class SQLite extends FreedomService
} }
} }
public void updatePlayerName(String oldName, String newName)
{
try
{
PreparedStatement statement = connection.prepareStatement(MessageFormat.format("UPDATE players SET username=? WHERE username=''{0}''", oldName));
statement = setUnknownType(statement, 1, newName);
statement.executeUpdate();
}
catch (SQLException e)
{
FLog.severe("Failed to update player name: " + e.getMessage());
}
}
public PreparedStatement setUnknownType(PreparedStatement statement, int index, Object value) throws SQLException public PreparedStatement setUnknownType(PreparedStatement statement, int index, Object value) throws SQLException
{ {
if (value == null) if (value == null)
@ -271,7 +258,7 @@ public class SQLite extends FreedomService
try try
{ {
PreparedStatement statement = connection.prepareStatement("INSERT INTO players VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); PreparedStatement statement = connection.prepareStatement("INSERT INTO players VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, player.getName()); statement.setString(1, player.getUuid().toString());
statement.setString(2, FUtil.listToString(player.getIps())); statement.setString(2, FUtil.listToString(player.getIps()));
statement.setString(3, FUtil.listToString(player.getNotes())); statement.setString(3, FUtil.listToString(player.getNotes()));
statement.setString(4, player.getTag()); statement.setString(4, player.getTag());
@ -314,11 +301,11 @@ public class SQLite extends FreedomService
return null; return null;
} }
public ResultSet getPlayerByName(String name) public ResultSet getPlayerByUuid(UUID uuid)
{ {
try try
{ {
ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM players WHERE username=''{0}''", name)); ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM players WHERE uuid=''{0}''", uuid.toString()));
if (resultSet.next()) if (resultSet.next())
{ {
return resultSet; return resultSet;
@ -326,7 +313,7 @@ public class SQLite extends FreedomService
} }
catch (SQLException e) catch (SQLException e)
{ {
FLog.severe("Failed to get player by name:"); FLog.severe("Failed to get player by UUID:");
FLog.severe(e); FLog.severe(e);
} }