Plex/src/main/java/dev/plex/admin/AdminList.java

91 lines
2.6 KiB
Java
Raw Normal View History

2021-01-03 07:21:15 +00:00
package dev.plex.admin;
2020-11-05 21:17:14 +00:00
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import dev.morphia.Datastore;
import dev.morphia.query.Query;
2022-03-04 01:51:07 +00:00
import dev.plex.PlexBase;
2021-01-03 07:21:15 +00:00
import dev.plex.player.PlexPlayer;
2022-01-04 03:04:39 +00:00
import dev.plex.rank.enums.Rank;
2021-01-03 07:21:15 +00:00
import dev.plex.storage.StorageType;
2020-11-05 21:17:14 +00:00
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* Cached storage for Admin objects
2022-02-07 05:59:26 +00:00
*
* @see Admin
*/
2020-11-05 21:17:14 +00:00
2022-03-04 01:51:07 +00:00
public class AdminList extends PlexBase
2020-11-05 21:17:14 +00:00
{
/**
* Key/Value storage, where the key is the unique ID of the admin
*/
2020-11-05 21:17:14 +00:00
private final Map<UUID, Admin> admins = Maps.newHashMap();
/**
* Adds the admin to cache
2022-02-07 05:59:26 +00:00
*
* @param admin The admin object
*/
2020-11-05 21:17:14 +00:00
public void addToCache(Admin admin)
{
admins.put(admin.getUuid(), admin);
}
/**
* Removes an admin from the cache
2022-02-07 05:59:26 +00:00
*
* @param uuid The unique ID of the admin
* @see UUID
*/
2020-11-05 21:17:14 +00:00
public void removeFromCache(UUID uuid)
{
admins.remove(uuid);
}
/**
* Gathers every admin's username (cached and databsed)
2022-02-07 05:59:26 +00:00
*
* @return An array list of the names of every admin
*/
2020-11-05 21:17:14 +00:00
public List<String> getAllAdmins()
{
List<String> admins = Lists.newArrayList();
2022-03-04 01:51:07 +00:00
if (plugin.getStorageType() == StorageType.MONGODB)
2020-11-05 21:17:14 +00:00
{
2022-03-04 01:51:07 +00:00
Datastore store = plugin.getMongoConnection().getDatastore();
2020-11-05 21:17:14 +00:00
Query<PlexPlayer> query = store.find(PlexPlayer.class);
admins.addAll(query.stream().filter(plexPlayer -> plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN)).map(PlexPlayer::getName).collect(Collectors.toList()));
2020-11-06 01:29:38 +00:00
}
else
{
2022-03-04 01:51:07 +00:00
try (Connection con = plugin.getSqlConnection().getCon())
2020-11-05 21:17:14 +00:00
{
PreparedStatement statement = con.prepareStatement("SELECT * FROM `players` WHERE rank IN(?, ?, ?)");
statement.setString(1, Rank.ADMIN.name().toLowerCase());
statement.setString(2, Rank.SENIOR_ADMIN.name().toLowerCase());
statement.setString(3, Rank.EXECUTIVE.name().toLowerCase());
ResultSet set = statement.executeQuery();
while (set.next())
{
admins.add(set.getString("name"));
}
2020-11-06 01:29:38 +00:00
}
catch (SQLException throwables)
{
2020-11-05 21:17:14 +00:00
throwables.printStackTrace();
}
}
return admins;
}
}