Plugin without JavaPlugin

Discussion in 'Plugin Development' started by KTB, May 16, 2015.

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

    KTB

    Hello,
    I am deveolping a plugin (or better call it a framework) and I don't want to extend JavaPlugin to my main class (or to any other class ;) )
    My problem is that my framework is something really different and calls like "this.getDatabase()" shouldn't be allowed (because I want to write my own methods and users may get confused which methods are mine and which are Bukkit's)

    So my question is : Is there a way to initialise a plugin without extending JavaPlugin or (if this not work) is there a way to extend JavaPlugin but not extend most of the useless methods (for example the onEnable-Method is useful with the getDatabase() isn't)

    Thanks :)
     
  2. You must have your main class extending JavaPlugin. But you can make your "Framework class" containing and accessing to the methods you want it to have (from the main class)
     
  3. Offline

    bohafr

    Yeah, @FisheyLP, but also there is way to create instance in main class..

    private static NameOfMainClass instance;
    public static getInstance(){return instance;}
    And in onEnable instance = this;

    In other classes you can do NameOfMainClass.getInstance().getDatabase()..
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Just create a non static method that returns the instance.
     
  6. Offline

    Gater12

    Konato_K and nverdier like this.
  7. Offline

    1Rogue

    Code:java
    1. public class MyClass {
    2.  
    3. public MyClass getInstance() {
    4. return this;
    5. }
    6.  
    7. }

    Code:java
    1. MyClass c = new MyClass();
    2. c.getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().getInstance().toString();
    3. c.toString();
     
    Totom3 likes this.
  8. other classes and not abusing static.
     
  9. Offline

    Gater12

    @TheGamesHawk2001
    It would not make sense to have a getInstance method which returns the main class instance, if you need the main class instance to invoke the method.
     
  10. Offline

    nverdier

    That's not abusing static.
     
  11. Offline

    mythbusterma

    Please, explain to me how creating a Singleton is abusing static? It's a perfectly valid use of static, although it's not particularly good OOP.

    Also, see 1Rogue's post.
     
    Totom3 and nverdier like this.
  12. Offline

    Konato_K

    @Gater12 @1Rogue
    Code:
    public void passInstance(SomeObject s)
    { passInstance(s.getInstance());
    }
    That's the point :D
     
  13. Offline

    teej107

    Forgot to nullify the field?
     
  14. Offline

    mythbusterma

    @teej107

    Why would you? As far as I know, the only time the Singleton is out of scope is when the class is no longer needed, at which point the JVM destroys the entire class, including its static fields.

    If the server reloads, the variable is reassigned in onEnable()
     
  15. Offline

    teej107

    Idk. I never used the singleton way for accessing the JavaPlugin. I've only heard nasty things for not nullifying the JavaPlugin field but you do make a good point. Maybe I got mixed up with static Player Collections which is terrible as well.
     
    mythbusterma likes this.
  16. Code:
    public class Main extends JavaPlugin {
    
    public static Main instance;
    private Framework f;
    
    
    public void onEnable() {
    instance = this;
    f = new Framework();
    f.onEnable();
    }
    
    public void onDisable() {
    instance = null;
    f.onDisable();
    }
    }
    Code:
    public class Framework {
    
    private Main m = Main.instance;
    
    public void onEnable() {
    }
    public void onDisable() {
    }
    public FileConfiguration getConfig() {
    return m.getConfig();
    }
    public void doStuff() {
    m.doStuff();
    }
    
    
    }
     
  17. Offline

    1Rogue

    Code:java
    1. Main.instance = null;


    Whoopsies
     
    Konato_K likes this.
  18. Considering Java is an OOP basic programming language, I tend to stick following those conventions.
     
  19. Offline

    mythbusterma

    @TheGamesHawk2001

    There is a good case to be made for using Singletons, especially when it starts becoming unwieldy to pass references around that much.
     
  20. A singleton that then provides the use of non static methods is alright i think.
     
  21. Who will do that ? And if somebody download your plugin and do that it aint your fault, now kill me.
     
  22. Offline

    1Rogue

    Sometimes it's not as obvious
     
Thread Status:
Not open for further replies.

Share This Page