package com.adzel.velocitybroadcast; import java.util.List; import com.adzel.velocitybroadcast.util.DatabaseManager; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.proxy.Player; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; public class VBCommand implements SimpleCommand { private final VelocityBroadcast plugin; private final DatabaseManager databaseManager; private final MiniMessage mm = MiniMessage.miniMessage(); public VBCommand(VelocityBroadcast plugin) { this.plugin = plugin; this.databaseManager = plugin.getDatabaseManager(); } @Override public void execute(Invocation invocation) { CommandSource source = invocation.source(); List args = List.of(invocation.arguments()); String fullCommand = "/vb" + (args.isEmpty() ? "" : " " + String.join(" ", args)); String username = (source instanceof Player) ? ((Player) source).getUsername() : "Console"; boolean success = true; try { if (args.isEmpty() || args.get(0).equalsIgnoreCase("help")) { source.sendMessage(parseFormatted("VelocityBroadcast Commands:")); source.sendMessage(parseFormatted("/vb - Broadcast a message to all players")); if (source.hasPermission("vb.admin")) { source.sendMessage(parseFormatted("/vb prefix - Change the broadcast prefix")); source.sendMessage(parseFormatted("/vb reload - Reload the plugin config")); } success = false; return; } String sub = args.get(0).toLowerCase(); List subArgs = args.subList(1, args.size()); switch (sub) { case "prefix" -> { if (!source.hasPermission("vb.admin")) { source.sendMessage(parseFormatted("You don't have permission to change the broadcast prefix.")); success = false; return; } if (subArgs.isEmpty()) { source.sendMessage(parseFormatted("Usage: /vb prefix ")); success = false; return; } String newPrefix = String.join(" ", subArgs); plugin.getConfigHandler().setPrefix(newPrefix); source.sendMessage(parseFormatted("Prefix updated to: " + newPrefix + "")); if (plugin.getConfigHandler().isDebugEnabled()) { plugin.getLogger().info("[Prefix] Updated prefix to: " + newPrefix); } } case "reload" -> { if (!source.hasPermission("vb.admin")) { source.sendMessage(parseFormatted("You don't have permission to reload the config.")); success = false; return; } plugin.getConfigHandler().reload(); source.sendMessage(parseFormatted("VelocityBroadcast config reloaded.")); if (plugin.getConfigHandler().isDebugEnabled()) { plugin.getLogger().info("[Reload] Config reloaded by " + source.toString()); } } default -> { // Treat as broadcast message if (!source.hasPermission("vb.broadcast")) { source.sendMessage(parseFormatted("You don't have permission to broadcast.")); success = false; return; } String fullMessage = plugin.getConfigHandler().getPrefix() + String.join(" ", args); Component broadcast = parseFormatted(fullMessage); plugin.getServer().getAllPlayers().forEach(p -> p.sendMessage(broadcast)); if (plugin.getConfigHandler().isDebugEnabled()) { plugin.getLogger().info("[Broadcast] Sent: " + fullMessage); } } } } catch (Exception e) { plugin.getLogger().error("Error executing /vb command", e); source.sendMessage(parseFormatted("An error occurred while executing the command.")); success = false; } finally { // ✅ Log the command usage databaseManager.logCommand(username, fullCommand, success); } } private Component parseFormatted(String input) { String mini = input .replace("&0", "") .replace("&1", "") .replace("&2", "") .replace("&3", "") .replace("&4", "") .replace("&5", "") .replace("&6", "") .replace("&7", "") .replace("&8", "") .replace("&9", "") .replace("&a", "") .replace("&b", "") .replace("&c", "") .replace("&d", "") .replace("&e", "") .replace("&f", "") .replace("&l", "") .replace("&n", "") .replace("&o", "") .replace("&m", "") .replace("&r", ""); return mm.deserialize(mini); } }