Entity.Inventory module

For working with inventories.

Usage

local Inventory = require('__stdlib__/stdlib/entity/inventory')

Functions

each(inventory, func[, ...]) Given a function, apply it to each slot in the given inventory.
each_reverse(inventory, func[, ...]) Given a function, apply it to each slot in the given inventory.
copy_as_simple_stacks(src, dest[, clear=false]) Copies the contents of source inventory to destination inventory by using Concepts.SimpleItemStack.
get_blueprint(stack[, is_bp_setup][, no_book]) Return a blueprint stack from either stack or blueprint_book
is_named_bp(stack, label) Is the stack a blueprint with label?
get_item_or_filter(inventory, idx[, item_only][, filter_only]) Returns either the item at a position, or the filter at the position if there isn’t an item there.
transfer_inventory(source, destination[, source_filters=nil]) Transfer items from 1 inventory to another.
swap_inventory(source, destination) Swap items from 1 inventory to another.

Functions

# each(inventory, func[, ...])

Given a function, apply it to each slot in the given inventory.

Passes the index of a slot as the second argument to the given function.

Iteration is aborted if the applied function returns true for any element during iteration.

Parameters:
  • inventory : (LuaInventory) the inventory to iterate
  • func : (function) the function to apply to values
  • ... : additional arguments passed to the function (optional)
Returns:
  • (nil or LuaItemStack) the slot where the iteration was aborted OR nil if not aborted
# each_reverse(inventory, func[, ...])

Given a function, apply it to each slot in the given inventory.

Passes the index of a slot as the second argument to the given function.

Iteration is aborted if the applied function returns true for any element during iteration.

Iteration is performed from last to first in order to support dynamically sized inventories.

Parameters:
  • inventory : (LuaInventory) the inventory to iterate
  • func : (function) the function to apply to values
  • ... : additional arguments passed to the function (optional)
Returns:
  • (nil or LuaItemStack) the slot where the iteration was aborted OR nil if not aborted
# copy_as_simple_stacks(src, dest[, clear=false])

Copies the contents of source inventory to destination inventory by using Concepts.SimpleItemStack.

Parameters:
  • src : (LuaInventory) the source inventory
  • dest : (LuaInventory) the destination inventory
  • clear : (boolean) clear the contents of the source inventory (default: false)
Returns:
# get_blueprint(stack[, is_bp_setup][, no_book])

Return a blueprint stack from either stack or blueprint_book

Parameters:
  • stack : (LuaItemStack)
  • is_bp_setup : (bool) (optional)
  • no_book : (bool) (optional)
Returns:
# is_named_bp(stack, label)

Is the stack a blueprint with label?

Parameters: Returns:
  • (bool)
# get_item_or_filter(inventory, idx[, item_only][, filter_only])

Returns either the item at a position, or the filter at the position if there isn’t an item there.

Parameters:
  • inventory : (LuaInventory)
  • idx : (int)
  • item_only : (bool) (optional)
  • filter_only : (bool) (optional)
Returns:
  • the item or filter
# transfer_inventory(source, destination[, source_filters=nil])

Transfer items from 1 inventory to another.

Parameters:
  • source : (LuaInventory)
  • destination : (LuaInventory)
  • source_filters : (table) the filters to use if the source is not filtered/filterable (default: nil)
Returns:
  • (nil or table) the filters if the destination does not support filters
# swap_inventory(source, destination)

Swap items from 1 inventory to another.

Parameters: