Class Inventory

java.lang.Object
net.minestom.server.inventory.AbstractInventory
net.minestom.server.inventory.Inventory
All Implemented Interfaces:
InventoryClickHandler, Taggable, TagReadable, TagWritable, Viewable
Direct Known Subclasses:
AnvilInventory, BeaconInventory, BrewingStandInventory, EnchantmentTableInventory, FurnaceInventory, VillagerInventory

public non-sealed class Inventory extends AbstractInventory implements Viewable
Represents an inventory which can be viewed by a collection of Player.

You can create one with Inventory(InventoryType, String) or by making your own subclass. It can then be opened using Player.openInventory(Inventory).

  • Constructor Details

    • Inventory

      public Inventory(@NotNull @NotNull InventoryType inventoryType, @NotNull @NotNull Component title)
    • Inventory

      public Inventory(@NotNull @NotNull InventoryType inventoryType, @NotNull @NotNull String title)
  • Method Details

    • getInventoryType

      @NotNull public @NotNull InventoryType getInventoryType()
      Gets the inventory type.
      Returns:
      the inventory type
    • getTitle

      @NotNull public @NotNull Component getTitle()
      Gets the inventory title.
      Returns:
      the inventory title
    • setTitle

      public void setTitle(@NotNull @NotNull Component title)
      Changes the inventory title.
      Parameters:
      title - the new inventory title
    • getWindowId

      public byte getWindowId()
      Gets this window id.

      This is the id that the client will send to identify the affected inventory, mostly used by packets.

      Returns:
      the window id
    • update

      public void update()
      Refreshes the inventory for all viewers.
      Specified by:
      update in class AbstractInventory
    • update

      public void update(@NotNull @NotNull Player player)
      Refreshes the inventory for a specific viewer.

      The player needs to be a viewer, otherwise nothing is sent.

      Parameters:
      player - the player to update the inventory
    • getViewers

      @NotNull public @NotNull Set<Player> getViewers()
      Description copied from interface: Viewable
      Gets all the viewers of this viewable element.
      Specified by:
      getViewers in interface Viewable
      Returns:
      A Set containing all the element's viewers
    • addViewer

      public boolean addViewer(@NotNull @NotNull Player player)
      This will not open the inventory for player, use Player.openInventory(Inventory).
      Specified by:
      addViewer in interface Viewable
      Parameters:
      player - the viewer to add
      Returns:
      true if the player has successfully been added
    • removeViewer

      public boolean removeViewer(@NotNull @NotNull Player player)
      This will not close the inventory for player, use Player.closeInventory().
      Specified by:
      removeViewer in interface Viewable
      Parameters:
      player - the viewer to remove
      Returns:
      true if the player has successfully been removed
    • getCursorItem

      @Deprecated @NotNull public @NotNull ItemStack getCursorItem(@NotNull @NotNull Player player)
      Deprecated.
      normal inventories no longer store cursor items
      Gets the cursor item of a player.
      See Also:
    • setCursorItem

      @Deprecated public void setCursorItem(@NotNull @NotNull Player player, @NotNull @NotNull ItemStack cursorItem)
      Deprecated.
      normal inventories no longer store cursor items
      Changes the cursor item of a player.
      See Also:
    • UNSAFE_itemInsert

      protected void UNSAFE_itemInsert(int slot, @NotNull @NotNull ItemStack itemStack, boolean sendPacket)
      Specified by:
      UNSAFE_itemInsert in class AbstractInventory
    • sendProperty

      protected void sendProperty(@NotNull @NotNull InventoryProperty property, short value)
      Sends a window property to all viewers.
      Parameters:
      property - the property to send
      value - the value of the property
      See Also:
    • leftClick

      public boolean leftClick(@NotNull @NotNull Player player, int slot)
      Description copied from interface: InventoryClickHandler
      Called when a Player left click in the inventory. Can also be to drop the cursor item
      Specified by:
      leftClick in interface InventoryClickHandler
      Parameters:
      player - the player who clicked
      slot - the slot number
      Returns:
      true if the click hasn't been cancelled, false otherwise
    • rightClick

      public boolean rightClick(@NotNull @NotNull Player player, int slot)
      Description copied from interface: InventoryClickHandler
      Called when a Player right click in the inventory. Can also be to drop the cursor item
      Specified by:
      rightClick in interface InventoryClickHandler
      Parameters:
      player - the player who clicked
      slot - the slot number
      Returns:
      true if the click hasn't been cancelled, false otherwise
    • shiftClick

      public boolean shiftClick(@NotNull @NotNull Player player, int slot)
      Description copied from interface: InventoryClickHandler
      Called when a Player shift click in the inventory
      Specified by:
      shiftClick in interface InventoryClickHandler
      Parameters:
      player - the player who clicked
      slot - the slot number
      Returns:
      true if the click hasn't been cancelled, false otherwise
    • changeHeld

      public boolean changeHeld(@NotNull @NotNull Player player, int slot, int key)
      Description copied from interface: InventoryClickHandler
      Called when a Player held click in the inventory
      Specified by:
      changeHeld in interface InventoryClickHandler
      Parameters:
      player - the player who clicked
      slot - the slot number
      key - the held slot (0-8) pressed
      Returns:
      true if the click hasn't been cancelled, false otherwise
    • middleClick

      public boolean middleClick(@NotNull @NotNull Player player, int slot)
      Specified by:
      middleClick in interface InventoryClickHandler
    • drop

      public boolean drop(@NotNull @NotNull Player player, boolean all, int slot, int button)
      Description copied from interface: InventoryClickHandler
      Called when a Player press the drop button
      Specified by:
      drop in interface InventoryClickHandler
      Parameters:
      player - the player who clicked
      all -
      slot - the slot number
      button - -999 if clicking outside, normal if he is not
      Returns:
      true if the drop hasn't been cancelled, false otherwise
    • dragging

      public boolean dragging(@NotNull @NotNull Player player, int slot, int button)
      Specified by:
      dragging in interface InventoryClickHandler
    • doubleClick

      public boolean doubleClick(@NotNull @NotNull Player player, int slot)
      Description copied from interface: InventoryClickHandler
      Called when a Player double click in the inventory
      Specified by:
      doubleClick in interface InventoryClickHandler
      Parameters:
      player - the player who clicked
      slot - the slot number
      Returns:
      true if the click hasn't been cancelled, false otherwise