Command Errors

Discussion in 'Plugin Development' started by KeybordPiano459, Jul 3, 2012.

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

    KeybordPiano459

    So I'm developing my first plugin and I went to test it and it always returns with 'An internal error occurred while attempting to perform this command'. When I try this in console, it's supposed to say 'This command can only be run by a player', but instead it says:
    Code:
    17:50:20 [WARNING] Unexpected exception while parsing console command
    org.bukkit.command.CommandException: Unhandled exception executing command 'warn' in plugin ECommands v1.1.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479)
        at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.java:475)
        at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:612)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:581)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
        at com.github.KeybordPiano459.ECommands.ecommands.onCommand(ecommands.java:16)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 7 more
    This is my main code:
    Code:
    package com.github.KeybordPiano459.ECommands;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class ecommands extends JavaPlugin {
     
        public void onEnable() {
            getLogger().info("ECommands 1.1.1 has been enabled!");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(cmd.getName().equalsIgnoreCase("warn")) {
            Player s = (Player)sender;
            Player target = s.getServer().getPlayer(args[0]);
            target.setFireTicks(10000);
            }
         
            if (cmd.getName().equalsIgnoreCase("warn")) {
                return true;
            } else if (cmd.getName().equalsIgnoreCase("warn2")) {
                Player player = (Player) sender;
                if (player == null) {
                    sender.sendMessage("This command can only be run by a player");
                } else {
            }
            return true;
        }
        return false;
    }
     
        public boolean onCommand2(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(cmd.getName().equalsIgnoreCase("warnagain")) {
            Player s = (Player)sender;
            Player target = s.getServer().getPlayer(args[0]);
            target.setFireTicks(10000);
            }
         
            if (cmd.getName().equalsIgnoreCase("warnagain")) {
                return true;
            } else if (cmd.getName().equalsIgnoreCase("warnagain2")) {
                Player player = (Player) sender;
                if (player == null) {
                    sender.sendMessage("This command can only be run by a player");
                } else {
            }
            return true;
        }
        return false;
    }
         
        public void onDisable() {
            getLogger().info("ECommands 1.1.1 has been disabled.");
        }
     
    }
    The strange thing is that /warnagain works, but I get errors only when using /warn. Can someone help me fix this? I am going to be using this for my server and I want it to be done :) The last thing I would like to address is that when I type warnagain in console, it says the usage that I put into my plugin.yml, but it's supposed to say 'This command can only be run by a player'.
     
  2. Offline

    krinsdeath

    Hello KeyboardPiano459 - you've got a very simple problem.

    You're casting your sender object to Player without checking whether it's a Player. Furthermore (in your 'warnagain2' conditional), you don't even need to do it. The CommandSender object has a sendMessage() method defined in its interface.
     
  3. Offline

    KeybordPiano459

    Can you show me an example of what this would look like and where I would put it? I'm not completely sure I understand you. I need to learn some more java, sorry.

    Ok I would also like to add, I just tested this with another player and it doesn't actually do anything. When I tested this with my friend, it just displayed the usage that I put in the plugin.yml. Please help, this is serious.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  4. Offline

    krinsdeath

    Note: this is not tested at all, however it should work. Basically, you're doing a lot of superfluous calculations and not enough error checking.

    Code:
    package com.github.KeybordPiano459.ECommands;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class ecommands extends JavaPlugin {
     
        public void onEnable() {
            getLogger().info("ECommands 1.1.1 has been enabled!");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(cmd.getName().equalsIgnoreCase("warn")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("This command must be executed by a player.");
                    return true;
                }
                try {
                    Player player = getServer().getPlayer(args[0]);
                    player.setFireTicks(10000);
                    player.sendMessage("You have been warned.");
                    sender.sendMessage("You set " + player.getName() + " on fire.");
                } catch (ArrayIndexOutOfBoundsException e) {
                    sender.sendMessage("Invalid number of arguments.");
                } catch (NullPointerException e) {
                    sender.sendMessage("No such player found.");
                }
                return true;
            } else if (cmd.getName().equalsIgnoreCase("warn2")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("This command must be executed by a player."); // why? it does nothing
                }
                return true;
            }
            return false;
        }
     
        public boolean onCommand2(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(cmd.getName().equalsIgnoreCase("warnagain")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("This command must be executed by a player.");
                    return true;
                }
                try {
                    Player player = getServer().getPlayer(args[0]);
                    player.setFireTicks(10000);
                    player.sendMessage("You have been warned.");
                    sender.sendMessage("You set " + player.getName() + " on fire.");
                } catch (ArrayIndexOutOfBoundsException e) {
                    sender.sendMessage("Invalid number of arguments.");
                } catch (NullPointerException e) {
                    sender.sendMessage("No such player found.");
                }
                return true;
            } else if (cmd.getName().equalsIgnoreCase("warnagain2")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("This command must be executed by a player."); // why? it does nothing
                }
                return true;
            }
            return false;
        }
       
        public void onDisable() {
            getLogger().info("ECommands 1.1.1 has been disabled.");
        }
     
    }
     
  5. Offline

    KeybordPiano459

    Thanks so much for the help. Unfortunately, some features were not included in this code, and I'm not saying it was your fault, you chose to help me. :) I'm officially making my plugin a WIP even though it will never be released :p
     
Thread Status:
Not open for further replies.

Share This Page