From 1d8ce6001c5502fe01b79217dc41e9ad823a168f Mon Sep 17 00:00:00 2001 From: Taah Date: Wed, 6 Apr 2022 16:59:34 -0700 Subject: [PATCH] add getting players by username --- src/main/java/dev/plex/cache/DataUtils.java | 24 +++++------ .../plex/cache/player/MongoPlayerData.java | 12 ++++++ .../dev/plex/cache/player/SQLPlayerData.java | 43 +++++++++++++++++++ src/main/java/dev/plex/player/PlexPlayer.java | 7 +-- 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/main/java/dev/plex/cache/DataUtils.java b/src/main/java/dev/plex/cache/DataUtils.java index fadb12c..b86e73e 100644 --- a/src/main/java/dev/plex/cache/DataUtils.java +++ b/src/main/java/dev/plex/cache/DataUtils.java @@ -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 * diff --git a/src/main/java/dev/plex/cache/player/MongoPlayerData.java b/src/main/java/dev/plex/cache/player/MongoPlayerData.java index c0e816a..f010153 100644 --- a/src/main/java/dev/plex/cache/player/MongoPlayerData.java +++ b/src/main/java/dev/plex/cache/player/MongoPlayerData.java @@ -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 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 * diff --git a/src/main/java/dev/plex/cache/player/SQLPlayerData.java b/src/main/java/dev/plex/cache/player/SQLPlayerData.java index f082c72..62f198a 100644 --- a/src/main/java/dev/plex/cache/player/SQLPlayerData.java +++ b/src/main/java/dev/plex/cache/player/SQLPlayerData.java @@ -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 ips = new Gson().fromJson(set.getString("ips"), new TypeToken>() + { + }.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 * diff --git a/src/main/java/dev/plex/player/PlexPlayer.java b/src/main/java/dev/plex/player/PlexPlayer.java index 5cc18ed..1b99f35 100644 --- a/src/main/java/dev/plex/player/PlexPlayer.java +++ b/src/main/java/dev/plex/player/PlexPlayer.java @@ -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;