Class WItemSlot

java.lang.Object
io.github.cottonmc.cotton.gui.widget.WWidget
io.github.cottonmc.cotton.gui.widget.WItemSlot

public class WItemSlot extends WWidget
A widget that displays an item that can be interacted with.

Item slot widgets can contain multiple visual slots themselves. For example, a slot widget might be 5x3 with 15 visual slots in total.

Item slots are handled with so-called peers in the background. They are instances of ValidatedSlot that handle the interactions between the player and the widget.

Filters

Item slots can have filters that check whether a player is allowed to insert or take out an item or not. The filters can be set with setInputFilter(Predicate) and setOutputFilter(Predicate). For example:
 
 // Only sand can be placed on this slot
 slot.setInputFilter(stack -> stack.isOf(Items.SAND));

 // Everything except glass can be taken out of this slot
 slot.setOutputFilter(stack -> !stack.isOf(Items.GLASS));
 
 

Listeners

Slot change listeners are instances of WItemSlot.ChangeListener that can handle changes to item stacks in slots. For example:
 
 slot.addChangeListener((slot, inventory, index, stack) -> {
     if (stack.isEmpty() || stack.getCount() < stack.getMaxCount()) {
         System.out.println("I'm not full yet!");
     }
 });
 
 
  • Field Details

  • Constructor Details

    • WItemSlot

      public WItemSlot(Inventory inventory, int startIndex, int slotsWide, int slotsHigh, boolean big)
  • Method Details

    • of

      public static WItemSlot of(Inventory inventory, int index)
    • of

      public static WItemSlot of(Inventory inventory, int startIndex, int slotsWide, int slotsHigh)
    • outputOf

      public static WItemSlot outputOf(Inventory inventory, int index)
    • ofPlayerStorage

      public static WItemSlot ofPlayerStorage(Inventory inventory)
      Creates a 9x3 slot widget from the "main" part of a player inventory.
      Parameters:
      inventory - the player inventory
      Returns:
      the created slot widget
      See Also:
    • getWidth

      public int getWidth()
      Overrides:
      getWidth in class WWidget
    • getHeight

      public int getHeight()
      Overrides:
      getHeight in class WWidget
    • canFocus

      public boolean canFocus()
      Description copied from class: WWidget
      Tests whether this widget can have the focus in the GUI.
      Overrides:
      canFocus in class WWidget
      Returns:
      true if this widget can be focused, false otherwise
    • getInventory

      public Inventory getInventory()
      Returns the inventory backing this slot.
      Returns:
      the inventory backing this slot
      Since:
      11.1.0
    • getStartIndex

      public int getStartIndex()
      Returns the starting index of slots in the backing inventory.
      Returns:
      the starting index of slots in the backing inventory
      Since:
      11.1.0
    • getSlotsWide

      public int getSlotsWide()
      Returns the width of this slot widget in individual slots.
      Returns:
      the width of this slot widget in individual slots
      Since:
      11.1.0
    • getSlotsHigh

      public int getSlotsHigh()
      Returns the height of this slot widget in individual slots.
      Returns:
      the height of this slot widget in individual slots
      Since:
      11.1.0
    • isBigSlot

      public boolean isBigSlot()
      Returns whether this slot is a big slot. Big slots are commonly used for crafting results and similar outputs.
      Returns:
      whether this slot is a big slot
    • getIcon

      @Nullable public @Nullable Icon getIcon()
      Returns the icon if set, otherwise null.
      Returns:
      the icon if set, otherwise null
      Since:
      4.1.0
    • setIcon

      public WItemSlot setIcon(@Nullable @Nullable Icon icon)
      Sets the icon to this slot. Can be used for labeling slots for certain activities.
      Parameters:
      icon - the icon
      Returns:
      this slot widget
      Since:
      4.1.0
    • isIconOnlyPaintedForEmptySlots

      public boolean isIconOnlyPaintedForEmptySlots()
      Checks whether icons should be rendered when the first slot of this widget contains an item.

      This property is true by default.

      Returns:
      true if the icon should always be painted, false otherwise
      Since:
      9.1.0
    • setIconOnlyPaintedForEmptySlots

      public WItemSlot setIconOnlyPaintedForEmptySlots(boolean iconOnlyPaintedForEmptySlots)
      Sets whether icons should be rendered when the first slot of this widget contains an item.
      Parameters:
      iconOnlyPaintedForEmptySlots - true if the icon should always be painted, false otherwise
      Returns:
      this item slot
      Since:
      9.1.0
    • isModifiable

      public boolean isModifiable()
      Returns true if the contents of this WItemSlot can be modified by players.
      Returns:
      true if items can be inserted into or taken from this slot widget, false otherwise
      Since:
      1.8.0
    • setModifiable

      public WItemSlot setModifiable(boolean modifiable)
    • isInsertingAllowed

      public boolean isInsertingAllowed()
      Returns whether items can be inserted into this slot.
      Returns:
      true if items can be inserted, false otherwise
      Since:
      1.10.0
    • setInsertingAllowed

      public WItemSlot setInsertingAllowed(boolean insertingAllowed)
      Sets whether inserting items into this slot is allowed.
      Parameters:
      insertingAllowed - true if items can be inserted, false otherwise
      Returns:
      this slot widget
      Since:
      1.10.0
    • isTakingAllowed

      public boolean isTakingAllowed()
      Returns whether items can be taken from this slot.
      Returns:
      true if items can be taken, false otherwise
      Since:
      1.10.0
    • setTakingAllowed

      public WItemSlot setTakingAllowed(boolean takingAllowed)
      Sets whether taking items from this slot is allowed.
      Parameters:
      takingAllowed - true if items can be taken, false otherwise
      Returns:
      this slot widget
      Since:
      1.10.0
    • getFocusedSlot

      public int getFocusedSlot()
      Gets the currently focused slot index.
      Returns:
      the currently focused slot, or -1 if this widget isn't focused
      Since:
      2.0.0
    • validate

      public void validate(GuiDescription host)
      Description copied from class: WWidget
      Creates component peers and initializes animation data for this Widget and all its children. The host screen handler must clear any heavyweight peers from its records before this method is called.

      This method must be called on the root panel of any screen once the widgets have been initialized.

      Overrides:
      validate in class WWidget
      Parameters:
      host - the host GUI description
    • onKeyPressed

      @Environment(CLIENT) public InputResult onKeyPressed(int ch, int key, int modifiers)
      Description copied from class: WWidget
      Notifies this widget that a key has been pressed.
      Overrides:
      onKeyPressed in class WWidget
      Parameters:
      key - the GLFW scancode of the key
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • createSlotPeer

      protected ValidatedSlot createSlotPeer(Inventory inventory, int index, int x, int y)
      Creates a slot peer for this slot widget.
      Parameters:
      inventory - the slot inventory
      index - the index in the inventory
      x - the X coordinate
      y - the Y coordinate
      Returns:
      the created slot instance
      Since:
      1.11.0
    • getPeers

      public @UnmodifiableView List<? extends ValidatedSlot> getPeers()
      Returns an unmodifiable list containing the current slot peers.
      Returns:
      an unmodifiable list containing the current slot peers
      Since:
      11.1.0
    • getPeerStartId

      public int getPeerStartId()
      Gets the starting ID for the slot peers.
      Returns:
      the starting ID for the slot peers, or -1 if this slot widget has no peers
      Since:
      11.1.0
    • getBackgroundPainter

      @Nullable @Environment(CLIENT) public @Nullable BackgroundPainter getBackgroundPainter()
      Gets this slot widget's background painter.
      Returns:
      the background painter
      Since:
      2.0.0
    • setBackgroundPainter

      @Environment(CLIENT) public void setBackgroundPainter(@Nullable @Nullable BackgroundPainter painter)
      Sets this item slot's background painter.
      Parameters:
      painter - the new painter
    • getInputFilter

      public Predicate<ItemStack> getInputFilter()
      Gets the item stack input filter of this slot.
      Returns:
      the item input filter
      Since:
      8.1.0
    • setInputFilter

      public WItemSlot setInputFilter(Predicate<ItemStack> inputFilter)
      Sets the item input filter of this item slot.
      Parameters:
      inputFilter - the new item input filter
      Returns:
      this item slot
      Since:
      8.1.0
    • getOutputFilter

      public Predicate<ItemStack> getOutputFilter()
      Gets the item stack output filter of this slot.
      Returns:
      the item output filter
      Since:
      8.1.0
    • setOutputFilter

      public WItemSlot setOutputFilter(Predicate<ItemStack> outputFilter)
      Sets the item output filter of this item slot.
      Parameters:
      outputFilter - the new item output filter
      Returns:
      this item slot
      Since:
      8.1.0
    • paint

      @Environment(CLIENT) public void paint(DrawContext context, int x, int y, int mouseX, int mouseY)
      Description copied from class: WWidget
      Paints this widget.
      Overrides:
      paint in class WWidget
      Parameters:
      context - the draw context
      x - this widget's X coordinate on the screen
      y - this widget's Y coordinate on the screen
      mouseX - the X coordinate of the cursor
      mouseY - the X coordinate of the cursor
    • getFocusModel

      @Nullable public @Nullable FocusModel<?> getFocusModel()
      Description copied from class: WWidget
      Returns the focus model of this widget. The focus model provides the focusable areas of this widget, and handles switching through them.

      If this widget can focus, it should return a nonnull focus model. The default implementation returns FocusModel.simple(this) when the widget can be focused.

      Overrides:
      getFocusModel in class WWidget
      Returns:
      the focus model, or null if not available
    • onFocusLost

      public void onFocusLost()
      Description copied from class: WWidget
      Notifies this widget that it has lost focus
      Overrides:
      onFocusLost in class WWidget
    • addChangeListener

      public void addChangeListener(WItemSlot.ChangeListener listener)
      Adds a change listener to this slot. Does nothing if the listener is already registered.
      Parameters:
      listener - the added listener
      Throws:
      NullPointerException - if the listener is null
      Since:
      3.0.0
    • onShown

      public void onShown()
      Description copied from class: WWidget
      Notifies this widget that it is visible and shows any hidden peers of itself and its children.
      Overrides:
      onShown in class WWidget
    • onMouseMove

      public InputResult onMouseMove(int x, int y)
      Description copied from class: WWidget
      Notifies this widget that the mouse has been moved while inside its bounds.
      Overrides:
      onMouseMove in class WWidget
      Parameters:
      x - The X coordinate of the event, in widget-space (0 is the left edge of this widget)
      y - The Y coordinate of the event, in widget-space (0 is the top edge of this widget)
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • onHidden

      public void onHidden()
      Description copied from class: WWidget
      Notifies this widget that it won't be drawn and hides any visible peers of itself and its children.

      The default implementation releases this widget's focus if it is focused. Overriding implementations might want to do this as well.

      Overrides:
      onHidden in class WWidget
    • addPainters

      @Environment(CLIENT) public void addPainters()
      Description copied from class: WWidget
      Adds the default background painters to this widget and all children.

      Always called before GuiDescription.addPainters() to allow users to modify painters.

      Overrides:
      addPainters in class WWidget
    • addNarrations

      @Environment(CLIENT) public void addNarrations(NarrationMessageBuilder builder)
      Description copied from class: WWidget
      Adds the narrations of this widget to a narration builder. Narrations will only apply if this widget is narratable.

      The widget needs to be focusable or hoverable, and also be focused/hovered for narrations to be added.

      Overrides:
      addNarrations in class WWidget
      Parameters:
      builder - the narration builder, cannot be null
    • getNarrationName

      @Nullable protected @Nullable Text getNarrationName()
      Returns a "narration name" for this slot. It's narrated before the slot index. One example of a narration name would be "hotbar" for the player's hotbar.
      Returns:
      the narration name, or null if there's none for this slot
      Since:
      4.2.0