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 = "default:" .. swap,
48 param2 = node.param2 })
49 minetest.sound_play(sound, {gain = 0.3, pos = pos, max_hear_distance = 10})
52 default.chest.open_chests = {}
54 minetest.register_on_player_receive_fields(function(player, formname, fields)
55 if formname ~= "default:chest" then
58 if not player or not fields.quit then
61 local pn = player:get_player_name()
63 if not default.chest.open_chests[pn] then
67 default.chest.chest_lid_close(pn)
71 minetest.register_on_leaveplayer(function(player)
72 local pn = player:get_player_name()
73 if default.chest.open_chests[pn] then
74 default.chest.chest_lid_close(pn)
78 function default.chest.register_chest(name, d)
79 local def = table.copy(d)
82 def.paramtype = "light"
83 def.paramtype2 = "facedir"
84 def.legacy_facedir_simple = true
85 def.is_ground_content = false
88 def.on_construct = function(pos)
89 local meta = minetest.get_meta(pos)
90 meta:set_string("infotext", S("Locked Chest"))
91 meta:set_string("owner", "")
92 local inv = meta:get_inventory()
93 inv:set_size("main", 8*4)
95 def.after_place_node = function(pos, placer)
96 local meta = minetest.get_meta(pos)
97 meta:set_string("owner", placer:get_player_name() or "")
98 meta:set_string("infotext", S("Locked Chest (owned by @1)", meta:get_string("owner")))
100 def.can_dig = function(pos,player)
101 local meta = minetest.get_meta(pos);
102 local inv = meta:get_inventory()
103 return inv:is_empty("main") and
104 default.can_interact_with_node(player, pos)
106 def.allow_metadata_inventory_move = function(pos, from_list, from_index,
107 to_list, to_index, count, player)
108 if not default.can_interact_with_node(player, pos) then
113 def.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
114 if not default.can_interact_with_node(player, pos) then
117 return stack:get_count()
119 def.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
120 if not default.can_interact_with_node(player, pos) then
123 return stack:get_count()
125 def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
126 if not default.can_interact_with_node(clicker, pos) then
130 minetest.sound_play(def.sound_open, {gain = 0.3,
131 pos = pos, max_hear_distance = 10})
132 if not default.chest.chest_lid_obstructed(pos) then
133 minetest.swap_node(pos,
134 { name = "default:" .. name .. "_open",
135 param2 = node.param2 })
137 minetest.after(0.2, minetest.show_formspec,
138 clicker:get_player_name(),
139 "default:chest", default.chest.get_chest_formspec(pos))
140 default.chest.open_chests[clicker:get_player_name()] = { pos = pos,
141 sound = def.sound_close, swap = name }
143 def.on_blast = function() end
144 def.on_key_use = function(pos, player)
145 local secret = minetest.get_meta(pos):get_string("key_lock_secret")
146 local itemstack = player:get_wielded_item()
147 local key_meta = itemstack:get_meta()
149 if itemstack:get_metadata() == "" then
153 if key_meta:get_string("secret") == "" then
154 key_meta:set_string("secret", minetest.parse_json(itemstack:get_metadata()).secret)
155 itemstack:set_metadata("")
158 if secret ~= key_meta:get_string("secret") then
162 minetest.show_formspec(
163 player:get_player_name(),
164 "default:chest_locked",
165 default.chest.get_chest_formspec(pos)
168 def.on_skeleton_key_use = function(pos, player, newsecret)
169 local meta = minetest.get_meta(pos)
170 local owner = meta:get_string("owner")
171 local pn = player:get_player_name()
173 -- verify placer is owner of lockable chest
175 minetest.record_protection_violation(pos, pn)
176 minetest.chat_send_player(pn, S("You do not own this chest."))
180 local secret = meta:get_string("key_lock_secret")
183 meta:set_string("key_lock_secret", secret)
186 return secret, S("a locked chest"), owner
189 def.on_construct = function(pos)
190 local meta = minetest.get_meta(pos)
191 meta:set_string("infotext", S("Chest"))
192 local inv = meta:get_inventory()
193 inv:set_size("main", 8*4)
195 def.can_dig = function(pos,player)
196 local meta = minetest.get_meta(pos);
197 local inv = meta:get_inventory()
198 return inv:is_empty("main")
200 def.on_rightclick = function(pos, node, clicker)
201 minetest.sound_play(def.sound_open, {gain = 0.3, pos = pos,
202 max_hear_distance = 10})
203 if not default.chest.chest_lid_obstructed(pos) then
204 minetest.swap_node(pos, {
205 name = "default:" .. name .. "_open",
206 param2 = node.param2 })
208 minetest.after(0.2, minetest.show_formspec,
209 clicker:get_player_name(),
210 "default:chest", default.chest.get_chest_formspec(pos))
211 default.chest.open_chests[clicker:get_player_name()] = { pos = pos,
212 sound = def.sound_close, swap = name }
214 def.on_blast = function(pos)
216 default.get_inventory_drops(pos, "main", drops)
217 drops[#drops+1] = "default:" .. name
218 minetest.remove_node(pos)
223 def.on_metadata_inventory_move = function(pos, from_list, from_index,
224 to_list, to_index, count, player)
225 minetest.log("action", player:get_player_name() ..
226 " moves stuff in chest at " .. minetest.pos_to_string(pos))
228 def.on_metadata_inventory_put = function(pos, listname, index, stack, player)
229 minetest.log("action", player:get_player_name() ..
230 " moves " .. stack:get_name() ..
231 " to chest at " .. minetest.pos_to_string(pos))
233 def.on_metadata_inventory_take = function(pos, listname, index, stack, player)
234 minetest.log("action", player:get_player_name() ..
235 " takes " .. stack:get_name() ..
236 " from chest at " .. minetest.pos_to_string(pos))
239 local def_opened = table.copy(def)
240 local def_closed = table.copy(def)
242 def_opened.mesh = "chest_open.obj"
243 for i = 1, #def_opened.tiles do
244 if type(def_opened.tiles[i]) == "string" then
245 def_opened.tiles[i] = {name = def_opened.tiles[i], backface_culling = true}
246 elseif def_opened.tiles[i].backface_culling == nil then
247 def_opened.tiles[i].backface_culling = true
250 def_opened.drop = "default:" .. name
251 def_opened.groups.not_in_creative_inventory = 1
252 def_opened.selection_box = {
254 fixed = { -1/2, -1/2, -1/2, 1/2, 3/16, 1/2 },
256 def_opened.can_dig = function()
259 def_opened.on_blast = function() end
261 def_closed.mesh = nil
262 def_closed.drawtype = nil
263 def_closed.tiles[6] = def.tiles[5] -- swap textures around for "normal"
264 def_closed.tiles[5] = def.tiles[3] -- drawtype to make them match the mesh
265 def_closed.tiles[3] = def.tiles[3].."^[transformFX"
267 minetest.register_node("default:" .. name, def_closed)
268 minetest.register_node("default:" .. name .. "_open", def_opened)
270 -- convert old chests to this new variant
271 minetest.register_lbm({
272 label = "update chests to opening chests",
273 name = "default:upgrade_" .. name .. "_v2",
274 nodenames = {"default:" .. name},
275 action = function(pos, node)
276 local meta = minetest.get_meta(pos)
277 meta:set_string("formspec", nil)
278 local inv = meta:get_inventory()
279 local list = inv:get_list("default:chest")
281 inv:set_size("main", 8*4)
282 inv:set_list("main", list)
283 inv:set_list("default:chest", nil)
289 default.chest.register_chest("chest", {
290 description = S("Chest"),
292 "default_chest_top.png",
293 "default_chest_top.png",
294 "default_chest_side.png",
295 "default_chest_side.png",
296 "default_chest_front.png",
297 "default_chest_inside.png"
299 sounds = default.node_sound_wood_defaults(),
300 sound_open = "default_chest_open",
301 sound_close = "default_chest_close",
302 groups = {choppy = 2, oddly_breakable_by_hand = 2},
305 default.chest.register_chest("chest_locked", {
306 description = S("Locked Chest"),
308 "default_chest_top.png",
309 "default_chest_top.png",
310 "default_chest_side.png",
311 "default_chest_side.png",
312 "default_chest_lock.png",
313 "default_chest_inside.png"
315 sounds = default.node_sound_wood_defaults(),
316 sound_open = "default_chest_open",
317 sound_close = "default_chest_close",
318 groups = {choppy = 2, oddly_breakable_by_hand = 2},
322 minetest.register_craft({
323 output = "default:chest",
325 {"group:wood", "group:wood", "group:wood"},
326 {"group:wood", "", "group:wood"},
327 {"group:wood", "group:wood", "group:wood"},
331 minetest.register_craft({
332 output = "default:chest_locked",
334 {"group:wood", "group:wood", "group:wood"},
335 {"group:wood", "default:steel_ingot", "group:wood"},
336 {"group:wood", "group:wood", "group:wood"},
340 minetest.register_craft( {
342 output = "default:chest_locked",
343 recipe = {"default:chest", "default:steel_ingot"},
346 minetest.register_craft({
348 recipe = "default:chest",
352 minetest.register_craft({
354 recipe = "default:chest_locked",