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 an item or not. The filter can be set with setFilter(Predicate). For example:
 
 // Only sand in this slot!
 slot.setFilter(stack -> stack.getItem() == Items.SAND);
 
 

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!");
     }
 });
 
 
  • Constructor Details

    • WItemSlot

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

    • of

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

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

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

      public static WItemSlot ofPlayerStorage(net.minecraft.inventory.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
    • isBigSlot

      public boolean isBigSlot()
    • 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
    • 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 void 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
    • createSlotPeer

      protected ValidatedSlot createSlotPeer(net.minecraft.inventory.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
    • 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
    • getFilter

      public Predicate<net.minecraft.item.ItemStack> getFilter()
      Gets the item filter of this item slot.
      Returns:
      the item filter
      Since:
      2.0.0
    • setFilter

      public WItemSlot setFilter(Predicate<net.minecraft.item.ItemStack> filter)
      Sets the item filter of this item slot.
      Parameters:
      filter - the new item filter
      Returns:
      this item slot
      Since:
      2.0.0
    • paint

      @Environment(CLIENT) public void paint(net.minecraft.client.util.math.MatrixStack matrices, int x, int y, int mouseX, int mouseY)
      Description copied from class: WWidget
      Paints this widget.
      Overrides:
      paint in class WWidget
      Parameters:
      matrices - the rendering matrix stack
      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
    • cycleFocus

      @Nullable public @Nullable WWidget cycleFocus(boolean lookForwards)
      Description copied from class: WWidget
      Cycles the focus inside this widget.

      If this widget is not focusable, returns null.

      Overrides:
      cycleFocus in class WWidget
      Parameters:
      lookForwards - whether this should cycle forwards (true) or backwards (false)
      Returns:
      the next focused widget, or null if should exit to the parent panel
    • 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(net.minecraft.client.gui.screen.narration.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 net.minecraft.text.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