Difference between revisions of "ItemStack"
Jump to navigation
Jump to search
Hybrid Dog (talk | contribs) (→Methods: https://github.com/minetest/minetest/commit/3c637b4bafcef7b90c98a8d656d300ccd8d37eac) |
|||
Line 32: | Line 32: | ||
| <source enclose="none">get_count()</source> | | <source enclose="none">get_count()</source> | ||
| returns number of items on the stack | | returns number of items on the stack | ||
+ | |- | ||
+ | | <source enclose="none">set_count(count)</source> | ||
+ | | returns true/false (success), clears item on failure | ||
|- | |- | ||
| <source enclose="none">get_definition()</source> | | <source enclose="none">get_definition()</source> | ||
Line 41: | Line 44: | ||
| <source enclose="none">get_metadata()</source> | | <source enclose="none">get_metadata()</source> | ||
| returns metadata (a string attached to an item stack) | | returns metadata (a string attached to an item stack) | ||
+ | |- | ||
+ | | <source enclose="none">set_metadata(metadata)</source> | ||
+ | | returns true | ||
|- | |- | ||
| <source enclose="none">get_name()</source> | | <source enclose="none">get_name()</source> | ||
| returns item name (e.g. <source enclose="none">"default:stone"</source>) | | returns item name (e.g. <source enclose="none">"default:stone"</source>) | ||
+ | |- | ||
+ | | <source enclose="none">set_name(itemname)</source> | ||
+ | | returns true/false (success), clears item on failure | ||
|- | |- | ||
| <source enclose="none">get_stack_max()</source> | | <source enclose="none">get_stack_max()</source> | ||
Line 53: | Line 62: | ||
| <source enclose="none">get_wear()</source> | | <source enclose="none">get_wear()</source> | ||
| returns tool wear (<code>0-65535</code>), <code>0</code> for non-tools | | returns tool wear (<code>0-65535</code>), <code>0</code> for non-tools | ||
+ | |- | ||
+ | | <source enclose="none">set_wear(wear)</source> | ||
+ | | returns true/false (success), clears item on failure | ||
|- | |- | ||
| <source enclose="none">[ItemStack]is_empty()</source> | | <source enclose="none">[ItemStack]is_empty()</source> |
Revision as of 12:31, 29 December 2014
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 worn 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
Method | Description |
---|---|
[ItemStack]add_item(item)
|
put some item or stack onto this stack, returns leftover ItemStack
|
add_wear(amount)
|
increases wear by amount if the item is a tool
|
clear()
|
removes all items from the stack, making it empty |
get_count()
|
returns number of items on the stack |
set_count(count)
|
returns true/false (success), clears item on failure |
get_definition()
|
returns the item definition table |
get_free_space()
|
returns get_stack_max() - get_count()
|
get_metadata()
|
returns metadata (a string attached to an item stack) |
set_metadata(metadata)
|
returns true |
get_name()
|
returns item name (e.g. "default:stone" )
|
set_name(itemname)
|
returns true/false (success), clears item on failure |
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 type |
get_wear()
|
returns tool wear (0-65535 ), 0 for non-tools
|
set_wear(wear)
|
returns true/false (success), clears item on failure |
[ItemStack]is_empty()
|
return true if stack is empty
|
is_known()
|
returns true if the item name refers to a defined item type
|
item_fits(item)
|
returns true if item or stack can be fully added to this one |
peek_item(n)
|
copy (don't remove) up to n items from this stack; returns copied ItemStack ; if n is omitted, n=1 is used
|
replace(item)
|
replace the contents of this stack (item can also be an itemstring or table)
|
take_item(n)
|
take (and remove) up to n items from this stack; returns taken ItemStack ; if n is omitted, n=1 is used
|
to_string()
|
returns the stack in itemstring 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
})