1.16.5 Edit command aliases of other plugins

Discussion in 'Spigot Plugin Development' started by Short, Jun 1, 2021.

  1. I would like to be able to add and remove command aliases of commands in other plugins, using a plugin. At the moment, I edit the plugin.yml file of other plugins to do this, but having to do this every time a plugin updates is a hassle. Hence, I would like a more automated way of doing it.

    Any help would be greatly appreciated.

  2. May I ask why? It seems pointless and like it would just make lots of incompatibilities.
  3. Unzip the jar file, go to plugin.yml and add aliases
  4. Read what he has written, he says he already uses that but it does not suit him
  5. This guy always gives answers like this, he just read the Premium Resource guidelines and tries to raise his post count now
    • Agree Agree x 3
    • Funny Funny x 1
  6. Well, i could write a script that automates the rewrite of the plugin.yml xD. It's unzipping and zipping, just without doingt it manually. When I understood your question correctly you are fine with this solusion, but just to get it automated, am I right?
  7. You could create a plugin which stores the aliases and tests it in the AsyncPlayerChatEvent. If the alias gets called, you call the real command with
    Code (Text):
    Bukkit.getServer().dispatchCommand(player, "someCommand");
    • Creative Creative x 1
  8. Quite nice, but then the players wouldnt have the command completetion. I'm not sure, but i think you are able to register aliases at runtime, or am i wrong?
  9. You could implement for that part the TabCompleter.

    Maybe its possible on a better way.
  10. Quite simply, a couple of plugins I use have aliases that I'm not a fan of, or don't have aliases that I would like them to have.
    Are these capable of adding and removing aliases from existing commands?
  11. You could write a little plugin that fixes your aliases by calling
    Code (Java):
    Objects.requireNonNull(Bukkit.getPluginCommand("baseCommand")).setAliases(Arrays.asList("alias1", "alias2"));      
    on each command you want to change
    • Like Like x 1
  12. If this works, it is exactly what I need. I shall try it later. Thank you!

    EDIT: So I've just tried it, any it doesn't seem to be doing anything. This is my code:
    Code (Text):

    public void onServerLoad(ServerLoadEvent event)
        SimpleCommandMap map = null;
            Server server = Bukkit.getServer();
            Field field = server.getClass().getDeclaredField("commandMap");
            map = (SimpleCommandMap) field.get(server);
        catch (Exception exception)
        if (map != null)
            for (Command cmd : map.getCommands())
                if (cmd.getName().equals("plots"))
                    cmd.setAliases(Arrays.asList("plot", "cell"));
    And yes, the if statement is returning true.
    #13 Short, Jun 2, 2021
    Last edited: Jun 2, 2021
  13. why don't you do it upon your plugin load? Make your plugin depend on the plugin you are trying to edit, then in the onLoad() method of your plugin, call Objects.requireNonNull(Bukkit.getPluginCommand("plots")).setAliases(Arrays.asList("plot", "cell"));
  14. What difference would it make when I call the method?
  15. you would ensure loading order
  16. I just checked the source code and it appears to be so that aliases can no longer be updated once the command is registered. You can *try* changing the `private List<String> activeAliases;` field in the Command class through reflection (not guaranteed that it will work though)

    Or try to see if you can intercept the registration
  17. So I'm trying the reflection method, but I'm getting a NoSuchFieldException. I've even checked Spigot's source code and the field is 100% there. Here's my code:
    Any ideas?
  18. A PluginCommand is of type PluginCommand which extends Command. The field you are looking for is in the Command class so you will need to change the line to:
    Field field = cmd.getClass().getSuperclass().getDeclaredField("activeAliases");
    (insert getSuperClass())
    • Informative Informative x 1
  19. Thanks man, that fixed the problem, but unfortunately changing that field didn't seem to change the registered aliases. :(

    I guess I'll have to intercept the registration like you suggested, but I have no idea where to begin with that.