Configs with Multiple classs

Discussion in 'Plugin Development' started by B3N909, Aug 22, 2014.

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

    B3N909

    My question is how can I use getConfig(); from not a main class? Thanks
     
  2. Offline

    TGRHavoc

    B3N909
    Create an instance of the main plugin that is static or, pass the plugin through to the class in the constructor then do "Main.getConfig()"
     
  3. Offline

    Sagacious_Zed Bukkit Docs

    TGRHavoc You cannot create a new instance of JavaPlugin, in fact the plugin loader will now forbid you from doing so.

    B3N909 You invoke the method an a variable that points to your java plugin
     
  4. Offline

    B3N909

    Sagacious_Zed could you show a small example of doing just that?
     
  5. Offline

    TGRHavoc

    Sagacious_Zed
    Code:
    public static MainClass mainClass;
     
    public void onEnable(){
    mainClass = this;
    }
    So this isn't instancing the main class?
     
    Xyplo likes this.
  6. Offline

    Sagacious_Zed Bukkit Docs

    Dragonphase and TGRHavoc like this.
  7. Offline

    B3N909

    Sagacious_Zed Sagacious_Zed
    Could anyone show me a example of that?
     
  8. Offline

    aredherring

    Create an instance of the class that needs your config from your plugin and pass it the config in the constructor

    Code:java
    1. public class MyClassThatNeedsConfiguration {
    2. private final Configuration _configuration;
    3.  
    4. public MyClassThatNeedsConfiguration(Configuration configuration) {
    5. _configuration = configuration;
    6. }
    7. }
    8.  
    9.  
    10. public class MyPlugin : JavaPlugin {
    11. public MyPlugin() {
    12. MyClassThatNeedsConfiguration myClass = new MyClassThatNeedsConfiguration(this.getConfig());
    13. }
    14. }


    If you don't need to mutate the settings you could go one further and inject only the settings you need from the configuration into the class instead which would be a better practice.

    Yes, you are leaking a reference to the instance of the Plugin outside of the plugin via a static field. Not only is this not safe but it is also violating encapsulation. This is really bad code and you should never use that approach.

    You should use the dependency injection approach I listed above, and you should constrain the dependencies you inject the most you can. So in this case, if you need the Configuration of the plugin, you inject the Configuration of the plugin, rather than the plugin itself.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  9. Offline

    B3N909

    aredherring thanks for the help but I am stuck a little.
    Main Class:
    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2.  
    3. ConfigClass myClass = ConfigClass(this.getConfig());
    4.  
    5. //On enable, on disable and other junk goes down here
    6.  
    7.  


    ConfigClass (It needs getConfig()):
    Code:java
    1. public class ConfigClass {
    2.  
    3. private final Configuration _configuration;
    4.  
    5. public ConfigClass(Configuration configuration) {
    6. _configuration = configuration;
    7. }
    8. //Rest of code goes here
    9. //Blah blah blah blah etc. etc.

    I am getting a error on the main class where I put the "ConfigClass myClass = ETC. ETC" Thanks
     
  10. Offline

    aredherring

    You need to use new to create new instances of a class. Sorry I omitted it.
     
  11. Both of these will work
    1:
    Code:
    public static Main main;
     
    public void onEnable(){
    main = this;
    }
    public void onDisable(){
    main = null;
    }
    //In second class
    Main.main.getConfig();
    
    Or in your second class
    Code:java
    1. private Main main;
    2. public ClassName(Main pl){
    3. pl = main;
    4. }
    5. //In second class
    6. main.getConfig();
     
  12. Offline

    aredherring

    The first one is awful, it should never be used.
    The second one is exactly what I just said but you are making it so the class is coupled to the plugin which is unnecessary.
     
Thread Status:
Not open for further replies.

Share This Page