Solved "Unexpected exception while parsing console command"

Discussion in 'Plugin Development' started by user_91277742, Nov 2, 2017.

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

    user_91277742

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if(sender instanceof Player){
                if(p.hasPermission("raza.neko")) {
                    ponerSombrero(p);   
                }
            }else{
                sender.sendMessage("You cant do this command in the console");
            }
            return true;
        }
    Hello ^^ , so... I'am doing a little plugin and in this case, just I want to get a message of the console "you cant do this command in the console" when I try to do a command, Its easy. I tried to do all , but I have this issue always . (I have the command registered in onEnable).

    Code:
    >neko
    [11:56:54 WARN]: Unexpected exception while parsing console command "neko"
    org.bukkit.command.CommandException: Unhandled exception executing command 'neko' in plugin RazasLFA v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchServerCommand(CraftServer.java:636) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.DedicatedServer.aM(DedicatedServer.java:437) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_144]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_11_R1.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
            at io.github.amaknajaja.principal.onCommand(principal.java:32) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            ... 8 more
     
  2. Offline

    MightyOne

    ClassCastException. Can you imagine what that is? You cast the sender to Player right in the beginning of onCommand without testing if it is a player.

    Btw what does you sombrero look like you give the player?
     
  3. Offline

    user_91277742

    I dont really understand you. In the second line I define the sender like a player. So.... if the sender is not a player, send a message by console with that message right? , sorry, Im noobie jajaja
     
  4. Offline

    MightyOne

    Firsr you have to check if sender is a player:
    Code:
    if(sender instanceof Player){
    Then you can cast him/her to player:
    Code:
    Player rfp = (Player) sender;
    Otherwise you will get an Exception if somebody tries to call the command from console = not a Player
     
    Last edited: Nov 3, 2017
  5. Offline

    user_91277742

    ah! , If i put first "Player p = (Player) sender;" I define before of the sentence , the sender like the player, okok , I understand and now works :D , thank you!
     
  6. Offline

    MightyOne

    good luck continiuing
     
Thread Status:
Not open for further replies.

Share This Page