setHealth ArrayIndexOutofBounds

Discussion in 'Plugin Development' started by warg, May 12, 2013.

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

    warg

    Can someone tell me why I can't set setHealth to integer 20?
     
  2. Offline

    chasechocolate

    What's line 22 of zufallsort.java?
     
  3. Offline

    warg

     
  4. Offline

    chasechocolate

    warg check if the args.length == 1.
     
  5. Offline

    warg

    Code:
     if(cmd.getName().equalsIgnoreCase("whatever")){
    if(args.length == 1){
    Player target = sender.getServer().getPlayer(args[0]);
    target.setHealth(20);
    return true;
    }
    if(args.length != 1){
    sender.sendMessage("Too many arguments!");
    return false;
    }
    }
    Btw, why am I checking for 1 argument if I need none? My command should set the HP of the player t0 full (20). So no argument is needed.
     
  6. Offline

    CubieX

    Erm, your command is "zufallsort".
    And the first argument is the name of the player. You are typing: /zufallsort PLAYER
    So PLAYER is your argument and therefore you have to check if args.length == 1.

    No argument would be just "/zufallsort".
     
  7. Offline

    warg

    Sorry for the confusion with 'zufallsort'. I have versions and I changed the 'zufallsort' in the snippets and error logs to 'whatever'. Again, sorry for that. :)

    What is necessary to get the command working without the PLAYER argument? So it's automatically calling the command on the player who types it in.
     
  8. Offline

    CubieX

    The player issuing the command is the "sender".
    You must check that this is actually a player, before assigning it to a Player object. (Because sender could also be the console)
    Code:
    if(sender instanceof Player)
    {
      Player p = (Player) sender;
    }
    Then you can heal this player p.
     
  9. Offline

    warg

    This code works as expected so far, except the fact that the error message isn't shown, but can someone of you guys explain me why I have to '(Player)' part in the variable target? I want to understand the code completely and I'm not quite sure what that part causes.

    Push. :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page