Skip to content
Ntdi edited this page Dec 17, 2022 · 4 revisions

Introduction to the command framework

NR-Core's command framework is one of it's most impactful features, you never have to touch the plugin.yml!

Using the command framework is simple and very straight forward.

Usage

In order to start using NR-Core's command framework, you first need to make a new class for said command.

HelloCommand.java

public class HelloCommand extends CommandCore implements CommandFunction {



}

We extend the class CommandCore as that is where all the command functionality and registering comes from, then we implement CommandFunction which allows us to actually run code when the command is called.


Next we need to give CommandCore a constructor matching the super()

HelloCommand.java

public class HelloCommand extends CommandCore implements CommandFunction {

    public HelloCommand() {
        super("hello", "sayhi", "hi");
        setDefaultFunction(this);
    }

}

It's very important to call setDefaultFunction(this); As this links the CommandCore to CommandFunction

What does each value relate to you may ask, well here's the answer.

"hello" is the command's name e.g. /hello .

"sayhi" is the permission used, this will automatically add the perfix permission to it. e.g. nrcore.sayhi, Remeber this can be changed in the config.

"hi" is the command alias, this will tell the server to relate /hi to /hello

BOTH the alias and permission are Nullable!


Now time to actually add some command logic!

HelloCommand.java

public class HelloCommand extends CommandCore implements CommandFunction {

    public HelloCommand() {
        super("hello", "sayhi", "hi");
        setDefaultFunction(this);
    }

    @Override
    public void execute(CommandSender sender, String[] args) {
        sender.sendMessage("Hello World!");
    }
}

We override the execute method from CommandFunction so we can actaully have some code run when we type /hello

Final step! We need to initalize the command, for simplicity we are going to use the onEnable() method in the main class but you can register the command any way you see fit.

main.java

@Override
public void onEnable() {
    // Plugin startup logic

    new HelloCommand(); // Create a new instance of the class to register the command
}

That's it! You've succesfully registered your command! You can now fully use NR-Core's command framework to your liking.

Debugging

Let's fix some errors,

Function marked as non-null but is null

This is simple, you forgot to call setDefaultFunction(this); in your constructor!

public Command() {
    super("hello", "sayhi", "hi"); // Placeholder super

    setDefaultFunction(this); // You forgot this!
}

Other issues? Make a new Issue!

Examples

Want to see how some commands are done?

Check out these classes!

Clone this wiki locally