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 an item or not. The filter can be set withsetFilter(Predicate). For example:
// Only sand in this slot!
slot.setFilter(stack -> stack.getItem() == Items.SAND);
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
-
Constructor Summary
ConstructorsConstructorDescriptionWItemSlot(net.minecraft.inventory.Inventory inventory, int startIndex, int slotsWide, int slotsHigh, boolean big) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddChangeListener(WItemSlot.ChangeListener listener) Adds a change listener to this slot.voidaddNarrations(net.minecraft.client.gui.screen.narration.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(net.minecraft.inventory.Inventory inventory, int index, int x, int y) Creates a slot peer for this slot widget.cycleFocus(boolean lookForwards) Cycles the focus inside this widget.Gets this slot widget's background painter.Predicate<net.minecraft.item.ItemStack> Gets the item filter of this item slot.intGets the currently focused slot index.intgetIcon()Returns the icon if set, otherwise null.protected @Nullable net.minecraft.text.TextReturns a "narration name" for this slot.intgetWidth()booleanbooleanReturns 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 WItemSlotof(net.minecraft.inventory.Inventory inventory, int index) static WItemSlotof(net.minecraft.inventory.Inventory inventory, int startIndex, int slotsWide, int slotsHigh) static WItemSlotofPlayerStorage(net.minecraft.inventory.Inventory inventory) Creates a 9x3 slot widget from the "main" part of a player inventory.voidonHidden()Notifies this widget that it won't be drawn and hides any visible peers of itself and its children.voidonKeyPressed(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 WItemSlotoutputOf(net.minecraft.inventory.Inventory inventory, int index) voidpaint(net.minecraft.client.util.math.MatrixStack matrices, int x, int y, int mouseX, int mouseY) Paints this widget.voidSets this item slot's background painter.Sets the item filter of this item slot.Sets the icon to this slot.setInsertingAllowed(boolean insertingAllowed) Sets whether inserting items into this slot is allowed.setModifiable(boolean modifiable) 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, onFocusLost, onKeyReleased, onMouseDown, onMouseDrag, onMouseScroll, onMouseUp, releaseFocus, renderTooltip, requestFocus, setHost, setHovered, setLocation, setParent, setSize, tick
-
Constructor Details
-
WItemSlot
public WItemSlot(net.minecraft.inventory.Inventory inventory, int startIndex, int slotsWide, int slotsHigh, boolean big)
-
-
Method Details
-
of
-
of
public static WItemSlot of(net.minecraft.inventory.Inventory inventory, int startIndex, int slotsWide, int slotsHigh) -
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. -
isBigSlot
public boolean isBigSlot() -
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
-
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
@Environment(CLIENT) public void onKeyPressed(int ch, int key, int modifiers) Description copied from class:WWidgetNotifies this widget that a key has been pressed.- Overrides:
onKeyPressedin classWWidget- 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 inventoryindex- the index in the inventoryx- the X coordinatey- the Y coordinate- Returns:
- the created slot instance
- Since:
- 1.11.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
-
getFilter
Gets the item filter of this item slot.- Returns:
- the item filter
- Since:
- 2.0.0
-
setFilter
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:WWidgetPaints this widget. -
cycleFocus
Description copied from class:WWidgetCycles the focus inside this widget.If this widget is not focusable, returns null.
- Overrides:
cycleFocusin classWWidget- 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
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
@Environment(CLIENT) public void addNarrations(net.minecraft.client.gui.screen.narration.NarrationMessageBuilder builder) 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
-