|
| 1 | +--- |
| 2 | +title: Inventories |
| 3 | +layout: default |
| 4 | +root: ../ |
| 5 | +--- |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +In this chapter you will learn how to use manipulate inventories, whether that |
| 10 | +is a player inventory, a node inventory, or a detached inventory. |
| 11 | +This chapter assumes that you already know how to create and manipulate |
| 12 | +[ItemStacks](itemstacks.html). |
| 13 | + |
| 14 | +* Basic Concepts. |
| 15 | +* Types of Inventories. |
| 16 | + * Player Inventories. |
| 17 | + * Node Inventories. |
| 18 | + * Detached Inventories. |
| 19 | +* InvRef and Lists. |
| 20 | + * Type of inventory. |
| 21 | + * List sizes. |
| 22 | + * List is empty. |
| 23 | + * Lua Tables. |
| 24 | + * Lua Tables for Lists. |
| 25 | +* InvRef, Items and Stacks. |
| 26 | + * Adding to a list. |
| 27 | + * Checking for room. |
| 28 | + * Taking items. |
| 29 | + * Contains. |
| 30 | + * Manipulating Stacks. |
| 31 | + |
| 32 | +## Basic Concepts |
| 33 | + |
| 34 | +Components of an inventory: |
| 35 | + |
| 36 | +* An **Inventory** is a collection of **Inventory List**s (also called a **list** when in the context of inventories). |
| 37 | +* An **Inventory List** is an array of **slot**s. (By array, I mean a table indexed by numbers). |
| 38 | +* A **slot** is a place a stack can be - there may be a stack there or there may not. |
| 39 | +* An **InvRef** is an object that represents an inventory, and has functions to manipulate it. |
| 40 | + |
| 41 | +There are three ways you can get inventories: |
| 42 | + |
| 43 | +* **Player Inventories** - an inventory attached to a player. |
| 44 | +* **Node Inventories** - an inventory attached to a node. |
| 45 | +* **Detached Inventories** - an inventory which is not attached to a node or player. |
| 46 | + |
| 47 | +<figure> |
| 48 | + <img src="{{ page.root }}/static/inventories_lists.png" alt="The player inventory formspec, with annotated list names."> |
| 49 | + <figcaption> |
| 50 | + This image shows the two inventories visible when you press i. |
| 51 | + The gray boxes are inventory lists.<br /> |
| 52 | + The creative inventory, left (in red) is detached and it made up of a |
| 53 | + single list.<br /> |
| 54 | + The player inventory, right (in blue) is a player inventory |
| 55 | + and is made up of three lists.<br /> |
| 56 | + Note that the trash can is a <a href="formspecs.html">formspec</a> |
| 57 | + element, and is not part of the inventory. |
| 58 | + </figcaption> |
| 59 | +</figure> |
| 60 | + |
| 61 | + |
| 62 | +## Types of Inventories |
| 63 | + |
| 64 | +There are three types of Inventories. |
| 65 | + |
| 66 | +### Player Inventories. |
| 67 | + |
| 68 | +This is what you see when you press i. |
| 69 | +A player inventory usually has two grids, one for the main inventory, one for crafting. |
| 70 | + |
| 71 | +{% highlight lua %} |
| 72 | +local inv = minetest.get_inventory({type="player", name="celeron55"}) |
| 73 | +{% endhighlight %} |
| 74 | + |
| 75 | +### Node Inventories. |
| 76 | + |
| 77 | +An inventory related to a position, such as a chest. |
| 78 | +The node must be loaded, as it's stored in [Node Metadata](node_metadata.html). |
| 79 | + |
| 80 | +{% highlight lua %} |
| 81 | +local inv = minetest.get_inventory({type="node", pos={x=, y=, z=}}) |
| 82 | +{% endhighlight %} |
| 83 | + |
| 84 | +### Detached Inventories |
| 85 | + |
| 86 | +A detached inventory is independent of players and nodes. |
| 87 | +One example of a detached inventory is the creative inventory is detached, |
| 88 | +as all players see the same inventory. |
| 89 | +You may also use this if you want multiple chests to share the same inventory. |
| 90 | + |
| 91 | +This is how you get a detached inventory: |
| 92 | + |
| 93 | +{% highlight lua %} |
| 94 | +local inv = minetest.get_inventory({type="detached", name="inventory_name"}) |
| 95 | +{% endhighlight %} |
| 96 | + |
| 97 | +And this is how you can create one: |
| 98 | + |
| 99 | +{% highlight lua %} |
| 100 | +minetest.create_detached_inventory("inventory_name", callbacks) |
| 101 | +{% endhighlight %} |
| 102 | + |
| 103 | +Creates a detached inventory. If it already exists, it is cleared. |
| 104 | +You can supply a [table of callbacks](../lua_api.html#detached-inventory-callbacks). |
| 105 | + |
| 106 | +## InvRef and Lists |
| 107 | + |
| 108 | +### Type of Inventory |
| 109 | + |
| 110 | +You can check where the inventory is from by doing: |
| 111 | + |
| 112 | +{% highlight lua %} |
| 113 | +local location = inv:get_location() |
| 114 | +{% endhighlight %} |
| 115 | + |
| 116 | +It will return a table like the one passed to `minetest.get_inventory()`. |
| 117 | + |
| 118 | +If the location is unknown, `{type="undefined"}` is returned. |
| 119 | + |
| 120 | +### List sizes |
| 121 | + |
| 122 | +Inventory lists have a size, for example `main` has size of 32 slots by default. |
| 123 | +They also have a width, which is used to divide them into a grid. |
| 124 | + |
| 125 | +{% highlight lua %} |
| 126 | +if inv:set_size("main", 32) then |
| 127 | + inv:set_width("main", 8) |
| 128 | + print("size: " .. inv.get_size("main")) |
| 129 | + print("width: " .. inv:get_width("main")) |
| 130 | +else |
| 131 | + print("Error!") |
| 132 | +end |
| 133 | +{% endhighlight %} |
| 134 | + |
| 135 | +<!--The width and height of an inventory in a [formspec](formspecs.html) is |
| 136 | +determined by the formspec element, not by the inventory. By that I mean |
| 137 | +a list doesn't have a width or height, only the maximum number of stacks/slots.--> |
| 138 | + |
| 139 | +### List is empty |
| 140 | + |
| 141 | +{% highlight lua %} |
| 142 | +if inv:is_empty("main") then |
| 143 | + print("The list is empty!") |
| 144 | +end |
| 145 | +{% endhighlight %} |
| 146 | + |
| 147 | +### Lua Tables |
| 148 | + |
| 149 | +You can convert an inventory to a Lua table using: |
| 150 | + |
| 151 | +{% highlight lua %} |
| 152 | +local lists = inv:get_lists() |
| 153 | +{% endhighlight %} |
| 154 | + |
| 155 | +It will be in this form: |
| 156 | + |
| 157 | +{% highlight lua %} |
| 158 | +{ |
| 159 | + list_one = { |
| 160 | + ItemStack, |
| 161 | + ItemStack, |
| 162 | + ItemStack, |
| 163 | + ItemStack, |
| 164 | + -- inv:get_size("list_one") elements |
| 165 | + }, |
| 166 | + list_two = { |
| 167 | + ItemStack, |
| 168 | + ItemStack, |
| 169 | + ItemStack, |
| 170 | + ItemStack, |
| 171 | + -- inv:get_size("list_two") elements |
| 172 | + } |
| 173 | +} |
| 174 | +{% endhighlight %} |
| 175 | + |
| 176 | +You can then set an inventory like this: |
| 177 | + |
| 178 | +{% highlight lua %} |
| 179 | +inv:set_lists(lists) |
| 180 | +{% endhighlight %} |
| 181 | + |
| 182 | +Please note that the sizes of lists will not change. |
| 183 | + |
| 184 | +### Lua Tables for Lists |
| 185 | + |
| 186 | +You can do the same as above, but for individual lists |
| 187 | + |
| 188 | +{% highlight lua %} |
| 189 | +local list = inv:get_list("list_one") |
| 190 | +{% endhighlight %} |
| 191 | + |
| 192 | +It will be in this form: |
| 193 | + |
| 194 | +{% highlight lua %} |
| 195 | +{ |
| 196 | + ItemStack, |
| 197 | + ItemStack, |
| 198 | + ItemStack, |
| 199 | + ItemStack, |
| 200 | + -- inv:get_size("list_one") elements |
| 201 | +} |
| 202 | +{% endhighlight %} |
| 203 | + |
| 204 | +You can then set the list like this: |
| 205 | + |
| 206 | +{% highlight lua %} |
| 207 | +inv:set_list("list_one", list) |
| 208 | +{% endhighlight %} |
| 209 | + |
| 210 | +Please note that the sizes of lists will not change. |
| 211 | + |
| 212 | +## InvRef, Items and Items |
| 213 | + |
| 214 | +### Adding to a list |
| 215 | + |
| 216 | +{% highlight lua %} |
| 217 | +local stack = ItemStack("default:stone 99") |
| 218 | +local leftover = inv:add_item("main", stack) |
| 219 | +if leftover:get_count() > 0 then |
| 220 | + print("Inventory is full! " .. leftover:get_count() .. " items weren't added") |
| 221 | +end |
| 222 | +{% endhighlight %} |
| 223 | + |
| 224 | +`"main"` is the name of the list you're adding to. |
| 225 | + |
| 226 | +### Checking for room |
| 227 | + |
| 228 | +{% highlight lua %} |
| 229 | +if not inv:room_for_item("main", stack) then |
| 230 | + print("Not enough room!") |
| 231 | +end |
| 232 | +{% endhighlight %} |
| 233 | + |
| 234 | +### Taking items |
| 235 | + |
| 236 | +{% highlight lua %} |
| 237 | +local taken = inv:remove_item("main", stack) |
| 238 | +print("Took " .. taken:get_count()) |
| 239 | +{% endhighlight %} |
| 240 | + |
| 241 | +### Contains |
| 242 | + |
| 243 | +This works if the item count is split up over multiple stacks, |
| 244 | +for example looking for "default:stone 200" will work if there |
| 245 | +are stacks of 99 + 95 + 6. |
| 246 | + |
| 247 | +{% highlight lua %} |
| 248 | +if not inv:contains_item(listname, stack) then |
| 249 | + print("Item not in inventory!") |
| 250 | +end |
| 251 | +{% endhighlight %} |
| 252 | + |
| 253 | +### Manipulating Stacks |
| 254 | + |
| 255 | +Finally, you can manipulate individual stacks like so: |
| 256 | + |
| 257 | + |
| 258 | +{% highlight lua %} |
| 259 | +local stack = inv:get_stack(listname, 0) |
| 260 | +inv:set_stack(listname, 0, stack) |
| 261 | +{% endhighlight %} |
0 commit comments