Files
src
manifest.kod
pom.xml
README.md
Code
22 1,439
src/main/java/com/example/staff/command/StaffCommand.java
package com.example.staff.command;
import com.example.staff.config.ConfigManager;
import com.example.staff.luckperms.LuckPermsService;
import com.example.staff.manager.NetworkPlayerManager;
import com.example.staff.manager.PunishmentManager;
import com.example.staff.model.Punishment;
import com.example.staff.model.PunishmentType;
import com.example.staff.service.ActionLogger;
import com.example.staff.service.MessageService;
import com.example.staff.service.NetworkStateService;
import com.example.staff.utility.DurationParser;
import com.example.staff.utility.TextUtil;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import net.kyori.adventure.text.Component;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class StaffCommand implements SimpleCommand {
private final ProxyServer proxy;
private final ConfigManager config;
private final MessageService messages;
private final LuckPermsService luckPerms;
private final NetworkPlayerManager players;
private final PunishmentManager punishments;
private final NetworkStateService state;
private final ActionLogger logger;
public StaffCommand(ProxyServer proxy, ConfigManager config, MessageService messages, LuckPermsService luckPerms, NetworkPlayerManager players, PunishmentManager punishments, NetworkStateService state, ActionLogger logger) {
this.proxy = proxy;
this.config = config;
this.messages = messages;
this.luckPerms = luckPerms;
this.players = players;
this.punishments = punishments;
this.state = state;
this.logger = logger;
}
@Override
public void execute(Invocation invocation) {
String[] args = invocation.arguments();
if (args.length == 0) {
if (check(invocation.source(), "use")) help(invocation.source());
return;
}
String subcommand = args[0].toLowerCase();
try {
switch (subcommand) {
case "help" -> { if (check(invocation.source(), "help")) help(invocation.source()); }
case "vanish" -> vanish(invocation.source());
case "chat" -> chat(invocation.source());
case "list" -> list(invocation.source());
case "alert" -> alert(invocation.source(), args);
case "freeze" -> freeze(invocation.source(), args, false);
case "unfreeze" -> freeze(invocation.source(), args, true);
case "kick" -> kick(invocation.source(), args);
case "ban" -> ban(invocation.source(), args);
case "unban" -> removePunishment(invocation.source(), args, PunishmentType.BAN);
case "mute" -> mute(invocation.source(), args);
case "unmute" -> removePunishment(invocation.source(), args, PunishmentType.MUTE);
case "tp" -> teleport(invocation.source(), args);
case "inspect" -> inspect(invocation.source(), args);
case "reload" -> reload(invocation.source());
default -> messages.send(invocation.source(), "messages.usage", Collections.emptyMap());
}
} catch (Exception exception) {
logger.log("ERROR", invocation.source().toString(), "-", "Command=" + subcommand + " Error=" + exception.getClass().getSimpleName());
messages.send(invocation.source(), "messages.command-error", Collections.emptyMap());
}
}
private void help(CommandSource source) {
source.sendMessage(messages.render("messages.help-header", Collections.emptyMap()));
String[][] entries = {{"help", "help", "Mostrar esta ayuda"}, {"vanish", "vanish", "Alternar vanish"}, {"chat", "chat", "Alternar Staff Chat"}, {"list", "stafflist", "Ver staff conectado"}, {"alert <mensaje>", "alert", "Enviar una alerta"}, {"freeze <jugador>", "freeze", "Congelar un jugador"}, {"unfreeze <jugador>", "unfreeze", "Descongelar un jugador"}, {"kick <jugador> <motivo>", "kick", "Expulsar un jugador"}, {"ban <jugador> [tiempo] <motivo>", "ban", "Banear un jugador"}, {"unban <jugador>", "unban", "Retirar un ban"}, {"mute <jugador> <tiempo> <motivo>", "mute", "Mutear un jugador"}, {"unmute <jugador>", "unmute", "Retirar un mute"}, {"tp <jugador>", "tp", "Conectar con su servidor"}, {"inspect <jugador>", "inspect", "Inspeccionar un jugador"}, {"reload", "reload", "Recargar configuración"}};
for (String[] entry : entries) {
if (source instanceof Player player && !luckPerms.hasPermission(player, config.permission(entry[1]))) continue;
Map<String, String> values = Map.of("command", entry[0], "description", entry[2]);
source.sendMessage(messages.render("messages.help-line", values));
}
source.sendMessage(messages.render("messages.help-footer", Collections.emptyMap()));
}
private void vanish(CommandSource source) {
if (!(source instanceof Player player)) { playerOnly(source); return; }
if (!check(player, "vanish")) return;
boolean enabled = state.toggleVanish(player);
messages.send(player, enabled ? "messages.vanish-enabled" : "messages.vanish-disabled", Collections.emptyMap());
logger.log("VANISH", player.getUsername(), player.getUsername(), "Enabled=" + enabled);
}
private void chat(CommandSource source) {
if (!(source instanceof Player player)) { playerOnly(source); return; }
if (!check(player, "chat")) return;
boolean enabled = state.toggleStaffChat(player.getUniqueId());
messages.send(player, enabled ? "messages.staff-chat-enabled" : "messages.staff-chat-disabled", Collections.emptyMap());
}
private void list(CommandSource source) {
if (!check(source, "stafflist")) return;
source.sendMessage(messages.render("messages.staff-list-header", Collections.emptyMap()));
for (Player player : players.online()) {
if (!luckPerms.hasPermission(player, config.permission("stafflist"))) continue;
source.sendMessage(messages.render("messages.staff-list-line", Map.of("player", player.getUsername(), "server", players.server(player))));
}
}
private void alert(CommandSource source, String[] args) {
if (!check(source, "alert")) return;
if (args.length < 2) { messages.send(source, "messages.reason-required", Collections.emptyMap()); return; }
String message = join(args, 1);
boolean global = source instanceof Player player && luckPerms.hasPermission(player, config.permission("alert-global"));
Component component = messages.render(global ? "formats.global-alert" : "formats.alert", Map.of("message", message));
if (global) players.online().forEach(player -> player.sendMessage(component));
else players.online().stream().filter(player -> luckPerms.hasPermission(player, config.permission("alert"))).forEach(player -> player.sendMessage(component));
proxy.getConsoleCommandSource().sendMessage(component);
messages.send(source, "messages.alert-sent", Collections.emptyMap());
logger.log("ALERT", actorName(source), "-", "Global=" + global + " Message=" + message);
}
private void freeze(CommandSource source, String[] args, boolean unfreeze) {
if (!check(source, unfreeze ? "unfreeze" : "freeze")) return;
if (args.length < 2) { messages.send(source, "messages.player-not-found", Collections.emptyMap()); return; }
Optional<Player> target = players.find(args[1]);
if (target.isEmpty()) { messages.send(source, "messages.player-not-found", Collections.emptyMap()); return; }
if (!protect(source, target.get())) return;
if (unfreeze) {
if (state.unfreeze(target.get().getUniqueId()) == null) { messages.send(source, "messages.not-frozen", Collections.emptyMap()); return; }
messages.send(source, "messages.unfreeze-success", Map.of("player", target.get().getUsername()));
logger.log("UNFREEZE", actorName(source), target.get().getUsername(), "Server=" + players.server(target.get()));
} else {
state.freeze(target.get().getUniqueId(), source instanceof Player player ? player.getUniqueId() : null, actorName(source));
target.get().sendMessage(messages.render("messages.frozen", Map.of("staff", actorName(source))));
messages.send(source, "messages.freeze-success", Map.of("player", target.get().getUsername()));
logger.log("FREEZE", actorName(source), target.get().getUsername(), "Server=" + players.server(target.get()));
}
}
private void kick(CommandSource source, String[] args) {
if (!check(source, "kick")) return;
if (args.length < 3) { messages.send(source, "messages.reason-required", Collections.emptyMap()); return; }
Optional<Player> target = players.find(args[1]);
if (target.isEmpty()) { messages.send(source, "messages.player-not-found", Collections.emptyMap()); return; }
if (!protect(source, target.get())) return;
String reason = join(args, 2);
target.get().disconnect(messages.render("messages.kicked", Map.of("staff", actorName(source), "reason", reason)));
messages.send(source, "messages.kick-success", Map.of("player", target.get().getUsername()));
logger.log("KICK", actorName(source), target.get().getUsername(), "Reason=" + reason + " Server=" + players.server(target.get()));
}
private void ban(CommandSource source, String[] args) {
if (!check(source, "ban")) return;
if (args.length < 3) { messages.send(source, "messages.reason-required", Collections.emptyMap()); return; }
Optional<Player> optionalTarget = players.find(args[1]);
if (optionalTarget.isEmpty()) { messages.send(source, "messages.player-not-found", Collections.emptyMap()); return; }
Player target = optionalTarget.get();
if (!protect(source, target)) return;
if (punishments.getBan(target.getUniqueId()) != null) { messages.send(source, "messages.already-banned", Collections.emptyMap()); return; }
DurationAndReason parsed = durationAndReason(args, 2);
if (parsed == null) return;
if (parsed.reason.isBlank()) { messages.send(source, "messages.reason-required", Collections.emptyMap()); return; }
Punishment punishment = punishments.create(target.getUniqueId(), target.getUsername(), source instanceof Player player ? player.getUniqueId() : null, actorName(source), PunishmentType.BAN, parsed.reason, parsed.duration);
target.disconnect(messages.render("messages.banned", Map.of("reason", parsed.reason, "duration", punishment.durationText(), "remaining", punishment.isPermanent() ? "permanent" : TextUtil.formatDuration(Duration.between(Instant.now(), punishment.getExpiresAt())))));
messages.send(source, "messages.ban-success", Map.of("player", target.getUsername(), "duration", punishment.durationText()));
logger.log("BAN", actorName(source), target.getUsername(), "Reason=" + parsed.reason + " Duration=" + punishment.durationText() + " Server=" + players.server(target));
}
private void mute(CommandSource source, String[] args) {
if (!check(source, "mute")) return;
if (args.length < 4) { messages.send(source, "messages.reason-required", Collections.emptyMap()); return; }
Optional<Player> optionalTarget = players.find(args[1]);
if (optionalTarget.isEmpty()) { messages.send(source, "messages.player-not-found", Collections.emptyMap()); return; }
Player target = optionalTarget.get();
if (!protect(source, target)) return;
if (punishments.getMute(target.getUniqueId()) != null) { messages.send(source, "messages.already-muted", Collections.emptyMap()); return; }
Duration duration = DurationParser.isPermanent(args[2]) ? null : DurationParser.parse(args[2]).orElse(null);
if (duration == null && !DurationParser.isPermanent(args[2])) { messages.send(source, "messages.invalid-duration", Collections.emptyMap()); return; }
String reason = join(args, 3);
Punishment punishment = punishments.create(target.getUniqueId(), target.getUsername(), source instanceof Player player ? player.getUniqueId() : null, actorName(source), PunishmentType.MUTE, reason, duration);
messages.send(source, "messages.mute-success", Map.of("player", target.getUsername(), "duration", punishment.durationText()));
logger.log("MUTE", actorName(source), target.getUsername(), "Reason=" + reason + " Duration=" + punishment.durationText());
}
private void removePunishment(CommandSource source, String[] args, PunishmentType type) {
if (!check(source, type == PunishmentType.BAN ? "unban" : "unmute")) return;
if (args.length < 2) { messages.send(source, "messages.player-not-found", Collections.emptyMap()); return; }
Optional<Player> target = players.find(args[1]);