[Solved] Help with Command Handler System Please

Discussion in 'Plugin Development' started by TruDan, Jul 28, 2012.

Thread Status:
Not open for further replies.
  1. Hello, i have started a command handler system, which i will release on here for other dev's to use also. I am basing it on how sk89q handles his commands, with a simple Annotation of @Command.
    I have created an interface for this:
    PHP:
    public @interface Command {
        
    String commandName();
        
    String commandArg();
        
    String commandPermission();
        
    String help() default "";
        
    int minArgs() default 0;
        
    int maxArgs() default -1;
    }
    However, I am not "well experienced" in java, and after looking through WorldEdit source. im struggling to work out how to make this as simple as possible. So is anyone willing to help out? i want to be able to make it work similar to the Bukkit Events system, where you register a class, and it uses the annotations to work whether the player has permission, check arguments etc etc.
     
  2. Offline

    MonsieurApple

    We wanted to use sk89q's command framework in our plugins as well, without the extra dependency of WorldEdit. We factored out his code into a public repository: https://github.com/ProjectAres/sk89q-command-framework

    Here's a sample of how it would work:
    Code:
    private void setupCommands() {
        this.commands = new CommandsManager<CommandSender>() {
            @Override public boolean hasPermission(CommandSender sender, String perm) {
                return sender.hasPermission(perm);
            }
        };
    
        CommandsManagerRegistration cmdRegister = new CommandsManagerRegistration(this, this.commands);
        cmdRegister.register(MapCommands.class);
        cmdRegister.register(AdminCommands.class);
    }
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        try {
            this.commands.execute(cmd.getName(), args, sender, sender);
        } catch (CommandPermissionsException e) {
            sender.sendMessage(ChatColor.RED + "You don't have permission.");
        } catch (MissingNestedCommandException e) {
            sender.sendMessage(ChatColor.RED + e.getUsage());
        } catch (CommandUsageException e) {
            sender.sendMessage(ChatColor.RED + e.getMessage());
            sender.sendMessage(ChatColor.RED + e.getUsage());
        } catch (WrappedCommandException e) {
            if (e.getCause() instanceof NumberFormatException) {
                sender.sendMessage(ChatColor.RED + "Number expected, string received instead.");
            } else {
                sender.sendMessage(ChatColor.RED + "An error has occurred. See console.");
                e.printStackTrace();
            }
        } catch (CommandException e) {
            sender.sendMessage(ChatColor.RED + e.getMessage());
        }
    
        return true;
    }
    
     
    SupaHam likes this.
  3. I love you <3 (no homo) :D
    Thank you very much :)

    Edit:

    I'm not sure if this is a stupid question. But, how do i get the player instances and stuff? Do they get passed through? I have this so far:
    PHP:
    import com.sk89q.minecraft.util.commands.Command;
    import com.sk89q.minecraft.util.commands.CommandPermissions;
     
    public class 
    GeneralCommands {
     
        @
    Command(aliases = { "jp select""jp sel" }, desc "Toggle JumpPorts region selection mode."max 1)
        @
    CommandPermissions("jumpports.admin.select")
        public 
    void select(){
       
        }
     
    }
    And eclipse has not given me any warnings about missing arguments etc, so im not sure how to get the player instance without having to manually pass it around from the main method

    (I am aware that the aliases may not work, but i intend to test it :p)
     
  4. Offline

    MonsieurApple

    An excerpt from our AdminCommands class
    Code:
    public class AdminCommands {
        @Command(
            aliases = {"restart"},
            desc = "Queues a server restart after a certain amount of time",
            usage = "[seconds] - defaults to 30 seconds",
            flags = "f",
            min = 0,
            max = 1
        )
        @CommandPermissions("server.restart")
        public static void restart(CommandContext args, CommandSender sender) throws CommandException {
        }
    }
    
     
    SupaHam likes this.
  5. Thank you! :D
     
Thread Status:
Not open for further replies.

Share This Page