Healing self doesn't work

Discussion in 'Plugin Development' started by Datdenkikniet, Jun 23, 2013.

Thread Status:
Not open for further replies.
  1. this is my code for the command:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("heal")){
    2. Player s = (Player) sender;
    3. Player target = getServer().getPlayer(args[0]);
    4. if (sender instanceof Player){
    5. if (args.length == 0){
    6. s.setHealth(20);
    7. s.setFireTicks(0);
    8. s.setFoodLevel(20);
    9. return true;
    10. }
    11. else if (args.length == 1){
    12. target.setHealth(15);
    13. target.setFireTicks(0);
    14. target.sendMessage("You have been healed by"+ " "+ sender.getName() + "!" );
    15. sender.sendMessage("You healed" + " " + target.getDisplayName());
    16. return true;
    17. }

    the args.length == 1 part does work, but the other part (with the args.length == 0) doesn't. Does anyone know how to solve this?
     
  2. Offline

    frymaster

    It would have been more helpful if you'd posted the error rather than just saying "doesn't work", but in any case:

    In line 3, you do something based on the first element of args[]

    In line 5, you check if args has no elements.

    You see the issue? By the time you're doing the stuff you do if there's no target, you've already assumed there is a target. Move line 3 to just after line 11.
     
  3. okay, but I fixed it already, because I just made another command for it (but thanks anyways)
     
Thread Status:
Not open for further replies.

Share This Page