# Inventory

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

**Since:** 1.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()](#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getType-) returns the screen name used for identification.  
Slot indices refer to the menu's full slot list. [getMap()](#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_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 {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_click-int-}

```js
click(int slot): Inventory<T extends AbstractContainerScreen<?>>
```

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

**Since:** 1.5.0

### click {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_click-int-int-}

```js
click(int slot, int mouseButton): Inventory<T extends AbstractContainerScreen<?>>
```

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

**Since:** 1.0.8

### close {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_close-}

```js
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 {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_closeAndDrop-}

```js
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 {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_contains-ItemStackHelper-}

```js
contains(ItemStackHelper item): boolean
```

**Parameters:**
- `item`: stack to compare against

**Returns:** `true` if any inventory stack equals `item`

**Since:** 1.8.4

### contains {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_contains-String-}

```js
contains(String item): boolean
```

**Parameters:**
- `item`: item ID to search for

**Returns:** `true` if any inventory stack has this item ID

**Since:** 1.8.4

### create {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_create-}

```js
static create(): Inventory<?>
```

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()](#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getSlotUnderMouse-) cannot be used
until its screen is current.

**Returns:** a wrapper for the open container or the player's inventory

**Since:** 1.0.8

### create {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_create-Screen-}

```js
static create(Screen s): Inventory<?>
```

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

**Since:** 1.0.8

### dragClick {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_dragClick-int[]-int-}

```js
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 {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_dropSlot-int-}

```js
dropSlot(int slot): Inventory<T extends AbstractContainerScreen<?>>
```

Drops one item from a menu slot.

**Parameters:**
- `slot`: index in the menu's full slot list

**Returns:** this inventory wrapper, for chaining

**Since:** 1.5.0

### dropSlot {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_dropSlot-int-boolean-}

```js
dropSlot(int slot, boolean stack): Inventory<T extends AbstractContainerScreen<?>>
```

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

**Since:** 1.8.4

### findFreeHotbarSlot {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_findFreeHotbarSlot-}

```js
findFreeHotbarSlot(): int
```

**Returns:** the first empty slot in the `hotbar` section, or `-1` if it has no empty
slot.

**Since:** 1.8.4

### findFreeInventorySlot {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_findFreeInventorySlot-}

```js
findFreeInventorySlot(): int
```

**Returns:** the first empty slot in the `main` or `hotbar` sections, or `-1`
if neither section has an empty slot.

**Since:** 1.8.4

### findFreeSlot {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_findFreeSlot-String[]-}

```js
findFreeSlot(String... mapIdentifiers): int
```

**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

**Since:** 1.8.4

### findItem {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_findItem-ItemStackHelper-}

```js
findItem(ItemStackHelper item): int[]
```

**Parameters:**
- `item`: stack to compare with each slot

**Returns:** indices of slots whose stacks equal `item`

**Since:** 1.8.4

### findItem {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_findItem-String-}

```js
findItem(String item): int[]
```

**Parameters:**
- `item`: item ID to match

**Returns:** indices of slots whose item ID equals `item`

**Since:** 1.8.4

### getContainerTitle {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getContainerTitle-}

```js
getContainerTitle(): String
```

**Returns:** the screen title as plain text

**Since:** 1.2.3

### getCurrentSyncId {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getCurrentSyncId-}

```js
getCurrentSyncId(): int
```

**Returns:** the menu's synchronization ID

**Since:** 1.6.0

### getHeld {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getHeld-}

```js
getHeld(): ItemStackHelper
```

**Returns:** the item stack currently carried by the menu

### getItemCount {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getItemCount-}

```js
getItemCount(): Map<String, Integer>
```

**Returns:** item IDs mapped to the sum of their stack counts across non-empty slots

**Since:** 1.8.4

### getItems {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getItems-}

```js
getItems(): List<ItemStackHelper>
```

**Returns:** non-empty stacks from the menu's full slot list

**Since:** 1.8.4

### getItems {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getItems-String[]-}

```js
getItems(String... mapIdentifiers): List<ItemStackHelper>
```

**Parameters:**
- `mapIdentifiers`: names of the inventory sections to include; unknown names are ignored

**Returns:** non-empty stacks from the specified sections

**Since:** 1.8.4

### getLocation {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getLocation-int-}

```js
getLocation(int slotNum): String
```

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

**Since:** 1.1.3

### getMap {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getMap-}

```js
getMap(): Map<String, int[]>
```

Returns this inventory's menu slot indices grouped by region name.  
In a Crafter, `preview` identifies the recipe preview, which cannot be taken.

**Returns:** slot indices grouped by region name

**Since:** 1.1.3

### getRawContainer {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getRawContainer-}

```js
getRawContainer(): T extends AbstractContainerScreen<?>
```

Returns the screen wrapped by this inventory, or `null` after [close()](#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_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

### getScreenPos {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getScreenPos-}

```js
getScreenPos(): Pos2D
```

Returns the top-left corner of the inventory panel. A synthetic inventory screen
has no on-screen origin until opened.

**Returns:** the panel position in scaled GUI coordinates

**Since:** 2.3.0

### getSelectedHotbarSlotIndex {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getSelectedHotbarSlotIndex-}

```js
getSelectedHotbarSlotIndex(): int
```

**Returns:** the selected hotbar slot index

**Since:** 1.2.5

### getSlot {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getSlot-int-}

```js
getSlot(int slot): ItemStackHelper
```

**Parameters:**
- `slot`: menu slot index

**Returns:** the stack in that menu slot

### getSlotPos {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getSlotPos-int-}

```js
getSlotPos(int slot): Pos2D
```

Locates the top-left corner of a slot in the current layout of the wrapped screen.  
A synthetic inventory screen has no on-screen origin until opened.  
Use [getScreenPos()](#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getScreenPos-) for the panel origin returned by older JsMacros versions.

**Parameters:**
- `slot`: menu slot index

**Returns:** the slot position in scaled GUI coordinates

**Since:** 1.8.4

### getSlots {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getSlots-String[]-}

```js
getSlots(String... mapIdentifiers): int[]
```

**Parameters:**
- `mapIdentifiers`: names of the inventory sections to include; unknown names are ignored

**Returns:** menu slot indices in the specified sections, in section order

**Since:** 1.8.4

### getSlotUnderMouse {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getSlotUnderMouse-}

```js
getSlotUnderMouse(): int
```

**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

**Since:** 1.1.3

### getTotalSlots {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getTotalSlots-}

```js
getTotalSlots(): int
```

**Returns:** the number of slots in the menu's full slot list

### getType {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getType-}

```js
getType(): String
```

**Returns:** the screen name used to identify this inventory type

**Since:** 1.1.3

### grabAll {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_grabAll-int-}

```js
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 {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_is-String[]-}

```js
is(String... types): boolean
```

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

**Since:** 1.9.0

### isContainer {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_isContainer-}

```js
isContainer(): boolean
```

**Returns:** `true` for container, dispenser, hopper, or shulker-box screens; `false` otherwise

**Since:** 1.8.4

### openGui {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_openGui-}

```js
openGui(): void
```

Opens this inventory's screen on the Minecraft client thread.

**Since:** 1.2.8

### quick {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_quick-int-}

```js
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 {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_quickAll-int-}

```js
quickAll(int slot): int
```

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

**Since:** 1.7.0

### quickAll {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_quickAll-int-int-}

```js
quickAll(int slot, int button): int
```

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

**Since:** 1.7.0

### setSelectedHotbarSlotIndex {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_setSelectedHotbarSlotIndex-int-}

```js
setSelectedHotbarSlotIndex(int index): void
```

Sets the selected hotbar slot; indices outside `0`–`8` are ignored.

**Parameters:**
- `index`: hotbar slot index

**Since:** 1.2.5

### split {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_split-int-int-}

```js
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)](#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_dragClick-int[]-int-).

**Parameters:**
- `slot1`: first empty menu slot
- `slot2`: second, distinct empty menu slot

**Returns:** this inventory wrapper

### swapHotbar {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_swapHotbar-int-int-}

```js
swapHotbar(int slot, int hotbarSlot): Inventory<T extends AbstractContainerScreen<?>>
```

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

**Since:** 1.6.4

### toString {#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_toString-}

```js
toString(): String
```

Returns a diagnostic label containing this inventory's current screen name from [getType()](#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_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"}`
