Assigning the executor for the enchant command

We are almost ready to start using the command on the server. The only remaining step is to assign the class that we just wrote to the enchant command. This is typically referred to as registering a command. In the onEnable method of the Enchanter class, we will get the enchant command using the getCommand("enchant") code.

Tip

The name of the command must be exactly as it is in plugin.yml. This also means that this code will only retrieve commands that are specific to this plugin.

Once we have the enchant command, we can set a new instance of EnchantCommand as the executor for the command. All of this can be done in one line, as shown in the following piece of code:

getCommand("enchant").setExecutor(new EnchantCommand());

All that you will have in the main class is shown in the following code:

package com.codisimus.enchanter;

import org.bukkit.plugin.java.JavaPlugin;

  /**
  * Enchants the item that the command sender is holding
  */
public class Enchanter extends JavaPlugin {
  @Override
  public void onEnable() {
    //Assign the executor of the enchant command
    getCommand("enchant").setExecutor(new EnchantCommand());
  }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.240.243