Solved Can't use commands through the console

Discussion in 'Plugin Development' started by Tadukoo, Jul 5, 2013.

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

    Tadukoo

    I'm working on making a plugin, and I don't have much yet, but, with what I have, I can use all the commands as a player without any problems. If I try any of the commands through the console, it gives an error. I believe the error is in the CommandExecutor class, but I don't know how to fix it. I believe the problem lies in the top of the class:

    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    3. String playerType;
    4. Player player = (Player) sender;
    5. if (sender instanceof Player){
    6. playerType = "player";
    7. }else if(sender instanceof ConsoleCommandSender){
    8. playerType = "console";
    9. }else if(sender instanceof BlockCommandSender){
    10. playerType = "block";
    11. }else{
    12. playerType = "unknown";
    13. }
    14. if(cmd.getName().equalsIgnoreCase("ping")){
    15. if(playerType == "player"){
    16. sender.sendMessage(ChatColor.GOLD + "Pong!");
    17. return true;
    18. }else{
    19. sender.sendMessage(ChatColor.DARK_RED + "You must be a player!");
    20. return true;
    21. }
    22. }


    Note: the ping command is only for testing purposes; it works fine for a player, but will give an error to the console instead of that message.
    Later I have the command /thelp, which would display help for the commands for a player, but sends an error to the console:
    Code:java
    1. }else if(cmd.getName().equalsIgnoreCase("thelp")){
    2. if(args.length == 0){
    3. player.sendMessage("----" + ChatColor.GREEN + "Tadukoo Plugin Help" + ChatColor.WHITE + "----");
    4. player.sendMessage(ChatColor.AQUA + "/ping" + ChatColor.WHITE + " | " + ChatColor.GREEN + "A testing command.");
    5. player.sendMessage(ChatColor.AQUA + "/tport" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Teleports players to other players.");
    6. player.sendMessage(ChatColor.AQUA + "/heal" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Heals a player.");
    7. player.sendMessage(ChatColor.AQUA + "/kill" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Kills a player.");
    8. player.sendMessage(ChatColor.AQUA + "/feed" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Feeds a player.");
    9. player.sendMessage(ChatColor.AQUA + "/starve" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Starves a player.");
    10. player.sendMessage(ChatColor.AQUA + "/suicide" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Commit suicide.");
    11. player.sendMessage(ChatColor.AQUA + "/thelp" + ChatColor.WHITE + " | " + ChatColor.GREEN + "Shows this help.");
    12. return true;
    13. }else{
    14. player.sendMessage(ChatColor.RED + "Sorry, we don't have search functionality yet.");
    15. return true;
    16. }
    17. }


    If there is no problem in these code excerpts, you may view the entire code at http://github.com/Tadukoo/Tadukoo-Plugin

    EDIT: I tried moving line 4 from the first excerpt to right below line 6, and it made it so no one could use the commands. It said something about not being able to find player.
     
  2. Offline

    CubieX

    Code:
    Player player = (Player) sender;
    if (sender instanceof Player){
    Check for instanceof Player before casting the sender to player.

    And only if it's a player, use "player" to send messages.
    Otherwise use "sender".

    Just clean up your logic here.
     
  3. Offline

    Tadukoo

    If I do that I have an error with my /tport command:

    Code:java
    1. }else if(cmd.getName().equalsIgnoreCase("tport")){
    2. if(playerType == "player"){
    3. if(args.length == 0){
    4. sender.sendMessage(ChatColor.DARK_RED + "TOO LITTLE ARGUMENTS!");
    5. return true;
    6. }else if(args.length == 1){
    7. Player targetPlayer = sender.getServer().getPlayer(args[0]);
    8. if(targetPlayer != null){
    9. Location targetPlayerLocation = targetPlayer.getLocation();
    10. player.teleport(targetPlayerLocation);
    11. sender.sendMessage("You have teleported yourself to " + targetPlayer.getDisplayName());
    12. return true;
    13. }else{
    14. sender.sendMessage(ChatColor.DARK_RED + "PLAYER NOT ONLINE!");
    15. return true;
    16. }
    17. }else if(args.length == 2){
    18. Player targetPlayer = sender.getServer().getPlayer(args[0]);
    19. Player targetPlayer1 = sender.getServer().getPlayer(args[1]);
    20. Location targetPlayer1Location = targetPlayer1.getLocation();
    21. if(targetPlayer1Location != null){
    22. targetPlayer.teleport(targetPlayer1Location);
    23. targetPlayer.sendMessage("You have been teleported to " + targetPlayer1.getDisplayName());
    24. return true;
    25. }else{
    26. targetPlayer.sendMessage(ChatColor.DARK_RED + "PLAYER IS OFFLINE!");
    27. return true;
    28. }
    29. }
    30. }else{
    31. sender.sendMessage(ChatColor.RED + "You must be a player!");
    32. }
    33. }

    Where I have player.teleport, it says "player cannot be resolved", and if I change it to sender it says "The method teleport(Location) is undefined for the type Command Sender"

    Also later for a heal command I get a similar error for "player.setHealth(20)" and "player.setFireTicks(20)"

    EDIT: I changed it to ((Player) sender).teleport I get no errors in Eclipse, but not sure how it'll work out in a server yet.
     
Thread Status:
Not open for further replies.

Share This Page