Solved Registering and unregistering commands? (CommandExecutor)

Discussion in 'Plugin Development' started by FisheyLP, May 1, 2015.

Thread Status:
Not open for further replies.
  1. I'm currently working on a All-In-One Plugin.
    It combines many of my plugins into one and each module (plugin functions) can be enabled/disabled.
    This is my code in the abstract Module class to register and unregister the commands:
    private Object getPrivateField(Object object, String field) throws SecurityException,
        NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Class<?> clazz = object.getClass();
        Field objectField = clazz.getDeclaredField(field);
        return objectField.get(object);
        public void registerCommand(String cmd) {
        public void unRegisterCommand(String cmd) {
        try {
            PluginCommand command = (PluginCommand) m.getCommand(cmd);
            Object result = getPrivateField(Bukkit.getPluginManager(), "commandMap");
            CommandMap commandMap = (ommandMap) result;
            HashMap<String, Command> knownCommands = (HashMap<String, Command>)
                    getPrivateField(commandMap, "knownCommands");
            for (String alias : command.getAliases()){
               if(knownCommands.containsKey(alias) &&
        } catch (Exception e) {
    (m is an instance of the Main class)
    The code works for registering commands, and too for unregistering commands. But once its unregistered, I cannot register it again :(
    Any idea how I can make this?

    remove the executor, but the command (and aliases) are still there...
    Last edited: May 1, 2015
  2. Offline



    What you are trying to do? :3 make your plugin able to register/unregister commands in runtime ? register commands without plugin.yml?
    in the first case:
    you can simply have a boolean in each command executor that when false will make your command executor do nuthing.
    oncommand: if !boolean: return.
    else: function

    if it is the second case: :- )
    FisheyLP likes this.
  3. @mine-care Thanks! I will look into this

    I've done it almost the same way as unregistering the commands with the CommandMap and the knownCommands HashMap using Reflections:
    1. public void registerCommand(String cmd) {
    2. try {
    3. for(Command command : PluginCommandYamlParser.parse(m)) {
    4. if(command.getName().equalsIgnoreCase(cmd)) {
    5. CommandMap map = (CommandMap) getPrivateField(Bukkit.getPluginManager(),
    6. "commandMap");
    7. HashMap<String, Command> knownCommands = (HashMap<String, Command>)
    8. getPrivateField(map, "knownCommands");
    9. knownCommands.put(command.getName(), command);
    10. for(String alias : command.getAliases()) {
    11. knownCommands.put(alias, command);
    12. }
    13. ((PluginCommand) command).setExecutor(this);
    14. }
    15. }
    16. } catch(Exception e) {
    17. Bukkit.getConsoleSender().sendMessage(l+"§4The command §c"+cmd+" §4couldn't be found in §cplugin.yml§4l!");
    18. }
    19. }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 12, 2016
    mine-care likes this.
Thread Status:
Not open for further replies.

Share This Page