Solved Properly register commands in code 1.15.2

Discussion in 'Spigot Plugin Development' started by Webmets, Feb 24, 2020.

  1. I'm trying to register commands in code, with the following setup
    Code (Text):

    try {
       CommandProcessor processor = new CommandProcessor(new CommandExecutor(commandClass));
       PluginCommand pluginCommand = manager.pluginCommandConstructor.newInstance(command, manager.plugin);

       pluginCommand.setUsage(usage);
       pluginCommand.setExecutor(processor);
       pluginCommand.setPermission(permission);
       pluginCommand.setDescription(description);
       pluginCommand.setPermissionMessage(permissionMessage);
       pluginCommand.register(manager.map);
       manager.commandClasses.add(commandClass.getClass());

       manager.map.register(command, "minecore", pluginCommand);
    } catch (UnknownExpressionException | InvocationTargetException | InstantiationException | IllegalAccessException e ) {
       e.printStackTrace();
    }

    private CommandMap map;
    private Constructor<PluginCommand> pluginCommandConstructor;

    try {
       pluginCommandConstructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
       pluginCommandConstructor.setAccessible(true);

       Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
       bukkitCommandMap.setAccessible(true);

       map = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
    } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) {
       plugin.getLogger().severe("Unable to load command map!");
       e.printStackTrace();

       instance = null;
    }
     
    This works, and commands are registered and functioning but they don't show up in the command suggestions, as you can see here
    [​IMG]
    Are there any steps I'm missing with the registration that causes this problem?
    Any help with this would be appreciated


    UPDATE
    This does work fine, it was causing issues because commands were registered later on, and not in the onEnable method
     
    #1 Webmets, Feb 24, 2020
    Last edited: Feb 24, 2020
  2. Do you have the permission to run the command or are you just op?
     
  3. I'd suggest using Mojang's Brigadier library, or Lucko's utility lib for Brigadier, Commodore. Either should simplify relying on Bukkit alone by a great deal.
     
  4. was just op, though on my live server where I did have perms it also didn't work

    Thanks for the tip, though this was what I was asked to make /shrug

    I found that this code does work fine, but only if registered in the onEnable, and not if in a scheduler or later on which is rather odd
     
    • Informative Informative x 1