Solved Reloading bukkit's command map?

Discussion in 'Plugin Development' started by IkeVoodoo, Jan 10, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    IkeVoodoo

    Hello, as the title says i need to fin a way to reload bukkit's command map (or anything similar, the reason to the problem is described below)

    Reason:

    I am trying to register commands at run time, so that i can put in a config file a command that sends a message (example), and then load it it, now i found this (https://bukkit.org/threads/tutorial-registering-commands-at-runtime.158461/) thread, wich showed how to register commands at runtime, i tested it and it works, no errors or anything, just when i try to run the command /test it says "Unknown command. Type "/help" for help.", i assume the command map needs to be reloaded, if it is not the right way, ill gladly accept anything said in regards to this, thank you :D

    EDIT:

    found this post (https://bukkit.org/threads/register-commands-without-plugin-yml.349373/) that explained it, tested it and it works! i will mark this as solved
     
    Last edited: Jan 11, 2021
  2. Offline

    marcelo.mb

    If you post your code we can have a look at it and try to help you out of your issue.
     
  3. Offline

    IkeVoodoo

    It is the same as the one in the link (added permission and executor), however here it is:

    Code:
        public static void registerCommand(CommandExecutor executor, String permission, String... aliases) {
            PluginCommand command = getCommand(aliases[0], Main.instance);
            command.setAliases(Arrays.asList(aliases));
            command.setExecutor(executor);
            if(permission != null && !permission.isBlank()) command.setPermission(permission);
            getCommandMap().register(Main.instance.getDescription().getName(), command);
        }
       
        private static PluginCommand getCommand(String name, Plugin plugin) {
            PluginCommand command = null;
         
            try {
                Constructor<PluginCommand> c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
                c.setAccessible(true);
         
                command = c.newInstance(name, plugin);
            } catch (Exception e) {
                e.printStackTrace();
            }
         
            return command;
        }
         
        private static CommandMap getCommandMap() {
            CommandMap commandMap = null;
         
            try {
                if (Bukkit.getPluginManager() instanceof SimplePluginManager) {
                    Field f = SimplePluginManager.class.getDeclaredField("commandMap");
                    f.setAccessible(true);
         
                    commandMap = (CommandMap) f.get(Bukkit.getPluginManager());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
         
            return commandMap;
        }
    
    
     
Thread Status:
Not open for further replies.

Share This Page