mirror of
https://github.com/AtlasMediaGroup/TotalFreedomMod.git
synced 2024-12-30 13:27:37 +00:00
Stage 1 of IP -> UUID migration: changing player data to use UUIDs instead
This commit is contained in:
parent
b656925e4f
commit
98388d0d23
@ -3,24 +3,23 @@ package me.totalfreedom.totalfreedommod.player;
|
||||
import com.google.common.collect.Lists;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
||||
import me.totalfreedom.totalfreedommod.shop.ShopItem;
|
||||
import me.totalfreedom.totalfreedommod.util.FLog;
|
||||
import me.totalfreedom.totalfreedommod.util.FUtil;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class PlayerData
|
||||
{
|
||||
private UUID uuid;
|
||||
private final List<String> ips = Lists.newArrayList();
|
||||
private final List<String> notes = Lists.newArrayList();
|
||||
private final List<String> backupCodes = Lists.newArrayList();
|
||||
private String name;
|
||||
private String tag = null;
|
||||
private String discordID = null;
|
||||
private Boolean masterBuilder = false;
|
||||
@ -48,7 +47,7 @@ public class PlayerData
|
||||
{
|
||||
try
|
||||
{
|
||||
name = resultSet.getString("username");
|
||||
uuid = UUID.fromString(resultSet.getString("uuid"));
|
||||
ips.clear();
|
||||
ips.addAll(FUtil.stringToList(resultSet.getString("ips")));
|
||||
notes.clear();
|
||||
@ -88,14 +87,13 @@ public class PlayerData
|
||||
|
||||
public PlayerData(Player player)
|
||||
{
|
||||
this.name = player.getName();
|
||||
this.uuid = player.getUniqueId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
|
||||
return "Player: " + name + "\n" +
|
||||
return "Player: " + getName() + "\n" +
|
||||
"- IPs: " + StringUtils.join(ips, ", ") + "\n" +
|
||||
"- Discord ID: " + discordID + "\n" +
|
||||
"- Master Builder: " + masterBuilder + "\n" +
|
||||
@ -232,7 +230,7 @@ public class PlayerData
|
||||
{
|
||||
return new HashMap<String, Object>()
|
||||
{{
|
||||
put("username", name);
|
||||
put("uuid", uuid.toString());
|
||||
put("ips", FUtil.listToString(ips));
|
||||
put("notes", FUtil.listToString(notes));
|
||||
put("tag", tag);
|
||||
@ -255,14 +253,14 @@ public class PlayerData
|
||||
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()
|
||||
|
@ -3,11 +3,8 @@ package me.totalfreedom.totalfreedommod.player;
|
||||
import com.google.common.collect.Maps;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
|
||||
import me.totalfreedom.totalfreedommod.FreedomService;
|
||||
import me.totalfreedom.totalfreedommod.admin.Admin;
|
||||
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, PlayerData> dataMap = Maps.newHashMap(); // username, data
|
||||
public final Map<UUID, PlayerData> dataMap = Maps.newHashMap(); // uuid, data
|
||||
|
||||
@Override
|
||||
public void onStart()
|
||||
@ -57,7 +54,7 @@ public class PlayerList extends FreedomService
|
||||
while (resultSet.next())
|
||||
{
|
||||
PlayerData playerData = load(resultSet);
|
||||
dataMap.put(playerData.getName(), playerData);
|
||||
dataMap.put(playerData.getUuid(), playerData);
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
@ -122,9 +119,9 @@ public class PlayerList extends FreedomService
|
||||
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)
|
||||
@ -204,7 +201,7 @@ public class PlayerList extends FreedomService
|
||||
{
|
||||
try
|
||||
{
|
||||
ResultSet currentSave = plugin.sql.getPlayerByName(player.getName());
|
||||
ResultSet currentSave = plugin.sql.getPlayerByUuid(player.getUuid());
|
||||
for (Map.Entry<String, Object> entry : player.toSQLStorable().entrySet())
|
||||
{
|
||||
Object storedValue = plugin.sql.getValue(currentSave, entry.getKey(), entry.getValue());
|
||||
@ -223,69 +220,56 @@ public class PlayerList extends FreedomService
|
||||
public PlayerData getData(Player player)
|
||||
{
|
||||
// Check for existing data
|
||||
PlayerData playerData = dataMap.get(player.getName());
|
||||
PlayerData playerData = dataMap.get(player.getUniqueId());
|
||||
if (playerData != null)
|
||||
{
|
||||
return playerData;
|
||||
}
|
||||
|
||||
// Load data
|
||||
playerData = loadByName(player.getName());
|
||||
playerData = loadByUuid(player.getUniqueId());
|
||||
|
||||
// Oh you don't have any data? Well now you do
|
||||
if (playerData == null)
|
||||
{
|
||||
playerData = loadByIp(FUtil.getIp(player));
|
||||
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;
|
||||
FLog.info("Creating new player data entry for " + player.getName());
|
||||
|
||||
playerData = new PlayerData(player);
|
||||
playerData.addIp(FUtil.getIp(player));
|
||||
}
|
||||
|
||||
// Create new data if nonexistent
|
||||
FLog.info("Creating new player verification entry for " + player.getName());
|
||||
// Store it in memory.
|
||||
dataMap.put(player.getUniqueId(), playerData);
|
||||
|
||||
// Create new player
|
||||
playerData = new PlayerData(player);
|
||||
playerData.addIp(FUtil.getIp(player));
|
||||
|
||||
// Store player
|
||||
dataMap.put(player.getName(), playerData);
|
||||
|
||||
// Save player
|
||||
// Send it to the SQL database.
|
||||
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)
|
||||
{
|
||||
// Check for existing data
|
||||
PlayerData playerData = dataMap.get(username);
|
||||
if (playerData != null)
|
||||
OfflinePlayer player = server.getPlayer(username);
|
||||
|
||||
if (player == null)
|
||||
{
|
||||
return playerData;
|
||||
player = server.getOfflinePlayer(username);
|
||||
}
|
||||
|
||||
playerData = loadByName(username);
|
||||
|
||||
if (playerData != null)
|
||||
{
|
||||
dataMap.put(username, playerData);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return playerData;
|
||||
return getData(player.getUniqueId());
|
||||
}
|
||||
|
||||
public PlayerData getDataByIp(String ip)
|
||||
@ -294,7 +278,7 @@ public class PlayerList extends FreedomService
|
||||
|
||||
if (player != null)
|
||||
{
|
||||
dataMap.put(player.getName(), player);
|
||||
dataMap.put(player.getUuid(), player);
|
||||
}
|
||||
|
||||
return player;
|
||||
@ -305,7 +289,7 @@ public class PlayerList extends FreedomService
|
||||
return playerMap;
|
||||
}
|
||||
|
||||
public Map<String, PlayerData> getDataMap()
|
||||
public Map<UUID, PlayerData> getDataMap()
|
||||
{
|
||||
return dataMap;
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.UUID;
|
||||
|
||||
import me.totalfreedom.totalfreedommod.FreedomService;
|
||||
import me.totalfreedom.totalfreedommod.admin.Admin;
|
||||
import me.totalfreedom.totalfreedommod.banning.Ban;
|
||||
@ -93,7 +95,7 @@ public class SQLite extends FreedomService
|
||||
{
|
||||
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)
|
||||
{
|
||||
@ -151,7 +153,7 @@ public class SQLite extends FreedomService
|
||||
try
|
||||
{
|
||||
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.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
|
||||
{
|
||||
if (value == null)
|
||||
@ -271,7 +258,7 @@ public class SQLite extends FreedomService
|
||||
try
|
||||
{
|
||||
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(3, FUtil.listToString(player.getNotes()));
|
||||
statement.setString(4, player.getTag());
|
||||
@ -314,11 +301,11 @@ public class SQLite extends FreedomService
|
||||
return null;
|
||||
}
|
||||
|
||||
public ResultSet getPlayerByName(String name)
|
||||
public ResultSet getPlayerByUuid(UUID uuid)
|
||||
{
|
||||
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())
|
||||
{
|
||||
return resultSet;
|
||||
@ -326,7 +313,7 @@ public class SQLite extends FreedomService
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
FLog.severe("Failed to get player by name:");
|
||||
FLog.severe("Failed to get player by UUID:");
|
||||
FLog.severe(e);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user