java.lang.ClassCastException: org.bukkit.craftbukkit.v1_5_R3.command. ColouredConsoleSender cannot

Discussion in 'Plugin Development' started by unityself, May 16, 2013.

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

    unityself

    Hi I am want to write my own plugin but it doesnt work.
    Here the Error:
    Code:
    [WARNING] Unexpected exception while parsing console command "check"
    org.bukkit.command.CommandException: Unhandled exception executing command 'chec
    k' in plugin Website API v0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    9)
            at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServe
    r.java:523)
            at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchServerCommand(Craf
    tServer.java:512)
            at net.minecraft.server.v1_5_R3.DedicatedServer.an(DedicatedServer.java:
    262)
            at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:2
    27)
            at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:4
    77)
            at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java
    :410)
            at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:5
    73)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_5_R3.command.
    ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
            at me.unityself.Main.onCommand(Main.java:52)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 8 more
    
    And here my code:

    plugin.yml
    Code:
    name: Website API
    main: me.unityself.Main
    version: 0.1
    author: unityself
    description: This is a simple API for Payment Developer
    commands:
      check:
        description: Check if everything is ok.
        permission: srmarket.check
        usage: /check
    
    Main.java
    Code:
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package me.unityself;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    /**
    *
    * @author Florian
    */
    public final class Main extends JavaPlugin {
        private Main plugin;
       
        [USER=90830440]Override[/USER]
        public void onEnable(){
            getLogger().info("Website API is activated");
            //mysql Default
            this.getConfig().addDefault("mysql.enable",false);
            this.getConfig().addDefault("mysql.server","localhost");
            this.getConfig().addDefault("mysql.port",3306);
            this.getConfig().addDefault("mysql.database","api");
            //url Default
            this.getConfig().addDefault("url.enable",false);
            this.getConfig().addDefault("url.packages","http://localhost/packages.txt");
            this.getConfig().addDefault("url.packagestype",".txt");
            this.getConfig().addDefault("url.contenttype",".con");
            //ftp Default
            this.getConfig().addDefault("ftp.enable",false);
            this.getConfig().addDefault("ftp.server","localhost");
            this.getConfig().addDefault("ftp.port",21);
            this.getConfig().addDefault("ftp.username","root");
            this.getConfig().addDefault("ftp.password","yourpassword");
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
     
        }
       
       
        [USER=90830440]Override[/USER]
        public void onDisable(){
            getLogger().info("Website API isnt activated");
        }
     
        [USER=90830440]Override[/USER]
        public boolean onCommand(CommandSender sender,Command cmd,String commandLabel, String[] args){
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("check")){
            if (args.length == 0) {
            this.plugin = plugin;
            if (!(sender instanceof Player)) {
        System.out.println("Dieser Befehl ist nur für Spieler!");
        return true;
            }
            else{
            if(this.plugin.getConfig().getBoolean("ftp.enable") == false){
            player.sendMessage(ChatColor.RED+"FTP is not enabled");
            }
            else{
            player.sendMessage(ChatColor.GREEN+"FTP is enabled");
            }
           
            if(this.plugin.getConfig().getBoolean("mysql.enable") == false){
            player.sendMessage(ChatColor.RED+"MYSQL is not enabled");
            }
            else{
            player.sendMessage(ChatColor.GREEN+"MYSQL is enabled");
            }
           
            if(this.plugin.getConfig().getBoolean("url.enable") == false){
            player.sendMessage(ChatColor.RED+"URL is not enabled");
            }
            else{
            player.sendMessage(ChatColor.GREEN+"URL is enabled");
            }
           
            player.sendMessage(ChatColor.GREEN+"Your check is done!");
            return true;
            }
            }
            }
            return false;
       
        }
    }
    
    (German)Ich freue mich auch über Beiträge auf Deutsch.(German)
    And here a screenshot from my Netbeans Project:
     
  2. Offline

    evilmidget38

    unityself In Main#onCommand you immediately cast the CommandSender to a Player before checking if they're actually a Player. This causes issues when the console or a command block uses your command.
     
    lukegb likes this.
  3. Offline

    unityself

    Code:
    java.lang.NullPointerException
    at me.unityself.Main.onCommand(Main.java:61)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more
    Why is the config empty or null

    Here my Java code:
    Code:
     
    if(this.plugin.getConfig().getBoolean("ftp.enable") == false){
    sender.sendMessage(ChatColor.RED+"FTP is not enabled");
    }
    else{
    sender.sendMessage(ChatColor.GREEN+"FTP is enabled");
    }
    
     
  4. Because you never assign plugin to a value. In fact, why you need such a plugin instance anyway when working in the main class?
     
Thread Status:
Not open for further replies.

Share This Page