Invalid commands

Discussion in 'Plugin Development' started by fussionzz97, Feb 10, 2014.

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

    fussionzz97

    I'm a Java newb and just writing up a player to player teleporting plugin as a challenge.

    This is what i have: http://pastie.org/8718695
    Error log if it helps: http://pastie.org/8718707

    What im wanting to know is how to silence 'internal error' so that it reports back with Invalid command or the first command (/tp) instead of the ugly and frightening internal error.

    Thanks :)
     
  2. Offline

    Assist

    fussionzz97
    May I see the line 38 in Teleporter class?
     
    fussionzz97 likes this.
  3. Offline

    NonameSL

    The problem is at line 38, and if im not wrong it is the
    Code:java
    1. Location targetPlayerLocation = targetPlayer.getLocation();

    Correct?
    I think I see the problem -
    1. You didnt check if the player is null
    2. There is a better way to get a player by name - Bukkit.getPlayerExact(args[0]);
    Your code should be:
    Code:java
    1.  
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    4. Player player = (Player) sender;
    5. if(commandLabel.equalsIgnoreCase("tp")){
    6. if(args.length == 0){
    7. player.sendMessage(ChatColor.DARK_RED + "Command: /tp [PLAYER1] [PLAYER2]");
    8. }
    9. else if(args.length == 1){ //Singular name to quickly TP
    10. Player targetPlayer = Bukkit.getPlayerExact(args[0]);
    11. if(targetPlayer==null){
    12. sender.sendMessage(ChatColor.DARK_RED+args[0]+" is not an online player!");
    13. }else{
    14. Location targetPlayerLocation = targetPlayer.getLocation();
    15. player.teleport(targetPlayerLocation);
    16. targetPlayer.sendMessage(player + " has teleported to you.");
    17. player.sendMessage("You have teleported to " + targetPlayer.getName());
    18. }
    19. }
    20. else if(args.length == 2){ //Multi TP used when teleporting player X to player Y
    21.  
    22. Player targetPlayer = Bukkit.getPlayerExact(args[0]);
    23. Player targetPlayer1 = Bukkit.getPlayerExact(args[1]);
    24. if(targetPlayer==null){
    25. sender.sendMessage(ChatColor.DARK_RED+args[0]+" is not an online player!");
    26. }
    27. if(targetPlayer1==null){
    28. sender.sendMessage(ChatColor.DARK_RED+args[1]+" is not an online player!");
    29. }
    30. if(targetPlayer!=null&&targetPlayer1!=null){
    31. Location targetPlayer1Location = targetPlayer1.getLocation();
    32. targetPlayer.teleport(targetPlayer1Location);
    33. targetPlayer.sendMessage("You have been teleported to " + player.getDisplayName());
    34. }
    35. }
    36. else if(args.length > 2){
    37. player.sendMessage(ChatColor.DARK_RED + "Command: /tp [PLAYER1] [PLAYER2]");
    38. }
    39. return true;
    40. }
    41. return false;
    42. }
    43.  
    44.  
     
    fussionzz97 likes this.
  4. Offline

    random_username

    fussionzz97 likes this.
  5. Offline

    fussionzz97

    NonameSL

    Ahh i see, thanks. Why do you recommend Bukkit.getPlayerExact(args[0]) over player.getServer().getPlayer ? I like player.getServer().getPlayer because it would guess the rest of the name if you only entered a few letters.

    Thanks for everyones help.
     
  6. Offline

    Assist

    fussionzz97
    Some people prefer getPlayerExact() just because of that reason, though it's a very situational preference.
     
    fussionzz97 likes this.
Thread Status:
Not open for further replies.

Share This Page