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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA listener for changes in an item slot. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final IdentifierThe default texture of item slots andBackgroundPainter.SLOT. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddChangeListener(WItemSlot.ChangeListener listener) Adds a change listener to this slot.voidaddNarrations(NarrationMessageBuilder builder) Adds the narrations of this widget to a narration builder.voidAdds the default background painters to this widget and all children.booleancanFocus()Tests whether this widget can have the focus in the GUI.protected ValidatedSlotcreateSlotPeer(Inventory inventory, int index, int x, int y) Creates a slot peer for this slot widget.Gets this slot widget's background painter.intGets the currently focused slot index.Returns the focus model of this widget.intgetIcon()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.intGets the starting ID for the slot peers.intReturns the height of this slot widget in individual slots.intReturns the width of this slot widget in individual slots.intReturns the starting index of slots in the backing inventory.intgetWidth()booleanReturns whether this slot is a big slot.booleanChecks whether icons should be rendered when the first slot of this widget contains an item.booleanReturns whether items can be inserted into this slot.booleanReturns true if the contents of thisWItemSlotcan be modified by players.booleanReturns whether items can be taken from this slot.static WItemSlotstatic WItemSlotstatic WItemSlotofPlayerStorage(Inventory inventory) Creates a 9x3 slot widget from the "main" part of a player inventory.voidNotifies this widget that it has lost focusvoidonHidden()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.voidonShown()Notifies this widget that it is visible and shows any hidden peers of itself and its children.static WItemSlotvoidpaint(DrawContext context, int x, int y, int mouseX, int mouseY) Paints this widget.voidSets 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.voidvalidate(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:WWidgetTests 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
trueby default.- Returns:
trueif the icon should always be painted,falseotherwise- Since:
- 9.1.0
-
setIconOnlyPaintedForEmptySlots
Sets whether icons should be rendered when the first slot of this widget contains an item.- Parameters:
iconOnlyPaintedForEmptySlots-trueif the icon should always be painted,falseotherwise- Returns:
- this item slot
- Since:
- 9.1.0
-
isModifiable
public boolean isModifiable()Returns true if the contents of thisWItemSlotcan 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:WWidgetCreates 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:WWidgetNotifies this widget that a key has been pressed.- Overrides:
onKeyPressedin classWWidget- Parameters:
key- the GLFW scancode of the key- Returns:
InputResult.PROCESSEDif the event is handled,InputResult.IGNOREDotherwise.
-
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:WWidgetPaints this widget. -
getFocusModel
Description copied from class:WWidgetReturns 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:
getFocusModelin classWWidget- Returns:
- the focus model, or
nullif not available
-
onFocusLost
public void onFocusLost()Description copied from class:WWidgetNotifies this widget that it has lost focus- Overrides:
onFocusLostin 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:WWidgetNotifies this widget that it is visible and shows any hidden peers of itself and its children. -
onMouseMove
Description copied from class:WWidgetNotifies this widget that the mouse has been moved while inside its bounds.- Overrides:
onMouseMovein 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.PROCESSEDif the event is handled,InputResult.IGNOREDotherwise.
-
onHidden
public void onHidden()Description copied from class:WWidgetNotifies 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:WWidgetAdds the default background painters to this widget and all children.Always called before
GuiDescription.addPainters()to allow users to modify painters.- Overrides:
addPaintersin classWWidget
-
addNarrations
Description copied from class:WWidgetAdds 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:
addNarrationsin 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
-