Containers turns props with certain models into storage for items.
A model is registered with Container:register_prop together with the size of its
inventory, its name, its description and its sounds; common furniture, crates and boxes of
Half-Life 2 are registered here. A prop that is spawned with such a model becomes
persistent, shows its name when it is looked at and can be opened from its interaction menu
by a player within reach of it. Its inventory is created the first time it is opened, or
when the map starts if it holds items, and is closed for a player who moves out of reach.
The items are saved in the database as being in the container, which the prop knows by
the number in its container_id field, and are loaded along with the other items of the
map.
Staff can give a container a name of its own, a message that is shown to whoever opens it
and a password that has to be entered before it opens, and fill it with random items.
All of this is done with the Container Tool, which needs the manage_containers
permission (and fill_containers to fill), or from code with
Container:set_container_name, Container:set_container_message,
Container:set_container_password and Container:fill. The name, the message and the
password are kept on the prop in its container_name, container_message and
container_password fields, so they are saved with it, as is its container_id. A
wrong password makes the player wait for container_password_delay seconds before the
next attempt.
Random items are picked among all registered items but the bases and those that set
ITEM.lootable = false, optionally from one item category.
When a container prop is removed, its items are destroyed with it, or dropped on the
ground if the container_spill_items config is on.
Hooks: PlayerCanOpenContainer and ShouldAskContainerPassword decide who opens a
container and who has to enter its password, PlayerEnteredContainerPassword tells about
every entered password, PreContainerOpen is run before a container is shown to a player
and OnContainerRemoved after the items of a removed container have been dealt with.
Summary
Hooks
Methods
Container:get_inventory.PlayerCanOpenContainer hook must not object.Container:set_container_name, or else the name that is registered for its model.lootable to false.container_spill_items config is on and destroys them otherwise, and runs the OnContainerRemoved hook.PreContainerOpen hook, shows its inventory and its message to the player.Hooks
Implements hook CanContainMoney
Allows the containers, physics props with a container model, to contain money.
Parameters
- object Entity
Returns
- Boolean true if the entity is a container, nil otherwise
Implements hook CanItemMenuOpen
Prevents opening the menu of items that are stored inside of containers.
Parameters
- item_obj Item
Returns
- Boolean false to prevent the menu from opening, nil otherwise
Implements hook CreateEntityInteractions
Adds the 'open' option to the interactions menu of the container props. The option has a lock for an icon if the container asks for a password.
Parameters
- menu Panel the interactions menu
- entity Entity
Implements hook DrawEntityTargetID
Draws the name and the description of the container that the local player is looking at.
Parameters
- entity Entity
- x Number screen position
- y Number screen position
- dist Number distance between the local player and the entity
Closes the inventory of a removed entity for everyone who views it and deletes the inventory from the server cache. If the entity is a container, its items are destroyed or dropped on the ground as well, unless the map is unloading or being cleaned up; in that case they wait for the container to come back.
Parameters
- entity Entity
See also
Implements hook GetEntityName
Returns the name of the container: its custom name, or the translated name of its model.
Parameters
- entity Entity
Returns
- String name, or nil if the entity is not a container
Implements hook OnInventoryClosed
Plays the closing sound of the container when its inventory gets closed.
Parameters
- _actor Player
- inventory Inventory
Implements hook OnMapItemsLoaded
Gives the containers of the map the items that have been loaded for them. Containers that are not there yet get them once they are.
Parameters
- items List<Item>
Implements hook PersistenceLoad
Networks the custom names, the password marks and the money of the containers that have been loaded with the persistent entities, and gives the containers the items that have been loaded for them. This is done on the next tick, once every handler of the hook has spawned its entities and given them their saved fields back.
Container:PlayerSpawnedProp(_actor, model, entity)
server plugins/containers/plugin/sv_hooks.lua:133Makes the spawned prop persistent if its model is a container, and gives it the number its items are saved under, which saves the persistent entities shortly afterward.
Parameters
- _actor Player
- model String
- entity Entity
Ends the map cleanup, after which removed containers lose their items again.
Implements hook PostPersistenceSave
Gives the containers their inventories back once the persistent entities have been saved, so that the containers that are open at that moment keep working.
Marks that the map is being cleaned up, so that the items of the containers that the cleanup removes are left alone: the persistent props are saved right before the cleanup and spawned again right after it, and their items, which stay saved as being in them, come back with them.
Implements hook PrePersistenceSave
Takes the inventory off every container before the persistent entities are saved, so that it is not saved with it. The inventories are put back by the 'PostPersistenceSave' hook.
Implements hook RegisterPermissions
Registers the 'manage_containers' and 'fill_containers' level design permissions.
Methods
Remembers loaded items of a container whose inventory does not exist, until the
container claims them with Container:get_inventory.
Parameters
- container_id Number
- items List<Item>
Returns all the registered containers.
Returns
- Map container data, keyed by the lowercase path to the model
Checks whether a player may open a container right now: the entity has to be a
container within reach of the player, and the PlayerCanOpenContainer hook must not
object. The password of the container is not a part of this check.
Parameters
- actor Player
- entity Entity the container prop
Returns
- Boolean true if the player may open the container
- String error phrase when they may not and there is something to tell them
Container:check_container_password(entity, password)
server plugins/containers/plugin/sv_plugin.lua:355Checks a text against the password of a container. The spaces around the text are ignored, as they are when the password is set.
Parameters
- entity Entity the container prop
- password String the text to check
Returns
- Boolean true if the text is the password; false if it is not, or if the container has no password
Creates the inventory of every container that has loaded items waiting for it, which puts the items into it.
Fills a container with random items until the given share of its slots is taken. Items
are picked with Container:get_loot_items; an item that does not fit, or that would
take the container past the share, is skipped. What is in the container already counts
towards the share.
-- Fills half of the container with food and drinks.
local added, error_text = Container:fill(entity, 0.5, 'item.category.consumables')
Parameters
- entity Entity the container prop
- share Number optional, defaults to
1share of the slots to fill, from 0 to 1 - category String optional, defaults to
nilitem category to pick from; all categories if nil
Returns
- Number amount of items that were added, or false if nothing could be added
- String error phrase when nothing could be added
Finds the container data that is registered for the model.
Parameters
- model String path to the model; case-insensitive
Returns
- Map container data, or nil if the model is not a container
Returns the container data of an entity, if the entity is a container: a valid physics prop with a registered model.
Parameters
- entity Entity
Returns
- Map container data, or nil if the entity is not a container
Returns the number that the items of a container are saved under, giving the container
one if it has none yet. The number is kept in the container_id field of the prop and
comes from the item instance ids, so no two containers ever share one. A prop that gets
a number has the persistent entities saved a second later, so that the prop is saved
with its number before its items can be.
Parameters
- entity Entity the container prop
Returns
- Number
Returns the message that is shown to the players who open a container.
Parameters
- entity Entity the container prop
Returns
- String the message, or nil if the container has none
Returns the name of a container: the one it was given with
Container:set_container_name, or else the name that is registered for its model.
local name, is_custom = Container:get_container_name(entity)
local text = is_custom and name or t(name)
Parameters
- entity Entity the container prop
Returns
- String the custom name as it was entered, or the language phrase of the default name; nil if the model of the entity is not registered
- Boolean true if the name is a custom one
Returns the inventory of a container, creating it if it does not exist yet. The inventory takes in the items that have been loaded for the container in the meantime.
Parameters
- entity Entity the container prop
Returns
- Inventory the inventory, or nil if the entity is not a container
Returns the categories of the items that containers can be filled with.
Returns
- List<String> item category IDs in alphabetical order
Returns the item templates that containers can be filled with: every registered item
except for the bases and the items that set lootable to false.
-- In an item file: keeps the item out of the randomly filled containers.
ITEM.lootable = false
Parameters
- category String optional, defaults to
nilitem category to pick from, e.g. 'item.category.consumables'; items of all categories if nil
Returns
- List<Item> item templates
Returns the loaded items of a container that wait for its inventory.
Parameters
- entity Entity the container prop
Returns
- List<Item> the items that are still loaded and saved in the container
Container:handle_removal(entity, inventory, item_ids)
server plugins/containers/plugin/sv_plugin.lua:613Deals with the items of a container whose prop is being removed: drops them on the
ground if the container_spill_items config is on and destroys them otherwise, and
runs the OnContainerRemoved hook. Does nothing while the server is shutting down or
changing the map, or while the map is being cleaned up: the containers are saved then,
and their items stay saved as being in them, to come back with them. Items that turn out
to be in the world or in another inventory are left alone.
The items are dropped or destroyed on the next tick. A map that is unloading never gets there, so the items of the containers are safe even if the shutdown was not noticed.
Parameters
- entity Entity the container prop
- inventory Inventory the inventory of the container, nil if it was never created
- item_ids List<Number> instance ids of the items of the container
Checks whether a password has to be entered to open a container. The password itself is only known to the server.
Parameters
- entity Entity the container prop
Returns
- Boolean
Checks whether an entity is a container: a valid physics prop with a registered model.
Parameters
- entity Entity
Returns
- Boolean
Opens a container for a player without any checks: plays its opening sound, runs the
PreContainerOpen hook, shows its inventory and its message to the player.
Parameters
- actor Player
- entity Entity the container prop
Returns
- Boolean true if the container was opened, false if the entity is not a container
Makes props with the specified model(s) work as containers.
Container:register_prop('models/props_c17/FurnitureDrawer002a.mdl', {
name = 'container.small_drawer.title',
desc = 'container.small_drawer.desc',
w = 2,
h = 1,
open_sound = 'physics/wood/wood_plank_impact_soft1.wav',
close_sound = 'physics/wood/wood_box_impact_hard6.wav'
})
Parameters
- model String/List<String> path to the model, or a list of them
- data Map container data: name and desc (language phrases), w and h (size of the inventory in slots), and optionally open_sound and close_sound
Unloads the loaded items whose container prop is not on the map, ten seconds from now, once the persistent entities have had the time to be spawned. Their rows stay as they are, so they come back if the prop does; each of them is reported in the console.
Opens a container for a player the way the 'open' option of its interaction menu does: checks that the player may open it, then opens it right away, or asks for its password first if it has one.
Parameters
- actor Player
- entity Entity the container prop
Returns
- Boolean true if the container was opened; false if it was not, or not yet because the player has been asked for the password
Sets the message that is shown to the players who open a container.
Parameters
- entity Entity the container prop
- message String optional, defaults to
nilthe message, cut to 256 characters; nil or an empty text removes it
Returns
- String the message that was set, or nil if the container has none now or the entity is not a container
Gives a container a name of its own, which replaces the name of its model in its target text and in the title of its inventory.
Container:set_container_name(entity, 'Evidence locker')
-- Back to the name registered for the model.
Container:set_container_name(entity)
Parameters
- entity Entity the container prop
- name String optional, defaults to
nilthe name, cut to 64 characters; nil or an empty text removes the custom name
Returns
- String the name that was set, or nil if the container has none now or the entity is not a container
Container:set_container_password(entity, password)
server plugins/containers/plugin/sv_plugin.lua:333Sets the password that a player has to enter to open a container, and closes the container for those who have it open. The password is never sent to clients.
Parameters
- entity Entity the container prop
- password String optional, defaults to
nilthe password, cut to 64 characters; nil or an empty text removes it
Returns
- String the password that was set, or nil if the container has none now or the entity is not a container
Sends what clients may know about a container to them: its custom name and whether it has a password. Called whenever either changes and after the saved containers are loaded.
Parameters
- entity Entity the container prop