Solved Condition 'args.length == 0' is always 'true'

Discussion in 'Spigot Plugin Development' started by Kubagg, Mar 31, 2020.

  1. Thx ;p
     
    #1 Kubagg, Mar 31, 2020
    Last edited: Mar 31, 2020
  2. in your code you are checking if it is less than 1
     
  3. Code (Java):
    if (args.length < 1) {
      if (sender instanceof Player) {
        Player player = (Player) sender;

        if (args.length == 0) {
    It only gets to args.length == 0 if args.length < 1
     
  4. It can only get to args.length == 0 if args.length < 1, meaning it's a redundant check, but it also makes the else statement unable to run unless it's outside of the args.length < 1 if statement.
     
  5. If your checking if the sender has permission before verifying that the sender is an instanceof the Interface Player.

    You should start by verifying the sender as the very first line below the parameter list.

    Code (Java):
    if (!(sender instanceof Player)) return false;
    To verify the arguments list you simply want to verify that the arguments are less than "1" primarily, than move down.

    Code (Java):
    if (arguments.length < 1) {
            player.sendMessage(ChatColor.RED + "Please provide valid arguments.");
    }
    and try not to abuse the else(ifs) I made the same mistake when I started. Simply verify them by utilizing the if keyword following by the correct and appropriate boolean logical statement.

    Code (Java):
    Player target;

    if ((target = Bukkit.getPlayerExact(arguments[1)) {
            player.sendMessage(ChatColor.RED + "That player is not online.");
    }
    With the line above, you're simply both verifying whether or not a player exists (if they do not proceed to this body) if they do indeed exist keep checking.

    Inserting an else statement isn't necessary.

    From reviewing the code so happily, I suggest you also review how Java works in terms of the overall core. Do not take this as a piece of harassment or me targeting you for the lack of knowledge you posses.

    If you'd like, you can view some nice resources online all throughout the web if you do a quick google search on Learn Java you'd find tons if not thousands of resources to assist you in learning and moving on.
     
    #5 Nerm, Mar 31, 2020
    Last edited: Mar 31, 2020