Unhandled exception.

Discussion in 'Plugin Development' started by SirLittlemonster7, Apr 25, 2013.

Thread Status:
Not open for further replies.
  1. I am having problems with this unhandled exception for a console command im trying to make..

    Code:
        @Override
        public boolean onCommand(final CommandSender sender, final Command cmd, final String cl, final String[] args) {
            CommandSender s = sender;
            if (cl.equalsIgnoreCase("user")) {
                Player target = Bukkit.getPlayer(args[1]);
                if (args.length == 2) {
                    if (target != null) {
                        if (args[0].equalsIgnoreCase("upgrade")) {
                            s.sendMessage(ChatColor.GREEN + "User upgraded.");
                            donors.add(target.getName());
                        } else if (args[0].equalsIgnoreCase("downgrade")) {
                            s.sendMessage(ChatColor.RED + "User downgraded.");
                            donors.remove(target.getName());
                        }
                    } else {
                        s.sendMessage(ChatColor.RED + "Player not online, or mispelled name.");
                    }
                } else {
                    s.sendMessage(ChatColor.RED + "Error: " + ChatColor.GRAY + "Usage is /User Upgrade / Downgrade (playername");
                }
            }
    Error:

    Code:
    23:43:03 [WARNING] Unexpected exception while parsing console command "user upgrade little"
    org.bukkit.command.CommandException: Unhandled exception executing command 'user' in plugin ptMcPvE v1.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:187)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.dispatchCommand(CraftServer.java:523)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.dispatchServerCommand(CraftServer.java:512)
        at net.minecraft.server.v1_5_R2.DedicatedServer.am(DedicatedServer.java:261)
        at net.minecraft.server.v1_5_R2.DedicatedServer.r(DedicatedServer.java:226)
        at net.minecraft.server.v1_5_R2.MinecraftServer.q(MinecraftServer.java:476)
        at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:409)
        at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_5_R2.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
        at me.littletoche.McPvE.MainClass.onCommand(MainClass.java:850)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
     
  2. Offline

    kreashenz

    Maybe add in there
    Code:java
    1. if(sender instanceof ConsoleCommandSender){
    under the if(cl.equals(....)){
     
  3. Nope :/
     
  4. Offline

    kreashenz

    Why have you ColouredConsoleSender ? That's whats causing the error. Try
    Code:java
    1. if(sender instanceof Player){
    then find that bracket, and put
    Code:java
    1. } else sender.sendMessage("§cYou must be a player to use that command");
    2.  
    3. [syntax=java]@Override
    4. public boolean onCommand(CommandSender sender, Command cmd, String cl, String[] args) {
    5. if (cmd.getName().equalsIgnoreCase("user")) {
    6. if(sender instanceof Player || sender instanceof ConsoleCommandSender){
    7. if (args.length == 2) {
    8. Player target = Bukkit.getPlayer(args[1]);
    9. if (target != null) {
    10. if (args[0].equalsIgnoreCase("upgrade")) {
    11. sender.sendMessage(ChatColor.GREEN + "User upgraded.");
    12. donors.add(target.getName());
    13. }
    14. if (args[0].equalsIgnoreCase("downgrade")) {
    15. sender.sendMessage(ChatColor.RED + "User downgraded.");
    16. donors.remove(target.getName());
    17. }
    18. } else sender.sendMessage(ChatColor.RED + "Player not online, or mispelled name.");
    19. } else sender.sendMessage("§cYou must be a player/console to use this command.");
    20. } else sender.sendMessage(ChatColor.RED + "Error: " + ChatColor.GRAY + "Usage is /User Upgrade / Downgrade (playername");
    21. }
    22. }
    23. [/syntax] Try this, maybe it should work. If it doesn't try replacing sender instanceof Player with sender instanceof ColouredConsoleSender
    24.  
    25. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  5. Solved it myself sorry xD.
     
Thread Status:
Not open for further replies.

Share This Page