Solved Does plugin manager register listener from static methods?

Discussion in 'Spigot Plugin Development' started by Msql, Nov 29, 2018.

  1. Does plugin manager register listeners from static methods?
    I am asking because I want to create static listener for my GUI objects map for my GUI-API source code that I am developing. It is not plugin but library so I need to do it this way.
    I need this listener to be static because I do not want to register listener everytime when my class will be initialized and force user of this API to unload it after.
    This one static listener will do checks in static map and do calls for abstract methods defined by user.
    Thanks for any answer :p
  2. drives_a_ford


    Take a look at the method used to register an event listener:
    You will need to register an instance of Listener.

    Even if it is a library, there is nothing that stops you from using a proper Listener instance.
    • Agree Agree x 1
  3. Thank you for reply!
    So it is only possible to register listener from instance? It is impossible to set it to listen to static method from class point instead from instance? I need it to be registered once because object will be initialized a lot of times. There is no way to add call to the list for static method in my class?
  4. drives_a_ford


    I don't see the problem.
    Just create a new class that implements Listener and use its constructor to create an instance.

    I know some people have also created a new listener for each instance of their class where applicable (i.e MagicSpells) and then only deal with the events that involve that particular instance. I'm sure there is overhead in doing this, as to how big it is, I'm unable to tell you.
    #4 drives_a_ford, Nov 29, 2018
    Last edited: Nov 29, 2018
  5. Hue? Why would you initialize a class containing a listener many times? What are you trying to achieve? Im pretty sure there is a thinking mistake.
  6. Look at part of my code, it is hard for me to explain. English unfortunetly is not my main language and it is hard for me to illustrate this using just words.
    Code (Java):
    public class ChestGUI implements Listener {

       //It will initialize once on class load.
       //It will not be linked to object, but to class.
       public static Map<Inventory, ChestGUI> chestGUIMap = new HashMap<>();

       @EventHandler(priority= EventPriority.HIGHEST)
       public static void inventoryClickListener(InventoryClickEvent event){
          if (chestGUIMap.get(event.getInventory()) != null){
             ChestGUI chestGUI = chestGUIMap.get(event.getInventory());

       private Integer size;
       private String title;
       private Plugin plugin;
       private Inventory inventory;
       //          Slot     ActionItem
       public Map<Integer, ActionItem> actionItemMap = new HashMap<>();

       public ChestGUI(Plugin plugin, Integer size, String title) {
          this.size = size;
          this.title = ChatColor.translateAlternateColorCodes('&', title+"");
          this.plugin = plugin;
          this.inventory = Bukkit.createInventory(null, size*9, title);
          chestGUIMap.put(inventory, this);
    I need bukkit to call my static method and I am asking if it is possible in some way. This is my problem that I am facing.

    User of this API will just have to do something like this:
    Code (Java):
    ChestGUI chestGUI = new ChestGUI(plugin, size, title);
    And note that I am developing it not from plugin point , but from library that will be exported into project (so I can not register listener in "onEnable" because this source is just an code that other projects will import and use).
    #6 Msql, Nov 29, 2018
    Last edited: Nov 29, 2018
  7. Outsource the listener into an own class and get create handler class for the chestguis. In that handler class you save all chest guis and when listening for the clickevent get all chest guis, check if it's the right gui and do whatever you have to do.
    • Agree Agree x 1
  8. So actually user of this code will have first to register listener by init class that implements and register listener on his onEnable() plugin method and then he will be able to create GUI. That was not exactly what I have expected but if there is no other way then it must be like this.
  9. drives_a_ford


    Not necessarily. You could have a static Listener instance within your class. Your constructor could check if this object is null and initiate it only if it is null.
    • Winner Winner x 1
  10. The solution of my problem was easier than I through it may be. Thanks!
  11. I know that's solved but you can just put
    Code (Java):
    plugin.getPluginManager().registerEvents(plugin, this)
    in your ChestGUI constructor and remove the static of your EventHandler method, but i think it should be better if you create a GUIManager class or something, initializing this GUIManager in the plugin main class and then add a method createGUI or what ever in this GUIManager, this will allow you to register only one listener too.

    I've done something similar here