Console Command Sender

Discussion in 'Plugin Development' started by beastman3226, Nov 26, 2012.

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

    beastman3226

    How do you make a command executable by the console?
     
  2. Offline

    zack6849

    if the command can be able to run by console remove the
    Code:
    if(sender instanceof Player){
    
     
  3. Offline

    sionzee

    and

    Player player = (Player) sender; ?
    Code:java
    1. caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
     
  4. Offline

    beastman3226

    Can you have:
    Code:java
    1. if (sender instanceof ConsoleCommandSender){
    2. //stuff
    3. }
    4. if (sender instanceof Player) {
    5. //same stuff
    6. }
    7.  
     
  5. Offline

    sionzee

    and what kind of : instanceoff all
    I need Player and Console.
     
  6. Offline

    kyran_

    Make it an else if since it's a XOR scenario.

    You can just remove the instanceof check if you don't care who sent it. Or if you want to filter out any other random possible command senders apart from player and console,
    Code:
    if ((sender instanceof Player) || (sender instanceof ConsoleCommandSender)) {
    ...
    }
     
  7. Offline

    sionzee

    what is XOR ?
     
  8. Offline

    kyran_

    Exclusive Or, meaning one or the other can happen but not both at the same time. The command sender could be a player or the console but it can't be a player AND the console.

    Using an else if in this scenario just optimizes it a bit. If it evaluates the first if statement and finds that the sender is a player, there's no need to check if it's the console, so using an else if for the second if statement causes it to be skipped if the first is true.
     
  9. Offline

    sionzee

    ok, i now have
    if ((sender instanceof Player) || (sender instanceof ConsoleCommandSender))

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String Label, String [] args)
    2. {
    3. Player player = (Player) sender;
    4.  
    5. if (cmd.getName().equalsIgnoreCase("freelance"))
    6. {
    7. if ((sender instanceof Player) || (sender instanceof ConsoleCommandSender))
    8. {
    9. if(player.hasPermission("freelance.cmd.freelance"))
    10. {
    11. player.sendMessage("test");
    12. return true;
    13. }
    14. }
    15. }
    16. return false;
    17. }


    Console give me error with line
    Player player = (Player) sender;
    why ? i need add to all commands separately ?

    Code:
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:502)
    at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.java:494)
    at net.minecraft.server.DedicatedServer.al(DedicatedServer.java:258)
    at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:223)
    at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:493)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:426)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
     
  10. Offline

    fireblast709

    If you execute it as the console, you cannot do the cast (you are 'casting' sender to Player, which is causing the ClassCastException)
     
  11. Offline

    raGan.

    Remove all checks and simply work with sender. If it can be executed by console, sender is probably sufficient.
    Example:
    Code:
    sender.sendMessage("Hello sender !")
     
  12. Offline

    beastman3226

    No need for checking who the sender is?
     
  13. Offline

    raGan.

    why ? do you really need to know ? sender.getName() returns console for console and player's name for player
     
  14. Offline

    kyran_

    You're casting the command sender to a player object and then checking the command name and then checking whether the sender is a player or the console. Not only is the cast to player in the wrong spot, but it's inappropriate since you can't cast the console to a player.

    the line:
    Code:
    Player player = (Player) sender; 
    means take the sender and store them as a player. This can't be done if the console is the sender. This is why we usually add:
    Code:
    if (sender instanceof Player) 
    beforehand so we know if it was a player who issued the command.
    If you use:
    Code:
    if ((sender instanceof Player) || (sender instanceof ConsoleCommandSender))
    it's basically saying "accept any players or the console as the sender of the command."

    This is all pretty redundant since it only filters out command senders that aren't a player or the console. As stated above, you should just remove all checks and the casting of sender to a Player object. For future reference, before you cast sender to a Player type, you need to check if the sender is actually a player with the instanceof check.
     
  15. Offline

    sionzee

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String Label, String [] args)
    2. {
    3. Player player = (Player) sender;
    4.  
    5. if (cmd.getName().equalsIgnoreCase("freelance"))
    6. {
    7. if ((sender instanceof Player) || (sender instanceof ConsoleCommandSender))
    8. {
    9. if(player.hasPermission("freelance.cmd.freelance"))
    10. {
    11. sender.sendMessage("test");
    12. return true;
    13. }
    14. }
    15. }
    16. return false;
    17. }
    18.  


    You think this ?
    i fixed on sender, always doesn't work
     
  16. Offline

    raGan.

    sionzee
    The guy above you just wrote about why it is important to put cast after instance check.
     
  17. Offline

    kyran_

    That's the exact same thing.

    Try this:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String Label, String [] args) {
        if (cmd.getName().equalsIgnoreCase("freelance")) {
            if (sender.hasPermissions("freelance.cmd.freelance")) {
                sender.sendMessage("test");
                return true;
            }
        }
        return false;
    }
    
     
  18. Offline

    sionzee

    okay now working... :)
     
Thread Status:
Not open for further replies.

Share This Page