Skip to content

Instantly share code, notes, and snippets.

@A248
Last active June 26, 2020 17:21
Show Gist options
  • Save A248/985e2df6bc24285c4061cf5b59f54118 to your computer and use it in GitHub Desktop.
Save A248/985e2df6bc24285c4061cf5b59f54118 to your computer and use it in GitHub Desktop.
Plugin Developers: Don't use Server#getPluginCommand

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'.

How do I solve this?

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.

What happens if I don't do this?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment