How to register commands with no plugin.yml

Discussion in 'Spigot Plugin Help' started by xeu1000, May 30, 2017.

  1. So when I search this topic 'How to register commands with no plugin.yml' it always comes up with old methods or NMS. Any suggestions?
     
    • Agree Agree x 1
    • Informative Informative x 1
    • Useful Useful x 1
    • Informative Informative x 1
  2. Another possibility would be to listen for the PlayerCommandPreprocessEvent instead, but this would obviously break stuff. Why don't you like the plugin.yml solution? I think it's a pretty smart solution because it allows users to add aliases without editing the source and recompiling your plugin.
     
  3. You could use something like this for easily adding commands to the server, just simply call the method and it will add the command, with description, usage and possible aliases to the server.

    This will add the command to the servers command map:
    Code (Java):

    public void register(String name, String description, String usage, List<String> aliases) {
            try {
                Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");

                bukkitCommandMap.setAccessible(true);
                CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());

                commandMap.register("commandname", new CustomCommand(name, description, usage, aliases));

            } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
                //TODO Handle exceptions
                e.printStackTrace();
            }
    }
     
    And this class will handle the command itself:
    Code (Java):

    public class CustomCommand extends BukkitCommand {

        public CustomCommand(String name, String description, String usageMessage, List<String> aliases) {
            super(name.replaceAll("/", ""), description, usageMessage, aliases);
        }

        @Override
        public boolean execute(CommandSender sender, String Commandlabel, String[] args) {
            //Do stuff
            return false;
        }

    }
     
    • Like Like x 1
  4. I have a modular plugin and want to allow other plugins to display other commands
     
  5. That's practicly the same as my suggestion :p