Skip to content

Commit e493199

Browse files
committed
ItemStacks: Adding and Taking Items
1 parent d09fc6c commit e493199

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

chapters/itemstacks.md

+35-9
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ In this chapter you will learn how to use ItemStacks.
1010

1111
* Creating ItemStacks
1212
* Name and Count
13+
* Adding and Taking Items
1314
* Wear
14-
* Copying a stack
1515
* Lua Tables
1616
* Metadata
1717
* More Methods
@@ -39,6 +39,12 @@ else
3939
end
4040
{% endhighlight %}
4141

42+
And you can copy stacks like this:
43+
44+
{% highlight lua %}
45+
local items2 = ItemStack(items)
46+
{% endhighlight %}
47+
4248
## Name and Count
4349

4450
{% highlight lua %}
@@ -58,6 +64,34 @@ else
5864
end
5965
{% endhighlight %}
6066

67+
## Adding and Taking Items
68+
69+
### Adding
70+
71+
Use `add_item` to add items to an ItemStack.
72+
An ItemStack of the items that could not be added is returned.
73+
74+
{% highlight lua %}
75+
local items = ItemStack("default:stone 50")
76+
local to_add = ItemStack("default:stone 100")
77+
local leftovers = items:add_item(to_add)
78+
79+
print("Could not add" .. leftovers:get_count() .. " of the items.")
80+
-- ^ will be 51
81+
{% endhighlight %}
82+
83+
## Taking
84+
85+
The following code takes **up to** 100.
86+
If there aren't enough items in the stack, it will take as much as it can.
87+
88+
{% highlight lua %}
89+
local taken = items:take_item(100)
90+
-- taken is the ItemStack taken from the main ItemStack
91+
92+
print("Took " .. taken:get_count() .. " items")
93+
{% endhighlight %}
94+
6195
## Wear
6296

6397
ItemStacks also have wear on them. Wear is a number out of 65535, the higher is
@@ -77,14 +111,6 @@ items:add_wear(65535 / (max_uses - 1))
77111
When digging a node, the amount of wear a tool gets may depends on the node
78112
being dug. So max_uses varies depending on what is being dug.
79113

80-
## Copying a stack
81-
82-
It's as simple as this:
83-
84-
{% highlight lua %}
85-
local items2 = ItemStack(items)
86-
{% endhighlight %}
87-
88114
## Lua Tables
89115

90116
{% highlight lua %}

0 commit comments

Comments
 (0)