DAOs were deprecated, replaced needed methods in mongo player data instead

This commit is contained in:
spacerocket62 2020-10-27 21:11:23 -07:00
parent f9630c9597
commit e75b5f3d87
8 changed files with 50 additions and 91 deletions

View File

@ -47,9 +47,9 @@
<!-- DATABASING --> <!-- DATABASING -->
<dependency> <dependency>
<groupId>org.mongodb.morphia</groupId> <groupId>dev.morphia.morphia</groupId>
<artifactId>morphia</artifactId> <artifactId>core</artifactId>
<version>1.3.2</version> <version>1.6.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -1,25 +1,27 @@
package me.totalfreedom.plex.cache; 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.Plex;
import me.totalfreedom.plex.player.PlexPlayer; 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 public class MongoPlayerData
{ {
private final PlexPlayerDAO plexPlayerDAO; private final Datastore datastore;
public MongoPlayerData() public MongoPlayerData()
{ {
this.plexPlayerDAO = new PlexPlayerDAO(PlexPlayer.class, Plex.get().getMongoConnection().getDatastore()); this.datastore = Plex.get().getMongoConnection().getDatastore();
} }
public boolean exists(UUID uuid) public boolean exists(UUID uuid)
{ {
Query<PlexPlayer> query = plexPlayerDAO.createQuery(); Query<PlexPlayer> 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) public PlexPlayer getByUUID(UUID uuid)
@ -29,16 +31,16 @@ public class MongoPlayerData
{ {
return PlayerCache.getPlexPlayerMap().get(uuid); return PlayerCache.getPlexPlayerMap().get(uuid);
} }
Query<PlexPlayer> query2 = plexPlayerDAO.createQuery().field("uuid").exists().field("uuid").equal(uuid.toString()); Query<PlexPlayer> query2 = datastore.createQuery(PlexPlayer.class).field("uuid").exists().field("uuid").equal(uuid.toString());
return query2.get(); return query2.first();
} }
public void update(PlexPlayer player) public void update(PlexPlayer player)
{ {
Query<PlexPlayer> filter = plexPlayerDAO.createQuery() Query<PlexPlayer> filter = datastore.createQuery(PlexPlayer.class)
.field("uuid").equal(player.getUuid()); .field("uuid").equal(player.getUuid());
UpdateOperations<PlexPlayer> updateOps = plexPlayerDAO.createUpdateOperations(); UpdateOperations<PlexPlayer> updateOps = datastore.createUpdateOperations(PlexPlayer.class);
updateOps.set("name", player.getName()); updateOps.set("name", player.getName());
updateOps.set("loginMSG", player.getLoginMSG()); updateOps.set("loginMSG", player.getLoginMSG());
@ -46,11 +48,12 @@ public class MongoPlayerData
updateOps.set("rank", player.getRank().toLowerCase()); updateOps.set("rank", player.getRank().toLowerCase());
updateOps.set("ips", player.getIps()); updateOps.set("ips", player.getIps());
updateOps.set("coins", player.getCoins()); 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);
} }
} }

View File

@ -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<PlexPlayer, Object>
{
public PlexPlayerDAO(Class<PlexPlayer> entityclass, Datastore ds)
{
super(entityclass, ds);
}
@Override
public boolean exists(Query<PlexPlayer> 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<PlexPlayer> query, UpdateOperations<PlexPlayer> ops)
{
return super.update(query, ops);
}
@Override
public boolean equals(Object obj)
{
return super.equals(obj);
}
@Override
public Key<PlexPlayer> save(PlexPlayer entity)
{
return super.save(entity);
}
}

View File

@ -54,14 +54,13 @@ public class SQLPlayerData
String prefix = set.getString("prefix"); String prefix = set.getString("prefix");
String rankName = set.getString("rank").toUpperCase(); String rankName = set.getString("rank").toUpperCase();
long coins = set.getLong("coins"); long coins = set.getLong("coins");
Rank rank = Rank.valueOf(rankName);
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>() List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>()
{ {
}.getType()); }.getType());
plexPlayer.setName(name); plexPlayer.setName(name);
plexPlayer.setLoginMSG(loginMSG); plexPlayer.setLoginMSG(loginMSG);
plexPlayer.setPrefix(prefix); plexPlayer.setPrefix(prefix);
plexPlayer.setRank(rankName.toLowerCase()); plexPlayer.setRank(rankName);
plexPlayer.setIps(ips); plexPlayer.setIps(ips);
plexPlayer.setCoins(coins); plexPlayer.setCoins(coins);
} }

View File

@ -35,7 +35,7 @@ public class PlayerListener implements Listener
plexPlayer.setName(player.getName()); //set the name of the player plexPlayer.setName(player.getName()); //set the name of the player
plexPlayer.setIps(Arrays.asList(player.getAddress().getAddress().getHostAddress().trim())); //set the arraylist of ips 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 else
{ {

View File

@ -33,10 +33,12 @@ public class PlexPlayer
private long coins; private long coins;
private Rank rank; private String rank;
private List<String> ips; private List<String> ips;
public PlexPlayer() {}
public PlexPlayer(UUID playerUUID) public PlexPlayer(UUID playerUUID)
{ {
this.uuid = playerUUID.toString(); this.uuid = playerUUID.toString();
@ -52,6 +54,11 @@ public class PlexPlayer
this.ips = new ArrayList<>(); this.ips = new ArrayList<>();
this.rank = null; this.rank = "";
}
public Rank getRankFromString()
{
return Rank.valueOf(rank.toUpperCase());
} }
} }

View File

@ -15,15 +15,15 @@ public class MongoConnection
public Datastore getDatastore() public Datastore getDatastore()
{ {
if (!plugin.config().getString("data.central.storage").equalsIgnoreCase("mongodb")) if (!plugin.config.getString("data.central.storage").equalsIgnoreCase("mongodb"))
{ {
return null; return null;
} }
String host = plugin.config().getString("data.central.hostname"); String host = plugin.config.getString("data.central.hostname");
int port = plugin.config().getInt("data.central.port"); int port = plugin.config.getInt("data.central.port");
String username = plugin.config().getString("data.central.user"); String username = plugin.config.getString("data.central.user");
String password = plugin.config().getString("data.central.password"); String password = plugin.config.getString("data.central.password");
String database = plugin.config().getString("data.central.db"); String database = plugin.config.getString("data.central.db");
String connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=" + database; String connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=" + database;
MongoClient client = new MongoClient(new MongoClientURI(connectionString)); MongoClient client = new MongoClient(new MongoClientURI(connectionString));

View File

@ -40,16 +40,19 @@ public class SQLConnection
try try
{ {
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `players` (\n" + if (connection != null)
"\t`uuid` VARCHAR(46),\n" + {
"\t`name` VARCHAR(18),\n" + connection.prepareStatement("CREATE TABLE IF NOT EXISTS `players` (\n" +
"\t`login_msg` VARCHAR(256),\n" + "\t`uuid` VARCHAR(46),\n" +
"\t`prefix` VARCHAR(30),\n" + "\t`name` VARCHAR(18),\n" +
"\t`rank` VARCHAR(256),\n" + "\t`login_msg` VARCHAR(256),\n" +
"\t`ips` VARCHAR(65535),\n" + "\t`prefix` VARCHAR(30),\n" +
"\t`coins` BIGINT,\n" + "\t`rank` VARCHAR(256),\n" +
"\tPRIMARY KEY (`uuid`)\n" + "\t`ips` VARCHAR(65535),\n" +
");").execute(); "\t`coins` BIGINT,\n" +
"\tPRIMARY KEY (`uuid`)\n" +
");").execute();
}
} }
catch (SQLException throwables) catch (SQLException throwables)
{ {