Solved ColouredConsoleSender cannot be cast, need to get to work with console

Discussion in 'Spigot Plugin Development' started by Nighterance, May 20, 2017.

  1. Hello!
    So I am making simple command, that sets players xp to 0.99f.
    From In game, it is working just fine, but from console, it gives me this error
    Code (Text):

     Unexpected exception while parsing console command "spawnerup"
    org.bukkit.command.CommandException: Unhandled exception executing command 'spawnerup' in plugin SpawnerLevel v1.3
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
            at me.Nighterance.SpawnerLevel.onCommand(SpawnerLevel.java:842) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 8 more
     

    This is the code

    Code (Text):

               if(cmd.getName().equalsIgnoreCase("spawnerup")){
                      if(args.length == 0){
                      }else if(args.length > 0){
                         Player t = Bukkit.getServer().getPlayer(args[0]);
                         
                          t.sendMessage(ChatColor.GOLD + "You were given xp needed to levelup!");
                          t.sendMessage(ChatColor.GOLD + "To rankup do" + " " + ChatColor.AQUA + "/rankup");
                        t.setExp(0.99f);
                      }


    }
     
    Before there are a lot of comments, I tried to check if player is instance of player earlier, but I need to make so it is working with console as well. (Tried to make check if sender is console, but did not work.)

    Thanks
     
  2. What is on line 842?
     
  3. From what the error says you're trying to cast ColouredConsoleSender as a player, but it can never be a player.
    Could I see the entire code / the place where you're creating the variables.
     
  4. Player p = (Player) sender; This is that line. I tried to make so if sender is console then skip that part.
     
  5. Does it need to be a player, where are you using that variable?

    Why did you have problems skipping that part? Is it because you moved it completely to inside a condition, rather than only moving the assignment inside it?
     

  6. The thing is I need to make so it does the same if player is console on player is player.
    When I used that sender is console it just did nothing when it is console and give xp when it is player. But I need for both to work
     
  7. When are you using the p variable, will you paste the lines of code that show it?

    And, that would be because you moved all your code inside the conditional, rather than just the player assignment.
     
  8. I tried not using p variable at all, so it just puts 0.99f exp to a player, that is Bukkit.getServer().getPlayer(args[0]);
     
  9. So why do you need it there? Just remove it.
     
  10. But the thing is I do not have check for console or player, i tried, but did not work.

    All I need to do is with command get args[0], then to args[0] that is player name set xp to 0.99f
     
  11. You don't need a check, just remove the p variable if you don't use it
     
  12. But the error still happens
     
  13. Have you also removed the if statement?

    Post your full onCommand here please.
     
  14. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player p = (Player) sender;


    //Other commands


    if(cmd.getName().equalsIgnoreCase("spawnerup")){


    if(args.length == 0){
    }else if(args.length > 0){
    Player t = Bukkit.getServer().getPlayer(args[0]);
    t.sendMessage(ChatColor.GOLD + "You were given xp needed to levelup!");
    t.sendMessage(ChatColor.GOLD + "To rankup do" + " " + ChatColor.AQUA + "/rankup");
    t.setExp(0.99f);
    }


    }
     
  15. Are you trying to use the command as a console? If so:
    Code (Text):
    if (!(sender instanceof Player) return true;
    I might have missunderstoond what you're asking but I can only assume this is your problem.
    If I'm incorrect please explain what are you doing in the first place, what happens and what should not happen.[/CODE]
     
  16. You didn't remove the "Player p = (Player) sender;"
    You said you weren't using it

    He said he wants it to work for both player and console... Your solution makes it work with only console.
     
  17. In my opinion you should simply add this

    Code (Text):
    if (sender instanceof CONSOLE) {
    // repeat the same as if he were a player without the colors
    }

    //If CONSOLE doesn't work then make it

    */
    if (!(sender instanceof Player) {
    // repeat the same as if he were a player without the colors
    } */


     

    I might be wrong but it's worth a shot.
     
  18. That would make it only work with console, also console can display colours.

    Edit: It's ConsoleCommandSender not CONSOLE just realised that's what you meant by it might be wrong.
     
  19. If you are going to send a message don't use the variable Player p = (Player) sender;

    Just send the message using sender.sendMessage("Test");

    This message will send both to the player and console.
     
    • Like Like x 1
  20. Code (Text):
            if (command.getName().equalsIgnoreCase("spawnerup")) {
                if (sender instanceof Player) {
                    if (args.length == 0) {
                        return true;
                    }
                        Player t = Bukkit.getServer().getPlayer(args[0]);
                        t.sendMessage(ChatColor.GOLD + "You were given xp needed to levelup!");
                        t.sendMessage(ChatColor.GOLD + "To rankup do" + " " + ChatColor.AQUA + "/rankup");
                        t.setExp(0.99f);
                }
                if (!(sender instanceof Player)) {
                    if (args.length == 0) {
                        return true;
                    }
                    Player t = Bukkit.getServer().getPlayer(args[0]);
                    t.sendMessage(ChatColor.GOLD + "You were given xp needed to levelup!");
                    t.sendMessage(ChatColor.GOLD + "To rankup do" + " " + ChatColor.AQUA + "/rankup");
                    t.setExp(0.99f);
                 
                }
            }
     
    I hope you understand what has happened here. I'm not entirely sure that it works so don't count on it.