LuaInventory

A storage of item stacks.

class LuaInventory - sort
clear() Make this inventory empty.
can_insert(items) → boolean Can at least some items be inserted?
insert(items) → uint Insert items into this inventory.
remove(items) → uint Remove items from this inventory.
get_item_count(item) → uint Get the number of all or some items in this inventory.
is_empty() → boolean Does this inventory contain nothing?
get_contents() → dictionary stringuint Get counts of all items in this inventory.
supports_bar() → boolean Does this inventory support a bar?
get_bar() → uint Get the current bar.
set_bar(bar) Set the current bar.
supports_filters() → boolean If this inventory supports filters.
is_filtered() → boolean If this inventory supports filters and has at least 1 filter set.
can_set_filter(index, filter) → boolean If the given inventory slot filter can be set to the given filter.
get_filter(index) → string Gets the filter for the given item stack index.
set_filter(index, filter) → boolean Sets the filter for the given item stack index.
find_item_stack(item) → LuaItemStack Gets the first LuaItemStack in the inventory that matches the given item name.
find_empty_stack(item) → LuaItemStack Finds the first empty stack.
count_empty_stacks(include_filtered) → uint Counts the number of empty stacks.
get_insertable_count(item) Gets the number of the given item that can be inserted into this inventory.
sort_and_merge() Sorts and merges the items in this inventory.
operator # :: uint [R] Get the number of slots in this inventory.
index :: uint [R] The inventory index this inventory uses.
entity_owner :: LuaEntity [R] The entity that owns this inventory or nil if this isn't owned by an entity.
player_owner :: LuaPlayer [R] The player that owns this inventory or nil if this isn't owned by a player.
equipment_owner :: LuaEntity [R] The equipment that owns this inventory or nil if this isn't owned by an equipment.
operator [] :: LuaItemStack [R] The indexing operator.
valid :: boolean [R] Is this object valid?
help() → string All methods, and properties that this object supports.
clear()

Make this inventory empty.

can_insert(items) → boolean

Can at least some items be inserted?

Parameters
items :: ItemStackSpecification: Items that would be inserted.
Return value
true if at least a part of the given items could be inserted into this inventory.
insert(items) → uint

Insert items into this inventory.

Parameters
items :: ItemStackSpecification: Items to insert.
Return value
Number of items actually inserted.
remove(items) → uint

Remove items from this inventory.

Parameters
items :: ItemStackSpecification: Items to remove.
Return value
Number of items actually removed.
get_item_count(item) → uint

Get the number of all or some items in this inventory.

Parameters
item :: string (optional): Prototype name of the item to count. If not specified, count all items.
is_empty() → boolean

Does this inventory contain nothing?

get_contents() → dictionary stringuint

Get counts of all items in this inventory.

Return value
The counts, indexed by item names.
supports_bar() → boolean

Does this inventory support a bar? Bar is the draggable red thing, found for example on chests, that limits the portion of the inventory that may be manipulated by machines.

Note: "Supporting a bar" doesn't mean that the bar is set to some nontrivial value. Supporting a bar means the inventory supports having this limit at all. The character's inventory is an example of an inventory without a bar; the wooden chest's inventory is an example of one with a bar.
get_bar() → uint

Get the current bar. This is the index at which the red area starts.

Note: Only useable if this inventory supports having a bar.
set_bar(bar)

Set the current bar.

Parameters
bar :: uint (optional): The new limit. Omitting this parameter will clear the limit.
Note: Only useable if this inventory supports having a bar.
supports_filters() → boolean

If this inventory supports filters.

is_filtered() → boolean

If this inventory supports filters and has at least 1 filter set.

can_set_filter(index, filter) → boolean

If the given inventory slot filter can be set to the given filter.

Parameters
index :: uint: The item stack index
filter :: string: The item name of the filter
get_filter(index) → string

Gets the filter for the given item stack index.

Parameters
index :: uint: The item stack index
Return value
The current filter or nil if none.
set_filter(index, filter) → boolean

Sets the filter for the given item stack index.

Parameters
index :: uint: The item stack index
filter :: string: The new filter or nil to erase the filter
Return value
If the filter was allowed to be set.
Note: Some inventory slots don't allow some filters (gun ammo can't be filtered for non-ammo).
find_item_stack(item) → LuaItemStack

Gets the first LuaItemStack in the inventory that matches the given item name.

Parameters
item :: string: The item name to find
Return value
or nil.
Note: This will also return the stack index if one is found that matches as a second return value.
find_empty_stack(item) → LuaItemStack

Finds the first empty stack. Filtered slots are excluded unless a filter item is given.

Parameters
item :: string (optional): If given, empty stacks that are filtered for this item will be included.
Return value
or nil.
Note: This will also return the stack index if one is found that matches as a second return value.
count_empty_stacks(include_filtered) → uint

Counts the number of empty stacks.

Parameters
include_filtered :: boolean (optional): If true, filtered slots will be included. Defaults to false.
get_insertable_count(item)

Gets the number of the given item that can be inserted into this inventory.

Parameters
item :: string: The item to check.
Note: This is a "best guess" number; things like assembling machine filtered slots, module slots, items with durability, and items with mixed health will cause the result to be inaccurate.
Note: The main use for this is in checking how many of a basic item can fit into a basic inventory.
Note: This accounts for the 'bar' on the inventory.
sort_and_merge()

Sorts and merges the items in this inventory.

operator # :: uint [Read-only]

Get the number of slots in this inventory.

Example
Will print the number of slots in the player's main inventory.
game.player.print(#game.player.get_main_inventory())
index :: uint [Read-only]

The inventory index this inventory uses.

entity_owner :: LuaEntity [Read-only]

The entity that owns this inventory or nil if this isn't owned by an entity.

player_owner :: LuaPlayer [Read-only]

The player that owns this inventory or nil if this isn't owned by a player.

equipment_owner :: LuaEntity [Read-only]

The equipment that owns this inventory or nil if this isn't owned by an equipment.

operator [] :: LuaItemStack [Read-only]

The indexing operator.

Example
Will get the first item in the player's inventory.
game.player.get_main_inventory()[1]