Skip to content

SlotWidget

Image title

The SlotWidget represents an interactive item slot within a container GUI. This widget supports configurable properties such as whether items can be taken or placed, customizable hover overlays and tooltips, and integration with JEI/REI/EMI systems for displaying ingredient details. It can be configured to represent a slot in either a standard inventory container or via an item transfer handler.

INFO

You cannot modify slot widget size.

Basic Properties

FieldDescription
canTakeItemsIndicates if items can be extracted from the slot (modifiable via setter)
canPutItemsIndicates if items can be inserted into the slot (modifiable via setter)
drawHoverOverlayDetermines whether a hover overlay is drawn when the mouse is over the slot
drawHoverTipsDetermines whether tooltips are shown when the mouse hovers over the slot
lastItemItem stored in the last tick.

APIs

setContainerSlot

Associates the widget with an inventory container by specifying the slot index. e.g. player inventory.

java
var player = ...;
slotWidget.setContainerSlot(player.getInventory(), 2); // bind the player inventory of 2 index.

setHandlerSlot

Configures the widget to use an item transfer handler with the specified slot index.

Java

For Java User !!!

If you want to use the handler of ItemStackHandler(from forge) or Storage<ItemVariant>(from fabric), you need one more line. Because Forge and Fabric has different APIs, you need to convert the handler into the LDLib one, which can be found in ItemTransferHelperImpl.

java
var itemHandler = ...;
var itemTransfer = ItemTransferHelperImpl.toItemTransfer(itemHandler);

KubeJS

KubeJS user do not need to do such mess. We have already convert them into a item transfer internal already.

javascript
slotWidget.setHandlerSlot(itemTransfer, 0);

setItem

Set the internal itemstack with/without notify.

java
slotWidget.setItem(itemstack); // it will also trigger the listen you set
slotWidget.setItem(itemstack, false); // it wont trigger the listen

getItem

Get the internal itemstack stored.

java
var itemstack = slotWidget.getItem();

setChangeListener

Configures additional slot location information, such as whether it belongs to the player container or hotbar. It will affect the shift move behaviour.

java
slotWidget.setChangeListener(() -> {
    var last = slotWidget.getLastItem();
    var current = slotWidget.getItem();
});

canPutStack

Whether can put stack into the slot.

java
slotWidget.canPutStack(true);

canTakeStack

Whether can take stack from the slot.

java
slotWidget.canTakeStack(true);

setLocationInfo

Configures additional slot location information, such as whether it belongs to the player container or hotbar. It will affect the shift move behaviour.

java
slotWidget.setLocationInfo(true, false); // (isPlayerContainer isPlayerHotBar)

Released under the MIT License.