From e75b5f3d87c6e0bf7de5e975de9d678a781fb721 Mon Sep 17 00:00:00 2001 From: spacerocket62 Date: Tue, 27 Oct 2020 21:11:23 -0700 Subject: [PATCH] DAOs were deprecated, replaced needed methods in mongo player data instead --- pom.xml | 6 +-- .../plex/cache/MongoPlayerData.java | 31 ++++++----- .../plex/cache/PlexPlayerDAO.java | 53 ------------------- .../plex/cache/SQLPlayerData.java | 3 +- .../plex/listeners/PlayerListener.java | 2 +- .../totalfreedom/plex/player/PlexPlayer.java | 11 +++- .../plex/storage/MongoConnection.java | 12 ++--- .../plex/storage/SQLConnection.java | 23 ++++---- 8 files changed, 50 insertions(+), 91 deletions(-) delete mode 100644 src/main/java/me/totalfreedom/plex/cache/PlexPlayerDAO.java diff --git a/pom.xml b/pom.xml index 53065a0..21dbb8d 100644 --- a/pom.xml +++ b/pom.xml @@ -47,9 +47,9 @@ - org.mongodb.morphia - morphia - 1.3.2 + dev.morphia.morphia + core + 1.6.0 compile diff --git a/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java b/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java index 70bdcc5..4ad54cc 100644 --- a/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java +++ b/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java @@ -1,25 +1,27 @@ package me.totalfreedom.plex.cache; -import java.util.UUID; +import dev.morphia.Datastore; +import dev.morphia.query.Query; +import dev.morphia.query.UpdateOperations; import me.totalfreedom.plex.Plex; import me.totalfreedom.plex.player.PlexPlayer; -import org.mongodb.morphia.query.Query; -import org.mongodb.morphia.query.UpdateOperations; + +import java.util.UUID; public class MongoPlayerData { - private final PlexPlayerDAO plexPlayerDAO; + private final Datastore datastore; public MongoPlayerData() { - this.plexPlayerDAO = new PlexPlayerDAO(PlexPlayer.class, Plex.get().getMongoConnection().getDatastore()); + this.datastore = Plex.get().getMongoConnection().getDatastore(); } public boolean exists(UUID uuid) { - Query query = plexPlayerDAO.createQuery(); + Query query = datastore.createQuery(PlexPlayer.class); - return query.field("uuid").exists().field("uuid").equal(uuid.toString()).get() != null; + return query.field("uuid").exists().field("uuid").equal(uuid.toString()).first() != null; } public PlexPlayer getByUUID(UUID uuid) @@ -29,16 +31,16 @@ public class MongoPlayerData { return PlayerCache.getPlexPlayerMap().get(uuid); } - Query query2 = plexPlayerDAO.createQuery().field("uuid").exists().field("uuid").equal(uuid.toString()); - return query2.get(); + Query query2 = datastore.createQuery(PlexPlayer.class).field("uuid").exists().field("uuid").equal(uuid.toString()); + return query2.first(); } public void update(PlexPlayer player) { - Query filter = plexPlayerDAO.createQuery() + Query filter = datastore.createQuery(PlexPlayer.class) .field("uuid").equal(player.getUuid()); - UpdateOperations updateOps = plexPlayerDAO.createUpdateOperations(); + UpdateOperations updateOps = datastore.createUpdateOperations(PlexPlayer.class); updateOps.set("name", player.getName()); updateOps.set("loginMSG", player.getLoginMSG()); @@ -46,11 +48,12 @@ public class MongoPlayerData updateOps.set("rank", player.getRank().toLowerCase()); updateOps.set("ips", player.getIps()); updateOps.set("coins", player.getCoins()); - plexPlayerDAO.update(filter, updateOps); + datastore.update(filter, updateOps); } - public PlexPlayerDAO getPlexPlayerDAO() + public void save(PlexPlayer plexPlayer) { - return plexPlayerDAO; + datastore.save(plexPlayer); } + } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/plex/cache/PlexPlayerDAO.java b/src/main/java/me/totalfreedom/plex/cache/PlexPlayerDAO.java deleted file mode 100644 index 464bee2..0000000 --- a/src/main/java/me/totalfreedom/plex/cache/PlexPlayerDAO.java +++ /dev/null @@ -1,53 +0,0 @@ -package me.totalfreedom.plex.cache; - -import me.totalfreedom.plex.player.PlexPlayer; -import org.mongodb.morphia.Datastore; -import org.mongodb.morphia.Key; -import org.mongodb.morphia.dao.BasicDAO; -import org.mongodb.morphia.query.Query; -import org.mongodb.morphia.query.UpdateOperations; -import org.mongodb.morphia.query.UpdateResults; - -public class PlexPlayerDAO extends BasicDAO -{ - public PlexPlayerDAO(Class entityclass, Datastore ds) - { - super(entityclass, ds); - } - - @Override - public boolean exists(Query query) - { - return super.exists(query); - } - - @Override - public PlexPlayer findOne(String key, Object value) - { - return super.findOne(key, value); - } - - @Override - public PlexPlayer get(Object id) - { - return super.get(id); - } - - @Override - public UpdateResults update(Query query, UpdateOperations ops) - { - return super.update(query, ops); - } - - @Override - public boolean equals(Object obj) - { - return super.equals(obj); - } - - @Override - public Key save(PlexPlayer entity) - { - return super.save(entity); - } -} diff --git a/src/main/java/me/totalfreedom/plex/cache/SQLPlayerData.java b/src/main/java/me/totalfreedom/plex/cache/SQLPlayerData.java index b941536..814dbf6 100644 --- a/src/main/java/me/totalfreedom/plex/cache/SQLPlayerData.java +++ b/src/main/java/me/totalfreedom/plex/cache/SQLPlayerData.java @@ -54,14 +54,13 @@ public class SQLPlayerData String prefix = set.getString("prefix"); String rankName = set.getString("rank").toUpperCase(); long coins = set.getLong("coins"); - 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.toLowerCase()); + plexPlayer.setRank(rankName); plexPlayer.setIps(ips); plexPlayer.setCoins(coins); } diff --git a/src/main/java/me/totalfreedom/plex/listeners/PlayerListener.java b/src/main/java/me/totalfreedom/plex/listeners/PlayerListener.java index a371178..591b00d 100644 --- a/src/main/java/me/totalfreedom/plex/listeners/PlayerListener.java +++ b/src/main/java/me/totalfreedom/plex/listeners/PlayerListener.java @@ -35,7 +35,7 @@ public class PlayerListener implements Listener plexPlayer.setName(player.getName()); //set the name of the player plexPlayer.setIps(Arrays.asList(player.getAddress().getAddress().getHostAddress().trim())); //set the arraylist of ips - mongoPlayerData.getPlexPlayerDAO().save(plexPlayer); //and put their document in mongo collection + mongoPlayerData.save(plexPlayer); //and put their document in mongo collection } else { diff --git a/src/main/java/me/totalfreedom/plex/player/PlexPlayer.java b/src/main/java/me/totalfreedom/plex/player/PlexPlayer.java index fa1a02e..13c2c0d 100644 --- a/src/main/java/me/totalfreedom/plex/player/PlexPlayer.java +++ b/src/main/java/me/totalfreedom/plex/player/PlexPlayer.java @@ -33,10 +33,12 @@ public class PlexPlayer private long coins; - private Rank rank; + private String rank; private List ips; + public PlexPlayer() {} + public PlexPlayer(UUID playerUUID) { this.uuid = playerUUID.toString(); @@ -52,6 +54,11 @@ public class PlexPlayer this.ips = new ArrayList<>(); - this.rank = null; + this.rank = ""; + } + + public Rank getRankFromString() + { + return Rank.valueOf(rank.toUpperCase()); } } diff --git a/src/main/java/me/totalfreedom/plex/storage/MongoConnection.java b/src/main/java/me/totalfreedom/plex/storage/MongoConnection.java index 4c7bec8..ed09f1a 100644 --- a/src/main/java/me/totalfreedom/plex/storage/MongoConnection.java +++ b/src/main/java/me/totalfreedom/plex/storage/MongoConnection.java @@ -15,15 +15,15 @@ public class MongoConnection public Datastore getDatastore() { - if (!plugin.config().getString("data.central.storage").equalsIgnoreCase("mongodb")) + if (!plugin.config.getString("data.central.storage").equalsIgnoreCase("mongodb")) { return null; } - String host = plugin.config().getString("data.central.hostname"); - int port = plugin.config().getInt("data.central.port"); - String username = plugin.config().getString("data.central.user"); - String password = plugin.config().getString("data.central.password"); - String database = plugin.config().getString("data.central.db"); + String host = plugin.config.getString("data.central.hostname"); + int port = plugin.config.getInt("data.central.port"); + String username = plugin.config.getString("data.central.user"); + String password = plugin.config.getString("data.central.password"); + String database = plugin.config.getString("data.central.db"); String connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=" + database; MongoClient client = new MongoClient(new MongoClientURI(connectionString)); diff --git a/src/main/java/me/totalfreedom/plex/storage/SQLConnection.java b/src/main/java/me/totalfreedom/plex/storage/SQLConnection.java index 41e28a9..d3eb221 100644 --- a/src/main/java/me/totalfreedom/plex/storage/SQLConnection.java +++ b/src/main/java/me/totalfreedom/plex/storage/SQLConnection.java @@ -40,16 +40,19 @@ public class SQLConnection try { - connection.prepareStatement("CREATE TABLE IF NOT EXISTS `players` (\n" + - "\t`uuid` VARCHAR(46),\n" + - "\t`name` VARCHAR(18),\n" + - "\t`login_msg` VARCHAR(256),\n" + - "\t`prefix` VARCHAR(30),\n" + - "\t`rank` VARCHAR(256),\n" + - "\t`ips` VARCHAR(65535),\n" + - "\t`coins` BIGINT,\n" + - "\tPRIMARY KEY (`uuid`)\n" + - ");").execute(); + if (connection != null) + { + connection.prepareStatement("CREATE TABLE IF NOT EXISTS `players` (\n" + + "\t`uuid` VARCHAR(46),\n" + + "\t`name` VARCHAR(18),\n" + + "\t`login_msg` VARCHAR(256),\n" + + "\t`prefix` VARCHAR(30),\n" + + "\t`rank` VARCHAR(256),\n" + + "\t`ips` VARCHAR(65535),\n" + + "\t`coins` BIGINT,\n" + + "\tPRIMARY KEY (`uuid`)\n" + + ");").execute(); + } } catch (SQLException throwables) {