How to use this.saveConfig(); in a class besides the one that extends JavaPlugin?

Discussion in 'Plugin Development' started by hankered, Jun 29, 2014.

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

    hankered

    How to use this.saveconfig in a separate class other then the one that extends javaplugin?
     
  2. Offline

    ZodiacTheories

    teej107 likes this.
  3. Offline

    GeorgeeeHD

    make a static variable of the actual plugin and a getter method for it in the class that extends JavaPlugin.

    so like private static Plugin plugin;

    then in the onEnable { plugin = this; }

    example of the getter:

    public static Plugin getPlugin() {
    return plugin;
    }

    then u can do MainClass.getPlugin().saveConfig();
     
  4. Offline

    hankered

  5. Offline

    ZodiacTheories

  6. Offline

    1Rogue

    [​IMG]

    https://forums.bukkit.org/threads/using-a-string-in-different-classes.184140/#post-1916182
    https://forums.bukkit.org/threads/getting-a-boolean-from-another-class.218713/#post-2168376
    https://forums.bukkit.org/threads/static-plugin-instance.197107/#post-2015647
    https://forums.bukkit.org/threads/static-plugin-instance.197107/#post-2015952
    https://forums.bukkit.org/threads/arraylists-not-working.245639/#post-2338265
    https://forums.bukkit.org/threads/code-help-error-return.192943/#post-1982000
    https://forums.bukkit.org/threads/s...n-using-multiple-classes.205991/#post-2083446
    https://forums.bukkit.org/threads/config-file-null-pointer-exception.275624/#post-2545077
    https://forums.bukkit.org/threads/plugin-cannot-be-null.232383/#post-2255775
    http://forums.bukkit.org/threads/plugin-null.283589/#post-2603712
    http://forums.bukkit.org/threads/plugin-null.283589/#post-2603765
    http://forums.bukkit.org/threads/nullpointerexception-caused-by-economyresponse.283301/#post-2606124
    http://forums.bukkit.org/threads/plugin-cannot-be-null.281536/#post-2589269
    http://forums.bukkit.org/threads/checking-arraylist-in-another-class-help.279188/#post-2572942
    https://forums.bukkit.org/threads/repeating-scheduler-in-another-class.192931/#post-1982574
    https://forums.bukkit.org/threads/bukkit-runnables-not-working.199479/#post-2034260
    https://forums.bukkit.org/threads/cant-get-stuff-from-main-to-another-class.230103/#post-2242088
    (about 50 results omitted)

    You don't need to use static, you shouldn't use static, and passing via constructor is the way it should be done.

    You shouldn't be constructing a new instance of your main class, you will get errors.

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

    mythbusterma

    I'd argue a case for the use of singleton design strategy, as discussed in the third link on the list.
     
    Konato_K likes this.
  8. Offline

    1Rogue


    Depending on the type of singleton, yes it can be appropriate. But the whole of your project should not be singletons. It creates coupling and makes your code unmanageable and unmodularized.

    There's a reason why in most standards software engineering considers singletons an antipattern
     
  9. Offline

    hankered

    1Rogue

    i'm dumb. Then what should I use?

    I've been doing:
    Code:java
    1.  
    2. Main m = new Main();
    3. FileConfiguration config = m.getConfig();
    4.  


    and it's been giving errors.

    "File is null" errors. Is it because I'm constructing a new instance of the main class?

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

    xTigerRebornx

    hankered Don't construct a new instance of the main class. Look at dependency injection. Or use the many methods Bukkit provides for accessing your main class
    JavaPlugin#getPlugin()
    Bukkit.getPluginManager()#getPlugin()
     
  11. Offline

    teej107

    hankered You should use the parameters in the constructors or methods.
     
  12. Offline

    1Rogue


    Yes that is why you are getting errors. You can probably click just about any one of the links in my first post to find an answer.

    http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
    It involves using java's constructors to pass a reference of the instance of your main class

    Code:java
    1. public class Example extends JavaPlugin {
    2.  
    3. private AnotherClass another;
    4.  
    5. public void onEnable() {
    6. this.another = new AnotherClass(this);
    7. this.another.doSomething();
    8. }
    9.  
    10. public void phoneHome() {
    11. this.getLogger().log(Level.INFO, "Hello world!");
    12. }
    13.  
    14. }

    Code:java
    1. public class AnotherClass {
    2.  
    3. private final Example project;
    4.  
    5. public AnotherClass(Example project) {
    6. this.project = project;
    7. }
    8.  
    9. public void doSomething() {
    10. this.project.phoneHome();
    11. }
    12.  
    13. }


    Try running something like that, and see if you can pick up on how it works.
     
    xTigerRebornx likes this.
  13. Offline

    hankered

    1Rogue

    Code:java
    1. Main plugin;
    2. public InteractListener (Main plugin) {
    3. this.plugin = plugin;
    4. }
    5. }


    So now I can use plugin.getConfig(); ?
     
  14. Offline

    1Rogue


    Yup! Though I would suggest adding "private final" modifiers to your field.
     
    mythbusterma likes this.
  15. Offline

    hankered

    1Rogue

    Alright thanks dude you're the best. I'll try it out now.
     
Thread Status:
Not open for further replies.

Share This Page