Command API - rodrigoo-r/Harmony GitHub Wiki
Harmony includes a built-in command API. With this API you can register commands and listen for chat commands. The most usual way to register commands is to add the commands into your plugin.yml
file, so they're loaded when your server starts.
Harmony can do that, but that's not it all. Harmony can dynamically register commands, even if they're not registered in your plugin.yml
.
Harmony can also listen por chat commands packets, you can process commands whenever a player executes it and prevent the "Player executed cocmmand: /command" message from being forwarded to the console. This mode requires ProtocolLib.
Declared commands are considered as commands you've manually added to your plugin.yml
file. If this is your case, you can easily create a CommandExecutor class.
public class MyCommand implements CommandExecutor {
// This is the standard Spigot way to listen for commands
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
// Your stuff goes here
return true;
}
}
public final class Example extends BackendPlugin {
@Override
public void whenInitialize() {
CommandUtil.registerListener("mycommand", new MyCommand());
}
@Override
public void whenUnloaded() {
// Plugin shutdown logic
}
}
If you want to register a command without including it directly into your plugin.yml
file, you can still use CommandUtil
public class a extends DynamicCommandListener {
public a(String commandName) {
super(commandName);
}
// Standard Harmony command executor
@Override
public boolean whenExecute(String[] arguments, CommandSender commandSender) {
// Your stuff here
return true;
// true -> Everything went correct
// false -> Something went wrong
}
@Override
public List<String> tabComplete(String[] strings, CommandSender commandSender) {
// Tab completion stuff
return null;
}
}
CommandUtil.registerListener(new a("myCommand"));
Important
This method is deprecated and it's no longer supported.
Important
This mode requires ProtocolLib to be added as a dependency to your plugin
This method directly listens for Packets and does not directly register commands. All it does is listen when a player tries to execute a command and intercept that interaction before it arrives to the server.
As this method does not register a command, the clients may not see the command if they try to tab completion. Even if they type the command, they'll see "Unknown command", however your stuff will be executed when they execute it. This is useful for internal commands that doesn't need to be directly registered.
[!IMPORTANT]
public class a extends UnregisteredCommandListener {
public a(String commandName) {
super(commandName);
}
@Override
public boolean whenExecute(String[] strings, Player player, PacketEvent packetEvent) {
// Find PacketEvent at the ProtocolLib API: com.comphenix.protocol.events.PacketEvent
return false;
// false -> Execute the event normally. Your stuff will be executed
// but the client will still get "Unknown Command. Type /help for help."
// true -> Cancel the event. The client won't get ant message
// The "Player executed a command" message won't be forwarded to the console
}
}
CommandUtil.registerListener(new a("myCommand"));