Plugin command required namespace to run.

Discussion in 'Plugin Development' started by sparkinex, Aug 14, 2019.

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

    sparkinex

    Hi I am new to plugin development and am working on my first plugin. I want to have one command which is the main command and it will have sub commands. I also want to create all my commands dynamically. My problem is I have a command /igvote with the aliases /v and /vote. /v and /vote work perfectly however /igvote doesn't run unless you run /igvote:igvote how can I go about fixing this? I tried adding igvote to the aliases list which didn't work.

    Main Plugin File:
    Code:
    public class IGVoting extends JavaPlugin {
       
        @Override
        public void onEnable() {
            registerCommands();
        }
       
    
        @Override
        public void onDisable() {
    
        }
    
        private void registerCommands() {
           
            CommandHandler handler = new CommandHandler();
            handler.register("help", new Help());   
           
            register("igvote", handler);
        }
       
        private void register(String fallback, BukkitCommand command) {
            try {
                Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
                bukkitCommandMap.setAccessible(true);
                CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
                commandMap.register(fallback, command);
            } catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException | SecurityException e) {
                e.printStackTrace();
            }
        }
    }
    
    CommandHandler:
    Code:
    public class CommandHandler extends BukkitCommand {
       
        private static HashMap<String, CommandInterface> commands = new HashMap<String, CommandInterface>();
       
        public CommandHandler() {
            super("igvote");
            this.setAliases(Arrays.asList("v", "vote"));
            this.setDescription("Main command for InGameVoting.");
            this.setUsage("Type /igvote help for information on commands.");
            this.setPermission("igvote.use");
        }
    
        @Override
        public boolean execute(CommandSender sender, String label, String[] args) {
            if (sender instanceof Player) {
                if (args.length == 0) {
                    sender.sendMessage("Type /igvote help for information on commands.");
                    return true;
                }
               
                if (args.length > 0) {
                    if (exists(args[0].toLowerCase())) {
                        this.getExecutor(args[0].toLowerCase()).execute(sender, label, args);
                        return true;
                    } else {
                        sender.sendMessage("This command doesn't exist!");
                        return true;
                    }
                }
            } else {
                sender.sendMessage(ChatColor.RED + "You must be a player to use this command.");
                return true;
            }
           
            return false;
        }
       
        public void register(String name, CommandInterface cmd) {
            commands.put(name.toLowerCase(), cmd);
        }
       
        public boolean exists(String name) {
            return commands.containsKey(name);
        }
       
        public CommandInterface getExecutor(String name) {
            return commands.get(name);
        }
    
    }
     
    Last edited by a moderator: Aug 18, 2019
Thread Status:
Not open for further replies.

Share This Page