Solved Command Args in separate class

Discussion in 'Plugin Development' started by shades161, May 31, 2015.

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

    shades161

    Hi, I'm trying to make a plugin that has /report as the base command, with several different arguments to do different things.
    I know how to handle arguments and such, but because it will become a big mess, I was wondering how to make it so I have the basecommand in one class, and then depending on what the argument was, call a method from another class to do something. I have tried making a static class and calling it (like I have seen on other posts here) but it doesn't seem to work.
    I would also like to know, if possible, that the method from the other class could send a message to the command executor.
    For example, if a player types "/report help" I want the command to list the commands, but call a method from another class. Or if a player types /report (player_name) it will call a method from another class, write something to a yml, and then message that player that the action is complete.
     
  2. Offline

    I Al Istannen

    @shades161 I have this in one of my plugins:
    Code:
    switch(args[1].toLowerCase(Locale.ENGLISH)) {
         case "join": {    
           joinTeam(args, player, arenaName);
           break;
         }
    }
    
    private void joinTeam(String[] args, Player player, String arenaName) {
      String[] argsToPass = new String[args.length - 2];
      for(int i = 0; i < argsToPass.length;i++) {
        argsToPass[i] = args[i+2];
      }
      JoinTeam.join(player, argsToPass, arenaName);
    }
    
    I have a Switch statement switching on the args[1] in my CommandExecutor class. If the args[1] match "join", i call the method "joinTeam". From there, I simply call the static method of the "JoinTeam" class. Because the method gets a player, it can send messages easily. You could also change the return type to a String and react in your CommandExecutor class dependig on the return value.

    This is easily expendable.
     
  3. Offline

    Zombie_Striker

    @shades161
    You should try to keep everything in one class. Having multiple classes can be messy and inefficient if you do not really know how to work with them. You can try a CommandExecutor for a specific command, but since there will only be one command it will be the same problem.

    I assume this will be a private plugin, meaning no one will look at your code. I would suggest sticking to what you know, even if it will be a a huge method. It's one thing to have it look good, another to have it work properly and efficiently.
     
  4. Offline

    shades161

    Thank you, I figured it out.
    I have this in my base command class
    Code:
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("report")) {
                if (args.length < 1) {
                    sender.sendMessage(ChatColor.RED + "Please use: " + ChatColor.GREEN + "/report help " + ChatColor.RED + "for a list of commands.");
                }
                else {
                    if (args.length == 1) {
                        String argument = args[0];
                        if (argument.equalsIgnoreCase("help")) {
                            sender.sendMessage(ChatColor.GOLD + "-~-~-" +ChatColor.GOLD + " PlayerReporter Help " + ChatColor.GOLD + "-~-~-");
                            HelpCommand.helpList(args[0], sender);
                        }
                    }              
                }
            }
        return false;
        }
    And this in the HelpCommand class
    Code:
        protected static void helpList(String args, CommandSender sender) {
            sender.sendMessage("Test");
        }
    }

    @Zombie_Striker See, i would keep everything in the same class, the issue is that there are a lot of different arguments for the command and it would be much easier for me to have it separated (up to a point) so that it will be clean and it allows me to organize it and change code when needed much easier.
     
Thread Status:
Not open for further replies.

Share This Page