Referring to main class

Discussion in 'Plugin Development' started by TheDiamondGuy, Oct 9, 2014.

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

    TheDiamondGuy

    Hello all,

    I'm coding a ChestGUI, and am doing some work in another class. The issue I'm having is that I cannot figure out how to refer to the main class. That about it:p

    Thanks:)
     
  2. Offline

    Aephout14

    The Main class is the one that extends JavaPlugin
     
  3. Offline

    GrimReaper52498


    I think he knows that, he wants to know how to call a method from the Main class(I think)

    Anyways, TheDiamondGuy You need to make a constructor


    Something like:

    Code:java
    1. public Main pl;
    2.  
    3. public ClassName(Main pl){
    4.  
    5. this.pl = pl;
    6.  
    7. }


    then just use pl whenever you need to use a method in the main class
     
  4. Offline

    CaptainUniverse

    if you want to call a method from the main class do <classname>.<method> and as for functions that require a plugin argument I preffer using Bukkit.getPluginManager().getPlugin(<yourplugin>) that going through constructors and stuff
     
    Code0 likes this.
  5. Offline

    rbrick

    CaptainUniverse That only works if the methods are static...please learn java before commenting. kthxbai
     
  6. Offline

    mythbusterma

    CaptainUniverse rbrick

    Although I don't think he knows what it's called, I think he meant static calls. Also, instead of doing that, simply pass an instance of your plugin into the constructor as GrimReaper52498 suggested, instead of doing the expensive, non-static call that CaptainUniverse suggested in err.
     
  7. Offline

    TheDiamondGuy

    So I've got 1 class with the main chest inventory, and I'm doing all the subs ones in another inventory. In the main class, I use this code
    Code:
                            e.getWhoClicked().openInventory(effectMenu);
    However, as the effectMenu is located in a diffrent class, it dosen't know what it is, so I need away to let the MAIN class refer to the submenu class, sorry If I was unclear:p


    @Mythbusterma
     
  8. Offline

    teej107

    TheDiamondGuy You created the submenu object and assigned it to a variable in the MAIN right?
     
  9. Offline

    TheDiamondGuy

    As far as I'm aware no. I think I'll need a bit of spoon-feeding here, as I've never done referring before. To be completelystraight, I have no clue what you mean:p
     
  10. Offline

    teej107

    Have you heard of and know what getters are?
     
  11. Offline

    TheDiamondGuy

  12. Offline

    teej107

    TheDiamondGuy
    Example:
    Code:java
    1. public class A
    2. {
    3. private B someObject;
    4.  
    5. public A()
    6. {
    7. someObject = new B(this); //Pass A to B through the parameters
    8. //You can access public method/fields/etc in B.
    9. }
    10. }

    Code:java
    1. public class B
    2. {
    3. private A otherObject;
    4.  
    5. public B(A objectYouPassThrough) //Must have it so the constructor of B will accept an A object
    6. {
    7. otherObject = objectYouPassThrough;
    8. //You can now access any public method/field/etc in A
    9. }
    10. {

    Wrote the code on here so excuse any syntax errors.

    http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
    http://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work
     
    coasterman10 and TheDiamondGuy like this.
  13. Offline

    FabeGabeMC

    TheDiamondGuy
    You can save a private static variable of the plugin class, then getting it.
    *example:
    Code:java
    1. public class Example extends JavaPlugin {
    2.  
    3. private static Example instance;
    4.  
    5. //Make sure you initialize it.
    6. @Override
    7. public void onEnable() {
    8. instance = this;
    9. }
    10.  
    11. public static Example getInstance() {
    12. return instance;
    13. }
    14.  
    15. }

    Then you can use getInstance() to get the main class's non-static methods.
     
  14. Offline

    fireblast709

    FabeGabeMC or you don't. Please stop abusing static for lazy access.
     
  15. Offline

    aaomidi

    Please, go read why static is bad and you should avoid using it. Static is a recipe for disaster.
     
  16. Offline

    coasterman10

    FabeGabeMC is using static correctly to make a singleton, however usually with smaller Bukkit plugins there isn't really any reason to use it and dependency injection is much better. Also the instance should be nulled when the plugin disables.

    I personally always use dependency injection. I recommend that any beginner should avoid static as much as possible except when they know exactly why they are using it and why it's better than not using static.
     
    FabeGabeMC likes this.
  17. Offline

    teej107

    Using encapsulation improves readability. When you make a public static declarations, you throw all of the encapsulation away. I still prefer passing the plugin through parameters since (as well as the already mention reason) static fields should be immutable primitives/strings.
     
Thread Status:
Not open for further replies.

Share This Page