Solved One normal arg, multiple special args.

Discussion in 'Plugin Development' started by Anono, Jul 4, 2017.

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

    Anono

    Alright, I'm making a simple warnings system, with a target player and reason.

    I can't figure out to how to get the target player, after of which all later arguments should be treated as a reason.

    My code:
    Code:
            if(cmd.getName().equalsIgnoreCase("warn")) {
                    Player player = (Player) sender;
                    Player targetPlayer = Bukkit.getPlayer(args[0]);
                    if(targetPlayer != null) {
                    String String = "";
                    for (int i = 0; i < args[1].length; i++) {
                     String arg = args[i] + " ";
                     String = String + arg;
                    }
                    if (myString.length() > 1) {
                    targetPlayer.sendMessage(ChatColor.DARK_RED + "- You have been warned by a staff member.");
                    targetPlayer.sendMessage(ChatColor.RED + "- Reason: " + String + ".");
                    player.sendMessage(ChatColor.GREEN + " Support request sent.");
                    return true;
                    } else {
                        player.sendMessage(ChatColor.RED + "Sorry, player not found.");
                    }
                    } else {
                    player.sendMessage("Enter a warn reason.");
                    } 
     
  2. Offline

    Sethburster

    I would suggest that instead of randomly getting arguments from the command. you should have an if statement at the beginning checking if the length of the args is 1. You then do something with that player who is the only argument. Otherwise, you check if the args is greater than 1. Then you still get the player as the first arg. And then concatenate the rest of the args to your reason string which should not be named String.

    TLDR: What you need to do is clean up your code and format it into an efficient system. You need to have checks for the length of args otherwise your code will be throwing IndeOutofBondsException's left and right.Also, where is myString declared in the code you posted? Add all the code to your question.
     
  3. Offline

    Anono

    I simply renamed myString to string in that example. Thanks for the assistance.

    I don't really get what you mean. My current code is the following:
    Code:
     
                if(cmd.getName().equalsIgnoreCase("warn")) {
                    if (args.length == 1) {
                        Player player = (Player) sender;
                        Player targetPlayer = Bukkit.getPlayer(args[0]);
                        if(targetPlayer == null) {
                            sender.sendMessage(ChatColor.RED + "Sorry, Player not found.");
                            return true;
                        } else {
                            targetPlayer.sendMessage(ChatColor.DARK_RED + "- You have been warned by a staff member.");
                            targetPlayer.sendMessage(ChatColor.RED + "- Reason: Breaking the rules.");
                            Bukkit.broadcast(ChatColor.GRAY + targetPlayer.getName() + " Has been warned by a staff member. Follow the rules!", "core.warn.see");
                            sender.sendMessage(ChatColor.GREEN + "Player warned successfully.");
                        }
                    } else {
                    Player player = (Player) sender;
                    Player targetPlayer = Bukkit.getPlayer(args[0]);
                    if(targetPlayer == null) {
                        sender.sendMessage(ChatColor.RED + "Sorry, Player not found.");
                    } else {
                   String myString = "";
                   for (int i = 0; i < args[1].length; i++) {
                     String arg = args[i] + " ";
                     myString = myString + arg;
                   }
                   if (myString.length() > 1) {
                    targetPlayer.sendMessage(ChatColor.DARK_RED + "- You have been warned by a staff member.");
                    targetPlayer.sendMessage(ChatColor.RED + "- Reason: ");
                    player.sendMessage(ChatColor.GREEN + " Support request sent.");
                    return true;
                   } else {
                      player.sendMessage(ChatColor.RED + "Sorry, player not found.");
                   }
                   } else {
                    player.sendMessage("Enter a warn reason.");
                   }
                }
            }
                
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 5, 2017
  4. Offline

    A5H73Y

    I've tidied your code up for you and applied the DRY principle.
    Try to avoid spaghetti code as it's ugly and hard to read.
    Btw I haven't tested this code, but I'm sure you can finish it.

    Code:
    if (args.length < 1) {
    
                                player.sendMessage("Please specify a player");
                                return false;
    
                            } else if (args.length < 2) {
                                player.sendMessage("Please specify a reason");
                                return false;
                            }
    
                            Player targetPlayer = Bukkit.getPlayer(args[0]);
    
                            if(targetPlayer == null) {
                                sender.sendMessage(ChatColor.RED + "Sorry, Player not found.");
                                return false;
                            }
    
                            StringBuffer sb = new StringBuffer();
    
                            for (int i=1; i < args.length; i++) {
                                sb.append(args[i] + " ");
                            }
                          
                            String warningMessage = sb.toString();
    
                            targetPlayer.sendMessage(ChatColor.DARK_RED + "- You have been warned by a staff member.");
                            targetPlayer.sendMessage(ChatColor.RED + "- Reason: " + warningMessage);
                            player.sendMessage(ChatColor.GREEN + " Support request sent.");
                            return true;
    
    Wouldn't consider this spoonfeeding as the code was there, it just needed tidying up.
    Please ask if you want want any explanation.
     
    Anono likes this.
  5. Offline

    Anono

    Thanks, that works perfectly. Marking as solved.
     
  6. Offline

    Horsey

    @A5H73Y it would be considered spoon-feeding, because rather than teaching what's wrong with the code and how to fix it, you're just giving code for the OP to just copy-paste.
     
Thread Status:
Not open for further replies.

Share This Page