Skip to content

Developer Command

ludgart edited this page Nov 11, 2020 · 2 revisions

Developer Command

Every created command run async.
That mean, the command run in background and not directly in the main thread. In future you can turn off these behavior.

Package

Create an new "command" package for all our commands.

Create in the package an new Java Class with your new command name. Example "TestCommand"

Extend from our API

public class TestCommand extends Command {

Implement the methods

@Override
public List<String> onTabComplete(CommandSender commandSender, String[] strings) {
  return null;
}

@Override
  public void onExecute(CommandSender commandSender, String[] strings) {
}

Constructor

public TestCommand() {
  super("test", "permission.test", new ArrayList<>());
}

With this constructur we call the super class from the extended "Command" class.
In the first part you can set the executing command. In this example is it "test".
In the second part you can set the required permission for the player.
The last part are the aliases, but in this scenario we dont need aliases.

Constructor with aliases

public TestCommand() {
  super("test", "permission.test", new String[]{"tests", "testo", "tester"});
}

Method onExecute()

@Override
public void onExecute(CommandSender sender, String[] args) {
# Check if only the player executed this command
if (!(sender instanceof Player)) return;
  if (args.length == 0) {
    Player player = (Player) sender;
    player.sendMessage(TextComponent.fromLegacyText("Test"));
    return;
  }
}

Command registration

To make that work, we need to register the command in our Main Java Class in the registerCommand() Method

@Override
public void registerCommand() {
  getCommand("test").setExecutor(new TestCommand());
}

One of the most common mistakes is, that we also need to run the registerCommand() Method from the onEnable() Method.
Switch to the method and add following line

@Override
public void onEnable() {
  super.onEnable();

  this.setDisplayItem(CompatibleMaterial.OAK_SIGN.getMaterial());
  this.setHelpCommand("core");
  this.setPluginName("MZP-Core");
		
  this.registerCommand();
}

Plugin.yml

The last step is to add the command into our Plugin.yml file.

name: Test
main: org.example.test.Main
author: ludgart
version: 0.0.1
api-version: 1.13
website: https://minecraft.plugin.zocker.pro/

commands:
  test:
    aliases: [tests,testo,tester]

Compile & Run

Compile the plugin and put the compiled JAR file into your /plugins directory. After the restart you can your new command. Try it out!

Information

Server Owner

Developer

Clone this wiki locally