Solved Having trouble with command argument states

Discussion in 'Plugin Development' started by htczed, Oct 2, 2020.

  1. Offline

    htczed

    So what I am basically trying to do is have a /kick command with two arguments as such "/kick <player> <reason>
    This is easy enough to create, I had no trouble to check if the player you are trying to kick exists and returning a message and stopping the code at that point if the player doesn't exist that's all fine and dandy, as well as permissions checks.
    The thing I want to do is to check if the second argument "<reason>" exists so for example if I were to do "/kick Notch" only with the player argument and not the reason argument it would return a message like "please enter a reason". I have tried everything to check if the argument is empty tried using Stringutils and such but have not been able to make it work, here is the command below.
    Code:
    if(cmd.getName().equalsIgnoreCase("kick")) {
                if(!(sender instanceof Player)) {
                    sender.sendMessage(methods.noConsoleMsg());
                    return true;
                }
                Player player = (Player) sender;
                if(!player.hasPermission("hello.kick.admin")) {
                    player.sendMessage(methods.noPermMsg());
                    return true;
                }
                if(args.length == 0) {
                    player.sendMessage(ChatColor.RED + "Command usage: /kick <playername> <reason>");
                    return true;
                }
                if(!(Bukkit.getPlayer(args[0].toString()) == null)) {
                    Player target = Bukkit.getPlayer(args[0].toString());
                    if((args.length == 0) && (args[1].isEmpty() == true)) {
                    target.kickPlayer(args[1].toString());
                    player.sendMessage(ChatColor.GREEN + "You have kicked player: " + ChatColor.GOLD + target.getDisplayName() + ChatColor.GREEN + " for " + ChatColor.RED + args[1].toString());
                    return true;
                    }
                    else {
                        player.sendMessage("no reason input");
                        return true;
                    }
                }
                else {
                    player.sendMessage(ChatColor.RED + "Player not found on the server! Use /kick <playername> <reason>");
                    return true;
                }
            }
    I've solved it if anyone's interested here's the code
    Code:
    if(cmd.getName().equalsIgnoreCase("kick")) {
                if(!(sender instanceof Player)) {
                    sender.sendMessage(methods.noConsoleMsg());
                    return true;
                }
                Player player = (Player) sender;
                if(!player.hasPermission("hello.kick.admin")) {
                    player.sendMessage(methods.noPermMsg());
                    return true;
                }
                if(args.length == 0) {
                    player.sendMessage(ChatColor.RED + "Command usage: /kick <playername> <reason>");
                    return true;
                }
                else if (args.length == 1 ) {
                    player.sendMessage(ChatColor.RED + "Command usage: /kick <playername> <reason>");
                }
                else {
                if(!(Bukkit.getPlayer(args[0].toString()) == null)) {
                    Player target = Bukkit.getPlayer(args[0].toString());
                    if(target == player) {
                        player.sendMessage(ChatColor.RED + "You can't kick yourself");
                        return true;
                    }
                    target.kickPlayer(args[1].toString());
                    player.sendMessage(ChatColor.GREEN + "You have kicked player: " + ChatColor.GOLD + target.getDisplayName() + ChatColor.GREEN + " for " + ChatColor.RED + args[1].toString());
                    return true;
                    }
                else {
                    player.sendMessage(ChatColor.RED + "Player not found on the server!");
                    return true;
                }
                }
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 2, 2020
  2. Offline

    KarimAKL

    @htczed Please mark the thread as solved.

    Btw, for anyone wondering about this, you need to check the length of the arguments array using array#length.
     

Share This Page