3 function default.chest.get_chest_formspec(pos)
4 local spos = pos.x .. "," .. pos.y .. "," .. pos.z
7 "list[nodemeta:" .. spos .. ";main;0,0.3;8,4;]" ..
8 "list[current_player;main;0,4.85;8,1;]" ..
9 "list[current_player;main;0,6.08;8,3;8]" ..
10 "listring[nodemeta:" .. spos .. ";main]" ..
11 "listring[current_player;main]" ..
12 default.get_hotbar_bg(0,4.85)
16 function default.chest.chest_lid_obstructed(pos)
17 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
18 local def = minetest.registered_nodes[minetest.get_node(above).name]
19 -- allow ladders, signs, wallmounted things and torches to not obstruct
21 (def.drawtype == "airlike" or
22 def.drawtype == "signlike" or
23 def.drawtype == "torchlike" or
24 (def.drawtype == "nodebox" and def.paramtype2 == "wallmounted")) then
30 function default.chest.chest_lid_close(pn)
31 local chest_open_info = default.chest.open_chests[pn]
32 local pos = chest_open_info.pos
33 local sound = chest_open_info.sound
34 local swap = chest_open_info.swap
36 default.chest.open_chests[pn] = nil
37 for k, v in pairs(default.chest.open_chests) do
38 if v.pos.x == pos.x and v.pos.y == pos.y and v.pos.z == pos.z then
43 local node = minetest.get_node(pos)
44 minetest.after(0.2, minetest.swap_node, pos, { name = "default:" .. swap,
45 param2 = node.param2 })
46 minetest.sound_play(sound, {gain = 0.3, pos = pos, max_hear_distance = 10})
49 default.chest.open_chests = {}
51 minetest.register_on_player_receive_fields(function(player, formname, fields)
52 if formname ~= "default:chest" then
55 if not player or not fields.quit then
58 local pn = player:get_player_name()
60 if not default.chest.open_chests[pn] then
64 default.chest.chest_lid_close(pn)
68 minetest.register_on_leaveplayer(function(player)
69 local pn = player:get_player_name()
70 if default.chest.open_chests[pn] then
71 default.chest.chest_lid_close(pn)
75 function default.chest.register_chest(name, d)
76 local def = table.copy(d)
79 def.paramtype = "light"
80 def.paramtype2 = "facedir"
81 def.legacy_facedir_simple = true
82 def.is_ground_content = false
85 def.on_construct = function(pos)
86 local meta = minetest.get_meta(pos)
87 meta:set_string("infotext", "Locked Chest")
88 meta:set_string("owner", "")
89 local inv = meta:get_inventory()
90 inv:set_size("main", 8*4)
92 def.after_place_node = function(pos, placer)
93 local meta = minetest.get_meta(pos)
94 meta:set_string("owner", placer:get_player_name() or "")
95 meta:set_string("infotext", "Locked Chest (owned by " ..
96 meta:get_string("owner") .. ")")
98 def.can_dig = function(pos,player)
99 local meta = minetest.get_meta(pos);
100 local inv = meta:get_inventory()
101 return inv:is_empty("main") and
102 default.can_interact_with_node(player, pos)
104 def.allow_metadata_inventory_move = function(pos, from_list, from_index,
105 to_list, to_index, count, player)
106 if not default.can_interact_with_node(player, pos) then
111 def.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
112 if not default.can_interact_with_node(player, pos) then
115 return stack:get_count()
117 def.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
118 if not default.can_interact_with_node(player, pos) then
121 return stack:get_count()
123 def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
124 if not default.can_interact_with_node(clicker, pos) then
128 minetest.sound_play(def.sound_open, {gain = 0.3,
129 pos = pos, max_hear_distance = 10})
130 if not default.chest.chest_lid_obstructed(pos) then
131 minetest.swap_node(pos,
132 { name = "default:" .. name .. "_open",
133 param2 = node.param2 })
135 minetest.after(0.2, minetest.show_formspec,
136 clicker:get_player_name(),
137 "default:chest", default.chest.get_chest_formspec(pos))
138 default.chest.open_chests[clicker:get_player_name()] = { pos = pos,
139 sound = def.sound_close, swap = name }
141 def.on_blast = function() end
142 def.on_key_use = function(pos, player)
143 local secret = minetest.get_meta(pos):get_string("key_lock_secret")
144 local itemstack = player:get_wielded_item()
145 local key_meta = itemstack:get_meta()
147 if itemstack:get_metadata() == "" then
151 if key_meta:get_string("secret") == "" then
152 key_meta:set_string("secret", minetest.parse_json(itemstack:get_metadata()).secret)
153 itemstack:set_metadata("")
156 if secret ~= key_meta:get_string("secret") then
160 minetest.show_formspec(
161 player:get_player_name(),
162 "default:chest_locked",
163 default.chest.get_chest_formspec(pos)
166 def.on_skeleton_key_use = function(pos, player, newsecret)
167 local meta = minetest.get_meta(pos)
168 local owner = meta:get_string("owner")
169 local pn = player:get_player_name()
171 -- verify placer is owner of lockable chest
173 minetest.record_protection_violation(pos, pn)
174 minetest.chat_send_player(pn, "You do not own this chest.")
178 local secret = meta:get_string("key_lock_secret")
181 meta:set_string("key_lock_secret", secret)
184 return secret, "a locked chest", owner
187 def.on_construct = function(pos)
188 local meta = minetest.get_meta(pos)
189 meta:set_string("infotext", "Chest")
190 local inv = meta:get_inventory()
191 inv:set_size("main", 8*4)
193 def.can_dig = function(pos,player)
194 local meta = minetest.get_meta(pos);
195 local inv = meta:get_inventory()
196 return inv:is_empty("main")
198 def.on_rightclick = function(pos, node, clicker)
199 minetest.sound_play(def.sound_open, {gain = 0.3, pos = pos,
200 max_hear_distance = 10})
201 if not default.chest.chest_lid_obstructed(pos) then
202 minetest.swap_node(pos, {
203 name = "default:" .. name .. "_open",
204 param2 = node.param2 })
206 minetest.after(0.2, minetest.show_formspec,
207 clicker:get_player_name(),
208 "default:chest", default.chest.get_chest_formspec(pos))
209 default.chest.open_chests[clicker:get_player_name()] = { pos = pos,
210 sound = def.sound_close, swap = name }
212 def.on_blast = function(pos)
214 default.get_inventory_drops(pos, "main", drops)
215 drops[#drops+1] = "default:" .. name
216 minetest.remove_node(pos)
221 def.on_metadata_inventory_move = function(pos, from_list, from_index,
222 to_list, to_index, count, player)
223 minetest.log("action", player:get_player_name() ..
224 " moves stuff in chest at " .. minetest.pos_to_string(pos))
226 def.on_metadata_inventory_put = function(pos, listname, index, stack, player)
227 minetest.log("action", player:get_player_name() ..
228 " moves " .. stack:get_name() ..
229 " to chest at " .. minetest.pos_to_string(pos))
231 def.on_metadata_inventory_take = function(pos, listname, index, stack, player)
232 minetest.log("action", player:get_player_name() ..
233 " takes " .. stack:get_name() ..
234 " from chest at " .. minetest.pos_to_string(pos))
237 local def_opened = table.copy(def)
238 local def_closed = table.copy(def)
240 def_opened.mesh = "chest_open.obj"
241 for i = 1, #def_opened.tiles do
242 if type(def_opened.tiles[i]) == "string" then
243 def_opened.tiles[i] = {name = def_opened.tiles[i], backface_culling = true}
244 elseif def_opened.tiles[i].backface_culling == nil then
245 def_opened.tiles[i].backface_culling = true
248 def_opened.drop = "default:" .. name
249 def_opened.groups.not_in_creative_inventory = 1
250 def_opened.selection_box = {
252 fixed = { -1/2, -1/2, -1/2, 1/2, 3/16, 1/2 },
254 def_opened.can_dig = function()
257 def_opened.on_blast = function() end
259 def_closed.mesh = nil
260 def_closed.drawtype = nil
261 def_closed.tiles[6] = def.tiles[5] -- swap textures around for "normal"
262 def_closed.tiles[5] = def.tiles[3] -- drawtype to make them match the mesh
263 def_closed.tiles[3] = def.tiles[3].."^[transformFX"
265 minetest.register_node("default:" .. name, def_closed)
266 minetest.register_node("default:" .. name .. "_open", def_opened)
268 -- convert old chests to this new variant
269 minetest.register_lbm({
270 label = "update chests to opening chests",
271 name = "default:upgrade_" .. name .. "_v2",
272 nodenames = {"default:" .. name},
273 action = function(pos, node)
274 local meta = minetest.get_meta(pos)
275 meta:set_string("formspec", nil)
276 local inv = meta:get_inventory()
277 local list = inv:get_list("default:chest")
279 inv:set_size("main", 8*4)
280 inv:set_list("main", list)
281 inv:set_list("default:chest", nil)
287 default.chest.register_chest("chest", {
288 description = "Chest",
290 "default_chest_top.png",
291 "default_chest_top.png",
292 "default_chest_side.png",
293 "default_chest_side.png",
294 "default_chest_front.png",
295 "default_chest_inside.png"
297 sounds = default.node_sound_wood_defaults(),
298 sound_open = "default_chest_open",
299 sound_close = "default_chest_close",
300 groups = {choppy = 2, oddly_breakable_by_hand = 2},
303 default.chest.register_chest("chest_locked", {
304 description = "Locked Chest",
306 "default_chest_top.png",
307 "default_chest_top.png",
308 "default_chest_side.png",
309 "default_chest_side.png",
310 "default_chest_lock.png",
311 "default_chest_inside.png"
313 sounds = default.node_sound_wood_defaults(),
314 sound_open = "default_chest_open",
315 sound_close = "default_chest_close",
316 groups = {choppy = 2, oddly_breakable_by_hand = 2},