Upload via Git Manager GUI - CustomCommandModule.java

This commit is contained in:
2026-04-01 10:15:26 +00:00
parent dbeae4ca40
commit c2f0e2d84d

View File

@@ -1,79 +1,79 @@
package net.viper.status.modules.customcommands; package net.viper.status.modules.customcommands;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.CopyOption; import java.nio.file.CopyOption;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ChatEvent; import net.md_5.bungee.api.event.ChatEvent;
import net.md_5.bungee.api.plugin.Command; import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin; // Import für das Interface Argument import net.md_5.bungee.api.plugin.Plugin; // Import für das Interface Argument
import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider; import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration; import net.md_5.bungee.config.YamlConfiguration;
import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventHandler;
import net.viper.status.StatusAPI; import net.viper.status.StatusAPI;
import net.viper.status.module.Module; import net.viper.status.module.Module;
public class CustomCommandModule implements Module, Listener { public class CustomCommandModule implements Module, Listener {
private StatusAPI plugin; private StatusAPI plugin;
private Configuration config; private Configuration config;
private Command chatCommand; private Command chatCommand;
public CustomCommandModule() { public CustomCommandModule() {
// Leerer Konstruktor // Leerer Konstruktor
} }
@Override @Override
public String getName() { public String getName() {
return "CustomCommandModule"; return "CustomCommandModule";
} }
@Override @Override
public void onEnable(Plugin plugin) { public void onEnable(Plugin plugin) {
// Hier casten wir 'Plugin' zu 'StatusAPI', da wir wissen, dass es das ist // Hier casten wir 'Plugin' zu 'StatusAPI', da wir wissen, dass es das ist
this.plugin = (StatusAPI) plugin; this.plugin = (StatusAPI) plugin;
this.plugin.getLogger().info("Lade CustomCommandModule..."); this.plugin.getLogger().info("Lade CustomCommandModule...");
reloadConfig(); reloadConfig();
if (this.config == null) { if (this.config == null) {
this.config = new Configuration(); this.config = new Configuration();
this.plugin.getLogger().warning("customcommands.yml konnte nicht geladen werden. Verwende leere Konfiguration."); this.plugin.getLogger().warning("customcommands.yml konnte nicht geladen werden. Verwende leere Konfiguration.");
} }
// /bcmds Reload Befehl registrieren // /bcmds Reload Befehl registrieren
this.plugin.getProxy().getPluginManager().registerCommand(this.plugin, new Command("bcmds") { this.plugin.getProxy().getPluginManager().registerCommand(this.plugin, new Command("bcmds") {
@Override @Override
public void execute(CommandSender sender, String[] args) { public void execute(CommandSender sender, String[] args) {
if (!sender.hasPermission("statusapi.bcmds")) { if (!sender.hasPermission("statusapi.bcmds")) {
sender.sendMessage(new TextComponent(ChatColor.RED + "You don't have permission.")); sender.sendMessage(new TextComponent(ChatColor.RED + "You don't have permission."));
} else { } else {
reloadConfig(); reloadConfig();
sender.sendMessage(new TextComponent(ChatColor.GREEN + "Config reloaded.")); sender.sendMessage(new TextComponent(ChatColor.GREEN + "Config reloaded."));
} }
} }
}); });
// /chat Befehl registrieren (falls aktiviert) // /chat Befehl registrieren (falls aktiviert)
if (config.getBoolean("chat-command", true)) { if (config.getBoolean("chat-command", true)) {
chatCommand = new Command("chat") { chatCommand = new Command("chat") {
@Override @Override
public void execute(CommandSender sender, String[] args) { public void execute(CommandSender sender, String[] args) {
if (sender instanceof ProxiedPlayer) { if (sender instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) sender; ProxiedPlayer player = (ProxiedPlayer) sender;
if (player.getServer() == null) { if (player.getServer() == null) {
player.sendMessage(new TextComponent(ChatColor.RED + "Konnte deinen Server nicht ermitteln. Bitte versuche es erneut.")); player.sendMessage(new TextComponent(ChatColor.RED + "Konnte deinen Server nicht ermitteln. Bitte versuche es erneut."));
@@ -86,40 +86,40 @@ public class CustomCommandModule implements Module, Listener {
} }
ChatEvent e = new ChatEvent(player, player.getServer(), msg); ChatEvent e = new ChatEvent(player, player.getServer(), msg);
ProxyServer.getInstance().getPluginManager().callEvent(e); ProxyServer.getInstance().getPluginManager().callEvent(e);
if (!e.isCancelled()) { if (!e.isCancelled()) {
if (!e.isCommand() || !ProxyServer.getInstance().getPluginManager().dispatchCommand(sender, msg.substring(1))) { if (!e.isCommand() || !ProxyServer.getInstance().getPluginManager().dispatchCommand(sender, msg.substring(1))) {
player.chat(msg); player.chat(msg);
} }
} }
} else { } else {
String msg = String.join(" ", args); String msg = String.join(" ", args);
if(msg.startsWith("/")) { if(msg.startsWith("/")) {
ProxyServer.getInstance().getPluginManager().dispatchCommand(sender, msg.substring(1)); ProxyServer.getInstance().getPluginManager().dispatchCommand(sender, msg.substring(1));
} else { } else {
sender.sendMessage(new TextComponent("Console cannot send chat messages via /chat usually.")); sender.sendMessage(new TextComponent("Console cannot send chat messages via /chat usually."));
} }
} }
} }
}; };
this.plugin.getProxy().getPluginManager().registerCommand(this.plugin, chatCommand); this.plugin.getProxy().getPluginManager().registerCommand(this.plugin, chatCommand);
} }
this.plugin.getProxy().getPluginManager().registerListener(this.plugin, this); this.plugin.getProxy().getPluginManager().registerListener(this.plugin, this);
} }
@Override @Override
public void onDisable(Plugin plugin) { public void onDisable(Plugin plugin) {
// Optional: Cleanup logic, falls nötig. // Optional: Cleanup logic, falls nötig.
// Wir nutzen hier das übergebene 'plugin' Argument (oder this.plugin, ist egal) // Wir nutzen hier das übergebene 'plugin' Argument (oder this.plugin, ist egal)
// Listener und Commands werden automatisch entfernt, wenn das Plugin stoppt. // Listener und Commands werden automatisch entfernt, wenn das Plugin stoppt.
} }
public void reloadConfig() { public void reloadConfig() {
try { try {
if (!this.plugin.getDataFolder().exists()) { if (!this.plugin.getDataFolder().exists()) {
this.plugin.getDataFolder().mkdirs(); this.plugin.getDataFolder().mkdirs();
} }
File file = new File(this.plugin.getDataFolder(), "customcommands.yml"); File file = new File(this.plugin.getDataFolder(), "customcommands.yml");
if (!file.exists()) { if (!file.exists()) {
// Kopieren aus Resources // Kopieren aus Resources
InputStream in = this.plugin.getResourceAsStream("customcommands.yml"); InputStream in = this.plugin.getResourceAsStream("customcommands.yml");
@@ -136,107 +136,107 @@ public class CustomCommandModule implements Module, Listener {
e.printStackTrace(); e.printStackTrace();
this.plugin.getLogger().severe("Konnte customcommands.yml nicht laden!"); this.plugin.getLogger().severe("Konnte customcommands.yml nicht laden!");
} }
} }
@EventHandler(priority = 64) @EventHandler(priority = 64)
public void onCommand(ChatEvent e) { public void onCommand(ChatEvent e) {
if (!e.isCommand()) return; if (!e.isCommand()) return;
if (!(e.getSender() instanceof ProxiedPlayer)) return; if (!(e.getSender() instanceof ProxiedPlayer)) return;
final ProxiedPlayer player = (ProxiedPlayer) e.getSender(); final ProxiedPlayer player = (ProxiedPlayer) e.getSender();
String[] split = e.getMessage().split(" "); String[] split = e.getMessage().split(" ");
String label = split[0].substring(1); String label = split[0].substring(1);
final List<String> args = new ArrayList<>(Arrays.asList(split)); final List<String> args = new ArrayList<>(Arrays.asList(split));
args.remove(0); args.remove(0);
Configuration cmds = config.getSection("commands"); Configuration cmds = config.getSection("commands");
if (cmds == null) return; if (cmds == null) return;
Configuration section = null; Configuration section = null;
String foundKey = null; String foundKey = null;
for (String key : cmds.getKeys()) { for (String key : cmds.getKeys()) {
Configuration cmdSection = cmds.getSection(key); Configuration cmdSection = cmds.getSection(key);
if (key.equalsIgnoreCase(label)) { if (key.equalsIgnoreCase(label)) {
section = cmdSection; section = cmdSection;
foundKey = key; foundKey = key;
break; break;
} }
for (String alias : cmdSection.getStringList("aliases")) { for (String alias : cmdSection.getStringList("aliases")) {
if (alias.equalsIgnoreCase(label)) { if (alias.equalsIgnoreCase(label)) {
section = cmdSection; section = cmdSection;
foundKey = key; foundKey = key;
break; break;
} }
} }
if (section != null) break; if (section != null) break;
} }
if (section == null) return; if (section == null) return;
String type = section.getString("type", "line"); String type = section.getString("type", "line");
String sendertype = section.getString("sender", "default"); String sendertype = section.getString("sender", "default");
String permission = section.getString("permission", ""); String permission = section.getString("permission", "");
final List<String> commands = section.getStringList("commands"); final List<String> commands = section.getStringList("commands");
if (!permission.isEmpty() && !player.hasPermission(permission)) { if (!permission.isEmpty() && !player.hasPermission(permission)) {
player.sendMessage(new TextComponent(ChatColor.RED + "You don't have permission.")); player.sendMessage(new TextComponent(ChatColor.RED + "You don't have permission."));
e.setCancelled(true); e.setCancelled(true);
return; return;
} }
e.setCancelled(true); e.setCancelled(true);
final CommandSender target; final CommandSender target;
if (sendertype.equals("default")) { if (sendertype.equals("default")) {
target = player; target = player;
} else if (sendertype.equals("admin")) { } else if (sendertype.equals("admin")) {
target = new ForwardSender(player, true); target = new ForwardSender(player, true);
} else if (sendertype.equals("console")) { } else if (sendertype.equals("console")) {
target = ProxyServer.getInstance().getConsole(); target = ProxyServer.getInstance().getConsole();
} else { } else {
ProxiedPlayer targetPlayer = ProxyServer.getInstance().getPlayer(sendertype); ProxiedPlayer targetPlayer = ProxyServer.getInstance().getPlayer(sendertype);
if (targetPlayer == null || !targetPlayer.isConnected()) { if (targetPlayer == null || !targetPlayer.isConnected()) {
player.sendMessage(new TextComponent(ChatColor.RED + "Player " + sendertype + " is not online.")); player.sendMessage(new TextComponent(ChatColor.RED + "Player " + sendertype + " is not online."));
return; return;
} }
target = targetPlayer; target = targetPlayer;
} }
String argsString = args.size() >= 1 ? String.join(" ", args) : ""; String argsString = args.size() >= 1 ? String.join(" ", args) : "";
final String finalArgs = argsString; final String finalArgs = argsString;
final String senderName = player.getName(); final String senderName = player.getName();
if (type.equals("random")) { if (type.equals("random")) {
int randomIndex = new Random().nextInt(commands.size()); int randomIndex = new Random().nextInt(commands.size());
String rawCommand = commands.get(randomIndex); String rawCommand = commands.get(randomIndex);
executeCommand(target, rawCommand, finalArgs, senderName); executeCommand(target, rawCommand, finalArgs, senderName);
} else if (type.equals("line")) { } else if (type.equals("line")) {
ProxyServer.getInstance().getScheduler().runAsync(this.plugin, new Runnable() { ProxyServer.getInstance().getScheduler().runAsync(this.plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
for (String rawCommand : commands) { for (String rawCommand : commands) {
executeCommand(target, rawCommand, finalArgs, senderName); executeCommand(target, rawCommand, finalArgs, senderName);
try { try {
Thread.sleep(100L); Thread.sleep(100L);
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
} }
}); });
} else { } else {
this.plugin.getLogger().warning("Unknown type '" + type + "' for command " + foundKey); this.plugin.getLogger().warning("Unknown type '" + type + "' for command " + foundKey);
} }
} }
private void executeCommand(CommandSender sender, String rawCommand, String args, String playerName) { private void executeCommand(CommandSender sender, String rawCommand, String args, String playerName) {
String parsed = rawCommand String parsed = rawCommand
.replace("%args%", args) .replace("%args%", args)
.replace("%sender%", playerName); .replace("%sender%", playerName);
String commandToDispatch = parsed.startsWith("/") ? parsed.substring(1) : parsed; String commandToDispatch = parsed.startsWith("/") ? parsed.substring(1) : parsed;
ProxyServer.getInstance().getPluginManager().dispatchCommand(sender, commandToDispatch); ProxyServer.getInstance().getPluginManager().dispatchCommand(sender, commandToDispatch);
} }
} }