2020-10-27 20:12:38 +00:00
|
|
|
package me.totalfreedom.plex.cache;
|
|
|
|
|
2020-10-28 04:11:23 +00:00
|
|
|
import dev.morphia.Datastore;
|
|
|
|
import dev.morphia.query.Query;
|
2020-10-28 19:07:02 +00:00
|
|
|
import dev.morphia.query.Update;
|
|
|
|
import dev.morphia.query.experimental.filters.Filters;
|
|
|
|
import dev.morphia.query.experimental.updates.UpdateOperators;
|
2020-11-06 01:29:38 +00:00
|
|
|
import java.util.UUID;
|
2020-10-27 20:12:38 +00:00
|
|
|
import me.totalfreedom.plex.Plex;
|
|
|
|
import me.totalfreedom.plex.player.PlexPlayer;
|
2020-10-28 04:11:23 +00:00
|
|
|
|
2020-10-27 20:12:38 +00:00
|
|
|
public class MongoPlayerData
|
|
|
|
{
|
2020-10-28 04:11:23 +00:00
|
|
|
private final Datastore datastore;
|
2020-10-27 20:12:38 +00:00
|
|
|
|
|
|
|
public MongoPlayerData()
|
|
|
|
{
|
2020-10-28 04:11:23 +00:00
|
|
|
this.datastore = Plex.get().getMongoConnection().getDatastore();
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 03:49:56 +00:00
|
|
|
public boolean exists(UUID uuid)
|
|
|
|
{
|
2020-10-28 19:07:02 +00:00
|
|
|
Query<PlexPlayer> query = datastore.find(PlexPlayer.class)
|
|
|
|
.filter(Filters.eq("uuid", uuid.toString()));
|
2020-10-27 20:12:38 +00:00
|
|
|
|
2020-10-28 19:07:02 +00:00
|
|
|
return query.first() != null;
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 03:49:56 +00:00
|
|
|
public PlexPlayer getByUUID(UUID uuid)
|
|
|
|
{
|
2020-10-27 21:04:05 +00:00
|
|
|
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
|
2020-10-27 20:12:38 +00:00
|
|
|
{
|
2020-10-27 21:04:05 +00:00
|
|
|
return PlayerCache.getPlexPlayerMap().get(uuid);
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
2020-10-28 19:07:02 +00:00
|
|
|
|
|
|
|
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.eq("uuid", uuid.toString()));
|
2020-10-28 04:11:23 +00:00
|
|
|
return query2.first();
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void update(PlexPlayer player)
|
|
|
|
{
|
2020-10-28 19:07:02 +00:00
|
|
|
Query<PlexPlayer> filter = datastore.find(PlexPlayer.class)
|
|
|
|
.filter(Filters.eq("uuid", player.getUuid()));
|
|
|
|
|
|
|
|
Update<PlexPlayer> updateOps = filter
|
|
|
|
.update(
|
|
|
|
UpdateOperators.set("name", player.getName()),
|
|
|
|
UpdateOperators.set("loginMSG", player.getLoginMSG()),
|
|
|
|
UpdateOperators.set("prefix", player.getPrefix()),
|
|
|
|
UpdateOperators.set("rank", player.getRank().toLowerCase()),
|
|
|
|
UpdateOperators.set("ips", player.getIps()),
|
2020-11-06 18:19:38 +00:00
|
|
|
UpdateOperators.set("coins", player.getCoins()),
|
|
|
|
UpdateOperators.set("vanished", player.isVanished()));
|
2020-10-28 19:07:02 +00:00
|
|
|
|
|
|
|
updateOps.execute();
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:17:14 +00:00
|
|
|
|
2020-10-28 04:11:23 +00:00
|
|
|
public void save(PlexPlayer plexPlayer)
|
2020-10-28 03:49:56 +00:00
|
|
|
{
|
2020-10-28 04:11:23 +00:00
|
|
|
datastore.save(plexPlayer);
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
2020-10-28 04:11:23 +00:00
|
|
|
|
2020-10-28 03:55:35 +00:00
|
|
|
}
|