Dead code?

Discussion in 'Plugin Development' started by BizeaxPvP, Aug 6, 2019.

  1. Offline

    BizeaxPvP

    Are you sure? It's not working for me at all. This is the code:
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            Player target2 = plugin.getServer().getPlayer(args[0]);
            if(cmd.getName().equalsIgnoreCase(punish)) {
                if (args.length == 1){
                    return false;
                } else if(args.length >=1) {
                    if (args[0] != null) {
                        player.sendMessage("test");
                        return true;
                    }else if (target2 == null) {
                            player.sendMessage("That player is null");
                            return true;
                        } if(args[1].contains("ban")) {
                            player.sendMessage("ban stuff here");
                            return true;
                        }else if(args[1].contains("mute")) {
                            player.sendMessage("mute stuff here");
                            return true;
                        }
                    }
                }
           
           
            return false;
        }
     
  2. Offline

    KarimAKL

    @BizeaxPvP
    1. You are trying to get the target before you check the length, don't do that.
    2. You are checking if args[0] is null, there's no need for that.
    3. You've set the length to >= 1, instead do > 1.
     
  3. Offline

    CraftCreeper6

  4. Offline

    BizeaxPvP

    Thanks for the help, but im giving up on this project. I did everything i could and it simply wont work. but thanks again for the help!
     
  5. Offline

    xX4w3s0m3Xx

    @BizeaxPvP If I were you I wouldn't give up on this. You almost have it and if you are able to solve this then you will only get better at it, unless that's not what you want. Go over your code once again.
    Check what karim said:
    1. You are trying to get the target before you check the length, don't do that.
      This point is about these lines of code, first trying to get the target
      Code:
      Player target2 = plugin.getServer().getPlayer(args[0]);
      and then checking the length
      Code:
      if (args.length == 1){
      } else if(args.length >=1) {
      At the moment of getting the target you don't know if the length of the args array is bigger or equal to 1, so you can never be certain that args[0] even exists. The best time to get the target is right after the else if statement

    2. You are checking if args[0] is null, there's no need for that.
      This is talking about
      Code:
      } else if(args.length >=1) {
                      if (args[0] != null) {
      You checked if the args length is bigger or equal to 1. This means that the args array always at least has 1 element, which is the 0-th element, so args[0] can't be null. This means that the
      Code:
      if (args[0] != null) {
      check is redundant and can be removed

    3. You've set the length to >= 1, instead do > 1.
      This is talking about the same if statement as before
      Code:
      } else if(args.length >=1) {
      You may ask why it should be > instead of >=
      The reason is that in the other if statement
      Code:
      if (args.length == 1){
      You are checking if the args length is equal to 1 when it is then you return false, which is okay.
      But in the else if statement you are checking if the args length is equal or bigger than 1, so you are again checking if it's equal to 1. This will never happen, because you return false when it's 1 in the other if statement.

    4. EqualsIgnoreCase with a variable, this should be a String
      I think you have got an error on this line
      Code:
      if(cmd.getName().equalsIgnoreCase(punish)) {
      If not, okay then.
      I think you want to check if the label equals(ignoring the case) "punish",
      So maybe you want to replace that code with
      Code:
      if (label.equalsIgnoreCase("punish")) {
    I hope this helps a bit, if you got any more troubles, just ask
     

Share This Page