Calling event handlers conditionally

Discussion in 'Plugin Development' started by Dragonphase, Jun 12, 2014.

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

    Dragonphase

    Is it possible to call a specific event handler based on a condition? For example, if I close an inventory with a certain name, I would like "onSpecificInventoryClose(InventoryCloseEvent event)" to fire. I know I can check if the inventory is the one I want to close in the handler itself, but it never seemed as efficient to handle events based on conditions that way.

    This is the C# equivalent of what I want to do, and the best way I can describe it.

    Code:java
    1. PlayerInventory inventory = player.getInventory();
    2.  
    3. inventory.onClose += (event) => {
    4. //do something
    5. }; //this event will only fire when the player's inventory is closed, and not any other inventory
     
  2. That's basically how you're meant to do it. You could, instead of having it in the same method (for some odd reason you don't want to), check if it the inventory (the conditions), then call this.onSpecificInventoryClose(event) in the event handler method and parse the event object. Another way is creating a custom event.
    E.g.
    Code:
    public class SpecificInventoryOpenEvent extends PlayerEvent {
         private static HanderList handerList = new HandlerList();
     
         private Inventory specificInventory = null;
         
         public SpecificInventoryOpenEvent(Player player, Inventory inventory) {
              super(player);
              if (inventory == null) throw new Exception("Inventory cannot be null!");
              this.specificInventory = inventory;
         }
         
         public Inventory getInventory() {
              return this.specificInventory;
         }
     
         public String getTitle() {
              return this.specificInventory.getTitle();
         }
     
         public HandlerList getHandlers() {
              return handlerList;
         }
         
         public static HandlerList getHandlerList() {
              return handlerList;
         }
    }
    
    Then listen to InventoryOpenEvent, check the conditions (and if the clicker is a Player) then do:
    Code:
    Player player = (Player) event.getWhoClicked();
    player.getServer().getPluginManager().callEvent(new SpecificInventoryOpenEvent(player, event.getInventory()));
    
    Then listen to SpecificInventoryOpenEvent.
    Code:
    @EventHandler
    public void onSpecificInventoryOpen(SpecificInventoryOpenEvent event) {
     
    }
    
     
  3. Offline

    Dragonphase

    KingFaris11
    Hmm, alright, thanks. It's not that I don't want to do it the original way, it's just that it never seemed so efficient to handle events that way, especially when you need to handle multiple events based on multiple conditions.

    Or it could be that it is efficient, and I'm just used to the C# coding style.
     
    KingFaris11 likes this.
  4. Offline

    xTrollxDudex

    Dragonphase
    You can call the event with PluginManager with a new instance of InventoryCloseEvent based on different parameters.
     
  5. Offline

    Garris0n

    Couldn't you just...run the close code after you close it?

    Why do you need to use an entire reflection-based event system to call a method?
     
  6. Offline

    Dragonphase

    xTrollxDudex I don't want to call a new instance of an event, I want to handle several instances of the same event which perform different actions based on conditions.

    Garris0n I could, but I was only wondering if there was a more modular way of handling events. I'll stick to the original way.
     
  7. Offline

    xTrollxDudex

    Dragonphase
    Well, the code that handles that specific event will need to handle that...
     
Thread Status:
Not open for further replies.

Share This Page