Solved Main class as plugin to other class

Discussion in 'BungeeCord Plugin Development' started by Ardno, Jan 16, 2020.

  1. Hello,
    I'm using a BungeeCord for first day. How can I get the method
    Code (Java):
    Main plugin = (Main) JavaPlugin.getServer().getPlugin(Main.class)
    But for BungeeCord?

    Thanks for all answers.
    - Ardno
     
  2. Inject your plugin as a parameter in your constructor.
     
    • Agree Agree x 1
  3. That's good point, but the command(class) has already constructor
    Code (Java):
    public Token(String name) {
            super("token");
        }
    I don't know, when I change the constructor, the command will available.
    My idea:
    Code (Java):
    public Main plugin;
    public Token(String name, Main main) {
            super("token");
            plugin = main;
    }
    The start of class code:
    Code (Java):
    public class Token extends Command {
        public Token(String name) {
            super("token");
        }

        @Override
        public void execute(CommandSender sender, String[] args) {
            ...some code here...
        }
    Can you please give me a example or in more detail description? @ysl3000
    Thank.
     
  4. Yes, you can change the constructor like you want.

    For example you can simply leave out the unused name parameter of your constructor:
    Code (Java):
    public Main plugin;
    public Token(Main main) {
            super("token");
            plugin = main;
    }
     
    • Winner Winner x 1
  5. It does works.
    Thank you.