package me.totalfreedom.plex.cache; import com.google.common.collect.Maps; 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 java.util.Map; import java.util.UUID; public class MongoPlayerData { private Map punishedPlayerMap = Maps.newHashMap(); private Map plexPlayerMap = Maps.newHashMap(); private PlexPlayerDAO plexPlayerDAO; public MongoPlayerData() { this.plexPlayerDAO = new PlexPlayerDAO(PlexPlayer.class, Plex.get().getMongoConnection().getDatastore()); } public boolean exists(UUID uuid) { Query query = plexPlayerDAO.createQuery(); if (query.field("uuid").exists().field("uuid").equal(uuid.toString()).find().tryNext() != null) { return true; } return false; } public PlexPlayer getByUUID(UUID uuid) { if (plexPlayerMap.containsKey(uuid)) { return plexPlayerMap.get(uuid); } Query query2 = plexPlayerDAO.createQuery().field("uuid").exists().field("uuid").equal(uuid.toString()); return query2.first(); } public void update(PlexPlayer player) { Query filter = plexPlayerDAO.createQuery() .field("uuid").equal(player.getUuid()); UpdateOperations updateOps = plexPlayerDAO.createUpdateOperations(); updateOps.set("name", player.getName()); updateOps.set("loginMSG", player.getLoginMSG()); updateOps.set("prefix", player.getPrefix()); updateOps.set("rank", player.getRank() == null ? "" : player.getRank().name().toLowerCase()); updateOps.set("ips", player.getIps()); plexPlayerDAO.update(filter, updateOps); } public Map getPlexPlayerMap() { return plexPlayerMap; } public Map getPunishedPlayerMap() { return punishedPlayerMap; } public PlexPlayerDAO getPlexPlayerDAO() { return plexPlayerDAO; } }