Error I can't find, Bukkit won't load plugin

Discussion in 'Plugin Development' started by haydenaa, Dec 26, 2014.

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

    1Rogue

    You should not make a new instance of JavaPlugin yourself.
     
  2. Offline

    haydenaa

    @1Rogue I'm so confused.... i've went through like 20 posts on here without getting a straight answer. How do I access the JavaPlugin methods from a different class besides my main???
     
  3. Offline

    teej107

  4. Offline

    1Rogue

    mythbusterma and teej107 like this.
  5. Offline

    haydenaa

    @teej107 @1Rogue I've looked through a ton of posts on both links lol- I saw in one of Rogue's posts where it said that
    it is good to use static methods but not variables, but if I try to do something such as
    Code:
    private Main plugin;
    
    and then do something else such as
    Code:
    public Main getMain() {
         return plugin;
    }
    
    it will, first of all, tell me that I have to make plugin static, and then second of all, plugin will return null.
    If I do something such as
    Code:
    public Main getMain() {
          return (Main) Bukkit.getPluginManager().getPlugin("Republics");
    }
    
    does it return the JavaPlugin methods and values?

    *Edit*
    Oop, discovered another post with some methods, will post them on here:
    Code:
    public class Test() {
    private static Test instance;
    private Test() {}
    public static Test getInstance() {
    if(instance == null) {
    instance = new Test();
    }
    return instance;
    }
    public static void foo() {
    System.out.println("FOO!");
    }
    }
    public class Test2() {
    private Test test = Test.getInstance();
    public void getFoo() {
    return test.foo();
    }
    }
    
    Would this work? It uses static but it looks like it would.
    Then maybe nullify the statics in the onDisable? Not sure.
     
    Last edited: Dec 29, 2014
  6. Offline

    mythbusterma

    @haydenaa

    That design (anti-)pattern is called a "Singleton," and it works and is well established. It does, however, create very tight coupling, not something I would guess you're too worried about. Also, the code you posted above that has the "private Main getMain()" is the most useless line of code in existence. You require an instance to invoke that method, that only returns the instance, it has no usage.
     
  7. Offline

    haydenaa

    @mythbusterma K, so it would be ok to use the singleton? It wouldn't impact Bukkit?
     
  8. Offline

    mythbusterma

    @haydenaa

    Yes, it's okay to use. It won't impact Bukkit.
     
  9. Offline

    haydenaa

    Ok thanks, will try that out!
     
  10. Offline

    1Rogue

    Or, in a world with constructors:

    Code:java
    1. public class Example {
    2.  
    3. private final MyProject project;
    4.  
    5. public Example(MyProject project) { //constructor
    6. this.project = project;
    7. }
    8.  
    9. }


    Code:java
    1. public class MyProject {
    2.  
    3. private Example example;
    4.  
    5. //in some method...
    6. this.example = new Example(this);
    7.  
    8. }
     
  11. Offline

    haydenaa

  12. Offline

    teej107

    @haydenaa Singleton seems easier because you don't need to pass though constructors. I prefer using constructors though.
     
  13. Offline

    haydenaa

    @teej107 @mythbusterma @1Rogue Ugh I tried the singleton way, still won't register my plugin in bukkit. Probably something to do with my main class. I'll post the singleton section.
    Code:
    private static Main plugin;
    private Main() {}
    public static Main getMethods() {
    if(plugin == null) {
    plugin = new Main();
    }
    return plugin;
    }
    
    And then to access from another class:
    Code:
    private Main plugin = Main.getMethods();
    
     
  14. Offline

    teej107

    @haydenaa
    1. Never, ever, for any reason, for any reason ever, instantiate the JavaPlugin extended class.
    2. I would stop trying to use singletons and learn OOP style programming.
    3. Doing this would be a good way:
      MyProject would be your JavaPlugin class. Notice how he doesn't create a new one.
     
    haydenaa and mine-care like this.
  15. Offline

    haydenaa

    @teej107 I saw the instantiation, I was a little iffy with it but went with it anyway. Now it makes sense. I'll try that constructor now, thanks.

    Also, instead of
    Code:
    private Example example;
    this.example = new Example(this);
    
    can you do
    Code:
    private Example example = new Example(this);
    
    @teej107 @mythbusterma @1Rogue Neither the constructors nor the singleton will work with bukkit, i'll post constructor methods below:
    Main
    Code:
    private SLAPI slapi1;
    
    private Ranks ranks1;
    
    private Republic republic1;
    
    public Main(SLAPI slapi) {
    
    this.slapi1 = slapi;
    
    }
    
    public Main(Ranks ranks) {
    
    this.ranks1 = ranks;
    
    }
    
    public Main(Republic republic) {
    
    this.republic1 = republic;
    
    }
    
    public Main() {}
    
    SLAPI
    Code:
    private Main plugin1;
    
    public SLAPI(Main plugin) {
    
    this.plugin1 = plugin;
    
    }
    
    Ranks
    Code:
    private Main plugin1;
    
    private SLAPI slapi;
    
    private Republic republic;
    
    private Listeners listeners;
    
    public Ranks(Main plugin) {
    
    this.plugin1 = plugin;
    
    }
    
    public Ranks(SLAPI slapi) {
    
    this.slapi = slapi;
    
    }
    
    public Ranks(Republic republic) {
    
    this.republic = republic;
    
    }
    
    public Ranks(Listeners listeners) {
    
    this.listeners = listeners;
    
    }
    
    Republic
    Code:
    private Main plugin1;
    
    private Ranks ranks1;
    
    private SLAPI slapi;
    
    public Republic(Main plugin) {
    
    this.plugin1 = plugin;
    
    }
    
    public Republic(Ranks ranks) {
    
    this.ranks1 = ranks;
    
    }
    
    
    public Republic(SLAPI slapi) {
    
    this.slapi = slapi;
    
    }
    
    Listeners
    Code:
    None
    
    And then for an example to access a class
    Code:
    private Main plugin = new Main(this);
    
    But again wouldn't that be a form of instantiation? That's why I did the
    Code:
    public Main() {}
    
    in my Main class.

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

    teej107

    @haydenaa
    Do you understand how constructors are used? I've already given too much information on here and I could have very well said "Learn Java" and left it at that. You can pass multiple objects through parameters. Do that!
     
  17. Offline

    mythbusterma

    @haydenaa

    Both methods work just fine with Bukkit. It is you that does not work well with Bukkit (or Java, for that matter).
     
    teej107 likes this.
  18. Offline

    haydenaa

    @teej107 @mythbusterma i've tried every single method I can think of. If I can't instantiate the JavaPlugin class, how can I get its methods then???? Maybe that's what this whole thread is about.
     
  19. Offline

    teej107

    Treat MyProject like your JavaPlugin class.
     
  20. Offline

    haydenaa

    @teej107 I was trying to, I needed classes to access the JavaPlugin methods so I was using stuff in my other classes like:
    Code:
    private static Main plugin = new Main(this);
    
    but obviously thats instantiation and i'm not supposed to do that for classes. I'm super confused about what to do if I can't use instantiation at all.
     
  21. Offline

    1Rogue


    @teej107 has been quoting my post repeatedly for a reason. You should probably give it a very close look.
     
Thread Status:
Not open for further replies.

Share This Page