add getting players by username

This commit is contained in:
Taah 2022-04-06 16:59:34 -07:00
parent 0b9f9d74ba
commit 1d8ce6001c
4 changed files with 71 additions and 15 deletions

View File

@ -55,6 +55,18 @@ public class DataUtils
}
}
public static PlexPlayer getPlayer(String username)
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return Plex.get().getMongoPlayerData().getByName(username);
}
else
{
return Plex.get().getSqlPlayerData().getByName(username);
}
}
/**
* Gets a player from cache or from the database
*
@ -80,18 +92,6 @@ public class DataUtils
}
}
/**
* Gets a player from cache or from the database
*
* @param name Username of the player
* @return a PlexPlayer object
* @see PlexPlayer
*/
public static PlexPlayer getPlayer(String name)
{
return getPlayer(Bukkit.getPlayer(name).getUniqueId());
}
/**
* Updates a player's information in the database
*

View File

@ -61,6 +61,18 @@ public class MongoPlayerData
return query2.first();
}
public PlexPlayer getByName(String username)
{
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getName().equalsIgnoreCase(username)).findFirst().orElse(null);
if (player != null)
{
return player;
}
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.regex("name").caseInsensitive().pattern(username));
return query2.first();
}
/**
* Gets the player from cache or from mongo's database
*

View File

@ -92,6 +92,49 @@ public class SQLPlayerData
return null;
}
public PlexPlayer getByName(String username)
{
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getName().equalsIgnoreCase(username)).findFirst().orElse(null);
if (player != null)
{
return player;
}
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement("SELECT * FROM `players` WHERE name=? LIMIT 1");
statement.setString(1, username);
ResultSet set = statement.executeQuery();
while (set.next())
{
PlexPlayer plexPlayer = new PlexPlayer(UUID.fromString(set.getString("uuid")));
String loginMSG = set.getString("login_msg");
String prefix = set.getString("prefix");
String rankName = set.getString("rank").toUpperCase();
long coins = set.getLong("coins");
boolean vanished = set.getBoolean("vanished");
boolean commandspy = set.getBoolean("commandspy");
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>()
{
}.getType());
plexPlayer.setName(username);
plexPlayer.setLoginMessage(loginMSG);
plexPlayer.setPrefix(prefix);
plexPlayer.setRank(rankName);
plexPlayer.setIps(ips);
plexPlayer.setCoins(coins);
plexPlayer.setVanished(vanished);
plexPlayer.setCommandSpy(commandspy);
return plexPlayer;
}
return null;
}
catch (SQLException throwables)
{
throwables.printStackTrace();
}
return null;
}
/**
* Gets the player from cache or from the SQL database
*

View File

@ -43,9 +43,10 @@ public class PlexPlayer
private boolean vanished;
private boolean commandSpy;
private boolean frozen;
private boolean muted;
private boolean lockedUp;
// These fields are transient so MongoDB doesn't automatically drop them in.
private transient boolean frozen;
private transient boolean muted;
private transient boolean lockedUp;
private long coins;