[SPIGOT-7021] Player.openWorkbench calls open and close inventory events before opening properly Created: 15/May/22  Updated: 25/Dec/24  Resolved: 15/May/22

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: Theodore Assignee: Unassigned
Resolution: Invalid Votes: 0
Labels: bug, spigot

Attachments: JPEG File spigot-issue.jpg    
Version: API version 1.18.2-R0.1-SNAPSHOT
Guidelines Read: Yes

 Description   

When opening a custom workbench using player.openWorkbench(location, true), InventoryOpenEvent and InventoryCloseEvent are both called before again calling InventoryOpenEvent. For example, the sequence of events include:

openWorkbench -> InventoryOpenEvent -> InventoryCloseEvent -> InventoryOpenEvent

 

After the final InventoryOpenEvent is called, the workbench can be used normally, and hitting escape to close the inventory will call InventoryCloseEvent as it should. All other inventories that can normally be opened in the vanilla game seem to call these events properly.

 

The following code includes the event handlers that demonstrate this bug:

@EventHandler
public void onInventoryClose(InventoryCloseEvent e) {
    InventoryType type = e.getInventory().getType();
    System.out.println(String.format("Close Inventory: %s", type));
}

@EventHandler
public void onInventoryOpen(InventoryOpenEvent e) {
    InventoryType type = e.getInventory().getType();
    System.out.println(String.format("Open Inventory: %s", type));
}

@EventHandler
public void onInteract(PlayerInteractEvent e) {
    if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
        Player p = e.getPlayer();
        Block block = e.getClickedBlock();
        if (block.getType() == Material.PETRIFIED_OAK_SLAB) {
            e.setCancelled(true);
            p.openWorkbench(block.getLocation(), true);
        }
    }
}

Material.PETRIFIED_OAK_SLAB was entire arbitrary and could have been any block material.

The only other code that is within this plugin is the enable function with the instantiation of the listener.

The only other plugin involved is PlugManX, which is used to live reload the test plugin.

 

The attached image shows the outputs in the console after one right click, which should only call InventoryOpenEvent once, but instead calls InventoryOpenEvent, then InventoryCloseEvent, then InventoryOpenEvent again.



 Comments   
Comment by Marvin Rieple [ 15/May/22 ]

The PlayerInteractEvent gets called for the main hand and offhand. Which means your code runs two times per interaction, those resulting in an extra open and close event.

You can see this by adding following code to your plugin: System.out.println("Got call " + e.getHand());

 

If you need further help with it please use the forum.

Generated at Tue Apr 08 03:01:46 UTC 2025 using Jira 10.3.3#10030003-sha1:d220e3fefc8dfc6d47f522d3b9a20c1455e12b7b.