Reputation: 1
I am doing a plugin in paper, and I need to get the player's output, when executing a command, for example, if a player uses: / tpa get an output like: "A teleportation request was sent to " while in the server console it only shows "user issued the command: /tpa "
package cherry.plugins;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerChatEvent;
public class CommandLogger implements Listener {
private final CherryCostume plugin;
// Variable para capturar la salida del comando antes de que se envíe al chat
private String currentCommandSender;
private String lastCommandOutput;
public CommandLogger(CherryCostume plugin) {
this.plugin = plugin;
Bukkit.getPluginManager().registerEvents(this, plugin);
}
@EventHandler
public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
String command = event.getMessage().substring(1); // Remueve la barra inicial
String playerName = event.getPlayer().getName();
// Verificamos si el comando es de tipo /tell o /msg
if (command.startsWith("tell") || command.startsWith("msg")) {
String[] commandParts = command.split(" ", 3);
if (commandParts.length >= 3) {
String targetPlayer = commandParts[1]; // Jugador objetivo
String message = commandParts[2]; // Mensaje del comando
// Guardamos el jugador que envió el comando para comparar más tarde
currentCommandSender = playerName;
// Logueamos el comando ejecutado
plugin.getLogger().info("Comando ejecutado: " + command);
plugin.getLogger().info("Salida: Comando enviado a " + targetPlayer + " con el mensaje: " + message);
}
}
}
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event) {
String message = event.getMessage();
// Verificamos si el mensaje corresponde a un susurro
if (message.startsWith("§7[§8[.*§r")) {
String sender = event.getPlayer().getName();
// Solo registramos el mensaje de chat si es de un jugador que acaba de ejecutar un comando
if (sender.equals(currentCommandSender)) {
// Capturamos la salida exacta como aparece en el chat
lastCommandOutput = message;
// Logueamos la salida exacta del chat generada por el comando
plugin.getLogger().info("Salida: " + lastCommandOutput);
}
}
}
}````
Upvotes: 0
Views: 44
Reputation: 16
The way you are handling commands is not the correct one. To create a command with the spigot API you need to first register your command in your plugin.yml
. Add these lines to it:
commands:
tpa:
After doing that, you need to create a class for the command. This class must implement CommandExecutor
(you can write implements CommandExecutor
after the class name). This implementation requires a method:
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
}
Inside of this method you must put the code you want executed when the player runs the command. CommandSender
can be casted to Player
by doing Player player = (Player) sender;
.
As for your question on console and player messages, the server automatically prints the "Player issued the command /command". If you want to show a message to the player that ran the command, you can create the Player
as shown before and then use player.sendMessage("message")
After doing all of this you need to register the command in the onEnable(), in the main plugin class. You can do this by writing getCommand("tpa").setExecutor(new TpaCommand(), this);
in the plugin's main class (the one that extends JavaPlugin
)
.
Hope this answers your question.
Upvotes: 0