Home Migration Guide Getting Started With Scripts Commands Examples
BracketHandlers

IInventory

Importing the class

It might be required for you to import the package if you encounter any issues (like casting an Array), so better be safe than sorry and add the import at the very top of the file.

script.zs
import crafttweaker.api.inventory.IInventory;

Methods

Clears this inventory of all items.

Return Type: void

script.zs
// IInventory.clear() as void
myIInventory.clear();

Marks the inventory as closed, this is used by chests and barrels to determine if they should have a closed texture / model, but other inventories may use it in a different way.

Return Type: void

script.zs
// IInventory.closeInventory(player as MCPlayerEntity) as void
myIInventory.closeInventory(player);
ParameterTypeDescription
Parameter
player
Type
MCPlayerEntity
Description
The player that opened the inventory.

Counts how many of the given Item is in this inventory.

NOTE: This does not work for IItemStacks, so all NBT will be ignored.

Returns: The amount of the Item in this inventory.
Return Type: int

script.zs
// IInventory.count(item as MCItemDefinition) as int
myIInventory.count(<item:minecraft:dirt>);
ParameterTypeDescription
Parameter
item
Type
MCItemDefinition
Description
The Item to look for.

Counts how many ItemStacks in this inventory match the given predicate.

Returns: The amount of ItemStacks in this inventory that match the predicate.
Return Type: int

script.zs
// IInventory.count(predicate as Predicate<IItemStack>) as int
myIInventory.count((stack) => stack.amount == 2);
ParameterTypeDescription
Parameter
predicate
Type
Predicate<IItemStack>
Description
The predicate to test against

Decreases the stack size of the stack in the given slot by the given count.

Returns: A new stack with how much was removed.
Return Type: IItemStack

script.zs
// IInventory.decrStackSize(index as int, count as int) as IItemStack
myIInventory.decrStackSize(2, 2);
ParameterTypeDescriptionOptionalDefaultValue
Parameter
index
Type
int
Description
The slot index to decrement.
Optional
false
DefaultValue
Parameter
count
Type
int
Description
How much should be removed.
Optional
true
DefaultValue
1

Gets how many slots this inventory has.

Example: A hopper will return 5

Returns: The amount of slots this inventory has.
Return Type: int

script.zs
// IInventory.getInventorySize() as int
myIInventory.getInventorySize();

Gets the max stack size that is allowed in this inventory.

This is nearly always 64, but some inventories like the Beacon and Compostor have a limit of 1.

Returns: The max stack size allowed in the inventory.
Return Type: int

script.zs
// IInventory.getInventoryStackLimit() as int
myIInventory.getInventoryStackLimit();

Gets the IItemStack in the given slot.

Returns: The IItemStack in the slot.
Return Type: IItemStack

script.zs
// IInventory.getStackInSlot(index as int) as IItemStack
myIInventory.getStackInSlot(2);
ParameterTypeDescription
Parameter
index
Type
int
Description
The index to get the stack from.

Gets the ItemStacks in this inventory that match the given predicate.

Returns: A list of IItemStacks that match the given predicate.
Return Type: stdlib.List<IItemStack>

script.zs
// IInventory.getStacks(predicate as Predicate<IItemStack>) as stdlib.List<IItemStack>
myIInventory.getStacks((stack) => stack.amount == 2;);
ParameterTypeDescription
Parameter
predicate
Type
Predicate<IItemStack>
Description
The predicate to test against.

Checks if this inventory has any of the given Items.

NOTE: This does not work for IItemStacks, so all NBT will be ignored.

Returns: True if this inventory has any of the items. False otherwise.
Return Type: boolean

script.zs
// IInventory.hasAny(list as stdlib.List<MCItemDefinition>) as boolean
myIInventory.hasAny([<item:minecraft:diamond>]);
ParameterTypeDescription
Parameter
list
Type
stdlib.List<MCItemDefinition>
Description
The Items to look for.

Checks if this inventory is empty.

Returns: True if this inventory is empty. False otherwise.
Return Type: boolean

script.zs
// IInventory.isEmpty() as boolean
myIInventory.isEmpty();

Checks if the given stack is valid for the given slot index.

Returns: True if the stack is valid. False otherwise.
Return Type: boolean

script.zs
// IInventory.isItemValidForSlot(index as int, stack as IItemStack) as boolean
myIInventory.isItemValidForSlot(2, <item:minecraft:dirt>);
ParameterTypeDescription
Parameter
index
Type
int
Description
The slot index to check.
Parameter
stack
Type
IItemStack
Description
The stack to check.

Checks if this IInventory can be used by the player.

Returns: True if the player can use this inventory. False otherwise.
Return Type: boolean

script.zs
// IInventory.isUsableByPlayer(player as MCPlayerEntity) as boolean
myIInventory.isUsableByPlayer(player);
ParameterTypeDescription
Parameter
player
Type
MCPlayerEntity
Description
The player to check if they can use this inventory.

Used by tile entities to ensure that chunks are up to date when they are saved to disk.

Return Type: void

script.zs
// IInventory.markDirty() as void
myIInventory.markDirty();

Marks the inventory as opened, this is used by chests and barrels to determine if they should have an open texture / model, but other inventories may use it in a different way.

Return Type: void

script.zs
// IInventory.openInventory(player as MCPlayerEntity) as void
myIInventory.openInventory(player);
ParameterTypeDescription
Parameter
player
Type
MCPlayerEntity
Description
The player that opened the inventory.

Removes the IItemStack from the given slot and returns it.

Returns: The removed stack from the slot.
Return Type: IItemStack

script.zs
// IInventory.removeStackFromSlot(index as int) as IItemStack
myIInventory.removeStackFromSlot(2);
ParameterTypeDescription
Parameter
index
Type
int
Description
The slot index to remove.

Sets the contents of the given slot to the given IItemStack.

Return Type: void

script.zs
// IInventory.setInventorySlotContents(index as int, stack as IItemStack) as void
myIInventory.setInventorySlotContents(2, <item:minecraft:diamond>);
ParameterTypeDescription
Parameter
index
Type
int
Description
The slot index to set.
Parameter
stack
Type
IItemStack
Description
The IItemStack to put in the slot.

Properties

NameTypeHas GetterHas SetterDescription
Name
inventorySize
Type
int
Has Getter
true
Has Setter
false
Description
Gets how many slots this inventory has.

Example:
A hopper will return 5
Name
inventoryStackLimit
Type
int
Has Getter
true
Has Setter
false
Description
Gets the max stack size that is allowed in this inventory.

This is nearly always 64, but some inventories like the Beacon and Compostor have a limit of 1.
Name
isEmpty
Type
boolean
Has Getter
true
Has Setter
false
Description
Checks if this inventory is empty.