Skip to content

1.3. Command Manager

ThisKarolGajda edited this page Nov 30, 2022 · 1 revision

Making commands with OpC is very simple, let's look on how you can register your own commands!

Creating a command

new OpCommand("command-name");

For now this command does nothing, it's like dummy command but even more useless!

Registering a command

Registering your command is easer than creating it! You can just call a register() method on it, and we will register it for you!

new OpCommand("command-name")
        .register();

Register method should be used only in the main command AND in the last line of the entire command

Adding a subcommand

Since we wanted to make sure our Command Manager is easy to learn, we made our main class the only class we need, that's why in order to create a subcommand, you call a new command.

new OpCommand("command-name")
        .addCommand(new OpCommand("sub"));

Adding arguments

Arguments are the keys of the command execution. We provide our custom arguments to make your life easier!
You have to create a new instance of a class extending OpCommandArg.class! (And yes, you can also create your own arguments!)

new OpCommand("command-name")
        .addArg(new StringArg("string-arg-name"));

As you can see each argument requires one parameter - String, it's the name of the argument.
We made it so you can call the arguments by their name and not their argument id or argument length!

Adding suggestions

Since our core is running on the latest minecraft version, we wanted to make sure that players receive ability to receive suggestions while typing command.
While same with arguments, that you can create your own suggestions, it's recommended to use static suggestions.
Of course you don't have to, you can just create your own things!

new OpCommand("command-name")
        .addSuggestion(StaticSuggestions.SUGGESTION_TYPE.ENCHANTS);

That looks a bit confusing, doesn't it?
Well actually no, there are 5 addSuggestion() methods!
And here they are:

.addSuggestion(StaticSuggestions.SUGGESTION_TYPE.ENCHANTS)
.addSuggestion(List.of("list of string"))
.addSuggestion(new OpSimpleSuggestion("op simple suggestion"))
.addSuggestion(new OpCommandSuggestion(((sender, strings) -> {})))
.addSuggestion("suggestion")

Remember that adding multiple suggestions to single command / subcommand will generate suggestion tree, that goes from first to last suggestion in the addSuggestion order.

Adding Argument Suggestion

Because we hate, when you need to create multiple lines for the same argument and suggestion we made it easier!

new OpCommand("command-name")
        .addArgSuggestion(new EnchantArg("enchant-arg"), StaticSuggestions.SUGGESTION_TYPE.ENCHANTS);

But wait! What does this thing do?
It's simple! It's just a combination of the two previous methods!

Permissions

Since we know that some commands shouldn't be available to everyone, we have made a command permission system!
There are currently two permissions that are assigned to each command and subcommand!
They are: seeTabComplete and useCommand!
You can change them using these methods:

new OpCommand("command-name")
        .setUseCommand("permission-to-use-command", "You don't have permission to use that command!!!! Wrong command!!!!")
        .setSeeTabComplete("permission-to-see-tab-complete");

Executing code

And here it comes, the main part of the entire Command Manager system.
There are currently 6 execute methods which are as follow:

  • execute(BiConsumer<OpCommandSender, OpCommandArgument> biConsumer)
  • executeAsPlayer(BiConsumer<OpCommandSender, OpCommandArgument> biConsumer)
  • executeAsPlayerWithArgLength(BiConsumer<OpCommandSender, OpCommandArgument> biConsumer, int argLength)
  • execute(Consumer<OpCommandSender> consumer)
  • executeWithArgLength(BiConsumer<OpCommandSender, OpCommandArgument> biConsumer, int argLength)
  • executeWithArgLength(Consumer<OpCommandSender> consumer, int argLength)

In order to get argument from execute method, you need to select method which has OpCommandArgument.class in it, and use (recommended due to not interfering with argument number) #get(String name) or #get(String name, int arg)

OpCommandSender class contains sender method. If you are sure that a player will execute a code (or you choose executor with that), you should use #getPlayer() method, if not you should first check if the sender is player using #isPlayer() and then get it using #getFixedPlayer() or #getPlayer().

What is argLength? Arg length is a Integer parameter that definies how long the command should be.

Example of using the execute(BiConsumer<OpCommandSender, OpCommandArgument> biConsumer) executor

new OpCommand("command-name")
        // We need to remove default command suggestion because we have to override it
        .setRemoveDefaultCommandSuggestion(true)
        // Add String argument with name arg and suggestion with list of animals names
        .addArgSuggestion(new StringArg("arg"), Arrays.asList("Cat", "Dog", "Bird"))
        .execute((sender, args) -> {
            String arg = (String) args.get("arg");
            if (arg.equalsIgnoreCase("Cat")) {
                sender.sender().sendMessage("I love cats!!!");
            } else {
                sender.sender().sendMessage("You have chosen: " + args.get("arg"));
            }
        })
        // Don't forget to register it!!!
        .register();

Additional things

If you want to make sure that there is no crime and errors while a non-player thing executes your command, you should use this method!

new OpCommand("command-name")
        .setHasToBePlayer(true);

And if you want to delete the suggestions that are your subcommands in the command class you can just call this method!

new OpCommand("command-name")
        .setRemoveDefaultCommandSuggestion(true);