Solved /Heal = Player = Good, Heal = Console = :(

Discussion in 'Plugin Development' started by C0lA_K1nG, Aug 20, 2013.

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

    C0lA_K1nG

    Basically my code is set up properly but when i issue /heal in-game it works fine but from the console it just gives me errors:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
    2. if(commandlabel.equalsIgnoreCase("heal")){
    3. Player player = (Player) sender;
    4. if(args.length == 0) {
    5. player.setHealth(20);
    6. player.sendMessage(ChatColor.DARK_AQUA + "You have healed yourself.");
    7. } else if(args.length == 1) {
    8. Player targetPlayer = Bukkit.getPlayer(args[0]);
    9. if (targetPlayer !=null) {
    10. targetPlayer.setHealth(20);
    11. targetPlayer.sendMessage(ChatColor.DARK_AQUA + "You have been healed by: " + ChatColor.GOLD + player.getName() + ChatColor.DARK_AQUA + ".");
    12. player.sendMessage(ChatColor.DARK_AQUA + "You have healed: " + ChatColor.GOLD + targetPlayer.getName() + ChatColor.DARK_AQUA + ".");
    13. }
    14. } else {
    15. sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Error" + ChatColor.DARK_RED + "]:" + ChatColor.GRAY + " Player not found. Is he/she offline?");
    16. }
    17. } else if(!(sender instanceof Player)) {
    18. if(args.length == 0) {
    19. sender.sendMessage("Sorry, the console cannot heal itself!");
    20. } else if(args.length == 1) {
    21. Player targetPlayer = Bukkit.getPlayer(args[0]);
    22. if (targetPlayer !=null) {
    23. targetPlayer.setHealth(20);
    24. targetPlayer.sendMessage(ChatColor.DARK_AQUA + "You have now been healed by the" + ChatColor.GOLD + " Console" + ChatColor.DARK_AQUA + ".");
    25. sender.sendMessage(ChatColor.DARK_AQUA + "You have healed: " + ChatColor.GOLD + targetPlayer.getName() + ChatColor.DARK_AQUA + ".");
    26. }
    27. } else {
    28. sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.RED + "Error" + ChatColor.DARK_RED + "]:" + ChatColor.GRAY + " Player not found. Is he/she offline?");
    29. }
    30. }
    31. return true;
    32. }
    33.  
    34. }
    35.  

    Any help?
     
  2. Offline

    ZeusAllMighty11

    You directly cast Player to sender, before checking instances.
     
  3. Offline

    C0lA_K1nG

    Thank you! Solved it :)
     
Thread Status:
Not open for further replies.

Share This Page