Problems with onCommand();

Discussion in 'Plugin Development' started by Raz, Mar 20, 2011.

Thread Status:
Not open for further replies.
  1. PHP:
       public boolean onCommand(CommandSender senderCommand cmdString commandLabelString[] args){
            if(
    commandLabel.equalsIgnoreCase("BLA")){
                
    sender.sendMessage(ChatColor.RED "Bla..");
                return 
    true;
            }
            return 
    false;
        }
    Soo.. This is my onCommand. It works great when i run it like this.
    Altought whenever i run any other code It will give me an "internal error" ingame and

    org.bukkit.command.CommandException: Unhandled exception executing command 'bla'
    in plugin Bla v0.1 in console
    Anyone knows why?
     
  2. Offline

    Meta1203

    What bukkit and CraftBukkit build are you using? Also, did you register your commands in the plugin.yml file? Can you post your whole code? This will help us all give you more help...
     
  3. I'm using latest successfull build from ci.bukkit.org,
    I registered commands in plugin.yml
    this is full code

    Code:
    package CakeModRaz.CakeModTP;
    
    import java.util.HashMap;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Server;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.craftbukkit.entity.CraftPlayer;
    /**
     * CakeModTP for Bukkit
     *
     * @author CakeModRaz
     */
    public class CakeModTP extends JavaPlugin {
        private final CakeModTPPlayerListener playerListener = new CakeModTPPlayerListener(this);
        private final CakeModTPBlockListener blockListener = new CakeModTPBlockListener(this);
        private final HashMap<Player, Boolean> debugees = new HashMap<Player, Boolean>();
     public void onEnable() {
    
            PluginManager pm = getServer().getPluginManager();
            PluginDescriptionFile pdfFile = this.getDescription();
            System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
        }
        public void onDisable() {
             System.out.println("CakeMod TP Disabled!");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("BLA")){
    
    <-- If i add any other code here, like getServer().getPlayer() etc and i will get the error -->
                sender.sendMessage(ChatColor.RED + "Bla..");
                return true;
            }
            return false;
        }
        public boolean isDebugging(final Player player) {
            if (debugees.containsKey(player)) {
                return debugees.get(player);
            } else {
                return false;
            }
        }
    
        public void setDebugging(final Player player, final boolean value) {
            debugees.put(player, value);
        }
    }
    
     
  4. Offline

    Drakia


    That error you're getting is just a portion of what is actually output, and tells us nothing of the actual cause of the problem. Most likely your code is causing an NPE (getServer().getPlayer("NonExistant").getHealth() or something to that effect)
     
  5. What i've tryed is:
    Code:
    String senders = sender.toString();
                Player player = getServer().getPlayer(senders);
    And
    Player victim = getServer().getPlayer(args[0]);

    Is this the cause of my error?
     
  6. Offline

    Drakia

    First one is a most definite yes, what you want to do is:
    Code:
    if (!(sender instanceof Player)) return false;
    Player player = (Player)sender;
    And the second one is a good chance of causing an NPE if you don't check to make sure args.length > 0
     
Thread Status:
Not open for further replies.

Share This Page