3 -- support for MT game translation.
4 local S = default.get_translator
6 function default.chest.get_chest_formspec(pos)
7 local spos = pos.x .. "," .. pos.y .. "," .. pos.z
10 "list[nodemeta:" .. spos .. ";main;0,0.3;8,4;]" ..
11 "list[current_player;main;0,4.85;8,1;]" ..
12 "list[current_player;main;0,6.08;8,3;8]" ..
13 "listring[nodemeta:" .. spos .. ";main]" ..
14 "listring[current_player;main]" ..
15 default.get_hotbar_bg(0,4.85)
19 function default.chest.chest_lid_obstructed(pos)
20 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
21 local def = minetest.registered_nodes[minetest.get_node(above).name]
22 -- allow ladders, signs, wallmounted things and torches to not obstruct
24 (def.drawtype == "airlike" or
25 def.drawtype == "signlike" or
26 def.drawtype == "torchlike" or
27 (def.drawtype == "nodebox" and def.paramtype2 == "wallmounted")) then
33 function default.chest.chest_lid_close(pn)
34 local chest_open_info = default.chest.open_chests[pn]
35 local pos = chest_open_info.pos
36 local sound = chest_open_info.sound
37 local swap = chest_open_info.swap
39 default.chest.open_chests[pn] = nil
40 for k, v in pairs(default.chest.open_chests) do
41 if v.pos.x == pos.x and v.pos.y == pos.y and v.pos.z == pos.z then
46 local node = minetest.get_node(pos)
47 minetest.after(0.2, minetest.swap_node, pos, { name = swap,
48 param2 = node.param2 })
49 minetest.sound_play(sound, {gain = 0.3, pos = pos,
50 max_hear_distance = 10}, true)
53 default.chest.open_chests = {}
55 minetest.register_on_player_receive_fields(function(player, formname, fields)
56 if formname ~= "default:chest" then
59 if not player or not fields.quit then
62 local pn = player:get_player_name()
64 if not default.chest.open_chests[pn] then
68 default.chest.chest_lid_close(pn)
72 minetest.register_on_leaveplayer(function(player)
73 local pn = player:get_player_name()
74 if default.chest.open_chests[pn] then
75 default.chest.chest_lid_close(pn)
79 function default.chest.register_chest(prefixed_name, d)
80 local name = prefixed_name:sub(1,1) == ':' and prefixed_name:sub(2,-1) or prefixed_name
81 local def = table.copy(d)
84 def.paramtype = "light"
85 def.paramtype2 = "facedir"
86 def.legacy_facedir_simple = true
87 def.is_ground_content = false
90 def.on_construct = function(pos)
91 local meta = minetest.get_meta(pos)
92 meta:set_string("infotext", S("Locked Chest"))
93 meta:set_string("owner", "")
94 local inv = meta:get_inventory()
95 inv:set_size("main", 8*4)
97 def.after_place_node = function(pos, placer)
98 local meta = minetest.get_meta(pos)
99 meta:set_string("owner", placer:get_player_name() or "")
100 meta:set_string("infotext", S("Locked Chest (owned by @1)", meta:get_string("owner")))
102 def.can_dig = function(pos,player)
103 local meta = minetest.get_meta(pos);
104 local inv = meta:get_inventory()
105 return inv:is_empty("main") and
106 default.can_interact_with_node(player, pos)
108 def.allow_metadata_inventory_move = function(pos, from_list, from_index,
109 to_list, to_index, count, player)
110 if not default.can_interact_with_node(player, pos) then
115 def.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
116 if not default.can_interact_with_node(player, pos) then
119 return stack:get_count()
121 def.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
122 if not default.can_interact_with_node(player, pos) then
125 return stack:get_count()
127 def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
128 if not default.can_interact_with_node(clicker, pos) then
132 minetest.sound_play(def.sound_open, {gain = 0.3,
133 pos = pos, max_hear_distance = 10}, true)
134 if not default.chest.chest_lid_obstructed(pos) then
135 minetest.swap_node(pos,
136 { name = name .. "_open",
137 param2 = node.param2 })
139 minetest.after(0.2, minetest.show_formspec,
140 clicker:get_player_name(),
141 "default:chest", default.chest.get_chest_formspec(pos))
142 default.chest.open_chests[clicker:get_player_name()] = { pos = pos,
143 sound = def.sound_close, swap = name }
145 def.on_blast = function() end
146 def.on_key_use = function(pos, player)
147 local secret = minetest.get_meta(pos):get_string("key_lock_secret")
148 local itemstack = player:get_wielded_item()
149 local key_meta = itemstack:get_meta()
151 if itemstack:get_metadata() == "" then
155 if key_meta:get_string("secret") == "" then
156 key_meta:set_string("secret", minetest.parse_json(itemstack:get_metadata()).secret)
157 itemstack:set_metadata("")
160 if secret ~= key_meta:get_string("secret") then
164 minetest.show_formspec(
165 player:get_player_name(),
166 "default:chest_locked",
167 default.chest.get_chest_formspec(pos)
170 def.on_skeleton_key_use = function(pos, player, newsecret)
171 local meta = minetest.get_meta(pos)
172 local owner = meta:get_string("owner")
173 local pn = player:get_player_name()
175 -- verify placer is owner of lockable chest
177 minetest.record_protection_violation(pos, pn)
178 minetest.chat_send_player(pn, S("You do not own this chest."))
182 local secret = meta:get_string("key_lock_secret")
185 meta:set_string("key_lock_secret", secret)
188 return secret, S("a locked chest"), owner
191 def.on_construct = function(pos)
192 local meta = minetest.get_meta(pos)
193 meta:set_string("infotext", S("Chest"))
194 local inv = meta:get_inventory()
195 inv:set_size("main", 8*4)
197 def.can_dig = function(pos,player)
198 local meta = minetest.get_meta(pos);
199 local inv = meta:get_inventory()
200 return inv:is_empty("main")
202 def.on_rightclick = function(pos, node, clicker)
203 minetest.sound_play(def.sound_open, {gain = 0.3, pos = pos,
204 max_hear_distance = 10}, true)
205 if not default.chest.chest_lid_obstructed(pos) then
206 minetest.swap_node(pos, {
207 name = name .. "_open",
208 param2 = node.param2 })
210 minetest.after(0.2, minetest.show_formspec,
211 clicker:get_player_name(),
212 "default:chest", default.chest.get_chest_formspec(pos))
213 default.chest.open_chests[clicker:get_player_name()] = { pos = pos,
214 sound = def.sound_close, swap = name }
216 def.on_blast = function(pos)
218 default.get_inventory_drops(pos, "main", drops)
219 drops[#drops+1] = name
220 minetest.remove_node(pos)
225 def.on_metadata_inventory_move = function(pos, from_list, from_index,
226 to_list, to_index, count, player)
227 minetest.log("action", player:get_player_name() ..
228 " moves stuff in chest at " .. minetest.pos_to_string(pos))
230 def.on_metadata_inventory_put = function(pos, listname, index, stack, player)
231 minetest.log("action", player:get_player_name() ..
232 " moves " .. stack:get_name() ..
233 " to chest at " .. minetest.pos_to_string(pos))
235 def.on_metadata_inventory_take = function(pos, listname, index, stack, player)
236 minetest.log("action", player:get_player_name() ..
237 " takes " .. stack:get_name() ..
238 " from chest at " .. minetest.pos_to_string(pos))
241 local def_opened = table.copy(def)
242 local def_closed = table.copy(def)
244 def_opened.mesh = "chest_open.obj"
245 for i = 1, #def_opened.tiles do
246 if type(def_opened.tiles[i]) == "string" then
247 def_opened.tiles[i] = {name = def_opened.tiles[i], backface_culling = true}
248 elseif def_opened.tiles[i].backface_culling == nil then
249 def_opened.tiles[i].backface_culling = true
252 def_opened.drop = name
253 def_opened.groups.not_in_creative_inventory = 1
254 def_opened.selection_box = {
256 fixed = { -1/2, -1/2, -1/2, 1/2, 3/16, 1/2 },
258 def_opened.can_dig = function()
261 def_opened.on_blast = function() end
263 def_closed.mesh = nil
264 def_closed.drawtype = nil
265 def_closed.tiles[6] = def.tiles[5] -- swap textures around for "normal"
266 def_closed.tiles[5] = def.tiles[3] -- drawtype to make them match the mesh
267 def_closed.tiles[3] = def.tiles[3].."^[transformFX"
269 minetest.register_node(prefixed_name, def_closed)
270 minetest.register_node(prefixed_name .. "_open", def_opened)
272 -- convert old chests to this new variant
273 if name == "default:chest" or name == "default:chest_locked" then
274 minetest.register_lbm({
275 label = "update chests to opening chests",
276 name = "default:upgrade_" .. name:sub(9,-1) .. "_v2",
278 action = function(pos, node)
279 local meta = minetest.get_meta(pos)
280 meta:set_string("formspec", nil)
281 local inv = meta:get_inventory()
282 local list = inv:get_list("default:chest")
284 inv:set_size("main", 8*4)
285 inv:set_list("main", list)
286 inv:set_list("default:chest", nil)
293 default.chest.register_chest("default:chest", {
294 description = S("Chest"),
296 "default_chest_top.png",
297 "default_chest_top.png",
298 "default_chest_side.png",
299 "default_chest_side.png",
300 "default_chest_front.png",
301 "default_chest_inside.png"
303 sounds = default.node_sound_wood_defaults(),
304 sound_open = "default_chest_open",
305 sound_close = "default_chest_close",
306 groups = {choppy = 2, oddly_breakable_by_hand = 2},
309 default.chest.register_chest("default:chest_locked", {
310 description = S("Locked Chest"),
312 "default_chest_top.png",
313 "default_chest_top.png",
314 "default_chest_side.png",
315 "default_chest_side.png",
316 "default_chest_lock.png",
317 "default_chest_inside.png"
319 sounds = default.node_sound_wood_defaults(),
320 sound_open = "default_chest_open",
321 sound_close = "default_chest_close",
322 groups = {choppy = 2, oddly_breakable_by_hand = 2},
326 minetest.register_craft({
327 output = "default:chest",
329 {"group:wood", "group:wood", "group:wood"},
330 {"group:wood", "", "group:wood"},
331 {"group:wood", "group:wood", "group:wood"},
335 minetest.register_craft({
336 output = "default:chest_locked",
338 {"group:wood", "group:wood", "group:wood"},
339 {"group:wood", "default:steel_ingot", "group:wood"},
340 {"group:wood", "group:wood", "group:wood"},
344 minetest.register_craft( {
346 output = "default:chest_locked",
347 recipe = {"default:chest", "default:steel_ingot"},
350 minetest.register_craft({
352 recipe = "default:chest",
356 minetest.register_craft({
358 recipe = "default:chest_locked",