Solved help

Discussion in 'Spigot Plugin Development' started by M7MDMCGamer, Apr 24, 2017.

  1. i want each command in my plugin be in a class alone how ?
     
  2. Create class for each command.
    Now check for the executed command and call the class where you wrote its code.
     
  3. how to call the command class to the main class
     
  4. Lets say got got the classes Main, Cmd1 and Cmd2.
    Now you could do something like
    Code (Text):
    if(command.equals("command1"){
        new Cmd1();
    else if(command.equals("command2"){
        new cmd2();
    }
    //Edit this is a code snippet of one of mine Plugins, where i do nearly the same:
    Code (Text):
    public boolean onCommand(CommandSender sender, Command arg1, String arg2, String[] args) {
            if(arg2.toLowerCase().equals("pr") || arg2.toLowerCase().equals("playtimerewards")){
                return cmdPr.onCommand(sender, arg1, arg2, args);
            }else if(arg2.toLowerCase().equals("pt") || arg2.toLowerCase().equals("playtime")){
                return cmdPt.onCommand(sender, arg1, arg2, args);
            }else{
                return false;
            }
        }
     
    #4 ExoBiTe, Apr 24, 2017
    Last edited: Apr 24, 2017
  5. ??

    Create a new class which implements CommandExecutor. Your IDE should now give you an option to implement unimplemented methods (will be the onCommand(...). In here you would type what you'd normally do, but don't check for the command's name. In your onEnable or on your likings, register the command by using JavaPlugin#getCommand(String cmd), the parameter would be your command's name (the one specified in your plugin.yml. This would return a PluginCommand which you would have to PluginCommand#setExecutor(CommandExecutor exec). The parameter would then be an instance of the new class you made, which implements CommandExecutor. You should now be all set. Do this for each command.

    OR... you could always just create your own command system, if your plugin is large and this is too much for every command (sub command etc).

    EDIT: Punctuation.

    EDIT Example:
    My command class implementing CommandExecutor
    Code (Text):
    public class MyCmd implements CommandExecutor {

      @Override
      public boolean onCommand(CommandSender sender, Command command, String arg, String[] args) {
        Player p = (Player) sender;
        p.sendMessage("Woho! Command MyCmd works");
      }
    }
    Main class extending JavaPlugin
    Code (Text):
    public class MyPlugin extends JavaPlugin {

        @Override
        public void onEnable() {
          // here i will register my command
          // "mycmd" is what is written in the plugin.yml:
          // commands:
          //   mycmd:
          //     description: My command!!
          getCommand("mycmd").setExecutor(new MyCmd());
        }
    }
    Might wanna have a look at Java Generics for N00bs.
     
    #5 ExpDev, Apr 24, 2017
    Last edited: Apr 24, 2017
    • Agree Agree x 1
  6. This is what i described a post before as an example.
     
  7. Yeh it's just that this
    doesn't make any sense.
     
  8. I think you just don´t understand me, as this is a method i am using in my plugins. May be my bad english, maybe you understand what i mean if you take a look at my example code above.
     
  9. I see now. Don't think you have to implement CommandExecutor for your classes for that. You basically just call a method. However, looks inefficient to me, and can't see how that works out for you in the long run.
     
    • Agree Agree x 1
  10. I use it in my Main class, so i dont need any CommandExecutors. Yes i am basically calling a method, thats what i was trying to tell him/you.
    I call commands this way, as it was easier to code a system which checks other loaded plugins for similair commands, so my Plugin will try to let the other plugin execute the command (it has a similiar effect like the function from essentials).
     
    • Optimistic Optimistic x 2
  11. Okay There is
    first you have to do implements CommandExecutor
    after you did the command go to main > onEnable and There write "Bukkit.getServer().getPluginManager().registerEvents(new <ClassName>(), this);"