Solved Why ?

Discussion in 'Plugin Development' started by Super10Gamer, Jan 31, 2016.

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

    Super10Gamer

    Hello , i don't can't use this: (EXEMPLE)
    >/ban Super10Mine_xD Hacking (KILL-AURA)
    Only:
    >/ban Super10Mine_xD Hacking

    My code:
    Code:
            if (command.getName().equalsIgnoreCase("ban")) {
                if (!sender.hasPermission("swordgames.ban")) {
                    sender.sendMessage("§cSem Permissão!");
                } else {
                    if (args.length == 0) {
                        sender.sendMessage("§cUse: /ban {PLAYER} {REASON}");
                    } else {
                        if (args.length == 1) {
                            sender.sendMessage("§cUse: /ban {PLAYER} {REASON}");
                        } else {
                            if (args.length == 2) {
                                Player target = Bukkit.getServer().getPlayer(args[0]);
                                if (target == null) {
                                    sender.sendMessage(ChatColor.RED + args[0] + " não esta Online");
                                    return true;
    
                                } else {
                                    String reason = String.valueOf(args[1]);
                                    target.setBanned(true);
                                    target.kickPlayer(reason);
                                    Bukkit.broadcastMessage(
                                            "§bO Jogador §c" + target.getName() + " §bFoi Banido! Rasão: §c" + reason);
                                    Bukkit.broadcastMessage("§bPor §c" + sender.getName());
                                }
                            }
                        }
                    }
                }
            }
     
  2. Offline

    I Al Istannen

    @Super10Gamer
    String reason can just be args[1], as that is already a String. But to your problem: Do you know what an array is?
    If not, look it up!
    That knowledge and maybe the StringBuilder should give you enough information.
     
  3. Offline

    Super10Gamer

    Dosen't Worked ;-;


    Code:
            if (command.getName().equalsIgnoreCase("ban")) {
                if (!sender.hasPermission("swordgames.ban")) {
                    sender.sendMessage("§cSem Permissão!");
                } else {
                    if (args.length == 0) {
                        sender.sendMessage("§cUse: /ban {PLAYER} {REASON}");
                    } else {
                        if (args.length == 1) {
                            sender.sendMessage("§cUse: /ban {PLAYER} {REASON}");
                        } else {
                            if (args.length == 2) {
                                Player target = Bukkit.getServer().getPlayer(args[0]);
                                if (target == null) {
                                    sender.sendMessage(ChatColor.RED + args[0] + " não esta Online");
                                    return true;
    
                                } else {
                                    StringBuilder sb = new StringBuilder();
                                    for(int i=1; i<args.length; i++){
                                        sb.append(args[1]).append(" ");
                                    }
                                    String reason = sb.toString();
                                    target.setBanned(true);
                                    target.kickPlayer(reason);
                                    Bukkit.broadcastMessage(
                                            "§bO Jogador §c" + target.getName() + " §bFoi Banido! Rasão: §c" + reason);
                                    Bukkit.broadcastMessage("§bPor §c" + sender.getName());
                                }
                            }
                        }
                    }
                }
            }
     
    Last edited: Jan 31, 2016
  4. Offline

    JoaoBM

    @Super10Gamer
    1. There's already a command called /ban . Since you can't override it, change the command's name
    2. Don't use } else { if ( ) { . Use } else if ( ) {
    3. Use a StringBuilder for the Ban Reason, like @I Al Istannen said
     
  5. Offline

    MOMOTHEREAL

    @Super10Gamer
    Just to clarify what @I Al Istannen just said, this is what you should be doing:

    You need to this that the variable "args" is an array of Strings, created by splitting the command you executed.
    For example, if you enter "/ban Momo Hello", the array would be composed as such:
    - Index #0 = Momo
    - Index #1 = Hello

    However, if you command contains more that 2 arguments, you need to join the arguments together, starting with index #1. For example, entering "/ban Momo You're not cool enough" would look like this:
    - Index #0 = Momo
    - Index #1 = You're
    - Index #2 = not
    - Index #3 = cool
    - Index #4 = enough

    In this case, you would have to loop through all the arguments, starting from 1 until the last index, using a simple for-loop:
    Code:
    StringBuilder sb = new StringBuilder("");
    for (int i = 1; i < args.length; i++) {
        sb.append(args[i]).append(" ");
    }
    sb.deleteCharAt(sb.lastIndexOf(" "));
    String reason = sb.toString();
    Also note that you will need to change your check from:
    Code:
    if (args.length == 2) {
    to:
    Code:
    if (args.length > 1) {
     
  6. Offline

    Super10Gamer

    Thanks It Worked
     
Thread Status:
Not open for further replies.

Share This Page