Solved Internal Error

Discussion in 'Plugin Development' started by Markyroson, Jun 13, 2015.

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

    Markyroson

    For some reason when I do /chat I get the message I want displayed but I get an internal error as well. The error is as follows:
    Code:
    command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    Any idea how to fix this?

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You must be a player to execute this command!");
            }
            Player p = (Player) sender;
            if(p.hasPermission("utils.chat.clear")) {
                if(cmd.getName().equalsIgnoreCase("chat")) {
                    p.sendMessage("The command is /chat clear");
                    if(args[0].equalsIgnoreCase("clear")) {
                            for(int i = 0; i < 100; i++) {
                                Bukkit.broadcastMessage(" ");
                            Bukkit.broadcastMessage(ChatColor.BLUE + "" + ChatColor.BOLD + sender.getName() + ChatColor.GRAY + " Has just cleared the chat!");
                        }
                        return true;
                    }
                }
            }
            return false;
        }
     
  2. Offline

    teej107

    Well why would you even try and cast a ColouredConsoleSender to a Player? The answer on how to fix this is easy. Don't cast the Object.
     
  3. Offline

    Markyroson

    How would I check if they have the permission or send them a message otherwise?
     
  4. Offline

    Zombie_Striker

    @Markyroson
    Why did not not add a return statement? Do you even know what you're doing?
     
  5. Offline

    teej107

    You don't need a Player for that :p
     
    Konato_K and Markyroson like this.
  6. Offline

    Markyroson

    That solved that error (and thank you) but now I get the exception ArrayIndexOutOfBounds: 0

    Line it points to as being out of bounds is
    Code:
                    if(args[0].equalsIgnoreCase("clear")) {
    It doesn't make any sense as that line appears to be correct and I have used a setup like that before with no issues.
     
  7. Offline

    teej107

    @Markyroson Do you know why that error is being thrown? If not, look up what that error is thrown for and you will easily find the answer.
     
    Markyroson likes this.
  8. Offline

    Markyroson

    Thanks for your help. I after a bit of trial and error I realized I was checking for args.length == 0 whereas I should have been checking args.length == 1 and now it is solved.


    Final, working code:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You must be a player to execute this command!");
            } else {
                if(label.equalsIgnoreCase("chat")) {
                    sender.sendMessage("The command is /chat clear");
                    if(args.length == 1) {
                       if(args[0].equalsIgnoreCase("clear")) {
                          if(sender.hasPermission("utils.chat.clear")) {
                               for(int i = 0; i < 100; i++) {
                                   Bukkit.broadcastMessage("");
                               }
                           Bukkit.broadcastMessage(ChatColor.BLUE + "" + ChatColor.BOLD + sender.getName() + ChatColor.GRAY + " Has just cleared the chat!");
                           return true;
                          }
                       }
                    }
                }
            }
            return false;
        }
    
     
Thread Status:
Not open for further replies.

Share This Page