Getting inventories from other classes

Discussion in 'Spigot Plugin Development' started by _Wiildanimal_, May 21, 2016.

  1. I have an inventory in my main class, and I want to get it from my listener class. The problem is I can't make inventories static, so I don't know what to do.
    I also have an instance of the main class in the listener.
  2. Code (Java):
    // Main Class
    public MainClass extends JavaPlugin {

       // register the listener with this
      Bukkit.getServer().getPluginManager().registerEvents(new ListenerClass(this), this);

    // The ListenerClass
    public ListenerClass implements Listener {
      private MainClass plugin;

       public ListenerClass(MainClass instance) {
        this.plugin = instance;

    // then you can freely use plugin.something();
  3. Why not?
  4. Stop spoon feeding please.
    • Agree Agree x 1
  5. sorry it won't happen again but I don't have any idea how to express in words.
  6. If you don't understand what's going on, here is some more information on constructors
  7. don't use it if you don't know how that all codes work.

    here are some tip I'll try my best to express them

    so for example the MainClass is our main class registered on plugin.yml

    then at your listener class create an object of main class called anything you want with this.

    Code (Java):
    MainClass plugin;
    at your main class when you are registering your event you have to create a new class with the this word between parenthesis something like

    Code (Java):
    new ListenerClass(this);
    then set the value of this at the listener class

    public ListenerClass(MainClass value) {
    then set the object that you've created to the value of the mainclass something like
    Code (Java):
    this.plugin = value;

    then as I said you could freely use plugin.something();

    prefer more to @ES359 given link.
  8. You must pass references to the class instance that contains the field(s) you wish to access. Unfortunately, static plugin instances are being abused far to frequently. If you wish to keep your code within standard OOP syntax you have to avoid abusing static. Here's an example:

    Main Class
    Code (Text):

    public class MyPluginClass extends JavaPlugin
         private String myStringObject = "foobar"; // this is the object you wish to access in another class

         public void onEnable()
              // here is where we need to give our listener the instance reference
              // please note that within the constructor of the Listener class we are actually passing in the reference to this class
              // this is the class that contains the object we wish to share to the listener class
              this.getServer().getPluginManager().registerEvents(new MyListenerClass(this), this);

         public String getMyStringObject() // we use a public getter function to retrieve the reference
              return this.myStringObject; // return the reference using the explicit "this"
    Listener Class
    Code (Text):

    public class MyListenerClass implements Listener
         private MyPluginClass myPluginClass; // allows this class to store a reference to the instance too

         public MyListenerClass(MyPluginClass myPluginClass) // here is where we actually pass the reference to the class that contains the field needed
                 this.myPluginClass = myPluginClass; // now we set this class's reference to match the one passed through the constructor

         public void onExampleEvent(PlayerJoinEvent event)
               // finally when the player joins we send them our string object using the references we defined above.
               event.getPlayer().sendMessage("Hello player, here is my object: " + this.myPluginClass.getMyStringObject());
    The syntax used above is far more acceptable because we are using pointers (or references) to give access to fields we require, maintaining the use of objects rather than static fields.