Setup Chat Management System (75% All Done)

This commit is contained in:
Trixkz 2023-10-25 19:35:21 -04:00
parent be65914faf
commit 9ff8cad8fd
11 changed files with 341 additions and 22 deletions

View File

@ -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<Player, PlayerMenuUtil> 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) {

View File

@ -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;
}
}
}
}

View File

@ -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());
}
}

View File

@ -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));
}
}

View File

@ -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<UUID> chatCooldown = new HashSet<UUID>();
private Map<UUID, Integer> chatCooldownTime = new HashMap<UUID, Integer>();
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);
}
}

View File

@ -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;

View File

@ -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);
}
}
}
}

View File

@ -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<String, List<String>> COMMAND_MESSAGES = new HashMap<String, List<String>>();
public static Map<String, List<String>> LISTENER_MESSAGES = new HashMap<String, List<String>>();
public static Map<String, List<String>> CHAT_MANAGEMENT_MESSAGES = new HashMap<String, List<String>>();
public static Map<String, String> PERMISSION_NODES = new HashMap<String, String>();
public Constants() {
// /chat
COMMAND_MESSAGES.put("chat.help", this.main.getMessagesConfig().getConfig().getStringList("MESSAGES.COMMANDS.CHAT.HELP"));
// /chat slow <amount>
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"));
}
}

View File

@ -109,37 +109,22 @@ public class Utils {
}
public static <K, V extends Comparable<V> > Map.Entry<K, V> getMaxEntryInMapBasedOnValue(Map<K, V> map) {
// To store the result
Map.Entry<K, V> entryWithMaxValue = null;
// Iterate in the map to find the required entry
for (Map.Entry<K, V> 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<K, V> 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("\\.", "");
}
}

View File

@ -0,0 +1,2 @@
PERMISSION-NODES:
CHAT-BYPASS: "eventcore.chat.bypass"

View File

@ -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 <amount> &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%"