Class WItemSlot
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 withsetInputFilter(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 ofWItemSlot.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!");
}
});
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
A listener for changes in an item slot. -
Field Summary
Modifier and TypeFieldDescriptionstatic final Identifier
The default texture of item slots andBackgroundPainter.SLOT
. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addChangeListener
(WItemSlot.ChangeListener listener) Adds a change listener to this slot.void
addNarrations
(NarrationMessageBuilder builder) Adds the narrations of this widget to a narration builder.void
Adds the default background painters to this widget and all children.boolean
canFocus()
Tests whether this widget can have the focus in the GUI.protected ValidatedSlot
createSlotPeer
(Inventory inventory, int index, int x, int y) Creates a slot peer for this slot widget.Gets this slot widget's background painter.int
Gets the currently focused slot index.Returns the focus model of this widget.int
getIcon()
Returns the icon if set, otherwise null.Gets the item stack input filter of this slot.Returns the inventory backing this slot.Returns a "narration name" for this slot.Gets the item stack output filter of this slot.@UnmodifiableView List
<? extends ValidatedSlot> getPeers()
Returns an unmodifiable list containing the current slot peers.int
Gets the starting ID for the slot peers.int
Returns the height of this slot widget in individual slots.int
Returns the width of this slot widget in individual slots.int
Returns the starting index of slots in the backing inventory.int
getWidth()
boolean
Returns whether this slot is a big slot.boolean
Checks whether icons should be rendered when the first slot of this widget contains an item.boolean
Returns whether items can be inserted into this slot.boolean
Returns true if the contents of thisWItemSlot
can be modified by players.boolean
Returns whether items can be taken from this slot.static WItemSlot
static WItemSlot
static WItemSlot
ofPlayerStorage
(Inventory inventory) Creates a 9x3 slot widget from the "main" part of a player inventory.void
Notifies this widget that it has lost focusvoid
onHidden()
Notifies this widget that it won't be drawn and hides any visible peers of itself and its children.onKeyPressed
(int ch, int key, int modifiers) Notifies this widget that a key has been pressed.onMouseMove
(int x, int y) Notifies this widget that the mouse has been moved while inside its bounds.void
onShown()
Notifies this widget that it is visible and shows any hidden peers of itself and its children.static WItemSlot
void
paint
(DrawContext context, int x, int y, int mouseX, int mouseY) Paints this widget.void
Sets this item slot's background painter.Sets the icon to this slot.setIconOnlyPaintedForEmptySlots
(boolean iconOnlyPaintedForEmptySlots) Sets whether icons should be rendered when the first slot of this widget contains an item.setInputFilter
(Predicate<ItemStack> inputFilter) Sets the item input filter of this item slot.setInsertingAllowed
(boolean insertingAllowed) Sets whether inserting items into this slot is allowed.setModifiable
(boolean modifiable) setOutputFilter
(Predicate<ItemStack> outputFilter) Sets the item output filter of this item slot.setTakingAllowed
(boolean takingAllowed) Sets whether taking items from this slot is allowed.void
validate
(GuiDescription host) Creates component peers and initializes animation data for this Widget and all its children.Methods inherited from class io.github.cottonmc.cotton.gui.widget.WWidget
addTooltip, canHover, canResize, getAbsoluteX, getAbsoluteY, getHost, getParent, getX, getY, hit, hoveredProperty, isActivationKey, isFocused, isHovered, isNarratable, isWithinBounds, onCharTyped, onClick, onFocusGained, onKeyReleased, onMouseDown, onMouseDrag, onMouseScroll, onMouseUp, releaseFocus, renderTooltip, requestFocus, setHost, setHovered, setLocation, setParent, setSize, shouldRenderInDarkMode, tick
-
Field Details
-
SLOT_TEXTURE
The default texture of item slots andBackgroundPainter.SLOT
.- Since:
- 6.2.0
-
-
Constructor Details
-
WItemSlot
-
-
Method Details
-
of
-
of
-
outputOf
-
ofPlayerStorage
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() -
getHeight
public int getHeight() -
canFocus
public boolean canFocus()Description copied from class:WWidget
Tests whether this widget can have the focus in the GUI. -
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
Returns the icon if set, otherwise null.- Returns:
- the icon if set, otherwise null
- Since:
- 4.1.0
-
setIcon
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
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 thisWItemSlot
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
-
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
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
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
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.
-
onKeyPressed
Description copied from class:WWidget
Notifies this widget that a key has been pressed.- Overrides:
onKeyPressed
in classWWidget
- Parameters:
key
- the GLFW scancode of the key- Returns:
InputResult.PROCESSED
if the event is handled,InputResult.IGNORED
otherwise.
-
createSlotPeer
Creates a slot peer for this slot widget.- Parameters:
inventory
- the slot inventoryindex
- the index in the inventoryx
- the X coordinatey
- the Y coordinate- Returns:
- the created slot instance
- Since:
- 1.11.0
-
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
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
Gets the item stack input filter of this slot.- Returns:
- the item input filter
- Since:
- 8.1.0
-
setInputFilter
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
Gets the item stack output filter of this slot.- Returns:
- the item output filter
- Since:
- 8.1.0
-
setOutputFilter
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
Description copied from class:WWidget
Paints this widget. -
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 classWWidget
- 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 classWWidget
-
addChangeListener
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. -
onMouseMove
Description copied from class:WWidget
Notifies this widget that the mouse has been moved while inside its bounds.- Overrides:
onMouseMove
in classWWidget
- 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.
-
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 classWWidget
-
addNarrations
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 classWWidget
- Parameters:
builder
- the narration builder, cannot be null
-
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
-