Class AbstractInventory

java.lang.Object
net.minestom.server.inventory.AbstractInventory
All Implemented Interfaces:
EventHandler<InventoryEvent>, InventoryClickHandler, Taggable, TagReadable, TagWritable, Viewable
Direct Known Subclasses:
Inventory, PlayerInventory

public abstract sealed class AbstractInventory extends Object implements InventoryClickHandler, Taggable, Viewable, EventHandler<InventoryEvent> permits Inventory, PlayerInventory
Represents an inventory where items can be modified/retrieved.
  • Field Details

  • Constructor Details

    • AbstractInventory

      protected AbstractInventory(int size)
  • Method Details

    • getWindowId

      public abstract 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
    • getViewers

      public 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(Player player)
      Description copied from interface: Viewable
      Adds a viewer.
      Specified by:
      addViewer in interface Viewable
      Parameters:
      player - the viewer to add
      Returns:
      true if the player has been added, false otherwise (could be because he is already a viewer)
    • removeViewer

      public boolean removeViewer(Player player)
      Description copied from interface: Viewable
      Removes a viewer.
      Specified by:
      removeViewer in interface Viewable
      Parameters:
      player - the viewer to remove
      Returns:
      true if the player has been removed, false otherwise (could be because he was not a viewer)
    • setItemStack

      public void setItemStack(int slot, ItemStack itemStack)
      Sets an ItemStack at the specified slot and send relevant update to the viewer(s).
      Parameters:
      slot - the slot to set the item
      itemStack - the item to set
    • setItemStack

      public void setItemStack(int slot, ItemStack itemStack, boolean sendPacket)
      Sets an ItemStack at the specified slot and send relevant update to the viewer(s).
      Parameters:
      slot - the slot to set the item
      itemStack - the item to set
      sendPacket - whether or not to send packets
    • UNSAFE_itemInsert

      protected void UNSAFE_itemInsert(int slot, ItemStack item, ItemStack previous, boolean sendPacket)
    • sendSlotRefresh

      public void sendSlotRefresh(int slot, ItemStack item)
    • processItemStack

      public <T> T processItemStack(ItemStack itemStack, TransactionType type, TransactionOption<T> option)
    • processItemStacks

      public <T> List<T> processItemStacks(List<ItemStack> itemStacks, TransactionType type, TransactionOption<T> option)
    • addItemStack

      public <T> T addItemStack(ItemStack itemStack, TransactionOption<T> option)
      Adds an ItemStack to the inventory and sends relevant update to the viewer(s).
      Parameters:
      itemStack - the item to add
      option - the transaction option
      Returns:
      true if the item has been successfully added, false otherwise
    • addItemStack

      public boolean addItemStack(ItemStack itemStack)
    • addItemStacks

      public <T> List<T> addItemStacks(List<ItemStack> itemStacks, TransactionOption<T> option)
      Adds ItemStacks to the inventory and sends relevant updates to the viewer(s).
      Parameters:
      itemStacks - items to add
      option - the transaction option
      Returns:
      the operation results
    • takeItemStack

      public <T> T takeItemStack(ItemStack itemStack, TransactionOption<T> option)
      Takes an ItemStack from the inventory and sends relevant update to the viewer(s).
      Parameters:
      itemStack - the item to take
      Returns:
      true if the item has been successfully fully taken, false otherwise
    • takeItemStacks

      public <T> List<T> takeItemStacks(List<ItemStack> itemStacks, TransactionOption<T> option)
      Takes ItemStacks from the inventory and sends relevant updates to the viewer(s).
      Parameters:
      itemStacks - items to take
      Returns:
      the operation results
    • replaceItemStack

      public void replaceItemStack(int slot, UnaryOperator<ItemStack> operator)
    • clear

      public void clear()
      Clears the inventory and send relevant update to the viewer(s).
    • update

      public void update()
      Refreshes the inventory for all viewers.
    • update

      public void update(Player player)
      Refreshes the inventory for a specific viewer.
      Parameters:
      player - the player to update the inventory for
    • getItemStack

      public ItemStack getItemStack(int slot)
      Gets the ItemStack at the specified slot.
      Parameters:
      slot - the slot to check
      Returns:
      the item in the slot slot
    • getItemStacks

      public ItemStack[] getItemStacks()
      Gets all the ItemStack in the inventory.

      Be aware that the returned array does not need to be the original one, meaning that modifying it directly may not work.

      Returns:
      an array containing all the inventory's items
    • getSize

      public int getSize()
      Gets the size of the inventory.
      Returns:
      the inventory's size
    • getInnerSize

      public int getInnerSize()
      Gets the size of the "inner inventory" (which includes only "usable" slots).
      Returns:
      inner inventory's size
    • copyContents

      public void copyContents(ItemStack[] itemStacks)
      Places all the items of itemStacks into the internal array.
      Parameters:
      itemStacks - the array to copy the content from
      Throws:
      IllegalArgumentException - if the size of the array is not equal to getSize()
      NullPointerException - if itemStacks contains one null element or more
    • tagHandler

      public TagHandler tagHandler()
      Specified by:
      tagHandler in interface Taggable
    • eventNode

      public EventNode<InventoryEvent> eventNode()
      Specified by:
      eventNode in interface EventHandler<InventoryEvent>