How to create multiple subcommands using one main command

Discussion in 'Spigot Plugin Development' started by StuffBoy, May 30, 2017.

  1. Hey guys, today I have hit a bit of a wall. I am trying to create mutiple subcommands for one main command. Here's an example of what I am trying to do, "/f create <name>, /f invite <player>, etc." If anyone has any suggestions or examples they would like to explain or show to me please feel free to comment down below. I have heard of "super" classes but have not had much luck. Thanks in advanced! :)
     
  2. Check if the length is 2 in this case with these subcommands, and for one check if args[0] is create or invite, and you know args[1] will be the player
     
  3. I do that but the subcommand, invite, registers but doesn't send the message.
     
  4. post code
     
  5. Code (Text):
    package factions;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import hcf.Main;
    import utils.Utils;

    public class Faction implements CommandExecutor {

        Main plugin;
        Manager link = new Manager();

        public Faction(Main instance) {
            plugin = instance;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("f")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("You cannot run this from console.");
                    return true;
                }
                Player p = (Player) sender;
                if (args.length == 0) {
                    Utils.sendMessage(p, "&a/f create: Create a faction.");
                    Utils.sendMessage(p, "&a/f invite: Invite a player to your faction.");
                    return true;
                }
                if (args[0].equalsIgnoreCase("create")) {
                    if (args.length == 1) {
                        Utils.sendMessage(p, "&c/f create <faction>");
                        return true;
                    }
                    if (args.length >= 2) {
                        StringBuilder name = new StringBuilder();
                        for (int i = 1; i < args.length; i++) {
                            name.append(args[i]);
                            if (i < args.length) {
                                name.append(" ");
                            }
                            if (name.length() <= 2) {
                                Utils.sendMessage(p, "&cFaction names have a minimum of 3 characters.");
                                return true;
                            }
                            if (name.length() >= 17) {
                                Utils.sendMessage(p, "&cFaction names have a maximum of 16 characters.");
                                return true;
                            }
                            if (name.length() >= 3 && name.length() <= 16) {
                                Utils.sendMessage(p, "This works.");
                                @SuppressWarnings({ "unused", "unchecked" })
                                List<String> captains = (List<String>) plugin.getConfig().getList("Captains");
                                ArrayList<String> memberlist = new ArrayList<String>();
                                plugin.getConfig().getList("Members");
                                String pn = p.getDisplayName();
                                memberlist.add(pn);
                                link.setup(name);
                                Utils.sendMessage(p, "&aThe faction " + name.toString().trim() + " has been created by "
                                        + p.getDisplayName());
                                link.getData().set("Name", name.toString().trim());
                                link.getData().set("Leader", p.getDisplayName());
                                link.getData().set("Members", Arrays.toString(memberlist.toArray()).trim());
                                link.getData().set("Balance", Integer.valueOf(0));
                                link.getData().set("DTR", Double.valueOf(1.1));
                                link.saveConfig();
                            }
                        }
                        if (args[0].equalsIgnoreCase("invite")) {
                            if (args.length == 1) {
                                Utils.sendMessage(p, "&c/f invite <player>");
                                return true;
                            }
                            if (args.length == 2) {
                                String leader = plugin.getConfig().getString("Leader");
                                Player target = Bukkit.getPlayer(args[1]);
                                if (!leader.equalsIgnoreCase(p.getDisplayName())) {
                                    Utils.sendMessage(p, "Only the leader can invite players to a faction.");
                                }
                                if (leader.equalsIgnoreCase(p.getDisplayName())) {
                                    link.getData().set("Invited", target.getDisplayName());
                                }
                            }
                        }
                    }
                }
            }
            return true;
        }
    }
     
  6. electronicboy

    IRC Staff

    your code is nested improperly, you're checking if args[0] is equal to create, and in the same if block, you're checking if args[0] is equal to invite. They all need to be on the same level, I also suggest using a switch statement and creating individual methods for each command to help cleanup code (and make it practically impossible to make the same mistake)
     
  7. Thank you electronicboy I will certainly try that.
     
  8. Thank you, the invite finally works! :)