Solved Unhandled Command Exception

Discussion in 'Plugin Help/Development/Requests' started by waylock, Apr 6, 2015.

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

    waylock

    Hi guys,

    Im working on a trading plugin, but I keep getting this annoying error:
    Code:
    [15:21:59 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'trad
    er' in plugin BukkitPlugin2 v1.5
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-63e28b1]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    0) ~[craftbukkit.jar:git-Bukkit-63e28b1]
            at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServe
    r.java:625) ~[craftbukkit.jar:git-Bukkit-63e28b1]
            at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerCon
    nection.java:1077) [craftbukkit.jar:git-Bukkit-63e28b1]
            at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java
    :937) [craftbukkit.jar:git-Bukkit-63e28b1]
            at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(SourceFile:37) [craft
    bukkit.jar:git-Bukkit-63e28b1]
            at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(SourceFile:9) [craftb
    ukkit.jar:git-Bukkit-63e28b1]
            at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:1
    3) [craftbukkit.jar:git-Bukkit-63e28b1]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.7.0_75]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_75]
            at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [craftbukki
    t.jar:git-Bukkit-63e28b1]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:6
    70) [craftbukkit.jar:git-Bukkit-63e28b1]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    36) [craftbukkit.jar:git-Bukkit-63e28b1]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    26) [craftbukkit.jar:git-Bukkit-63e28b1]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :534) [craftbukkit.jar:git-Bukkit-63e28b1]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_75]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R2.entity.C
    raftPlayer cannot be cast to mc.waylock.main.Tradable
            at mc.waylock.main.PluginClass.onCommand(PluginClass.java:69) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-63e28b1]
            ... 15 more
    My java Code:
    Code:
    package mc.waylock.main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class PluginClass extends JavaPlugin {
    
        public MenuInv tradeHost;
        public InviteHandler invHandler;
        public Player target;
        public Tradable tradableTarget;
    
        public void onEnable() {
            Bukkit.getServer().getLogger().info("Plugin Enabled");
    
            tradeHost = new MenuInv(this);
            invHandler = new InviteHandler(null, null);
        }
    
        public void onDisable() {
            Bukkit.getServer().getLogger().info("Plugin Disabled");
    
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
    
                if (cmd.getLabel().equalsIgnoreCase("gm")) {
                    if (args.length == 0) {
                        p.sendMessage("Please specify a gamemode");
                    }
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("0")) {
                            p.setGameMode(GameMode.SURVIVAL);
                            p.sendMessage("Gamemode set to " + ChatColor.RED
                                    + "Survival");
                        }
                        if (args[0].equalsIgnoreCase("1")) {
                            p.setGameMode(GameMode.CREATIVE);
                            p.sendMessage("Gamemode set to " + ChatColor.GREEN
                                    + "Creative");
    
                        }
                        if (args[0].equalsIgnoreCase("2")) {
                            p.setGameMode(GameMode.SURVIVAL);
                            p.sendMessage("Gamemode set to " + ChatColor.DARK_GRAY
                                    + "Adventure");
                        }
                    }
                }
    
                if (cmd.getLabel().equalsIgnoreCase("trader")) {
                    if (args.length == 0) {
                        p.sendMessage("For Help use: /trader help");
                    }
                    if (args.length > 0) {
                        if (args[0].equalsIgnoreCase("invite")) {
    
                            if(args[1] != null){
                                @SuppressWarnings("deprecation")
                                Tradable tradableTarget = (Tradable) Bukkit.getServer().getPlayer(args[1]);
                            }else {
                                p.sendMessage("Please specify a player");
                            }
                            if (tradableTarget == null) {
                                p.sendMessage("That player is not online");
                                return true;
                            }
                           
                            if (tradableTarget.getTrading()) {
                                p.sendMessage("That Player is already trading!");
                                return true;
                            } else {
                                invHandler = new InviteHandler(p, target);
                            }
    
                        }
                        if (args[0].equalsIgnoreCase("accept")) {
                            invHandler.reactionHandler(p, args[0].toString());
                        }
                        if (args[0].equalsIgnoreCase("deny")) {
                            invHandler.reactionHandler(p, args[0].toString());
                        }
                        if (args[0].equalsIgnoreCase("help")) {
                            p.sendMessage(ChatColor.GREEN
                                    + "**************************************");
                            p.sendMessage(ChatColor.DARK_GRAY
                                    + "Trader Commands and its function");
                            p.sendMessage(ChatColor.RED + "/trader invite "
                                    + ChatColor.WHITE + "Invite someone to a trade");
                            p.sendMessage(ChatColor.RED + "/trader accept "
                                    + ChatColor.WHITE + "Accept an invitation");
                            p.sendMessage(ChatColor.RED + "/trader deny "
                                    + ChatColor.WHITE + "Deny an invitation");
                            p.sendMessage(ChatColor.RED + "/trader help "
                                    + ChatColor.WHITE
                                    + "Get help about this plugin");
                            p.sendMessage(ChatColor.GREEN
                                    + "**************************************");
                        }
                    }
                }
    
            } else {
                sender.sendMessage("This command is for players only");
            }
    
            return true;
        }
    
    }
    
    I know it has something to do with passing a null value, but i cant see why.
    Does someone knows what to do?

    -Waylock
     
  2. Moved to Bukkit Alternates.
     
  3. Offline

    nverdier

    @waylock
    1. No need to log enable/disable messages; Bukkit already does this for you.
    2. Why are all of your fields public? Make them private. If you need to access them from other classes, use getters.
    3. Use cmd.getName() instead of cmd.getLabel()
    Now to the actual problem:
    Which is happening on this line:
    So. What on Earth could this problem be?!?!
     
  4. Offline

    waylock

    Wow, thank you so much. Im new at this so yeah.
     
  5. Offline

    nverdier

    @waylock No problem! And make sure you tag or quote me if you want to be sure I see your post!
     
Thread Status:
Not open for further replies.

Share This Page