Keeping commands in the same package but not in main class?

Discussion in 'Plugin Development' started by Callum.K, Jan 1, 2014.

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

    Callum.K

    How do I make a class that referees to the main class that handles onCommand() ? I've tried searching but couldn't find what I was after.
     
  2. Offline

    nuno1212sss

    CommandExecutor xD
     
  3. Offline

    random_username

    For your command class:
    Code:java
    1. public class classnamehere implements CommandExecutor{
    2. //onCommand
    3. @Override
    4. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    5. if(cmd.getName().equalsIgnoreCase("commandname")){
    6. //code here
    7. }
    8. return false;
    9. }
    10. }


    And in your Main class:
    Code:java
    1. public class Main extends JavaPlugin{
    2.  
    3. public void onEnable() {
    4. getCommand("commandname").setExecutor(new classnamehere());
    5. //other code
    6. }
    7.  
    8.  
    9. }

    Hope this helps :) p.s Sorry if I made a small typo, didn't write the code with an IDE :p
     
  4. Offline

    Callum.K

    It says string can not be converted to commandExecutor also if I wanted a player and block listener class how would I do that? And if I had multiple commands do I just add another getCommand()?
     
  5. Offline

    random_username

    Could you please paste your current code? :)
    And for a listener class, implement Listener, and to register the class in the onEnable try:
    Code:java
    1. public void onEnable() {
    2. PluginManager pm = Bukkit.getServer().getPluginManager();
    3. pm.registerEvents(new classname(), this);
    4. }

    Callum.K
    Whoops, made a mistake in my first post while registering the command in the onEnable, I just wrote it correctly. Feel free to check it again :)
     
  6. Offline

    BungeeTheCookie

    random_username
    That is absolutely the wrong way to register a command in the main class.
    It is
    PHP:
    getCommand("commandname").setExecutor(new ClassName());
     
  7. Offline

    random_username

    Read my edit above :p my bad
     
Thread Status:
Not open for further replies.

Share This Page