About the getCommand

Discussion in 'Plugin Development' started by skawke, Aug 6, 2011.

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

    skawke

    Hey guys, I'm quite new to java....(came over from python). I'm not exactly sure what this does:

    getCommand("command").setExecutor(this);

    I know you need that to register a command, but I tried registering 2 different commands (/command and /command1)

    getCommand("command").setExecutor(this);
    getCommand("command1").setExecutor(this);

    All it does is give me a nullpointer exeception when the server starts up.

    Help please? :D
     
  2. Offline

    stelar7

    post your code for additional help...

    in my main file (onEnable()) i put this:
    Code:
    getCommand("loc").setExecutor(new CDEX(this));
    
    in my CDEX file i put this:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd,
    String commandLabel, String[] args) {
    if (commandLabel.equalsIgnoreCase("loc")){
    if (!sender.isOp()) {
    Player player = (Player) sender;
    
    Location Loc = player.getLocation();
    
    player.sendMessage(ChatColor.GREEN + "coordinates :" + ChatColor.WHITE
    + " X " + Loc.getX());
    
    player.sendMessage(ChatColor.GREEN + "coordinates :" + ChatColor.WHITE
    + " Y " + Loc.getY());
    
    player.sendMessage(ChatColor.GREEN + "coordinates :" + ChatColor.WHITE
    + " Z " + Loc.getZ());
    
    return true;
    }
    
    ingame this returns:

    coordinates : 12 X
    coordinates : 34 Y
    coordinates : 64 Z


    if you have the command in your main file you dont need this:
    Code:
    getCommand("loc").setExecutor(new CDEX(this));
    
     
  3. Offline

    Bone008

    Every command that you register in your plugin.yml has a CommandExecutor assigned. That thing is adressed when the command is performed (via its onCommand-method).
    By default, every command will have your main plugin class as the CommandExecutor, which means setExecutor(this) is pretty pointless in your main class.

    It's useful to set the executor to some other class to keep your main class tight.

    And you probably get your NPE because you registered the command(s) not at all or not correctly in your plugin.yml.
    Post it, and we can help you better ;)
     
Thread Status:
Not open for further replies.

Share This Page