page. Mods can use it to add additional formspec elements onto the default
creative inventory formspec to be drawn after each update.
+Chests API
+----------
+
+The chests API allows the creation of chests, which have their own inventories for holding items.
+
+`default.chest.get_chest_formspec(pos)`
+
+ * Returns a formspec for a specific chest.
+ * `pos` Location of the chest node, e.g `{x = 1, y = 1, z = 1}`
+
+`default.chest.chest_lid_obstructed(pos)`
+
+ * Returns a boolean depending on whether or not a chest has its top obstructed by a solid node.
+ * `pos` Location of the chest node, e.g `{x = 1, y = 1, z = 1}`
+
+`default.chest.chest_lid_close(pn)`
+
+ * Closes the chest that a player is currently looking in.
+ * `pn` The name of the player whose chest is going to be closed
+
+`default.chest.open_chests`
+
+ * A table indexed by player name to keep track of who opened what chest.
+ * Key: The name of the player.
+ * Value: A table containing information about the chest the player is looking at.
+ e.g `{ pos = {1, 1, 1}, sound = null, swap = "chest" }`
+
+`default.chest.register_chest(name, def)`
+
+ * Registers new chest
+ * `name` Name for chest
+ * `def` See [#Chest Definition]
+
+### Chest Definition
+
+ description = "Chest",
+ tiles = {
+ "default_chest_top.png",
+ "default_chest_top.png",
+ "default_chest_side.png",
+ "default_chest_side.png",
+ "default_chest_front.png",
+ "default_chest_inside.png"
+ }, -- Textures which are applied to the chest model.
+ sounds = default.node_sound_wood_defaults(),
+ sound_open = "default_chest_open",
+ sound_close = "default_chest_close",
+ groups = {choppy = 2, oddly_breakable_by_hand = 2},
+ protected = false, -- If true, only placer can modify chest.
+
Doors API
---------
* Entire formspec for the survival inventory
-`default.get_chest_formspec(pos)`
-
- * Get the chest formspec using the defined GUI elements
- * pos: Location of the node
-
`default.get_furnace_active_formspec(fuel_percent, item_percent)`
* Get the active furnace formspec using the defined GUI elements
-- Tools / "Advanced" crafting / Non-"natural"
--
-function default.get_chest_formspec(pos)
+default.chest = {}
+
+function default.chest.get_chest_formspec(pos)
local spos = pos.x .. "," .. pos.y .. "," .. pos.z
local formspec =
"size[8,9]" ..
return formspec
end
-local function chest_lid_obstructed(pos)
+function default.chest.chest_lid_obstructed(pos)
local above = {x = pos.x, y = pos.y + 1, z = pos.z}
local def = minetest.registered_nodes[minetest.get_node(above).name]
-- allow ladders, signs, wallmounted things and torches to not obstruct
return true
end
-local open_chests = {}
-
-local function chest_lid_close(pn)
- local pos = open_chests[pn].pos
- local sound = open_chests[pn].sound
- local swap = open_chests[pn].swap
+function default.chest.chest_lid_close(pn)
+ local chest_open_info = default.chest.open_chests[pn]
+ local pos = chest_open_info.pos
+ local sound = chest_open_info.sound
+ local swap = chest_open_info.swap
- open_chests[pn] = nil
- for k, v in pairs(open_chests) do
+ default.chest.open_chests[pn] = nil
+ for k, v in pairs(default.chest.open_chests) do
if v.pos.x == pos.x and v.pos.y == pos.y and v.pos.z == pos.z then
return true
end
minetest.sound_play(sound, {gain = 0.3, pos = pos, max_hear_distance = 10})
end
+default.chest.open_chests = {}
+
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "default:chest" then
return
end
local pn = player:get_player_name()
- if not open_chests[pn] then
+ if not default.chest.open_chests[pn] then
return
end
- chest_lid_close(pn)
+ default.chest.chest_lid_close(pn)
return true
end)
minetest.register_on_leaveplayer(function(player)
local pn = player:get_player_name()
- if open_chests[pn] then
- chest_lid_close(pn)
+ if default.chest.open_chests[pn] then
+ default.chest.chest_lid_close(pn)
end
end)
-function default.register_chest(name, d)
+function default.chest.register_chest(name, d)
local def = table.copy(d)
def.drawtype = "mesh"
def.visual = "mesh"
minetest.sound_play(def.sound_open, {gain = 0.3,
pos = pos, max_hear_distance = 10})
- if not chest_lid_obstructed(pos) then
+ if not default.chest.chest_lid_obstructed(pos) then
minetest.swap_node(pos,
{ name = "default:" .. name .. "_open",
param2 = node.param2 })
end
minetest.after(0.2, minetest.show_formspec,
clicker:get_player_name(),
- "default:chest", default.get_chest_formspec(pos))
- open_chests[clicker:get_player_name()] = { pos = pos,
+ "default:chest", default.chest.get_chest_formspec(pos))
+ default.chest.open_chests[clicker:get_player_name()] = { pos = pos,
sound = def.sound_close, swap = name }
end
def.on_blast = function() end
minetest.show_formspec(
player:get_player_name(),
"default:chest_locked",
- default.get_chest_formspec(pos)
+ default.chest.get_chest_formspec(pos)
)
end
def.on_skeleton_key_use = function(pos, player, newsecret)
def.on_rightclick = function(pos, node, clicker)
minetest.sound_play(def.sound_open, {gain = 0.3, pos = pos,
max_hear_distance = 10})
- if not chest_lid_obstructed(pos) then
+ if not default.chest.chest_lid_obstructed(pos) then
minetest.swap_node(pos, {
name = "default:" .. name .. "_open",
param2 = node.param2 })
end
minetest.after(0.2, minetest.show_formspec,
clicker:get_player_name(),
- "default:chest", default.get_chest_formspec(pos))
- open_chests[clicker:get_player_name()] = { pos = pos,
+ "default:chest", default.chest.get_chest_formspec(pos))
+ default.chest.open_chests[clicker:get_player_name()] = { pos = pos,
sound = def.sound_close, swap = name }
end
def.on_blast = function(pos)
})
end
-
-default.register_chest("chest", {
+default.chest.register_chest("chest", {
description = "Chest",
tiles = {
"default_chest_top.png",
groups = {choppy = 2, oddly_breakable_by_hand = 2},
})
-default.register_chest("chest_locked", {
+default.chest.register_chest("chest_locked", {
description = "Locked Chest",
tiles = {
"default_chest_top.png",