1.16.5 Can I somehow know WHEN a player exits an anvil (e.g. by pressing escape)

Discussion in 'Spigot Plugin Development' started by stelioshah, Jun 14, 2021.

  1. So I have made a plugin based on the PrepareAnvilEvent, where I store some variables of the players that uses the anvil. At the end, I delete their variables either when the anvil gets empty or when they pick the resulted item. If they press escape though (without picking the resulted item) and then does NOT use the anvil again, their variables remain initialized since I have not found a way to track when they exited the anvil.

    The PrepareAnvilEvent is not Cancellable, and when a player closes the anvil gui the event does NOT run again as it usally does by doing anything else.

    Is there a solution to this? I thought of adding a task to remove them later, but I would preferably avoid this.
     
    #1 stelioshah, Jun 14, 2021
    Last edited: Jun 14, 2021
  2. I'm pretty sure that InventoryCloseEvent should fire in this case
     
    • Winner Winner x 1
  3. Yeah InventoryCloseEvent should do the job, you can then check with InventoryType if the closed inventory was an anvil, e.g.

    Code (Java):
    @EventHandler
    public void onClose(InventoryCloseEvent event){
        if(event.getInventory().getType() == InventoryType.ANVIL) {
            //Do Stuff
        }
    }
     
    • Like Like x 1
  4. That worked like a charm. I would like to make a simple question because I am pretty new to this. What I do not understand is the following:
    Let's say that I am now using the InventoryCloseEvent in order to track the initialized players to remove them from the memory, this event obviously triggers every time a player closes their inventory. Doesn't that make this event processing power hungry?
     
    #4 stelioshah, Jun 14, 2021
    Last edited: Jun 14, 2021
  5. I actually just immediately checked if the player is initialized (their attributes are stored in Maps which make this pretty straightforward), but I guess checking if the inventory is an anvil first, is the proper way to do it.
     
  6. No, it is ABSOLUTELY irrelevant for server performance :) Just imagine that the PlayerMoveEvent gets called 20 times per second for EVERY player and that a huge amount of physics, etc is calculated anyway on each tick.
     
    • Informative Informative x 1
  7. Thank you, I will keep that in mind.