Default: Expose open_chests and chest_lid_obstructed

This commit is contained in:
ForbiddenJ 2017-11-26 12:44:55 +01:00 committed by paramat
parent a2d7678ffd
commit b75a17984a
3 changed files with 80 additions and 30 deletions

View File

@ -88,6 +88,56 @@ The contents of `creative.formspec_add` is appended to every creative inventory
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
---------
@ -706,11 +756,6 @@ GUI and formspecs
* 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

View File

@ -34,3 +34,6 @@ if minetest.get_modpath("player_api") then
default.player_set_textures = player_api.set_textures
default.player_set_animation = player_api.set_animation
end
-- Chests
default.register_chest = default.chest.register_chest

View File

@ -1811,7 +1811,9 @@ minetest.register_node("default:lava_flowing", {
-- 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]" ..
@ -1827,7 +1829,7 @@ function default.get_chest_formspec(pos)
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
@ -1841,15 +1843,14 @@ local function chest_lid_obstructed(pos)
return true
end
local open_chests = {}
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
local function chest_lid_close(pn)
local pos = open_chests[pn].pos
local sound = open_chests[pn].sound
local swap = open_chests[pn].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
@ -1861,6 +1862,8 @@ local function chest_lid_close(pn)
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
@ -1870,22 +1873,22 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
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"
@ -1940,15 +1943,15 @@ function default.register_chest(name, d)
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
@ -1969,7 +1972,7 @@ function default.register_chest(name, d)
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)
@ -2007,15 +2010,15 @@ function default.register_chest(name, d)
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)
@ -2093,8 +2096,7 @@ function default.register_chest(name, d)
})
end
default.register_chest("chest", {
default.chest.register_chest("chest", {
description = "Chest",
tiles = {
"default_chest_top.png",
@ -2110,7 +2112,7 @@ default.register_chest("chest", {
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",