Player Data Management System

This commit is contained in:
Trixkz 2023-10-26 13:36:05 -04:00
parent 3683e2151e
commit a2657aaf14
7 changed files with 173 additions and 7 deletions

View File

@ -79,6 +79,12 @@
<version>1.20-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@ -4,7 +4,9 @@ import com.loganmagnan.eventcore.managers.CooldownManager;
import com.loganmagnan.eventcore.managers.PlayerDataManager;
import com.loganmagnan.eventcore.managers.SpawnManager;
import com.loganmagnan.eventcore.managers.ChatManager;
import com.loganmagnan.eventcore.managers.mongo.MongoManager;
import com.loganmagnan.eventcore.menusystem.PlayerMenuUtil;
import com.loganmagnan.eventcore.playerdata.PlayerData;
import com.loganmagnan.eventcore.runnables.CooldownRunnable;
import com.loganmagnan.eventcore.utils.ClassRegistrationUtils;
import com.loganmagnan.eventcore.utils.ColorUtils;
@ -15,7 +17,6 @@ import com.loganmagnan.eventcore.utils.config.FileConfig;
import com.loganmagnan.eventcore.utils.config.file.Config;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
@ -32,6 +33,7 @@ public class EventCore extends JavaPlugin {
private FileConfig messagesConfig;
// Managers
private MongoManager mongoManager;
private PlayerDataManager playerDataManager;
private SpawnManager spawnManager;
private ChatManager chatManager;
@ -53,11 +55,10 @@ public class EventCore extends JavaPlugin {
new Constants();
Bukkit.getConsoleSender().sendMessage(Utils.chatBar);
Bukkit.getConsoleSender().sendMessage(ColorUtils.getMessageType("&dEventCore &7- &av" + this.getDescription().getVersion()));
Bukkit.getConsoleSender().sendMessage(ColorUtils.getMessageType("&7Made by &eLoganM Development"));
Bukkit.getConsoleSender().sendMessage(Utils.chatBar);
this.getServer().getConsoleSender().sendMessage(Utils.chatBar);
this.getServer().getConsoleSender().sendMessage(ColorUtils.getMessageType("&dEventCore &7- &av" + this.getDescription().getVersion()));
this.getServer().getConsoleSender().sendMessage(ColorUtils.getMessageType("&7Made by &eLoganM Development"));
this.getServer().getConsoleSender().sendMessage(Utils.chatBar);
this.loadCommands();
this.loadManagers();
this.loadListeners();
@ -66,6 +67,14 @@ public class EventCore extends JavaPlugin {
@Override
public void onDisable() {
for (Player player : this.getServer().getOnlinePlayers()) {
PlayerData playerData = this.playerDataManager.getPlayerData(player.getUniqueId());
this.playerDataManager.savePlayerData(playerData);
}
this.mongoManager.disconnect();
instance = null;
}
@ -74,6 +83,7 @@ public class EventCore extends JavaPlugin {
}
private void loadManagers() {
this.mongoManager = new MongoManager();
this.playerDataManager = new PlayerDataManager();
this.spawnManager = new SpawnManager();
this.chatManager = new ChatManager();

View File

@ -0,0 +1,24 @@
package com.loganmagnan.eventcore.commands;
import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.utils.command.BaseCommand;
import com.loganmagnan.eventcore.utils.command.Command;
import com.loganmagnan.eventcore.utils.command.CommandArguments;
import org.bukkit.entity.Player;
public class ChatColorCommand extends BaseCommand {
private EventCore main = EventCore.getInstance();
@Command(name = "chatcolor", permission = "eventcore.command.chatcolor")
@Override
public void executeAs(CommandArguments command) {
Player player = command.getPlayer();
String[] args = command.getArgs();
if (args.length == 0) {
// To Do: Setup Chat Color Menu
}
}
}

View File

@ -1,6 +1,7 @@
package com.loganmagnan.eventcore.listeners;
import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.playerdata.PlayerData;
import com.loganmagnan.eventcore.utils.ColorUtils;
import com.loganmagnan.eventcore.utils.Constants;
import org.bukkit.entity.Player;
@ -16,6 +17,8 @@ public class AsyncPlayerChatListener implements Listener {
public void onAsyncPlayerChat(AsyncPlayerChatEvent event) {
Player player = event.getPlayer();
PlayerData playerData = this.main.getPlayerDataManager().getPlayerData(player.getUniqueId());
if (this.main.getChatManager().isMuted()) {
event.setCancelled(true);
@ -39,7 +42,9 @@ public class AsyncPlayerChatListener implements Listener {
}
}
// To Do: Setup Chat Color
if (playerData.getChatColor() != null) {
event.setMessage(playerData.getChatColor() + event.getMessage());
}
this.main.getCooldownManager().setChatCooldown(player.getUniqueId(), this.main.getChatManager().getDelayAmount());
}

View File

@ -4,7 +4,12 @@ import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.playerdata.PlayerData;
import com.loganmagnan.eventcore.playerdata.PlayerState;
import com.loganmagnan.eventcore.utils.PlayerUtil;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import lombok.Getter;
import org.bson.Document;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;
@ -28,11 +33,21 @@ public class PlayerDataManager {
}
public void loadPlayerData(PlayerData playerData) {
Document document = this.main.getMongoManager().getPlayers().find(Filters.eq("unique-id", playerData.getUniqueId().toString())).first();
if (document != null) {
playerData.setChatColor(ChatColor.valueOf(document.getString("chat-color")));
}
playerData.setLoaded(true);
}
public void savePlayerData(PlayerData playerData) {
Document document = new Document();
document.put("unique-id", playerData.getUniqueId().toString());
document.put("chat-color", playerData.getChatColor().name());
this.main.getMongoManager().getPlayers().replaceOne(Filters.eq("unique-id", playerData.getUniqueId().toString()), document, new UpdateOptions().upsert(true));
}
public void deletePlayer(UUID uniqueId) {
@ -42,6 +57,13 @@ public class PlayerDataManager {
});
}
public MongoCursor<Document> getPlayersSorted(String stat) {
Document sort = new Document();
sort.put(stat, -1);
return this.main.getMongoManager().getPlayers().find().sort(sort).limit(10).iterator();
}
public void sendToSpawnAndResetPlayer(Player player) {
this.resetPlayer(player);
this.giveSpawnItemsToPlayer(player);

View File

@ -0,0 +1,81 @@
package com.loganmagnan.eventcore.managers.mongo;
import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.utils.ColorUtils;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import lombok.Getter;
import org.bson.Document;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.Collections;
@Getter
public class MongoManager {
private EventCore plugin = EventCore.getInstance();
private final MongoManager instance;
private final FileConfiguration config = this.plugin.getMainConfig().getConfig();
private MongoClient mongoClient;
private MongoDatabase mongoDatabase;
private final boolean uriMode = config.getBoolean("MONGO.URI-MODE");
private final String host = config.getString("MONGO.NORMAL.HOST");
private final int port = config.getInt("MONGO.NORMAL.PORT");
private final String database = config.getString("MONGO.NORMAL.DATABASE");
private final boolean auth = config.getBoolean("MONGO.NORMAL.AUTH.ENABLED");
private final String user = config.getString("MONGO.NORMAL.AUTH.USERNAME");
private final String password = config.getString("MONGO.NORMAL.AUTH.PASSWORD");
private final String authDatabase = config.getString("MONGO.NORMAL.AUTH.AUTH-DATABASE");
private final String uriString = config.getString("MONGO.URI.CONNECTION-STRING");
private final String uriDatabase = config.getString("MONGO.URI.DATABASE-NAME");
private boolean connected;
private MongoCollection<Document> players;
public MongoManager() {
instance = this;
try {
if (this.uriMode) {
MongoClientURI mongoClientURI = new MongoClientURI(this.uriString);
MongoClient mongoClient = new MongoClient(mongoClientURI);
mongoClient.getDatabase(this.uriDatabase);
mongoDatabase = mongoClient.getDatabase(this.uriDatabase);
} else {
if (auth) {
final MongoCredential credential = MongoCredential.createCredential(user, authDatabase, password.toCharArray());
mongoClient = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential));
} else {
mongoClient = new MongoClient(host, port);
}
mongoDatabase = mongoClient.getDatabase(database);
}
connected = true;
Bukkit.getConsoleSender().sendMessage(ColorUtils.getMessageType("&b[EventCore] &aSuccessfully connected to the database!"));
this.players = this.mongoDatabase.getCollection("players");
} catch (Exception exception) {
connected = false;
Bukkit.getConsoleSender().sendMessage(ColorUtils.getMessageType("&b[EventCore] &cFailed to connect to the database!"));
exception.printStackTrace();
Bukkit.getPluginManager().disablePlugin(this.plugin);
Bukkit.getConsoleSender().sendMessage(ColorUtils.getMessageType("&b[EventCore] &cDisabling EventCore..."));
}
}
public void disconnect() {
if (this.mongoClient != null) {
this.mongoClient.close();
this.connected = false;
Bukkit.getConsoleSender().sendMessage(ColorUtils.getMessageType("&b[EventCore] &aSuccessfully disconnected from the database!"));
}
}
}

View File

@ -1,2 +1,20 @@
# --------------------------------------------------
# MongoDB configuration
# --------------------------------------------------
MONGO:
URI-MODE: false
NORMAL:
HOST: ""
PORT: 27017
DATABASE: ""
AUTH:
ENABLED: false
USERNAME: "user"
PASSWORD: "pass"
AUTH-DATABASE: "admin"
URI:
CONNECTION-STRING: ""
DATABASE-NAME: ""
PERMISSION-NODES:
CHAT-BYPASS: "eventcore.chat.bypass"