ItemStack
A stack of items. Can be created via ItemStack(itemstack or itemstring or table or nil)
.
Please note that all functions returning ItemStack
return its copy which modifications won't affect original ItemStack
.
Definitions
Items and item stacks can exist in three formats:
- Serialized; This is called
stackstring
oritemstring
:
'default:dirt 5'
'default:pick_wood 21323'
'default:apple'
- Table format:
{name="default:dirt", count=5, wear=0, metadata=""}
— 5 dirt nodes{name="default:pick_wood", count=1, wear=21323, metadata=""}
— wooden pick about 1/3 weared out{name="default:apple", count=1, wear=0, metadata=""}
— an apple.
- ItemStack. C++ native format with many helper methods. Useful for converting between formats. See this article for details.
Methods
[ItemStack]add_item(item)
— put some item or stack onto this stack, returns leftoverItemStack
add_wear(amount)
— increases wear byamount
if the item is a toolclear()
— removes all items from the stack, making it emptyget_count()
— returns number of items on the stackget_definition()
— returns the item definition tableget_free_space()
— returnsget_stack_max() - get_count()
get_metadata()
— returns metadata (a string attached to an item stack)get_name()
— returns item name (e.g."default:stone"
)get_stack_max()
— returns the maximum size of the stack (depends on the item)get_tool_capabilities()
— returns the digging properties of the item, or those of the hand if none are defined for this item typeget_wear()
— returns tool wear (0-65535
),0
for non-tools[ItemStack]is_empty()
— returntrue
if stack is emptyis_known()
— returnstrue
if the item name refers to a defined item typeitem_fits(item)
— returns true if item or stack can be fully added to this onepeek_item(n)
— copy (don't remove) up ton
items from this stack; returns copiedItemStack
; ifn
is omitted,n=1
is usedreplace(item)
— replace the contents of this stack (item
can also be an itemstring or table)take_item(n)
— take (and remove) up ton
items from this stack; returns takenItemStack
; ifn
is omitted,n=1
is usedto_string()
— returns the stack initemstring
form[ItemStack]to_table()
— returns the stack in Lua table form
Example
minetest.register_craftitem("modname:tool", {
description = "Example Tool",
inventory_image = "modname_test.png",
on_use = function(itemstack, user, pointed_thing)
-- Takes one item from the stack
itemstack:take_item()
return itemstack
end
})