Reload configuration from another class that doesn't extend JavaPlugin

Discussion in 'Spigot Plugin Development' started by Faraaz, Jun 2, 2017.

  1. Hi Everyone,

    Can someone give me some pseudocode or a direction on how I can call the #reloadConfig method from another class that doesn't extend JavaPlugin? I tried searching it up but the other thread didn't help at all.

    Thanks,
    Faraaz
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Plugin#reloadConfig() is just a shortcut for resetting the variable with YamlConfiguration#loadConfiguration(File).
     
    • Like Like x 1
  3. Thanks, quick and simple.
     
  4. Quick question, should I create a new Plugin object and use that instance to call a method? If so, what do I initialize Plugin with because I cannot instantiate it as it is abstract.
     
  5. In your onEnable method you can set a private static variable of your main class as 'this', then make a static method to return that (Make sure to set null that variable in onDisable). Then call that static method, and just reload the config from there.
     
    • Informative Informative x 1
  6. That's not how you're supposed to use static. It's a memory tool, not an access modifier.

    Pass the instance to the class where you want to reload the config, through f.e the constructor.
     
  7. Java 101: Dependency Injection.
    Code (Text):

    public class MyPlugin extends JavaPlugin {
        @Override
        public void onEnable() {
            new SomeOtherClass(this);
        }
    }
     
    Code (Text):

    public class SomeOtherClass {
        private MyPlugin plugin;

        public SomeOtherClass(MyPlugin plugin) {
            this.plugin = plugin;
        }

        public void someWeirdMethodThatDoesStuff() {
            plugin.reloadConfig();
        }
    }