Skip to content

Inventory ​

com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory

Since1.0.8

Wraps a container screen's menu; specialized subclasses represent recognized screen types.
Use instanceof Inventories.Crafting and the other Inventories names to branch on those wrapper types; getType() returns the screen name used for identification.
Slot indices refer to the menu's full slot list. getMap() groups applicable indices by section name; available sections depend on the screen.

js
const inv = Player.openInventory();  
// which kind of container is this?
Chat.log(`this is ${inv.getType()}`);  
// the named sections, rather than slot numbers that move per screen
Chat.log(`main: ${inv.getSlots("main")}`);  
Chat.log(`hotbar: ${inv.getSlots("hotbar")}`);  
// count what is in there, without touching any slot
const counts = inv.getItemCount();  
for (const id of counts.keySet()) {
  Chat.log(`${id} x ${counts.get(id)}`);  
}

Available in scripts as Inventories.Inventory.

Type Parameters ​

  • T: concrete container screen type wrapped by this inventory

Methods ​

click ​

click(int slot): Inventory<T extends AbstractContainerScreen<?>>Since1.5.0#
Performs a primary-button pickup click on a menu slot.

Parameters:

  • slot: index in the menu's full slot list
Returns: this inventory for chaining
click(int slot, int mouseButton): Inventory<T extends AbstractContainerScreen<?>>Since1.0.8#
Performs a pickup click, except button 2 clones the stack.

Parameters:

  • slot: index in the menu's full slot list
  • mouseButton: 2 to clone; all other values perform a pickup click
Returns: this inventory wrapper

close ​

close(): void#
Requests closure of the player's current container on the client thread and immediately clears this wrapper's screen and menu references.

closeAndDrop ​

closeAndDrop(): Inventory<T extends AbstractContainerScreen<?>>#
Drops the entire carried stack outside the menu, then closes the inventory.
Returns: this wrapper, which now refers to the closed inventory

contains ​

contains(ItemStackHelper item): booleanSince1.8.4#

Parameters:

  • item: stack to compare against
Returns: true if any inventory stack equals item
contains(String item): booleanSince1.8.4#

Parameters:

  • item: item ID to search for
Returns: true if any inventory stack has this item ID

create ​

static create(): Inventory<?>Since1.0.8#
Wraps the current container screen, or the player's inventory if no container screen is open. The fallback is a synthetic screen wrapper, so getSlotUnderMouse() cannot be used until its screen is current.
Returns: a wrapper for the open container or the player's inventory
static create(Screen s): Inventory<?>Since1.0.8#
Wraps a container screen without falling back to another inventory. Recognized screen types receive specialized wrappers; other container screens receive a base wrapper.

Parameters:

  • s: the screen to describe
Returns: a handler for the container behind it, or null if it is not a container screen

dragClick ​

dragClick(int[] slots, int mouseButton): Inventory<T extends AbstractContainerScreen<?>>#
Distributes the carried stack across the specified menu slots using quick-craft.

Parameters:

  • slots: the slots to spread across, which do not have to be adjacent or in order
  • mouseButton: 0 selects even distribution; any other value selects greedy distribution
Returns: this inventory wrapper

dropSlot ​

dropSlot(int slot): Inventory<T extends AbstractContainerScreen<?>>Since1.5.0#
Drops one item from a menu slot.

Parameters:

  • slot: index in the menu's full slot list
Returns: this inventory wrapper, for chaining
dropSlot(int slot, boolean stack): Inventory<T extends AbstractContainerScreen<?>>Since1.8.4#
Drops one item or the whole stack from a menu slot.

Parameters:

  • slot: index in the menu's full slot list
  • stack: true to drop the whole stack; false to drop one item
Returns: this inventory wrapper

findFreeHotbarSlot ​

findFreeHotbarSlot(): intSince1.8.4#
Returns: the first empty slot in the hotbar section, or -1 if it has no empty slot.

findFreeInventorySlot ​

findFreeInventorySlot(): intSince1.8.4#
Returns: the first empty slot in the main or hotbar sections, or -1 if neither section has an empty slot.

findFreeSlot ​

findFreeSlot(String... mapIdentifiers): intSince1.8.4#

Parameters:

  • mapIdentifiers: names of the inventory sections to check; unknown names are ignored
Returns: the first empty slot in the specified sections, or -1 if none is empty

findItem ​

findItem(ItemStackHelper item): int[]Since1.8.4#

Parameters:

  • item: stack to compare with each slot
Returns: indices of slots whose stacks equal item
findItem(String item): int[]Since1.8.4#

Parameters:

  • item: item ID to match
Returns: indices of slots whose item ID equals item

getContainerTitle ​

getContainerTitle(): StringSince1.2.3#
Returns: the screen title as plain text

getCurrentSyncId ​

getCurrentSyncId(): intSince1.6.0#
Returns: the menu's synchronization ID

getHeld ​

getHeld(): ItemStackHelper#
Returns: the item stack currently carried by the menu

getItemCount ​

getItemCount(): Map<String, Integer>Since1.8.4#
Returns: item IDs mapped to the sum of their stack counts across non-empty slots

getItems ​

getItems(): List<ItemStackHelper>Since1.8.4#
Returns: non-empty stacks from the menu's full slot list
getItems(String... mapIdentifiers): List<ItemStackHelper>Since1.8.4#

Parameters:

  • mapIdentifiers: names of the inventory sections to include; unknown names are ignored
Returns: non-empty stacks from the specified sections

getLocation ​

getLocation(int slotNum): StringSince1.1.3#
Returns the region name mapped to a menu slot index.

Parameters:

  • slotNum: menu slot index
Returns: the region name containing the slot, or null if it is not mapped

getMap ​

getMap(): Map<String, int[]>Since1.1.3#
Returns this inventory's menu slot indices grouped by region name.
Returns: slot indices grouped by region name

getRawContainer ​

getRawContainer(): T extends AbstractContainerScreen<?>#
Returns the screen wrapped by this inventory, or null after close(). Unlike the methods around it this one carries no @since: the version it arrived in is not recorded anywhere, so it is left unstated rather than guessed. [citation needed]
Returns: the wrapped screen

getSelectedHotbarSlotIndex ​

getSelectedHotbarSlotIndex(): intSince1.2.5#
Returns: the selected hotbar slot index

getSlot ​

getSlot(int slot): ItemStackHelper#

Parameters:

  • slot: menu slot index
Returns: the stack in that menu slot

getSlotPos ​

getSlotPos(int slot): Pos2DSince1.8.4#
Returns the inventory screen's top-left position, not the position of the specified slot.

Parameters:

  • slot: menu slot index; accessing it validates the index and fails if it is out of range
Returns: the screen's top-left position

getSlots ​

getSlots(String... mapIdentifiers): int[]Since1.8.4#

Parameters:

  • mapIdentifiers: names of the inventory sections to include; unknown names are ignored
Returns: menu slot indices in the specified sections, in section order

getSlotUnderMouse ​

getSlotUnderMouse(): intSince1.1.3#
Returns: the menu slot index under the mouse, or -999 if the pointer is outside all slots; throws if this inventory's screen is not open

getTotalSlots ​

getTotalSlots(): int#
Returns: the number of slots in the menu's full slot list

getType ​

getType(): StringSince1.1.3#
Returns: the screen name used to identify this inventory type

grabAll ​

grabAll(int slot): Inventory<T extends AbstractContainerScreen<?>>#
Picks up the selected slot's stack, then attempts to collect matching items from other menu slots into the carried stack.

Parameters:

  • slot: slot whose stack to pick up
Returns: this inventory

is ​

is(String... types): booleanSince1.9.0#
Checks whether this inventory's screen name matches any supplied name.

Parameters:

  • types: screen names to test
Returns: true if the screen name matches one of the supplied names

isContainer ​

isContainer(): booleanSince1.8.4#
Returns: true for container, dispenser, hopper, or shulker-box screens; false otherwise

openGui ​

openGui(): voidSince1.2.8#
Opens this inventory's screen on the Minecraft client thread.

quick ​

quick(int slot): Inventory<T extends AbstractContainerScreen<?>>#
Performs a quick-move click on a menu slot (the usual shift-click action).

Parameters:

  • slot: index in the menu's full slot list
Returns: this inventory wrapper, for chaining

quickAll ​

quickAll(int slot): intSince1.7.0#
Quick-moves items matching the selected slot's stack from its backing container.

Parameters:

  • slot: menu slot index whose stack and backing container determine the matches
Returns: the total stack count in matching slots considered for quick-move
quickAll(int slot, int button): intSince1.7.0#
Quick-moves matching stacks from the selected slot's backing container.

Parameters:

  • slot: menu slot index whose stack and backing container determine the matches
  • button: mouse button value passed with each quick-move click
Returns: the total stack count in matching slots considered for quick-move

setSelectedHotbarSlotIndex ​

setSelectedHotbarSlotIndex(int index): voidSince1.2.5#
Sets the selected hotbar slot; indices outside 0–8 are ignored.

Parameters:

  • index: hotbar slot index

split ​

split(int slot1, int slot2): Inventory<T extends AbstractContainerScreen<?>>#
Performs a secondary-button pickup click on slot1, then a primary-button pickup click on slot2. The slots must be distinct and empty, or this method throws an exception. For quick-craft distribution, use Inventory.dragClick(int[], int).

Parameters:

  • slot1: first empty menu slot
  • slot2: second, distinct empty menu slot
Returns: this inventory wrapper

swapHotbar ​

swapHotbar(int slot, int hotbarSlot): Inventory<T extends AbstractContainerScreen<?>>Since1.6.4#
Swaps a menu slot with a hotbar slot or the offhand slot.

Parameters:

  • slot: menu slot to swap
  • hotbarSlot: hotbar index from 0 to 8, or 40 for offhand
Returns: this inventory

toString ​

toString(): String#
Returns a diagnostic label containing this inventory's current screen name from getType(). The result is not JSON serialization; the screen name is inserted without JSON escaping.
Returns: a label in the form Inventory:{"Type": "screen name"}