Solved Help with command aliases

Discussion in 'Spigot Plugin Development' started by Cerus, Sep 24, 2018.

  1. Hey,
    I know that I can set aliases in my plugin.yml, but is there a way to set them in the code? I know the method Command#setAliases, but it doesnt work for me. I dont get any errors.
    Code (Java):
    PluginCommand command = getCommand("teammanager");
            command.setAliases(storage.getFileManager().getSettings().get().getStringList("command-aliases"));
            command.setExecutor(new TeamManagerCommand(storage));
    The string list exists in my config.
    Any help?
     
  2. I'm not sure if I remember right but I think the alias must be set prior to registering your command otherwise you have to manually register it in the command map which can result in unexpected issues.
     
    • Informative Informative x 1
  3. But how can I set the aliases before registering?
     
  4. This is what Towny uses:
    https://www.spigotmc.org/threads/small-easy-register-command-without-plugin-yml.38036/

    Code (Text):

        private void registerSpecialCommands() {
            List<Command> commands = new ArrayList<>();
            commands.add(new AcceptCommand(TownySettings.getAcceptCommand()));
            commands.add(new DenyCommand(TownySettings.getDenyCommand()));
            commands.add(new ConfirmCommand(TownySettings.getConfirmCommand()));
            commands.add(new CancelCommand(TownySettings.getCancelCommand()));
            try {
                final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");

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

                commandMap.registerAll("towny", commands);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    You will need to make an object that extends BukkitCommand it should look similar to this, obviously the code in execute is merely an example.
    Code (Text):
    public class AcceptCommand extends BukkitCommand {
        public AcceptCommand(String name) {
            super(name);
            this.description = "Accept command for Towny";
            this.usageMessage = "/" + name + " <Town>";
        }

        @Override
        public boolean execute(CommandSender commandSender, String s, String[] strings) {
            if (commandSender instanceof Player) {
                InviteCommand.parseAccept((Player) commandSender, strings);
                return true;
            } else {
                return true;
            }
        }
    }
     
    • Useful Useful x 1
  5. Ah, so I have to use the command map. Alright, I'll take a look at it and close this thread when I come to a conclusion. Thank you all!
    Edit: Solved. :)
     
    #5 Cerus, Sep 24, 2018
    Last edited: Sep 24, 2018
    • Friendly Friendly x 1