use Switch Case instead of If-Else

Discussion in 'Plugin Development' started by PetchRex, May 22, 2021.

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

    PetchRex

    I am new here and i try to write my first plugin, but i need to know my code is hard or easy for devolop in the future.

    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    
            if (command.getName().equalsIgnoreCase("prp")) {
                switch (args.length) {
                    case 0:
                        ((Player) sender).sendMessage(ChatColor.WHITE + "try \"/prp help\"");
                        break;
                    case 1:
                        if (args[0].equalsIgnoreCase("rtp")) {
                            rptCommand((Player) sender);
                        } else if (args[0].equalsIgnoreCase("foo")) {
                            // next command
                        }
                        break;
                    case 2:
                        if (args[0].equalsIgnoreCase("rtp")) {
                            player = Main.getPlugin(Main.class).getServer().getPlayer(args[1]);
                            if (player != null) {
                                rptCommand(player);
                            } else {
                                ((Player) sender).sendMessage(ChatColor.RED + "Player " + args[1] + "not exist!");
                            }
                        } else if (args[0].equalsIgnoreCase("foo")) {
                            // next command
                        }
                        break;
                    default:
                        ((Player) sender).sendMessage(ChatColor.RED + "Please try again!");
                }
            }
        }
    
     
    Last edited: May 22, 2021
  2. Offline

    davidclue

    Why are you checking the name of the command? You should use an individual class for the command to keep it more organized and use
    Code:
    this.getCommand("commandName").setTabCompleter(new CommandClass(this));
    In your main class in your onEnable() method to also add a tab completer which is basically a must have for commands, and in your command class do
    Code:
    public CommandClass(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("commandName").setExecutor(this);
    }
    And make sure your command class implements CommandExecutor and TabCompleter, so for the tab completion you need to add this method
    Code:
    @Override
        public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
            final List<String> list = new ArrayList<>();
            if (!(sender.hasPermission("yourplugin.commandName"))) return list;
            if (args.length == 1) {
                final String[] completions = {"completetion1","anotherPossibleArgument","couldBeThisToo"};
                StringUtil.copyPartialMatches(args[0], Arrays.asList(completions), list);
            }
            Collections.sort(list);
            return list;
    }
    
    Also, make sure you are checking if the command sender is a player before you cast it so like this
    Code:
    if (!(sender instanceof Player)) {
        sender.sendMessage("You must be a player to use this!");
        return true;
    }
    It really doesn't matter if you use a switch-case statement or if-else statements although if-else statements are a little bit nicer to use and easier to read.
     
  3. Offline

    PetchRex

    @davidclue thanks, but how tabComplete work ? i know the name tell it self, after onTabComplete how to call onCommand method ? (in my opinion you replace onTabComplete instead of onCommand then call other command method by condition, right ?).
     
  4. Offline

    davidclue

    @PetchRex Just implement both CommandExecutor and TabCompleter in the same class and have both methods.
     
  5. Offline

    Strahan

    You could also just implement TabExecutor by itself.
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page