Bump the version to 1.4-SNAPSHOT

Remove every trace of MongoDB
Fix issue where notes were not being notified on a permissions based system
This commit is contained in:
Taah 2023-08-25 01:30:41 -07:00
parent f97411ce09
commit 175c7db23d
13 changed files with 109 additions and 505 deletions

View File

@ -5,7 +5,7 @@ plugins {
}
group = "dev.plex"
version = "1.3.1-SNAPSHOT"
version = "1.4-SNAPSHOT"
description = "Plex"
subprojects {

View File

@ -12,12 +12,10 @@ import dev.plex.player.PlexPlayer;
import dev.plex.punishment.PunishmentManager;
import dev.plex.rank.RankManager;
import dev.plex.services.ServiceManager;
import dev.plex.storage.MongoConnection;
import dev.plex.storage.RedisConnection;
import dev.plex.storage.SQLConnection;
import dev.plex.storage.StorageType;
import dev.plex.storage.permission.SQLPermissions;
import dev.plex.storage.player.MongoPlayerData;
import dev.plex.storage.player.SQLPlayerData;
import dev.plex.storage.punishment.SQLNotes;
import dev.plex.storage.punishment.SQLPunishment;
@ -52,12 +50,10 @@ public class Plex extends JavaPlugin
public File modulesFolder;
private StorageType storageType = StorageType.SQLITE;
private SQLConnection sqlConnection;
private MongoConnection mongoConnection;
// private MongoConnection mongoConnection;
private RedisConnection redisConnection;
private PlayerCache playerCache;
private MongoPlayerData mongoPlayerData;
private SQLPlayerData sqlPlayerData;
private SQLPunishment sqlPunishment;
@ -118,7 +114,7 @@ public class Plex extends JavaPlugin
commands.load(false);
sqlConnection = new SQLConnection();
mongoConnection = new MongoConnection();
// mongoConnection = new MongoConnection();
redisConnection = new RedisConnection();
playerCache = new PlayerCache();
@ -167,17 +163,10 @@ public class Plex extends JavaPlugin
PlexLog.log("Redis is disabled in the configuration file, not connecting.");
}
if (storageType == StorageType.MONGODB)
{
mongoPlayerData = new MongoPlayerData();
}
else
{
sqlPlayerData = new SQLPlayerData();
sqlPunishment = new SQLPunishment();
sqlNotes = new SQLNotes();
sqlPermissions = new SQLPermissions();
}
new ListenerHandler();
new CommandHandler();
@ -228,14 +217,7 @@ public class Plex extends JavaPlugin
plugin.getAdminList().removeFromCache(plexPlayer.getUuid());
}
if (mongoPlayerData != null) //back to mongo checking
{
mongoPlayerData.update(plexPlayer); //update the player's document
}
else if (sqlPlayerData != null) //sql checking
{
sqlPlayerData.update(plexPlayer);
}
});
if (redisConnection != null && redisConnection.isEnabled() && redisConnection.getJedis().isConnected())
{

View File

@ -62,14 +62,6 @@ public class AdminList implements PlexBase
public List<String> getAllAdmins()
{
List<String> admins = Lists.newArrayList();
if (plugin.getStorageType() == StorageType.MONGODB)
{
Datastore store = plugin.getMongoConnection().getDatastore();
Query<PlexPlayer> query = store.find(PlexPlayer.class);
admins.addAll(query.stream().filter(plexPlayer -> plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN) && plexPlayer.isAdminActive()).map(PlexPlayer::getName).toList());
}
else
{
try (Connection con = plugin.getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement("SELECT * FROM `players` WHERE rank IN(?, ?, ?) AND adminActive=true");
@ -87,7 +79,6 @@ public class AdminList implements PlexBase
{
throwables.printStackTrace();
}
}
return admins;
}
@ -99,14 +90,6 @@ public class AdminList implements PlexBase
public List<PlexPlayer> getAllAdminPlayers()
{
List<PlexPlayer> plexPlayers = Lists.newArrayList();
if (plugin.getStorageType() == StorageType.MONGODB)
{
Datastore store = plugin.getMongoConnection().getDatastore();
Query<PlexPlayer> query = store.find(PlexPlayer.class);
return query.stream().toList().stream().filter(player -> plugin.getRankManager().isAdmin(player)).collect(Collectors.toList());
}
else
{
try (Connection con = plugin.getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement("SELECT * FROM `players` WHERE rank IN(?, ?, ?) AND adminActive=true");
@ -145,7 +128,6 @@ public class AdminList implements PlexBase
{
throwables.printStackTrace();
}
}
return plexPlayers;
}
}

View File

@ -19,28 +19,14 @@ public class DataUtils
* @return true if the player is registered in the database
*/
public static boolean hasPlayedBefore(UUID uuid)
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return Plex.get().getMongoPlayerData().exists(uuid);
}
else
{
return Plex.get().getSqlPlayerData().exists(uuid);
}
}
public static boolean hasPlayedBefore(String username)
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return Plex.get().getMongoPlayerData().exists(username);
}
else
{
return Plex.get().getSqlPlayerData().exists(username);
}
}
/**
* Gets a player from cache or from the database
@ -61,15 +47,8 @@ public class DataUtils
return Plex.get().getPlayerCache().getPlexPlayerMap().get(uuid);
}
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return Plex.get().getMongoPlayerData().getByUUID(uuid);
}
else
{
return Plex.get().getSqlPlayerData().getByUUID(uuid, loadExtraData);
}
}
public static PlexPlayer getPlayer(String username)
{
@ -84,15 +63,8 @@ public class DataUtils
return plexPlayer.get();
}
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return Plex.get().getMongoPlayerData().getByName(username);
}
else
{
return Plex.get().getSqlPlayerData().getByName(username, loadExtraData);
}
}
/**
* Gets a player from cache or from the database
@ -109,15 +81,8 @@ public class DataUtils
return player;
}
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return Plex.get().getMongoPlayerData().getByIP(ip);
}
else
{
return Plex.get().getSqlPlayerData().getByIP(ip);
}
}
/**
* Updates a player's information in the database
@ -126,16 +91,9 @@ public class DataUtils
* @see PlexPlayer
*/
public static void update(PlexPlayer plexPlayer)
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
Plex.get().getMongoPlayerData().update(plexPlayer);
}
else
{
Plex.get().getSqlPlayerData().update(plexPlayer);
}
}
/**
* Inserts a player's information in the database
@ -144,17 +102,8 @@ public class DataUtils
* @see PlexPlayer
*/
public static void insert(PlexPlayer plexPlayer)
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
Plex.get().getMongoPlayerData().save(plexPlayer);
}
else
{
Plex.get().getSqlPlayerData().insert(plexPlayer);
}
}
/* REDIS METHODS AT ONE POINT FOR BANS, AND JSON METHODS FOR PUNISHMENTS */
}

View File

@ -48,8 +48,6 @@ public class NotesCMD extends PlexCommand
switch (args[1].toLowerCase())
{
case "list":
{
if (plugin.getStorageType() != StorageType.MONGODB)
{
plugin.getSqlNotes().getNotes(plexPlayer.getUuid()).whenComplete((notes, ex) ->
{
@ -60,16 +58,6 @@ public class NotesCMD extends PlexCommand
}
readNotes(sender, plexPlayer, notes);
});
}
else
{
List<Note> notes = plexPlayer.getNotes();
if (notes.size() == 0)
{
return messageComponent("noNotes");
}
readNotes(sender, plexPlayer, notes);
}
return null;
}
case "add":
@ -83,14 +71,7 @@ public class NotesCMD extends PlexCommand
{
Note note = new Note(plexPlayer.getUuid(), content, playerSender.getUniqueId(), ZonedDateTime.now(ZoneId.of(TimeUtils.TIMEZONE)));
plexPlayer.getNotes().add(note);
if (plugin.getStorageType() != StorageType.MONGODB)
{
plugin.getSqlNotes().addNote(note);
}
else
{
DataUtils.update(plexPlayer);
}
return messageComponent("noteAdded");
}
}
@ -109,8 +90,6 @@ public class NotesCMD extends PlexCommand
{
return messageComponent("unableToParseNumber", args[2]);
}
if (plugin.getStorageType() != StorageType.MONGODB)
{
plugin.getSqlNotes().getNotes(plexPlayer.getUuid()).whenComplete((notes, ex) ->
{
boolean deleted = false;
@ -129,40 +108,15 @@ public class NotesCMD extends PlexCommand
}
plexPlayer.getNotes().removeIf(note -> note.getId() == id);
});
}
else
{
if (plexPlayer.getNotes().removeIf(note -> note.getId() == id))
{
return messageComponent("removedNote", id);
}
return messageComponent("noteNotFound");
}
return null;
}
case "clear":
{
if (plugin.getStorageType() != StorageType.MONGODB)
{
plugin.getSqlNotes().getNotes(plexPlayer.getUuid()).whenComplete((notes, ex) ->
{
for (Note note : notes)
{
plugin.getSqlNotes().deleteNote(note.getId(), plexPlayer.getUuid());
}
plexPlayer.getNotes().clear();
send(sender, messageComponent("clearedNotes", notes.size()));
});
}
else
{
int count = plexPlayer.getNotes().size();
plexPlayer.getNotes().clear();
DataUtils.update(plexPlayer);
return messageComponent("clearedNotes", count);
}
return null;
}
default:
{
return usage();

View File

@ -82,29 +82,18 @@ public class PlayerListener<T> extends PlexListener
PermissionsUtil.setupPermissions(player);
if (plugin.getStorageType() != StorageType.MONGODB)
{
plexPlayer.loadNotes();
}
if (plugin.getStorageType() == StorageType.MONGODB)
{
plexPlayer.loadPunishments();
}
if (plugin.getStorageType() != StorageType.MONGODB)
{
plugin.getSqlNotes().getNotes(plexPlayer.getUuid()).whenComplete((notes, ex) ->
{
String plural = notes.size() == 1 ? "note." : "notes.";
if (!notes.isEmpty())
{
PlexUtils.broadcastToAdmins(Component.text(plexPlayer.getName() + " has " + notes.size() + " " + plural).color(NamedTextColor.GOLD));
PlexUtils.broadcastToAdmins(Component.text("Click to view their " + plural).clickEvent(ClickEvent.runCommand("/notes " + plexPlayer.getName() + " list")).color(NamedTextColor.GOLD));
PlexUtils.broadcastToAdmins(Component.text(plexPlayer.getName() + " has " + notes.size() + " " + plural).color(NamedTextColor.GOLD), "plex.notes.notify");
PlexUtils.broadcastToAdmins(Component.text("Click to view their " + plural).clickEvent(ClickEvent.runCommand("/notes " + plexPlayer.getName() + " list")).color(NamedTextColor.GOLD), "plex.notes.notify");
}
});
}
}
// saving the player's data
@EventHandler(priority = EventPriority.HIGHEST)

View File

@ -91,10 +91,7 @@ public class PlexPlayer
if (loadExtraData)
{
this.loadPunishments();
if (Plex.get().getStorageType() != StorageType.MONGODB)
{
this.permissions.addAll(Plex.get().getSqlPermissions().getPermissions(this.uuid));
}
// this.permissions.addAll(Plex.get().getSqlPermissions().getPermissions(this.uuid));
}
}
@ -129,20 +126,14 @@ public class PlexPlayer
}
public void loadPunishments()
{
if (Plex.get().getStorageType() != StorageType.MONGODB)
{
this.setPunishments(Plex.get().getSqlPunishment().getPunishments(this.getUuid()));
}
}
public void loadNotes()
{
if (Plex.get().getStorageType() != StorageType.MONGODB)
{
Plex.get().getSqlNotes().getNotes(this.getUuid());
}
}
public String toJSON()
{

View File

@ -102,18 +102,8 @@ public class PunishmentManager implements PlexBase
public void issuePunishment(PlexPlayer plexPlayer, Punishment punishment)
{
plexPlayer.getPunishments().add(punishment);
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
CompletableFuture.runAsync(() ->
{
DataUtils.update(plexPlayer);
});
}
else
{
Plex.get().getSqlPunishment().insertPunishment(punishment);
}
}
private boolean isNotEmpty(File file)
{
@ -159,16 +149,6 @@ public class PunishmentManager implements PlexBase
}
public CompletableFuture<List<Punishment>> getActiveBans()
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return CompletableFuture.supplyAsync(() ->
{
List<PlexPlayer> players = Plex.get().getMongoPlayerData().getPlayers();
return players.stream().map(PlexPlayer::getPunishments).flatMap(Collection::stream).filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN).toList();
});
}
else
{
//PlexLog.debug("Checking active bans mysql");
CompletableFuture<List<Punishment>> future = new CompletableFuture<>();
@ -181,7 +161,6 @@ public class PunishmentManager implements PlexBase
});
return future;
}
}
public void unban(Punishment punishment)
{
@ -189,22 +168,9 @@ public class PunishmentManager implements PlexBase
}
public CompletableFuture<Void> unban(UUID uuid)
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return CompletableFuture.runAsync(() ->
{
PlexPlayer plexPlayer = DataUtils.getPlayer(uuid);
plexPlayer.setPunishments(plexPlayer.getPunishments().stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN)
.peek(punishment -> punishment.setActive(false)).collect(Collectors.toList()));
DataUtils.update(plexPlayer);
});
}
else
{
return Plex.get().getSqlPunishment().removeBan(uuid);
}
}
private void doPunishment(PlexPlayer player, Punishment punishment)
{

View File

@ -1,56 +0,0 @@
package dev.plex.storage;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import dev.morphia.Datastore;
import dev.morphia.Morphia;
import dev.morphia.mapping.MapperOptions;
import dev.plex.PlexBase;
import dev.plex.player.PlexPlayer;
import dev.plex.storage.codec.ZonedDateTimeCodec;
import dev.plex.util.PlexLog;
import org.bson.codecs.configuration.CodecRegistries;
public class MongoConnection implements PlexBase
{
// USE MORPHIA API FOR MONGO <3
public Datastore getDatastore()
{
if (!plugin.config.getString("data.central.storage").equalsIgnoreCase("mongodb"))
{
return null;
}
String host = plugin.config.getString("data.central.hostname");
int port = plugin.config.getInt("data.central.port");
String username = plugin.config.getString("data.central.user");
String password = plugin.config.getString("data.central.password");
String database = plugin.config.getString("data.central.db");
String connectionString;
if (username != null && password != null && !username.isEmpty() && !password.isEmpty())
{
if (database != null && !database.isEmpty())
{
connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=" + database + "&uuidRepresentation=STANDARD";
}
else
{
connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?uuidRepresentation=STANDARD";
}
}
else
{
connectionString = "mongodb://" + host + ":" + port + "/?uuidRepresentation=STANDARD";
}
PlexLog.debug("Using mongo connection string: " + connectionString);
MongoClient client = MongoClients.create(MongoClientSettings.builder().codecRegistry(CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), CodecRegistries.fromCodecs(new ZonedDateTimeCodec()))).applyConnectionString(new ConnectionString(connectionString)).build());
Datastore datastore = Morphia.createDatastore(client, database == null ? "admin" : database, MapperOptions.DEFAULT);
datastore.getMapper().map(PlexPlayer.class);
datastore.ensureIndexes();
plugin.setStorageType(StorageType.MONGODB);
return datastore;
}
}

View File

@ -2,5 +2,5 @@ package dev.plex.storage;
public enum StorageType
{
MONGODB, MARIADB, SQLITE
MARIADB, SQLITE
}

View File

@ -1,147 +0,0 @@
package dev.plex.storage.player;
import dev.morphia.Datastore;
import dev.morphia.query.Query;
import dev.morphia.query.Update;
import dev.morphia.query.filters.Filters;
import dev.morphia.query.updates.UpdateOperators;
import dev.plex.Plex;
import dev.plex.player.PlexPlayer;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
/**
* Mongo fetching utilities for players
*/
public class MongoPlayerData
{
/**
* The datastore object / database
*/
private final Datastore datastore;
/**
* Creates an instance of the player data
*/
public MongoPlayerData()
{
this.datastore = Plex.get().getMongoConnection().getDatastore();
}
/**
* Checks whether the player exists in mongo's database
*
* @param uuid The unique ID of the player
* @return true if the player was found
*/
public boolean exists(UUID uuid)
{
Query<PlexPlayer> query = datastore.find(PlexPlayer.class)
.filter(Filters.eq("uuid", uuid));
return query.first() != null;
}
public boolean exists(String username)
{
Query<PlexPlayer> query = datastore.find(PlexPlayer.class)
.filter(Filters.regex("name").caseInsensitive().pattern(username));
return query.first() != null;
}
/**
* Gets the player from cache or from mongo's database
*
* @param uuid The unique ID of the player
* @return a PlexPlayer object
* @see PlexPlayer
*/
public PlexPlayer getByUUID(UUID uuid)
{
if (Plex.get().getPlayerCache().getPlexPlayerMap().containsKey(uuid))
{
return Plex.get().getPlayerCache().getPlexPlayerMap().get(uuid);
}
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.eq("uuid", uuid));
return query2.first();
}
public PlexPlayer getByName(String username)
{
PlexPlayer player = Plex.get().getPlayerCache().getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getName().equalsIgnoreCase(username)).findFirst().orElse(null);
if (player != null)
{
return player;
}
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.regex("name").caseInsensitive().pattern(username));
return query2.first();
}
/**
* Gets the player from cache or from mongo's database
*
* @param ip The IP address of the player.
* @return a PlexPlayer object
* @see PlexPlayer
*/
public PlexPlayer getByIP(String ip)
{
PlexPlayer player = Plex.get().getPlayerCache().getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
if (player != null)
{
return player;
}
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.in("ips", Collections.singleton(ip)));
return query2.first();
}
/**
* Updates a player's information in the mongo database
*
* @param player The PlexPlayer object
* @see PlexPlayer
*/
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("loginMessage", player.getLoginMessage()),
UpdateOperators.set("prefix", player.getPrefix()),
UpdateOperators.set("vanished", player.isVanished()),
UpdateOperators.set("commandSpy", player.isCommandSpy()),
UpdateOperators.set("adminActive", player.isAdminActive()),
UpdateOperators.set("rank", player.getRank().toLowerCase()),
UpdateOperators.set("ips", player.getIps()),
UpdateOperators.set("coins", player.getCoins()),
UpdateOperators.set("punishments", player.getPunishments()),
UpdateOperators.set("notes", player.getNotes()));
updateOps.execute();
}
public List<PlexPlayer> getPlayers()
{
return datastore.find(PlexPlayer.class).stream().toList();
}
/**
* Saves the player's information in the database
*
* @param plexPlayer The PlexPlayer object
* @see PlexPlayer
*/
public void save(PlexPlayer plexPlayer)
{
datastore.save(plexPlayer);
}
}

View File

@ -90,18 +90,12 @@ public class PlexUtils implements PlexBase
}
catch (SQLException e)
{
if (Plex.get().getMongoConnection().getDatastore() != null)
{
PlexLog.log("Successfully enabled MongoDB!");
}
PlexLog.error("Unable to connect to the SQL Server");
}
}
else
{
if (Plex.get().getMongoConnection().getDatastore() != null)
{
PlexLog.log("Successfully enabled MongoDB!");
}
PlexLog.error("Unable to initialize hikari data source!");
}
}
@ -241,9 +235,9 @@ public class PlexUtils implements PlexBase
Bukkit.broadcast(component);
}
public static void broadcastToAdmins(Component component)
public static void broadcastToAdmins(Component component, String permission)
{
Bukkit.getOnlinePlayers().stream().filter(pl -> plugin.getPlayerCache().getPlexPlayer(pl.getUniqueId()).isAdminActive()).forEach(pl ->
Bukkit.getOnlinePlayers().stream().filter(pl -> plugin.getPlayerCache().getPlexPlayer(pl.getUniqueId()).isAdminActive() || pl.hasPermission(permission)).forEach(pl ->
{
pl.sendMessage(component);
});

View File

@ -43,7 +43,7 @@ loginmessages:
data:
central:
storage: sqlite # Use mariadb, mongodb, or sqlite here
storage: sqlite # Use mariadb, or sqlite here
user: ""
password: ""
hostname: 127.0.0.1