Skip to content

Commit fb18a5b

Browse files
authoredApr 13, 2020
Make default.chest.register_chest() usable for other mods (#2127)
1 parent ea4ce80 commit fb18a5b

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed
 

‎game_api.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ The chests API allows the creation of chests, which have their own inventories f
136136
* A table indexed by player name to keep track of who opened what chest.
137137
* Key: The name of the player.
138138
* Value: A table containing information about the chest the player is looking at.
139-
e.g `{ pos = {1, 1, 1}, sound = null, swap = "chest" }`
139+
e.g `{ pos = {1, 1, 1}, sound = null, swap = "default:chest" }`
140140

141141
`default.chest.register_chest(name, def)`
142142

143143
* Registers new chest
144-
* `name` Name for chest
144+
* `name` Name for chest e.g. "default:chest"
145145
* `def` See [#Chest Definition]
146146

147147
### Chest Definition

‎mods/default/chests.lua

+28-25
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function default.chest.chest_lid_close(pn)
4444
end
4545

4646
local node = minetest.get_node(pos)
47-
minetest.after(0.2, minetest.swap_node, pos, { name = "default:" .. swap,
47+
minetest.after(0.2, minetest.swap_node, pos, { name = swap,
4848
param2 = node.param2 })
4949
minetest.sound_play(sound, {gain = 0.3, pos = pos,
5050
max_hear_distance = 10}, true)
@@ -76,7 +76,8 @@ minetest.register_on_leaveplayer(function(player)
7676
end
7777
end)
7878

79-
function default.chest.register_chest(name, d)
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
8081
local def = table.copy(d)
8182
def.drawtype = "mesh"
8283
def.visual = "mesh"
@@ -132,7 +133,7 @@ function default.chest.register_chest(name, d)
132133
pos = pos, max_hear_distance = 10}, true)
133134
if not default.chest.chest_lid_obstructed(pos) then
134135
minetest.swap_node(pos,
135-
{ name = "default:" .. name .. "_open",
136+
{ name = name .. "_open",
136137
param2 = node.param2 })
137138
end
138139
minetest.after(0.2, minetest.show_formspec,
@@ -203,7 +204,7 @@ function default.chest.register_chest(name, d)
203204
max_hear_distance = 10}, true)
204205
if not default.chest.chest_lid_obstructed(pos) then
205206
minetest.swap_node(pos, {
206-
name = "default:" .. name .. "_open",
207+
name = name .. "_open",
207208
param2 = node.param2 })
208209
end
209210
minetest.after(0.2, minetest.show_formspec,
@@ -215,7 +216,7 @@ function default.chest.register_chest(name, d)
215216
def.on_blast = function(pos)
216217
local drops = {}
217218
default.get_inventory_drops(pos, "main", drops)
218-
drops[#drops+1] = "default:" .. name
219+
drops[#drops+1] = name
219220
minetest.remove_node(pos)
220221
return drops
221222
end
@@ -248,7 +249,7 @@ function default.chest.register_chest(name, d)
248249
def_opened.tiles[i].backface_culling = true
249250
end
250251
end
251-
def_opened.drop = "default:" .. name
252+
def_opened.drop = name
252253
def_opened.groups.not_in_creative_inventory = 1
253254
def_opened.selection_box = {
254255
type = "fixed",
@@ -265,29 +266,31 @@ function default.chest.register_chest(name, d)
265266
def_closed.tiles[5] = def.tiles[3] -- drawtype to make them match the mesh
266267
def_closed.tiles[3] = def.tiles[3].."^[transformFX"
267268

268-
minetest.register_node("default:" .. name, def_closed)
269-
minetest.register_node("default:" .. name .. "_open", def_opened)
269+
minetest.register_node(prefixed_name, def_closed)
270+
minetest.register_node(prefixed_name .. "_open", def_opened)
270271

271272
-- convert old chests to this new variant
272-
minetest.register_lbm({
273-
label = "update chests to opening chests",
274-
name = "default:upgrade_" .. name .. "_v2",
275-
nodenames = {"default:" .. name},
276-
action = function(pos, node)
277-
local meta = minetest.get_meta(pos)
278-
meta:set_string("formspec", nil)
279-
local inv = meta:get_inventory()
280-
local list = inv:get_list("default:chest")
281-
if list then
282-
inv:set_size("main", 8*4)
283-
inv:set_list("main", list)
284-
inv:set_list("default:chest", nil)
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",
277+
nodenames = {name},
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")
283+
if list then
284+
inv:set_size("main", 8*4)
285+
inv:set_list("main", list)
286+
inv:set_list("default:chest", nil)
287+
end
285288
end
286-
end
287-
})
289+
})
290+
end
288291
end
289292

290-
default.chest.register_chest("chest", {
293+
default.chest.register_chest("default:chest", {
291294
description = S("Chest"),
292295
tiles = {
293296
"default_chest_top.png",
@@ -303,7 +306,7 @@ default.chest.register_chest("chest", {
303306
groups = {choppy = 2, oddly_breakable_by_hand = 2},
304307
})
305308

306-
default.chest.register_chest("chest_locked", {
309+
default.chest.register_chest("default:chest_locked", {
307310
description = S("Locked Chest"),
308311
tiles = {
309312
"default_chest_top.png",

0 commit comments

Comments
 (0)
Please sign in to comment.