Solved Checking if arg [] is set

Discussion in 'Plugin Development' started by Omer H., Aug 1, 2017.

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

    Omer H.

    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            int length = args.length;
            Player player = (Player) sender;
    
            if (command.getName().equalsIgnoreCase("paintball")) {
                if (player.getName().equals("YoItsOmer")) {
                    if (length == 4) {
                        if (args[0].equalsIgnoreCase("gun")) {
                            if (args[1].equalsIgnoreCase("give")) {
                                int tier = Integer.parseInt(args[2]);
                                if (tier == 1 || tier == 2 || tier == 3) {
                                    if(args[3] == null){
                                        Player target = Bukkit.getPlayer(args[3]);
                                        for (Player online : Bukkit.getOnlinePlayers()) {
                                            if (target.getName() == online.getName()) {
                                                target.getInventory().addItem(gun.getItem(tier));
                                                break;
                                            } else {
                                                player.sendMessage(instance.getPrefix() + "Unable to find player §8:§c" + args[3]);
                                            }
                                        }
                                    }else{ // This else does not get triggered when args[3] == null, please help :P
                                        player.getInventory().addItem(gun.getItem(tier));
                                    }
    
                                } else {
                                    player.sendMessage(instance.getPrefix() + "You must specify a number between 1-3");
                                    player.sendMessage("- Tier 1§8: §9Rifle");
                                    player.sendMessage("- Tier 2§8: §9Shotgun");
                                    player.sendMessage("- Tier 3§8: §9Machine Gun");
    
                                }
    
    
                            } else {
    
                                commandHelp(player);
    
                            }
                        }
                    } else if (length == 0) {
                        commandHelp(player);
                    }
    
    
    
            } else {
                player.sendMessage(instance.getPrefix() + "You are not Omer.");
            }
    
    
    
        }
            return false;
        }
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    Machine Maker

    @Omer H.
    Please explain your issue, what is going wrong? Are there any errors? If so please post the full console log to pastebin.com.
     
  4. Offline

    FrostDevStudios

    @Omer H.
    Are you asking if the args[] contains a specific value.?
     
  5. Offline

    Omer H.

  6. Offline

    timtower Administrator Administrator Moderator

    @Omer H. Because it won't be null when the length of the array is 4
     
  7. Offline

    ThePandaPlayer

    It won't trigger if the length of the array is 4. There HAS to be a string value in the [3] value of the array for the length to be 4.
     
  8. Offline

    xize

    @Omer H.

    You need to use args.length to determine if any argument is filled in.

    The length behind the arguments is a litteral count from 1-??? but to get the actual latest argument you need to minus it by -1 from the length because arrays are made from 0-maxsize that is how arrays are made.

    So basicly it is like:

    Code:java
    1.  
    2. if args.length == 0
    3. no arguments
    4.  
    5. if args.length == 1
    6. one argument given in, and to get the latest one: args[0]
    7.  
    8. if args.length == 2
    9. 2 arguments given in, and the first argument is args[0] and the second is args[1] and args[2] is null or gives a IndexOutOfBoundsException.
    10.  


    That is how you can work with arguments, and to check if arguments are being used.
     
  9. Offline

    Omer H.

    Thanks!
     
Thread Status:
Not open for further replies.

Share This Page