Server Commands Not Working

Discussion in 'Plugin Development' started by SteppingHat, Jul 25, 2012.

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

    SteppingHat

    Hey. So, I have a plugin that completely works on the player's side of things. Messages gets logged to the console and all that other fancy stuff happens too. But when the console comes to execute a command, I get this error:

    Code:
    20:17:54 [WARNING] Unexpected exception while parsing console command
    org.bukkit.command.CommandException: Unhandled exception executing command 'slap
    ' in plugin TrollTools v1.4
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    6)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:4
    79)
            at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.
    java:475)
            at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:612)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:581)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.command.Coloured
    ConsoleSender cannot be cast to org.bukkit.entity.Player
            at com.gmail.steppinghat.TrollTools.onCommand(TrollTools.java:36)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
            ... 7 more
    Here is the code for one of the commands. With all the stuff that are completely unrelated to the problem removed for neatness

    Code:
    final Player player = (Player) sender;
    PluginDescriptionFile pdfFile = this.getDescription();
     
    if(commandLabel.equalsIgnoreCase("slap")) {
    if ((sender instanceof Player)) {
     
    if(player.hasPermission("trolltools.slap") || player.isOp()) {
    ...
    } else {
    player.sendMessage(ChatColor.RED + "You do not have permission");
    }
     
    } else {
    ...
    }
    return true;
    }
    If anyone can help me out that would be awesome.
    Thanks heaps in advance :)
     
  2. Offline

    Scizzr

    It looks like you're trying to cast a CommandSender to a Player when it's not a Player that's sending the command.
    You need to check if it's a player BEFORE casting it:
    Code:
    if(commandLabel.equalsIgnoreCase("slap")) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("This command can only be executed by players.");
            return true;
        } else {
            final Player player = (Player)sender;
            if (player.hasPermission("trolltools.slap") || player.isOp()) {
                ...
            } else {
                player.sendMessage(ChatColor.RED + "You do not have permission");
            }
        }
        return true;
    }
    
    And do yourself a double: tab your code! :D
     
  3. ConsoleSender cannot be cast to org.bukkit.entity.Player <- You're executing the command from console but try to cast to a player. Your
    final Player player = (Player) sender;
    has to be after
    if ((sender instanceof Player)) {
     
  4. Offline

    Scizzr

    Hi again. Competition ftw? xD
     
  5. Lol, no... I was just writing slowly. At the time I started writing your post wasn't there. :)
     
    ZeusAllMighty11 likes this.
  6. Offline

    Scizzr

    Just giving you a hard time. I'm just trolling the Plugin Dev forum myself for something to do (as a break from the plugin I'm working on). ^_^
     
  7. Offline

    SteppingHat

    Sweet thanks! I'll tell you if it worked.

    And btw, I do tab my code. Just copying it over to chrome untabbed it
    I can't stand untabbed code :p

    Thanks! Works fine, but there's a problem. If the server executed the command, I still want it to do something and it requires to get the target player.
    Code:
    if(player.getServer().getPlayer(args[0]) != null) {
        Player targetPlayer = player.getServer().getPlayer(args[0]);
        ...
    }
    This no longer works because since I moved final Player player = (Player) sender; into the player event, it no longer applies to the server event. So player in the code above no longer works. How would one fix this?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  8. Offline

    ZeusAllMighty11

    What are you trying to cast, and to who?

    If it's related to the event:

    Sender of command:
    final Player sender = (Player) sender

    Other args for player:
    final Player target = Bukkit.getPlayer(args[X])
     
  9. Offline

    SteppingHat

    Ahh. Never mind, I just added the final Player player = (Player) sender; inside the server event and it works fine. Thanks heaps for your help guys!

    Thats probably a better way. I'll implement that instead. Thanks!

    ZeusAllMighty11
    Doesn't work. Neither way. I'm trying to check if a player is on when a command is executed via the console btw.

    Code:
    if (!(sender instanceof Player)) {
        if(args.length==0) {
            log.info("Too little arguments!");
        } else if(args.length==1) {
            final Player ARGS_targetPlayer = Bukkit.getPlayer(args[0])
            if(ARGS_targetPlayer .getServer().getPlayer(args[0]) != null) {
                Player targetPlayer = ARGS_targetPlayer .getServer().getPlayer(args[0]);
                ...
            } else {
                log.info(args[0] + " is not online");
            }
        } else {
            log.info(ChatColor.RED + "Too many arguments!");
        }
    }
    That's basically the jist of the scenario that I'm trying to get this to work in.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  10. Offline

    ZeusAllMighty11


    Well to check if someone is online can't you do like bukkit.getOnlinePlayers().contains(args[0]) or something like that/?
     
  11. Overkilled. The OT is going the right way, but his codes look a bit weird... Basically he tries to get the player, then tries to get the player from the player (which may be null) and then he tries to get the player from the player he got from the player... Player inception :confused: ... let me try to clean them up:
    Code:java
    1.  
    2. if(!(sender instanceof Player))
    3. {
    4. Player player = (Player)sender;
    5.  
    6. if(args.length < 1)
    7. player.sendMessage("Too little arguments!");
    8. else
    9. {
    10. Player targetPlayer = Bukkit.getPlayer(args[0]);
    11. if(targetPlayer != null)
    12. {
    13. targetPlayer.sendMessage(player.getName()+" executed a command with you as a argument!");
    14. player.sendMessage(targetPlayer.getName()+" found!");
    15. }
    16. else
    17. {
    18. player.sendMessage(args[0] + " is not online");
    19. }
    20. }
    21. }
     
    ZeusAllMighty11 likes this.
  12. Offline

    ZeusAllMighty11


    All I can say is: damn
     
Thread Status:
Not open for further replies.

Share This Page