ifdef::env-github[]
:tip-caption: :bulb:
:note-caption: :information_source:
:important-caption: :heavy_exclamation_mark:
:caution-caption: :fire:
:warning-caption: :warning:
endif::[]
:toc: left
:toclevels: 5

= Inventory

This module adds an inventory/item system.

== Extension

=== User

[source,lua]
----
self.cdata.inventory

-- return map of fullid => amount
User:getInventory()

-- try to give an item
-- dry: if passed/true, will not affect
-- return true on success or false
User:tryGiveItem(fullid,amount,dry,no_notify)

-- try to take an item from inventory
-- dry: if passed/true, will not affect
-- return true on success or false
User:tryTakeItem(fullid,amount,dry,no_notify)

-- get item amount in the inventory
User:getItemAmount(fullid)

-- return inventory total weight
User:getInventoryWeight()

-- return maximum weight of inventory
User:getInventoryMaxWeight()

User:clearInventory()

-- open a chest by identifier (GData)
-- cb_close(id): called when the chest is closed (optional)
-- cb_in(chest_id, fullid, amount): called when an item is added (optional)
-- cb_out(chest_id, fullid, amount): called when an item is taken (optional)
-- return chest menu or nil
User:openChest(id, max_weight, cb_close, cb_in, cb_out)
----

=== Server

[source,lua]
----
self.cfg
self.items -- item definitions
self.computed_items -- computed item definitions
self.chests -- loaded chests

-- define an inventory item (parametric or plain text data)
-- id: unique item identifier (string, no "." or "|")
-- name: display name, value or genfunction(args)
-- description: value or genfunction(args) (html)
-- menu_builder: (optional) genfunction(args, menu)
-- weight: (optional) value or genfunction(args)
--
-- genfunction are functions returning a correct value as: function(args, ...)
-- where args is a list of {base_idname,args...}
Inventory:defineItem(id,name,description,menu_builder,weight)

-- compute item definition (cached)
-- return computed item or nil
-- computed item {}
--- name
--- description
--- weight
--- menu_builder: can be nil
--- args: parametric args
Inventory:computeItem(fullid)

-- compute weight of a list of items (in inventory/chest format)
Inventory:computeItemsWeight(items)

-- load global chest
-- id: identifier (string)
-- return chest (as inventory, map of fullid => amount)
Inventory:loadChest(id)

-- unload global chest
-- id: identifier (string)
Inventory:unloadChest(id)
----

== Menu

=== inventory

Player inventory menu.

=== inventory.item

Player inventory item sub-menu.

.data
[horizontal]
fullid:: item fullid

=== chest

Chest menu.

.data
[horizontal]
id:: chest id
chest:: loaded chest
max_weight:: chest max weight
cb_close:: cb_close
cb_in:: cb_in
cb_out:: cb_out

=== chest.put

Chest put sub-menu. Same data as `chest`.

=== chest.take

Chest take sub-menu. Same data as `chest`.

== Permission

=== item

Item amount comparison.

`!item.<fullid>.<op>`

[horizontal]
fullid:: item fullid
op:: >x, <x, x (equal) amount

.Examples
[horizontal]
`!item.edible|tacos.>0`:: one or more tacos
`!item.dirty_money.0`:: no dirty money

== Transformer processor

Consume and produce items in transformers.

`items`:: map of item fullid => amount

.Example
----
...
items = {
  ["edible|peach"] = 1
}
----
