From 6e768c0e0f933d3b7c48f2ade31a9f8aa5bd4ef4 Mon Sep 17 00:00:00 2001 From: Taah Date: Wed, 30 Mar 2022 23:50:53 -0700 Subject: [PATCH] find player by ip + reload system on /plex reload --- src/main/java/dev/plex/cache/DataUtils.java | 25 +++++++++ .../java/dev/plex/cache/MongoPlayerData.java | 21 +++++++ .../java/dev/plex/cache/SQLPlayerData.java | 56 +++++++++++++++++++ .../java/dev/plex/command/impl/PlexCMD.java | 1 + 4 files changed, 103 insertions(+) diff --git a/src/main/java/dev/plex/cache/DataUtils.java b/src/main/java/dev/plex/cache/DataUtils.java index ad47056..4a12dc4 100644 --- a/src/main/java/dev/plex/cache/DataUtils.java +++ b/src/main/java/dev/plex/cache/DataUtils.java @@ -53,6 +53,31 @@ public class DataUtils } } + /** + * Gets a player from cache or from the database + * + * @param ip The IP address of the player. + * @return a PlexPlayer object + * @see PlexPlayer + */ + public static PlexPlayer getPlayerByIP(String ip) + { + PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null); + if (player != null) + { + return player; + } + + if (Plex.get().getStorageType() == StorageType.MONGODB) + { + return Plex.get().getMongoPlayerData().getByIP(ip); + } + else + { + return Plex.get().getSqlPlayerData().getByIP(ip); + } + } + /** * Gets a player from cache or from the database * diff --git a/src/main/java/dev/plex/cache/MongoPlayerData.java b/src/main/java/dev/plex/cache/MongoPlayerData.java index 1e355a9..51116ef 100644 --- a/src/main/java/dev/plex/cache/MongoPlayerData.java +++ b/src/main/java/dev/plex/cache/MongoPlayerData.java @@ -7,6 +7,8 @@ import dev.morphia.query.experimental.filters.Filters; import dev.morphia.query.experimental.updates.UpdateOperators; import dev.plex.Plex; import dev.plex.player.PlexPlayer; + +import java.util.Collections; import java.util.UUID; /** @@ -59,6 +61,25 @@ public class MongoPlayerData return query2.first(); } + /** + * Gets the player from cache or from mongo's database + * + * @param ip The IP address of the player. + * @return a PlexPlayer object + * @see PlexPlayer + */ + public PlexPlayer getByIP(String ip) + { + PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null); + if (player != null) + { + return player; + } + + Query query2 = datastore.find(PlexPlayer.class).filter(Filters.in("ips", Collections.singleton(ip))); + return query2.first(); + } + /** * Updates a player's information in the mongo database * diff --git a/src/main/java/dev/plex/cache/SQLPlayerData.java b/src/main/java/dev/plex/cache/SQLPlayerData.java index 3caf4f3..5a8a784 100644 --- a/src/main/java/dev/plex/cache/SQLPlayerData.java +++ b/src/main/java/dev/plex/cache/SQLPlayerData.java @@ -10,6 +10,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.UUID; +import java.util.concurrent.CompletableFuture; /** * SQL fetching utilities for players @@ -92,6 +93,61 @@ public class SQLPlayerData return null; } + /** + * Gets the player from cache or from the SQL database + * + * @param ip The IP address of the player. + * @return a PlexPlayer object + * @see PlexPlayer + */ + public PlexPlayer getByIP(String ip) + { + PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null); + if (player != null) + { + return player; + } + + try (Connection con = Plex.get().getSqlConnection().getCon()) + { + PreparedStatement statement = con.prepareStatement("select * from `players` where json_search(ips, ?, ?) IS NOT NULL LIMIT 1"); + statement.setString(1, "one"); + statement.setString(2, ip); + ResultSet set = statement.executeQuery(); + + PlexPlayer plexPlayer = null; + while (set.next()) + { + String uuid = set.getString("uuid"); + String name = set.getString("name"); + 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 = new PlexPlayer(UUID.fromString(uuid)); + plexPlayer.setName(name); + plexPlayer.setLoginMessage(loginMSG); + plexPlayer.setPrefix(prefix); + plexPlayer.setRank(rankName); + plexPlayer.setIps(ips); + plexPlayer.setCoins(coins); + plexPlayer.setVanished(vanished); + plexPlayer.setCommandSpy(commandspy); + } + return plexPlayer; + } + catch (SQLException throwables) + { + throwables.printStackTrace(); + } + return null; + } + /** * Updates a player's information in the SQL database * diff --git a/src/main/java/dev/plex/command/impl/PlexCMD.java b/src/main/java/dev/plex/command/impl/PlexCMD.java index 8ef69a9..458060d 100644 --- a/src/main/java/dev/plex/command/impl/PlexCMD.java +++ b/src/main/java/dev/plex/command/impl/PlexCMD.java @@ -50,6 +50,7 @@ public class PlexCMD extends PlexCommand plugin.getRankManager().importDefaultRanks(); send(sender, "Imported ranks"); send(sender, "Plex successfully reloaded."); + plugin.setSystem(plugin.config.getString("commands.permissions")); return null; } else if (args[0].equalsIgnoreCase("redis"))