Plex/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java

63 lines
1.9 KiB
Java
Raw Normal View History

package me.totalfreedom.plex.cache;
import dev.morphia.Datastore;
import dev.morphia.query.Query;
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;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.player.PlexPlayer;
public class MongoPlayerData
{
private final Datastore datastore;
public MongoPlayerData()
{
this.datastore = Plex.get().getMongoConnection().getDatastore();
}
2020-10-28 03:49:56 +00:00
public boolean exists(UUID uuid)
{
Query<PlexPlayer> query = datastore.find(PlexPlayer.class)
.filter(Filters.eq("uuid", uuid.toString()));
return query.first() != null;
}
2020-10-28 03:49:56 +00:00
public PlexPlayer getByUUID(UUID uuid)
{
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
{
return PlayerCache.getPlexPlayerMap().get(uuid);
}
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.eq("uuid", uuid.toString()));
return query2.first();
}
public void update(PlexPlayer player)
{
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()),
UpdateOperators.set("coins", player.getCoins()));
updateOps.execute();
}
2020-11-05 21:17:14 +00:00
public void save(PlexPlayer plexPlayer)
2020-10-28 03:49:56 +00:00
{
datastore.save(plexPlayer);
}
2020-10-28 03:55:35 +00:00
}