Solved Cancelling PlayerInventoryClickEvents while in a specific container

Discussion in 'Plugin Development' started by PureAspiration, Oct 20, 2020.

  1. Offline

    PureAspiration

    So I want to make a GUI which players can click on, but you can put stuff in there. Even after replacing all empty slots with glass, players can still shift-click items which the same metadata into the GUI.

    Here is my code for now
    Code:
    @EventHandler
    public void event(InventoryClickEvent event) {
    if (event.getClickedInventory().getHolder() instanceof RecipesGUI) {
    event.setCancelled(true)
    }
    }
    However, this only stops moving of items in the inventory, not the players inventory. So how can I cancel player's inventory while in that GUI?

    Thanks
     
  2. Offline

    Raymondliu1

    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/HumanEntity.html#getOpenInventory()

    This would probably allow you to do something like this

    Code:
    @EventHandler
    public void event(InventoryClickEvent e){
      if(e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof RecipesGUI) event.setCancelled(true);
    
    }
    The code above is not tested but the concept should be there?

    Also I'm a bit confused here... why is the RecipesGUI an InventoryHolder?
     
  3. Offline

    PureAspiration

    Thanks so much! Solved!

    btw its event.getWhoClicked()
     

Share This Page