Skip to content

Commit 7bcb0d1

Browse files
committed
Update chest registration
1 parent 9717f58 commit 7bcb0d1

File tree

3 files changed

+47
-45
lines changed

3 files changed

+47
-45
lines changed

technic_chests/chests.lua

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11

2-
local function register_chests(name, data)
3-
for _,t in pairs({"", "_locked", "_protected"}) do
4-
local data_copy = {}
5-
for k, v in pairs(data) do
6-
data_copy[k] = v
7-
end
2+
local S = rawget(_G, "intllib") and intllib.Getter() or function(s) return s end
3+
4+
local function register_chests(data)
5+
local name = data.description:lower()
6+
local type_description = {"Chest", "Locked Chest", "Protected Chest"}
7+
for i,t in ipairs({"", "_locked", "_protected"}) do
8+
local data_copy = table.copy(data)
89
data_copy.locked = t == "_locked"
910
data_copy.protected = t == "_protected"
10-
technic.chests.register_chest(name, data_copy)
11+
data_copy.texture_prefix = "technic_"..name.."_chest"
12+
data_copy.description = S(("%s %s"):format(data.description, type_description[i]))
13+
technic.chests.register_chest(":technic:"..name..t.."_chest", data_copy)
1114
end
1215
end
1316

@@ -68,7 +71,8 @@ local function register_crafts(name, material, base_open, base_locked, base_prot
6871
end
6972

7073
-- Iron
71-
register_chests("Iron", {
74+
register_chests({
75+
description = "Iron",
7276
width = 9,
7377
height = 5,
7478
sort = true,
@@ -83,7 +87,8 @@ register_crafts(
8387
)
8488

8589
-- Copper
86-
register_chests("Copper", {
90+
register_chests({
91+
description = "Copper",
8792
width = 12,
8893
height = 5,
8994
sort = true,
@@ -99,7 +104,8 @@ register_crafts(
99104
)
100105

101106
-- Silver
102-
register_chests("Silver", {
107+
register_chests({
108+
description = "Silver",
103109
width = 12,
104110
height = 6,
105111
sort = true,
@@ -116,7 +122,8 @@ register_crafts(
116122
)
117123

118124
-- Gold
119-
register_chests("Gold", {
125+
register_chests({
126+
description = "Gold",
120127
width = 15,
121128
height = 6,
122129
sort = true,
@@ -134,7 +141,8 @@ register_crafts(
134141
)
135142

136143
-- Mithril
137-
register_chests("Mithril", {
144+
register_chests({
145+
description = "Mithril",
138146
width = 15,
139147
height = 6,
140148
sort = true,

technic_chests/formspec.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function technic.chests.update_formspec(pos, data, edit_infotext)
155155
meta:set_string("formspec", formspec)
156156
end
157157

158-
function technic.chests.get_receive_fields(data)
158+
function technic.chests.get_receive_fields(nodename, data)
159159
return function(pos, formname, fields, player)
160160
if not fields or not player then
161161
return
@@ -221,9 +221,9 @@ function technic.chests.get_receive_fields(data)
221221
if fields["color_button"..i] then
222222
local node = minetest.get_node(pos)
223223
if technic.chests.colors[i] then
224-
node.name = data.node_name.."_"..technic.chests.colors[i][1]
224+
node.name = nodename.."_"..technic.chests.colors[i][1]
225225
else
226-
node.name = data.node_name
226+
node.name = nodename
227227
end
228228
minetest.swap_node(pos, node)
229229
meta:set_int("color", i)

technic_chests/register.lua

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,57 +28,51 @@ local node_groups_no_inv = {
2828
not_in_creative_inventory = 1,
2929
}
3030

31-
local function get_tiles(name, data, color)
32-
local tiles = {
33-
"technic_"..name.."_chest_top.png"..tube_entry,
34-
"technic_"..name.."_chest_top.png"..tube_entry,
35-
"technic_"..name.."_chest_side.png"..tube_entry,
36-
"technic_"..name.."_chest_side.png"..tube_entry,
37-
"technic_"..name.."_chest_side.png"..tube_entry,
38-
"technic_"..name.."_chest_front.png"
31+
local function get_tiles(data, color)
32+
local tiles = data.tiles and table.copy(data.tiles) or {
33+
data.texture_prefix.."_top.png"..tube_entry,
34+
data.texture_prefix.."_top.png"..tube_entry,
35+
data.texture_prefix.."_side.png"..tube_entry,
36+
data.texture_prefix.."_side.png"..tube_entry,
37+
data.texture_prefix.."_side.png"..tube_entry,
38+
data.texture_prefix.."_front.png"
3939
}
4040
if data.color and color then
4141
tiles[6] = tiles[6].."^technic_chest_overlay_"..technic.chests.colors[color][1]..".png"
4242
end
4343
if data.locked then
44-
tiles[6] = tiles[6].."^technic_"..name.."_chest_lock_overlay.png"
44+
tiles[6] = tiles[6].."^"..data.texture_prefix.."_lock_overlay.png"
4545
elseif data.protected then
4646
tiles[6] = tiles[6]..protector_overlay
4747
end
4848
return tiles
4949
end
5050

51-
function technic.chests.register_chest(name, data)
52-
local lname = name:lower()
53-
name = S(name)
51+
function technic.chests.register_chest(nodename, data)
52+
assert(data.tiles or data.texture_prefix, "technic.chests.register_chest: tiles or texture_prefix required")
53+
assert(data.description, "technic.chests.register_chest: description required")
54+
local colon
55+
colon, nodename = nodename:match("^(:?)(.+)")
56+
5457
if data.digilines and not has_digilines then
5558
data.digilines = nil
5659
end
57-
if data.locked then
58-
data.node_name = "technic:"..lname.."_locked_chest"
59-
data.description = S("%s Locked Chest"):format(name)
60-
elseif data.protected then
61-
data.node_name = "technic:"..lname.."_protected_chest"
62-
data.description = S("%s Protected Chest"):format(name)
63-
else
64-
data.node_name = "technic:"..lname.."_chest"
65-
data.description = S("%s Chest"):format(name)
66-
end
60+
6761
data.formspec = technic.chests.get_formspec(data)
6862
local def = {
6963
description = data.description,
70-
tiles = get_tiles(lname, data),
64+
tiles = data.tiles or get_tiles(data),
7165
paramtype2 = "facedir",
7266
legacy_facedir_simple = true,
7367
groups = node_groups,
7468
sounds = default.node_sound_wood_defaults(),
75-
drop = data.node_name,
69+
drop = nodename,
7670
after_place_node = function(pos, placer)
7771
local meta = minetest.get_meta(pos)
7872
if data.locked then
7973
local owner = placer:get_player_name() or ""
8074
meta:set_string("owner", owner)
81-
meta:set_string("infotext", S("%s Locked Chest (owned by %s)"):format(name, owner))
75+
meta:set_string("infotext", S("%s (owned by %s)"):format(data.description, owner))
8276
else
8377
meta:set_string("infotext", data.description)
8478
end
@@ -198,11 +192,11 @@ function technic.chests.register_chest(name, data)
198192
end
199193
local drops = {}
200194
default.get_inventory_drops(pos, "main", drops)
201-
drops[#drops+1] = data.node_name
195+
drops[#drops+1] = nodename
202196
minetest.remove_node(pos)
203197
return drops
204198
end,
205-
on_receive_fields = technic.chests.get_receive_fields(data),
199+
on_receive_fields = technic.chests.get_receive_fields(nodename, data),
206200
}
207201
if data.locked then
208202
def.on_skeleton_key_use = function(pos, player, newsecret)
@@ -231,16 +225,16 @@ function technic.chests.register_chest(name, data)
231225
},
232226
}
233227
end
234-
minetest.register_node(":"..data.node_name, def)
228+
minetest.register_node(colon..nodename, def)
235229
if data.color then
236230
for i = 1, 15 do
237231
local colordef = {}
238232
for k, v in pairs(def) do
239233
colordef[k] = v
240234
end
241235
colordef.groups = node_groups_no_inv
242-
colordef.tiles = get_tiles(lname, data, i)
243-
minetest.register_node(":"..data.node_name.."_"..technic.chests.colors[i][1], colordef)
236+
colordef.tiles = get_tiles(data, i)
237+
minetest.register_node(colon..nodename.."_"..technic.chests.colors[i][1], colordef)
244238
end
245239
end
246240
end

0 commit comments

Comments
 (0)