From 9ff8cad8fdf12708c4f9e781b792c4367c5cdee7 Mon Sep 17 00:00:00 2001 From: Trixkz Date: Wed, 25 Oct 2023 19:35:21 -0400 Subject: [PATCH] Setup Chat Management System (75% All Done) --- .../com/loganmagnan/eventcore/EventCore.java | 22 +++++- .../eventcore/commands/ChatCommand.java | 63 +++++++++++++++++ .../listeners/AsyncPlayerChatListener.java | 46 +++++++++++++ .../eventcore/managers/ChatManager.java | 68 +++++++++++++++++++ .../eventcore/managers/CooldownManager.java | 37 ++++++++++ .../eventcore/playerdata/PlayerData.java | 5 +- .../eventcore/runnables/CooldownRunnable.java | 32 +++++++++ .../eventcore/utils/Constants.java | 37 ++++++++++ .../loganmagnan/eventcore/utils/Utils.java | 23 ++----- src/main/resources/config.yml | 2 + src/main/resources/messages.yml | 28 ++++++++ 11 files changed, 341 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/loganmagnan/eventcore/commands/ChatCommand.java create mode 100644 src/main/java/com/loganmagnan/eventcore/listeners/AsyncPlayerChatListener.java create mode 100644 src/main/java/com/loganmagnan/eventcore/managers/ChatManager.java create mode 100644 src/main/java/com/loganmagnan/eventcore/managers/CooldownManager.java create mode 100644 src/main/java/com/loganmagnan/eventcore/runnables/CooldownRunnable.java create mode 100644 src/main/java/com/loganmagnan/eventcore/utils/Constants.java create mode 100644 src/main/resources/messages.yml diff --git a/src/main/java/com/loganmagnan/eventcore/EventCore.java b/src/main/java/com/loganmagnan/eventcore/EventCore.java index c415dc0..ed75974 100644 --- a/src/main/java/com/loganmagnan/eventcore/EventCore.java +++ b/src/main/java/com/loganmagnan/eventcore/EventCore.java @@ -1,12 +1,17 @@ package com.loganmagnan.eventcore; +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.menusystem.PlayerMenuUtil; +import com.loganmagnan.eventcore.runnables.CooldownRunnable; import com.loganmagnan.eventcore.utils.ClassRegistrationUtils; import com.loganmagnan.eventcore.utils.ColorUtils; +import com.loganmagnan.eventcore.utils.Constants; import com.loganmagnan.eventcore.utils.Utils; import com.loganmagnan.eventcore.utils.command.CommandFramework; +import com.loganmagnan.eventcore.utils.config.FileConfig; import com.loganmagnan.eventcore.utils.config.file.Config; import lombok.Getter; import lombok.Setter; @@ -19,22 +24,34 @@ import java.util.HashMap; @Setter public class EventCore extends JavaPlugin { + // Main Class Instance @Getter private static EventCore instance; + // Configuration Files private Config mainConfig; + private FileConfig messagesConfig; + // Managers private PlayerDataManager playerDataManager; private SpawnManager spawnManager; + private ChatManager chatManager; + private CooldownManager cooldownManager; + // Menu System private HashMap playerMenuUtilMap = new HashMap<>(); + // Command Framework private CommandFramework commandFramework = new CommandFramework(this); + @Override public void onEnable() { instance = this; this.saveDefaultConfig(); this.mainConfig = new Config("config", this); + this.messagesConfig = new FileConfig(this, "messages.yml"); + + new Constants(); Bukkit.getConsoleSender().sendMessage(Utils.chatBar); Bukkit.getConsoleSender().sendMessage(ColorUtils.getMessageType("&dEventCore &7- &av" + this.getDescription().getVersion())); @@ -47,6 +64,7 @@ public class EventCore extends JavaPlugin { this.loadRunnables(); } + @Override public void onDisable() { instance = null; } @@ -58,6 +76,8 @@ public class EventCore extends JavaPlugin { private void loadManagers() { this.playerDataManager = new PlayerDataManager(); this.spawnManager = new SpawnManager(); + this.chatManager = new ChatManager(); + this.cooldownManager = new CooldownManager(); } private void loadListeners() { @@ -65,7 +85,7 @@ public class EventCore extends JavaPlugin { } private void loadRunnables() { - + this.getServer().getScheduler().runTaskTimerAsynchronously(this, new CooldownRunnable(), 0, 20); } public PlayerMenuUtil getPlayerMenuUtil(Player player) { diff --git a/src/main/java/com/loganmagnan/eventcore/commands/ChatCommand.java b/src/main/java/com/loganmagnan/eventcore/commands/ChatCommand.java new file mode 100644 index 0000000..95697d1 --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/commands/ChatCommand.java @@ -0,0 +1,63 @@ +package com.loganmagnan.eventcore.commands; + +import com.loganmagnan.eventcore.EventCore; +import com.loganmagnan.eventcore.utils.ColorUtils; +import com.loganmagnan.eventcore.utils.Constants; +import com.loganmagnan.eventcore.utils.CustomLocation; +import com.loganmagnan.eventcore.utils.Utils; +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 ChatCommand extends BaseCommand { + + private EventCore main = EventCore.getInstance(); + + @Command(name = "chat", permission = "eventcore.command.chat") + @Override + public void executeAs(CommandArguments command) { + Player player = command.getPlayer(); + + String[] args = command.getArgs(); + + if (args.length == 0) { + for (String string : Constants.COMMAND_MESSAGES.get("chat.help")) { + player.sendMessage(ColorUtils.getMessageType(string.replace("%line%", Utils.chatBar))); + } + } else { + switch (args[0]) { + case "manage": + // To Do: Setup Chat Management Menu + + break; + case "toggle": + this.main.getChatManager().toggleChat(player); + + break; + case "slow": + if (args[1] == null) { + player.sendMessage(ColorUtils.getMessageType(Constants.COMMAND_MESSAGES.get("chat.slow.invalid-argument").get(0))); + + return; + } + + if (!Utils.isNumeric(args[1])) { + player.sendMessage(ColorUtils.getMessageType(Constants.COMMAND_MESSAGES.get("chat.slow.invalid-amount").get(0))); + + return; + } + + int amount = Integer.parseInt(args[1]); + + this.main.getChatManager().slowChat(player, amount); + + break; + case "clear": + this.main.getChatManager().clearChat(player); + + break; + } + } + } +} diff --git a/src/main/java/com/loganmagnan/eventcore/listeners/AsyncPlayerChatListener.java b/src/main/java/com/loganmagnan/eventcore/listeners/AsyncPlayerChatListener.java new file mode 100644 index 0000000..f5aaee3 --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/listeners/AsyncPlayerChatListener.java @@ -0,0 +1,46 @@ +package com.loganmagnan.eventcore.listeners; + +import com.loganmagnan.eventcore.EventCore; +import com.loganmagnan.eventcore.utils.ColorUtils; +import com.loganmagnan.eventcore.utils.Constants; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.AsyncPlayerChatEvent; + +public class AsyncPlayerChatListener implements Listener { + + private EventCore main = EventCore.getInstance(); + + @EventHandler + public void onAsyncPlayerChat(AsyncPlayerChatEvent event) { + Player player = event.getPlayer(); + + if (this.main.getChatManager().isMuted()) { + event.setCancelled(true); + + player.sendMessage(ColorUtils.getMessageType(Constants.LISTENER_MESSAGES.get("chat-muted").get(0))); + + return; + } + + if (!player.hasPermission(Constants.PERMISSION_NODES.get("chat-bypass"))) { + if (this.main.getCooldownManager().isOnChatCooldown(player.getUniqueId())) { + int minutes = this.main.getCooldownManager().getChatCooldownTime(player.getUniqueId()) / 60; + int seconds = this.main.getCooldownManager().getChatCooldownTime(player.getUniqueId()) % 60; + + event.setCancelled(true); + + for (String string : Constants.LISTENER_MESSAGES.get("chat-cooldown")) { + player.sendMessage(ColorUtils.getMessageType(string.replace("%time%", String.format("%02d:%02d", minutes, seconds)))); + } + + return; + } + } + + // To Do: Setup Chat Color + + this.main.getCooldownManager().setChatCooldown(player.getUniqueId(), this.main.getChatManager().getDelayAmount()); + } +} diff --git a/src/main/java/com/loganmagnan/eventcore/managers/ChatManager.java b/src/main/java/com/loganmagnan/eventcore/managers/ChatManager.java new file mode 100644 index 0000000..4b6e8c7 --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/managers/ChatManager.java @@ -0,0 +1,68 @@ +package com.loganmagnan.eventcore.managers; + +import com.loganmagnan.eventcore.EventCore; +import com.loganmagnan.eventcore.utils.ColorUtils; +import com.loganmagnan.eventcore.utils.Constants; +import lombok.Getter; +import lombok.Setter; +import org.bukkit.entity.Player; +import java.util.UUID; + +@Getter +@Setter +public class ChatManager { + + private EventCore main = EventCore.getInstance(); + + // Toggle Chat System + private boolean muted = false; + private UUID toggledBy = null; + private long lastToggledTime = 0; + + // Slow Chat System + private int delayAmount = 0; + private UUID delayedBy = null; + private long lastDelayedTime = 0; + + // Clear Chat System + private UUID clearedBy = null; + private long lastClearedTime = 0; + + // Mute Chat + public void toggleChat(Player player) { + this.toggledBy = player.getUniqueId(); + this.lastToggledTime = System.currentTimeMillis(); + + if (this.muted) { + this.muted = false; + this.broadcastMessage(Constants.CHAT_MANAGEMENT_MESSAGES.get("unmuted").get(0).replace("%player%", player.getName())); + } else { + this.muted = true; + this.broadcastMessage(Constants.CHAT_MANAGEMENT_MESSAGES.get("muted").get(0).replace("%player%", player.getName())); + } + } + + // Slow Chat + public void slowChat(Player player, int amount) { + this.delayAmount = amount; + this.delayedBy = player.getUniqueId(); + this.lastDelayedTime = System.currentTimeMillis(); + this.broadcastMessage(Constants.CHAT_MANAGEMENT_MESSAGES.get("slowed").get(0).replace("%player%", player.getName()).replace("%amount%", amount + " &csecond" + (amount > 1 ? "s" : ""))); + } + + // Clear Chat + public void clearChat(Player player) { + for (int i = 0; i < 100; i++) { + this.broadcastMessage(""); + } + + this.clearedBy = player.getUniqueId(); + this.lastClearedTime = System.currentTimeMillis(); + this.broadcastMessage(Constants.CHAT_MANAGEMENT_MESSAGES.get("cleared").get(0).replace("%player%", player.getName())); + } + + // Broadcast Message + public void broadcastMessage(String message) { + this.main.getServer().broadcastMessage(ColorUtils.getMessageType(message)); + } +} diff --git a/src/main/java/com/loganmagnan/eventcore/managers/CooldownManager.java b/src/main/java/com/loganmagnan/eventcore/managers/CooldownManager.java new file mode 100644 index 0000000..e7ca253 --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/managers/CooldownManager.java @@ -0,0 +1,37 @@ +package com.loganmagnan.eventcore.managers; + +import com.loganmagnan.eventcore.EventCore; +import lombok.Getter; +import lombok.Setter; +import java.util.*; + +@Getter +@Setter +public class CooldownManager { + + private EventCore main = EventCore.getInstance(); + + private Set chatCooldown = new HashSet(); + + private Map chatCooldownTime = new HashMap(); + + private int chatCooldownCount; + + public int getChatCooldownTime(UUID player) { + return this.chatCooldownTime.get(player); + } + + public void setChatCooldown(UUID player, int number) { + this.chatCooldown.add(player); + this.chatCooldownTime.put(player, number); + } + + public void setChatCooldownTime(UUID player, int time) { + this.chatCooldownTime.remove(player); + this.chatCooldownTime.put(player, time); + } + + public boolean isOnChatCooldown(UUID player) { + return this.chatCooldown.contains(player); + } +} diff --git a/src/main/java/com/loganmagnan/eventcore/playerdata/PlayerData.java b/src/main/java/com/loganmagnan/eventcore/playerdata/PlayerData.java index fab5982..4d483ec 100644 --- a/src/main/java/com/loganmagnan/eventcore/playerdata/PlayerData.java +++ b/src/main/java/com/loganmagnan/eventcore/playerdata/PlayerData.java @@ -2,11 +2,10 @@ package com.loganmagnan.eventcore.playerdata; import com.loganmagnan.eventcore.EventCore; import com.loganmagnan.eventcore.managers.PlayerDataManager; -import com.loganmagnan.eventcore.managers.arena.Arena; import com.loganmagnan.eventcore.playerdata.currentgame.PlayerCurrentGameData; import lombok.Getter; import lombok.Setter; - +import org.bukkit.ChatColor; import java.util.UUID; @Getter @@ -22,6 +21,8 @@ public class PlayerData { private UUID uniqueId; private boolean loaded; + private ChatColor chatColor; + public PlayerData(UUID uniqueId) { this.uniqueId = uniqueId; this.loaded = false; diff --git a/src/main/java/com/loganmagnan/eventcore/runnables/CooldownRunnable.java b/src/main/java/com/loganmagnan/eventcore/runnables/CooldownRunnable.java new file mode 100644 index 0000000..3e2907b --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/runnables/CooldownRunnable.java @@ -0,0 +1,32 @@ +package com.loganmagnan.eventcore.runnables; + +import com.loganmagnan.eventcore.EventCore; +import org.bukkit.entity.Player; + +public class CooldownRunnable implements Runnable { + + private EventCore main = EventCore.getInstance(); + + @Override + public void run() { + this.main.getCooldownManager().setChatCooldownCount(this.main.getCooldownManager().getChatCooldownCount() + 1); + + for (Player player : this.main.getServer().getOnlinePlayers()) { + if (this.main.getCooldownManager().isOnChatCooldown(player.getUniqueId())) { + int count = this.main.getCooldownManager().getChatCooldownTime(player.getUniqueId()); + count--; + + this.main.getCooldownManager().setChatCooldownTime(player.getUniqueId(), count); + + if (count == 0) { + this.main.getCooldownManager().getChatCooldown().remove(player.getUniqueId()); + this.main.getCooldownManager().getChatCooldownTime().remove(player.getUniqueId()); + } + } + + if (this.main.getCooldownManager().getChatCooldownCount() == this.main.getChatManager().getDelayAmount()) { + this.main.getCooldownManager().setChatCooldownCount(0); + } + } + } +} diff --git a/src/main/java/com/loganmagnan/eventcore/utils/Constants.java b/src/main/java/com/loganmagnan/eventcore/utils/Constants.java new file mode 100644 index 0000000..90d50b3 --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/utils/Constants.java @@ -0,0 +1,37 @@ +package com.loganmagnan.eventcore.utils; + +import com.loganmagnan.eventcore.EventCore; +import java.util.*; + +public class Constants { + + private EventCore main = EventCore.getInstance(); + + public static Map> COMMAND_MESSAGES = new HashMap>(); + public static Map> LISTENER_MESSAGES = new HashMap>(); + public static Map> CHAT_MANAGEMENT_MESSAGES = new HashMap>(); + + public static Map PERMISSION_NODES = new HashMap(); + + public Constants() { + // /chat + COMMAND_MESSAGES.put("chat.help", this.main.getMessagesConfig().getConfig().getStringList("MESSAGES.COMMANDS.CHAT.HELP")); + + // /chat slow + COMMAND_MESSAGES.put("chat.slow.invalid-argument", Collections.singletonList(this.main.getMessagesConfig().getConfig().getString("MESSAGES.COMMANDS.CHAT.SLOW.INVALID-ARGUMENT"))); + COMMAND_MESSAGES.put("chat.slow.invalid-amount", Collections.singletonList(this.main.getMessagesConfig().getConfig().getString("MESSAGES.COMMANDS.CHAT.SLOW.INVALID-AMOUNT"))); + + // AsyncPlayerChatEvent + LISTENER_MESSAGES.put("chat-muted", Collections.singletonList(this.main.getMessagesConfig().getConfig().getString("MESSAGES.LISTENERS.CHAT-MUTED"))); + LISTENER_MESSAGES.put("chat-cooldown", this.main.getMessagesConfig().getConfig().getStringList("MESSAGES.LISTENERS.CHAT-COOLDOWN")); + + // Chat Management System + CHAT_MANAGEMENT_MESSAGES.put("muted", Collections.singletonList(this.main.getMessagesConfig().getConfig().getString("MESSAGES.CHAT-MANAGEMENT.MUTED"))); + CHAT_MANAGEMENT_MESSAGES.put("unmuted", Collections.singletonList(this.main.getMessagesConfig().getConfig().getString("MESSAGES.CHAT-MANAGEMENT.UNMUTED"))); + CHAT_MANAGEMENT_MESSAGES.put("slowed", Collections.singletonList(this.main.getMessagesConfig().getConfig().getString("MESSAGES.CHAT-MANAGEMENT.SLOWED"))); + CHAT_MANAGEMENT_MESSAGES.put("cleared", Collections.singletonList(this.main.getMessagesConfig().getConfig().getString("MESSAGES.CHAT-MANAGEMENT.CLEARED"))); + + // Permission Nodes + PERMISSION_NODES.put("chat-bypass", this.main.getMainConfig().getConfig().getString("PERMISSION-NODES.CHAT-BYPASS")); + } +} diff --git a/src/main/java/com/loganmagnan/eventcore/utils/Utils.java b/src/main/java/com/loganmagnan/eventcore/utils/Utils.java index dde505f..73c3979 100644 --- a/src/main/java/com/loganmagnan/eventcore/utils/Utils.java +++ b/src/main/java/com/loganmagnan/eventcore/utils/Utils.java @@ -109,37 +109,22 @@ public class Utils { } public static > Map.Entry getMaxEntryInMapBasedOnValue(Map map) { - // To store the result Map.Entry entryWithMaxValue = null; - // Iterate in the map to find the required entry - for (Map.Entry currentEntry : - map.entrySet()) { - - if ( - // If this is the first entry, set result as - // this - entryWithMaxValue == null - - // If this entry's value is more than the - // max value Set this entry as the max - || currentEntry.getValue().compareTo( - entryWithMaxValue.getValue()) - > 0) { - + for (Map.Entry currentEntry : map.entrySet()) { + if (entryWithMaxValue == null || currentEntry.getValue().compareTo(entryWithMaxValue.getValue()) > 0) { entryWithMaxValue = currentEntry; } } - // Return the entry with highest value return entryWithMaxValue; } - public boolean isNumeric(String string) { + public static boolean isNumeric(String string) { return regexNumeric(string).length() == 0; } - public String regexNumeric(String string) { + public static String regexNumeric(String string) { return string.replaceAll("[0-9]", "").replaceFirst("\\.", ""); } } \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e69de29..c7e9ce5 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -0,0 +1,2 @@ +PERMISSION-NODES: + CHAT-BYPASS: "eventcore.chat.bypass" \ No newline at end of file diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml new file mode 100644 index 0000000..da4f7ac --- /dev/null +++ b/src/main/resources/messages.yml @@ -0,0 +1,28 @@ +MESSAGES: + COMMANDS: + CHAT: # Permission Node - eventcore.command.chat + HELP: + - "%line%" + - "&bChat Commands" + - "%line%" + - "&7⚫ &9/chat &7- &eList of commands" + - "&7⚫ &9/chat manage &7- &eManagement menu" + - "&7⚫ &9/chat toggle &7- &eToggle the chat" + - "&7⚫ &9/chat slow &7- &eSlow the chat" + - "&7⚫ &9/chat clear &7- &eClear the chat" + - "%line%" + SLOW: + INVALID-ARGUMENT: "&cEnter an amount of time to slow the chat for" + INVALID-AMOUNT: "&cEnter a valid amount of time to slow the chat for" + LISTENERS: + CHAT-MUTED: "&cChat is muted" + CHAT-COOLDOWN: + - "" + - "&c&lChat Cooldown" + - "&cYou're on a chat cooldown for &c&l%time%" + - "" + CHAT-MANAGEMENT: + MUTED: "&cChat has been muted by &c&l%player%" + UNMUTED: "&aChat has been unmuted by &a&l%player%" + SLOWED: "&cChat has been slowed for &c&l%amount% by &c&l%player%" + CLEARED: "&cChat has been cleared by &c&l%player%" \ No newline at end of file