Is there any method to extract code out of the class?

Discussion in 'Plugin Development' started by lecreep, Dec 5, 2019.

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

    lecreep

    I`m doing wrong
    Code:
    public class MainActivity extends JavaPlugin implements Listener{
        private Extras extras=new Extras();
     
        @Override
        public void onEnable() {
            extras.echo("Hello");
        }
    }
    
    Code:
    public class Extras extends JavaPlugin  {
        public void echo(Object obj) {
            getLogger().info(String.valueOf(obj));
        }
    }
    
    Got a message that says:
     
  2. Offline

    timtower Administrator Administrator Moderator

    @lecreep Change Object to String.
    Don't have multiple classes extending JavaPlugin in the same plugin.
     
  3. Offline

    lecreep

    @timtower
    thats the question, if i don't extend java plugin i can't acces to getlogger from Extras

    echo works great if is in the MainActivity class
     
  4. Offline

    timtower Administrator Administrator Moderator

    That is why you can use getters for the main class to get the logger through there.
    The error that you added late is due to the fact that you have multiple classes extending JavaPlugin.
     
  5. Offline

    Strahan

    Another option is to pass an instance of the main class to the classes that need it. i.e.
    Code:
    public class MyClassName extends JavaPlugin {
      public void onEnable() {
        getServer().getPluginManager().registerEvents(new EventClass(this), this);
        getCommand("somecommand").setExecutor(new CommandClass(this));
      }
    
    public class EventClass implements Listener {
      MyClassName plugin;
    
      public EventClass(MyClassName plugin) {
        this.plugin = plugin;
      }
     
  6. Offline

    lecreep

    @Strahan I feel fool after your answer, I was making the easy dificult

    @timtower I am sorry to tell you that I did not understand you, for now...

    Thanks...
     
Thread Status:
Not open for further replies.

Share This Page