diff --git a/src/main/java/me/totalfreedom/plex/Plex.java b/src/main/java/me/totalfreedom/plex/Plex.java index dfbd2b6..bc7f1a0 100644 --- a/src/main/java/me/totalfreedom/plex/Plex.java +++ b/src/main/java/me/totalfreedom/plex/Plex.java @@ -4,6 +4,7 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; import me.totalfreedom.plex.cache.MongoPlayerData; +import me.totalfreedom.plex.cache.SQLPlayerData; import me.totalfreedom.plex.config.Config; import me.totalfreedom.plex.config.YamlConfig; import me.totalfreedom.plex.listeners.PlayerListener; @@ -27,6 +28,7 @@ public class Plex extends JavaPlugin private MongoConnection mongoConnection; private MongoPlayerData mongoPlayerData; + private SQLPlayerData sqlPlayerData; @Override public void onLoad() @@ -50,6 +52,8 @@ public class Plex extends JavaPlugin if (storageType == StorageType.MONGO) { mongoPlayerData = new MongoPlayerData(); + } else { + sqlPlayerData = new SQLPlayerData(); } getServer().getPluginManager().registerEvents(new PlayerListener(), this); diff --git a/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java b/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java index 83ca26b..b8ec048 100644 --- a/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java +++ b/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java @@ -13,9 +13,6 @@ import java.util.UUID; public class MongoPlayerData { - private Map punishedPlayerMap = Maps.newHashMap(); - private Map plexPlayerMap = Maps.newHashMap(); - private PlexPlayerDAO plexPlayerDAO; public MongoPlayerData() @@ -36,9 +33,9 @@ public class MongoPlayerData public PlexPlayer getByUUID(UUID uuid) { - if (plexPlayerMap.containsKey(uuid)) + if (PlayerCache.getPlexPlayerMap().containsKey(uuid)) { - return plexPlayerMap.get(uuid); + return PlayerCache.getPlexPlayerMap().get(uuid); } Query query2 = plexPlayerDAO.createQuery().field("uuid").exists().field("uuid").equal(uuid.toString()); return query2.first(); @@ -59,15 +56,6 @@ public class MongoPlayerData plexPlayerDAO.update(filter, updateOps); } - - public Map getPlexPlayerMap() { - return plexPlayerMap; - } - - public Map getPunishedPlayerMap() { - return punishedPlayerMap; - } - public PlexPlayerDAO getPlexPlayerDAO() { return plexPlayerDAO; } diff --git a/src/main/java/me/totalfreedom/plex/cache/PlayerCache.java b/src/main/java/me/totalfreedom/plex/cache/PlayerCache.java new file mode 100644 index 0000000..b5670ac --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/cache/PlayerCache.java @@ -0,0 +1,23 @@ +package me.totalfreedom.plex.cache; + +import com.google.common.collect.Maps; +import me.totalfreedom.plex.player.PlexPlayer; +import me.totalfreedom.plex.player.PunishedPlayer; + +import java.util.Map; +import java.util.UUID; + +public class PlayerCache +{ + + private static Map plexPlayerMap = Maps.newHashMap(); + private static Map punishedPlayerMap = Maps.newHashMap(); + + public static Map getPunishedPlayerMap() { + return punishedPlayerMap; + } + + public static Map getPlexPlayerMap() { + return plexPlayerMap; + } +} diff --git a/src/main/java/me/totalfreedom/plex/cache/SQLPlayerData.java b/src/main/java/me/totalfreedom/plex/cache/SQLPlayerData.java new file mode 100644 index 0000000..431865f --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/cache/SQLPlayerData.java @@ -0,0 +1,115 @@ +package me.totalfreedom.plex.cache; + +import com.google.common.collect.Maps; +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; +import dev.morphia.query.Query; +import dev.morphia.query.UpdateOperations; +import me.totalfreedom.plex.Plex; +import me.totalfreedom.plex.player.PlexPlayer; +import me.totalfreedom.plex.player.PunishedPlayer; +import me.totalfreedom.plex.rank.Rank; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public class SQLPlayerData +{ + + private final String SELECT = "SELECT * FROM `players` WHERE uuid=?"; + private final String UPDATE = "UPDATE `players` SET name=?, login_msg=?, prefix=?, rank=?, ips=? WHERE uuid=?"; + private final String INSERT = "INSERT INTO `players` (`uuid`, `name`, `login_msg`, `prefix`, `rank`, `ips`) VALUES (?, ?, ?, ?, ?, ?);"; + + public boolean exists(UUID uuid) + { + try (Connection con = Plex.get().getSqlConnection().getCon()) + { + PreparedStatement statement = con.prepareStatement(SELECT); + statement.setString(1, uuid.toString()); + ResultSet set = statement.executeQuery(); + return set.next(); + } + catch (SQLException throwables) { + throwables.printStackTrace(); + } + return false; + } + + public PlexPlayer getByUUID(UUID uuid) + { + if (PlayerCache.getPlexPlayerMap().containsKey(uuid)) + { + return PlayerCache.getPlexPlayerMap().get(uuid); + } + + try (Connection con = Plex.get().getSqlConnection().getCon()) + { + PreparedStatement statement = con.prepareStatement(SELECT); + statement.setString(1, uuid.toString()); + ResultSet set = statement.executeQuery(); + PlexPlayer plexPlayer = new PlexPlayer(uuid); + while (set.next()) + { + String name = set.getString("name"); + String loginMSG = set.getString("login_msg"); + String prefix = set.getString("prefix"); + String rankName = set.getString("rank").toUpperCase(); + Rank rank = Rank.valueOf(rankName); + List ips = new Gson().fromJson(set.getString("ips"), new TypeToken>(){}.getType()); + plexPlayer.setName(name); + plexPlayer.setLoginMSG(loginMSG); + plexPlayer.setPrefix(prefix); + plexPlayer.setRank(rankName.isEmpty() ? null : rank); + plexPlayer.setIps(ips); + } + return plexPlayer; + } + catch (SQLException throwables) { + throwables.printStackTrace(); + } + return null; + } + + public void update(PlexPlayer player) + { + try (Connection con = Plex.get().getSqlConnection().getCon()) + { + PreparedStatement statement = con.prepareStatement(UPDATE); + statement.setString(1, player.getName()); + statement.setString(2, player.getLoginMSG()); + statement.setString(3, player.getPrefix()); + statement.setString(4, player.getRank() == null ? "" : player.getRank().name().toLowerCase()); + statement.setString(5, new Gson().toJson(player.getIps())); + statement.setString(6, player.getUuid()); + statement.executeUpdate(); + } + catch (SQLException throwables) { + throwables.printStackTrace(); + } + } + + public void insert(PlexPlayer player) + { + try (Connection con = Plex.get().getSqlConnection().getCon()) + { + PreparedStatement statement = con.prepareStatement(INSERT); + statement.setString(1, player.getUuid()); + statement.setString(2, player.getName()); + statement.setString(3, player.getLoginMSG()); + statement.setString(4, player.getPrefix()); + statement.setString(5, player.getRank() == null ? "" : player.getRank().name().toLowerCase()); + statement.setString(6, new Gson().toJson(player.getIps())); + statement.execute(); + } + catch (SQLException throwables) { + throwables.printStackTrace(); + } + } + +} diff --git a/src/main/java/me/totalfreedom/plex/listeners/PlayerListener.java b/src/main/java/me/totalfreedom/plex/listeners/PlayerListener.java index 63021e1..3b97e47 100644 --- a/src/main/java/me/totalfreedom/plex/listeners/PlayerListener.java +++ b/src/main/java/me/totalfreedom/plex/listeners/PlayerListener.java @@ -2,6 +2,8 @@ package me.totalfreedom.plex.listeners; import me.totalfreedom.plex.Plex; import me.totalfreedom.plex.cache.MongoPlayerData; +import me.totalfreedom.plex.cache.PlayerCache; +import me.totalfreedom.plex.cache.SQLPlayerData; import me.totalfreedom.plex.player.PlexPlayer; import me.totalfreedom.plex.player.PunishedPlayer; import me.totalfreedom.plex.util.PlexLog; @@ -17,46 +19,67 @@ public class PlayerListener implements Listener { private MongoPlayerData mongoPlayerData = Plex.get().getMongoPlayerData() != null ? Plex.get().getMongoPlayerData() : null; + private SQLPlayerData sqlPlayerData = Plex.get().getSqlPlayerData() != null ? Plex.get().getSqlPlayerData() : null; @EventHandler public void onJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); + + PlexPlayer plexPlayer = null; + if (mongoPlayerData != null) // Alright, check if we're saving with Mongo first { if (!mongoPlayerData.exists(player.getUniqueId())) //okay, we're saving with mongo! now check if the player's document exists { PlexLog.log("AYO THIS MAN DONT EXIST"); // funi msg - PlexPlayer plexPlayer = new PlexPlayer(player.getUniqueId()); //it doesn't! okay so now create the object + plexPlayer = new PlexPlayer(player.getUniqueId()); //it doesn't! okay so now create the object plexPlayer.setName(player.getName()); //set the name of the player plexPlayer.setIps(Arrays.asList(player.getAddress().getAddress().getHostAddress().trim())); //set the arraylist of ips - Plex.get().getMongoPlayerData().getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache - Plex.get().getMongoPlayerData().getPunishedPlayerMap().put(player.getUniqueId(), new PunishedPlayer(player.getUniqueId())); - - Plex.get().getMongoPlayerData().getPlexPlayerDAO().save(plexPlayer); //and put their document in mongo collection + mongoPlayerData.getPlexPlayerDAO().save(plexPlayer); //and put their document in mongo collection } else { - PlexPlayer plexPlayer = Plex.get().getMongoPlayerData().getByUUID(player.getUniqueId()); //oh they do exist! + plexPlayer = mongoPlayerData.getByUUID(player.getUniqueId()); //oh they do exist! + plexPlayer.setName(plexPlayer.getName()); //set the name! + } + } else if (sqlPlayerData != null) + { + if (!sqlPlayerData.exists(player.getUniqueId())) //okay, we're saving with mongo! now check if the player's document exists + { + PlexLog.log("AYO THIS MAN DONT EXIST"); // funi msg + plexPlayer = new PlexPlayer(player.getUniqueId()); //it doesn't! okay so now create the object + plexPlayer.setName(player.getName()); //set the name of the player + plexPlayer.setIps(Arrays.asList(player.getAddress().getAddress().getHostAddress().trim())); //set the arraylist of ips + + sqlPlayerData.insert(plexPlayer); //and put their document in mongo collection + + } else { + plexPlayer = sqlPlayerData.getByUUID(player.getUniqueId()); //oh they do exist! plexPlayer.setName(plexPlayer.getName()); //set the name! - Plex.get().getMongoPlayerData().getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //cache them! - Plex.get().getMongoPlayerData().getPunishedPlayerMap().put(player.getUniqueId(), new PunishedPlayer(player.getUniqueId())); } } + + PlayerCache.getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache + PlayerCache.getPunishedPlayerMap().put(player.getUniqueId(), new PunishedPlayer(player.getUniqueId())); } @EventHandler public void onQuit(PlayerQuitEvent event) { + PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId()); //get the player because it's literally impossible for them to not have an object + if (mongoPlayerData != null) //back to mongo checking { - PlexPlayer plexPlayer = Plex.get().getMongoPlayerData().getPlexPlayerMap().get(event.getPlayer().getUniqueId()); //get the player because it's literally impossible for them to not have an object - Plex.get().getMongoPlayerData().update(plexPlayer); //update the player's document - - Plex.get().getMongoPlayerData().getPlexPlayerMap().remove(event.getPlayer().getUniqueId()); //remove them from cache - Plex.get().getMongoPlayerData().getPunishedPlayerMap().remove(event.getPlayer().getUniqueId()); + mongoPlayerData.update(plexPlayer); //update the player's document + } else if (sqlPlayerData != null) + { + sqlPlayerData.update(plexPlayer); } + PlayerCache.getPlexPlayerMap().remove(event.getPlayer().getUniqueId()); //remove them from cache + PlayerCache.getPunishedPlayerMap().remove(event.getPlayer().getUniqueId()); + } } diff --git a/src/main/java/me/totalfreedom/plex/storage/SQLConnection.java b/src/main/java/me/totalfreedom/plex/storage/SQLConnection.java index 4bcaa69..9b2c2aa 100644 --- a/src/main/java/me/totalfreedom/plex/storage/SQLConnection.java +++ b/src/main/java/me/totalfreedom/plex/storage/SQLConnection.java @@ -47,7 +47,6 @@ public class SQLConnection "\t`ips` VARCHAR(65535),\n" + "\tPRIMARY KEY (`uuid`)\n" + ");").execute(); - PlexLog.log("Successfully created table `players`!"); } catch (SQLException throwables) { throwables.printStackTrace(); }