Skip to content

Creating commands

kiinse edited this page Mar 24, 2023 · 1 revision

Create commands

MineCore makes it easier to create commands.

import kiinse.me.plugins.minecore.api.MineCorePlugin;
import kiinse.me.plugins.minecore.api.commands.MineCommand;
import kiinse.me.plugins.minecore.api.commands.MineCoreCommand;
import kiinse.me.plugins.minecore.api.commands.MineSubCommand;
import kiinse.me.plugins.minecore.api.files.locale.MineLocale;
import kiinse.me.plugins.minecore.lib.commands.MineContext;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

// @Command(command = "test", permission = "test.command.first") - This can also be done if you do not want to do any actions on the command without arguments
public class TestCommands extends MineCoreCommand {

    public TestCommands(@NotNull MineCorePlugin plugin) {
        super(plugin);
    }

    @MineCommand(command = "test", permission = "test.command.first")
    public void command(@NotNull MineContext context) {
        MineCorePlugin plugin = getPlugin(); // Getting plugin
        CommandSender sender = context.getSender(); // Getting command sender
        MineLocale senderLocale = context.getSenderLocale(); // Getting command sender locale
        String[] args = context.getArgs(); // Getting command args
        // Actions for the /test command
    }

    @MineSubCommand(command = "2", permission = "test.command.second", parameters = 1, disallowNonPlayer = true)
    public void command2(@NotNull MineContext context) {
        // Actions on the /test 2 <argument> command, which is only allowed to users (Cannot be executed from the console). The specified argument will be in args[0]
    }
}

If the user does not have rights to this command (or there are not enough arguments (or more than allowed)), then a message will be sent to him, taken from messages.json from MineCore. Those. no need to catch it all.

Register commands

It remains only to register this command in the main class.

import kiinse.me.plugins.minecore.api.MineCorePlugin;
import kiinse.me.plugins.minecore.api.commands.CommandManager;
import kiinse.me.plugins.minecore.lib.commands.MineCommandManager;

public class TestPlugin extends MineCorePlugin {

    @Override
    public void onStart() throws Exception {
        CommandManager commandManager = new MineCommandManager(this);
        commandManager.registerCommand(new TestCommands());
    }

    @Override
    public void onStop() throws Exception {
        // Shutdown code
    }
}