Registering commands in the Bukkit API is one of the most basic things plugin developers do. The procedure is fairly straightforward: add the command in the plugin.yml, set the executor, implement the command.
However, if you are not careful, you might end up registering your executors in the wrong way. There are a few ways you might see this done:
plugin.getCommand("cmd").setExecutor(new CmdExecutor());
plugin.getServer().getPluginCommand("cmd").setExecutor(new CmdExecutor());
Bukkit.getPluginCommand("cmd").setExecutor(new CmdExecutor());
The last two are actually the same thing (Bukkit just holds a static reference to the Server). However, they are both wrong and can result in your plugin interfering with another, causing confusion and annoyance.
JavaPlugin#getCommand
is the right way. It ensures your plugin only uses its own commands. If another plugin
registers the same command, there will be no issue - plugin:cmd can be used by server owners to differentiate.
Bukkit was designed to handle multiple plugins registering commands with the same name. It provides ways to
escape these conflicts, such as by using the commands.yml. However, if you use Server#getPluginCommand
,
there WILL be conflicts. You must use JavaPlugin#getCommand
to ensure your commands do not conflict with
other plugins'.
Do a find and replace, in all of your source files, for the text "getPluginCommand". As soon as you find it, refactor your code to use JavaPlugin#getCommand
.
Don't. You will experience problems with other plugins registering commands with the same name, which can lead to weird behaviour. See DevLeoko/AdvancedBan#373