SmallJoker
2023-08-26 dfcf64c1d07f4006045af37b0b01dbfc82dbb1d1
commit | author | age
41a10a 1 local S = rawget(_G, "intllib") and intllib.Getter() or function(s) return s end
179364 2
fcef0e 3 local pipeworks = rawget(_G, "pipeworks")
0f6bdb 4 local fs_helpers
8a987b 5 local tubelib_exists = minetest.global_exists("tubelib")
11f20d 6
d4609f 7 local registered_chest_data = {} -- data passed to :register()
S 8
11f20d 9 local allow_label = ""
b6d343 10 local tube_entry = ""
11f20d 11
fcef0e 12 if not minetest.get_modpath("pipeworks") then
R 13     -- Pipeworks is not installed. Simulate using a dummy table...
14     pipeworks = {}
11f20d 15     fs_helpers = {}
fcef0e 16     local pipeworks_meta = {}
R 17     setmetatable(pipeworks, pipeworks_meta)
18     local dummy = function()
19         end
20     pipeworks_meta.__index = function(table, key)
21             print("[technic_chests] WARNING: variable or method '"..key.."' not present in dummy pipeworks table - assuming it is a method...")
22             pipeworks[key] = dummy
23             return dummy
24         end
25     pipeworks.after_place = dummy
26     pipeworks.after_dig = dummy
11f20d 27     fs_helpers.cycling_button = function() return "" end
VE 28 else
29     fs_helpers = pipeworks.fs_helpers
d4609f 30     allow_label = "Allow splitting incoming stacks from tubes"
b6d343 31     tube_entry = "^pipeworks_tube_connection_metallic.png"
fcef0e 32 end
R 33
d4609f 34 -- Change the appearance of the chest
78cacd 35 local chest_mark_colors = {
179364 36     {"black", S("Black")},
S 37     {"blue", S("Blue")},
38     {"brown", S("Brown")},
39c41a 39     {"cyan", S("Cyan")},
179364 40     {"dark_green", S("Dark Green")},
S 41     {"dark_grey", S("Dark Grey")},
42     {"green", S("Green")},
43     {"grey", S("Grey")},
44     {"magenta", S("Magenta")},
45     {"orange", S("Orange")},
46     {"pink", S("Pink")},
47     {"red", S("Red")},
48     {"violet", S("Violet")},
49     {"white", S("White")},
50     {"yellow", S("Yellow")},
78cacd 51 }
S 52
53
54 local function colorid_to_postfix(id)
179364 55     return chest_mark_colors[id] and "_"..chest_mark_colors[id][1] or ""
78cacd 56 end
S 57
58
1bf52c 59 local function get_color_buttons(coleft, lotop)
78cacd 60     local buttons_string = ""
S 61     for y = 0, 3 do
62         for x = 0, 3 do
63             local file_name = "technic_colorbutton"..(y * 4 + x)..".png"
64             buttons_string = buttons_string.."image_button["
1bf52c 65                 ..(coleft + 0.1 + x * 0.7)..","..(lotop + 0.1 + y * 0.7)
78cacd 66                 ..";0.8,0.8;"..file_name..";color_button"
S 67                 ..(y * 4 + x + 1)..";]"
68         end
69     end
70     return buttons_string
71 end
72
73
74 local function check_color_buttons(pos, meta, chest_name, fields)
75     for i = 1, 16 do
76         if fields["color_button"..i] then
d60e3f 77             local node = minetest.get_node(pos)
Z 78             node.name = chest_name..colorid_to_postfix(i)
79             minetest.swap_node(pos, node)
179364 80             meta:set_string("color", i)
78cacd 81             return
S 82         end
83     end
84 end
85
5c689a 86 local function set_formspec(pos, data, page)
R 87     local meta = minetest.get_meta(pos)
d4609f 88
S 89     -- Static formspec elements are in base_formspec
90     local fs = { data.base_formspec }
91
92     -- Pipeworks splitting setting
93     fs[#fs + 1] = fs_helpers.cycling_button(
94         meta,
95         "image_button[0,0.5;1,0.6",
96         "splitstacks",
97         {
98             pipeworks.button_off,
99             pipeworks.button_on
100         }
101     )
11f20d 102
5c689a 103     if data.autosort then
R 104         local status = meta:get_int("autosort")
d4609f 105         fs[#fs + 1] = ("checkbox[%g,%g;autosort_to_%s;%s;%s]"):format(
S 106             data.hileft + 2.2, data.lotop - 1.15,
107             tostring(1 - status), S("Auto-sort upon exit"), tostring(status == 1))
78cacd 108     end
d4609f 109
5c689a 110     if data.infotext then
78cacd 111         local formspec_infotext = minetest.formspec_escape(meta:get_string("infotext"))
d4609f 112
S 113         local button_fmt = "image_button[%g,0;0.8,0.8;%s;%s;]"
5c689a 114         if page == "main" then
d4609f 115             fs[#fs + 1] = button_fmt:format(data.hileft + 6.1,
S 116                 "technic_pencil_icon.png", "edit_infotext")
117
118             fs[#fs + 1] = "label["..(data.hileft+7.1)..",0.1;"..formspec_infotext.."]"
5c689a 119         elseif page == "edit_infotext" then
d4609f 120             fs[#fs + 1] = button_fmt:format(data.hileft + 6.1,
S 121                 "technic_checkmark_icon.png", "save_infotext")
122
123             fs[#fs + 1] = "field["..(data.hileft+7.3)..",0.2;4,1;"
124                     .."infotext_box;;"
78cacd 125                     ..formspec_infotext.."]"
S 126         end
5c689a 127     end
R 128     if data.color then
129         local colorID = meta:get_int("color")
130         local colorName
131         if chest_mark_colors[colorID] then
132             colorName = chest_mark_colors[colorID][2]
133         else
134             colorName = S("None")
135         end
d4609f 136         fs[#fs + 1] = "label["..(data.coleft+0.2)..","..(data.lotop+3)..";"..S("Color Filter: %s"):format(colorName).."]"
5c689a 137     end
d4609f 138     meta:set_string("formspec", table.concat(fs))
5c689a 139 end
R 140
141 local function sort_inventory(inv)
142     local inlist = inv:get_list("main")
143     local typecnt = {}
144     local typekeys = {}
145     for _, st in ipairs(inlist) do
146         if not st:is_empty() then
147             local n = st:get_name()
148             local w = st:get_wear()
149             local m = st:get_metadata()
150             local k = string.format("%s %05d %s", n, w, m)
151             if not typecnt[k] then
930c51 152                 typecnt[k] = {st}
5c689a 153                 table.insert(typekeys, k)
930c51 154             else
D 155                 table.insert(typecnt[k], st)
5c689a 156             end
R 157         end
158     end
159     table.sort(typekeys)
930c51 160     inv:set_list("main", {})
5c689a 161     for _, k in ipairs(typekeys) do
930c51 162         for _, item in ipairs(typecnt[k]) do
D 163             inv:add_item("main", item)
5c689a 164         end
R 165     end
166 end
167
168 local function get_receive_fields(name, data)
169     local lname = name:lower()
170     return function(pos, formname, fields, sender)
171         local meta = minetest.get_meta(pos)
172         local page = "main"
4a9ad9 173
HL 174         local owner = meta:get_string("owner")
175         if owner ~= "" then
176             -- prevent modification of locked chests
177             if owner ~= sender:get_player_name() then return end
178         elseif not fields.quit then
179             -- prevent modification of protected chests
180             if minetest.is_protected(pos, sender:get_player_name()) then return end
181         end
182
5c689a 183         if fields.sort or (data.autosort and fields.quit and meta:get_int("autosort") == 1) then
R 184             sort_inventory(meta:get_inventory())
d4609f 185             return -- No formspec update
5c689a 186         end
R 187         if fields.edit_infotext then
188             page = "edit_infotext"
189         end
190         if fields.autosort_to_1 then meta:set_int("autosort", 1) end
191         if fields.autosort_to_0 then meta:set_int("autosort", 0) end
192         if fields.infotext_box then
193             meta:set_string("infotext", fields.infotext_box)
194         end
78cacd 195         if data.color then
S 196             -- This sets the node
197             local nn = "technic:"..lname..(data.locked and "_locked" or "").."_chest"
198             check_color_buttons(pos, meta, nn, fields)
199         end
11f20d 200         if fields["fs_helpers_cycling:0:splitstacks"]
VE 201           or fields["fs_helpers_cycling:1:splitstacks"] then
202             if not pipeworks.may_configure(pos, sender) then return end
203             fs_helpers.on_receive_fields(pos, fields)
204         end
205
5c689a 206         set_formspec(pos, data, page)
78cacd 207     end
S 208 end
209
1d7cb7 210 function technic.chests:definition(name, data)
78cacd 211     local lname = name:lower()
179364 212     name = S(name)
78cacd 213
d4609f 214     -- Calculate formspec positions
1bf52c 215     data.lowidth = 8
Z 216     data.ovwidth = math.max(data.lowidth, data.width)
217     data.hileft = (data.ovwidth - data.width) / 2
218     data.loleft = (data.ovwidth - data.lowidth) / 2
219     if data.color then
220         if data.lowidth + 3 <= data.ovwidth then
221             data.coleft = data.ovwidth - 3
222             if data.loleft + data.lowidth > data.coleft then
223                 data.loleft = data.coleft - data.lowidth
224             end
225         else
226             data.loleft = 0
227             data.coleft = data.lowidth
228             data.ovwidth = data.lowidth + 3
229         end
230     end
231     data.lotop = data.height + 2
232     data.ovheight = data.lotop + 4
78cacd 233
d4609f 234     -- Set up constant formspec fields
S 235     local fs = {
236         "size["..data.ovwidth..","..data.ovheight.."]",
237         "label[0,0;"..S("%s Chest"):format(name).."]",
238         "list[context;main;"..data.hileft..",1;"..data.width..","..data.height..";]",
239         "list[current_player;main;"..data.loleft..","..data.lotop..";8,4;]",
240         "listring[]"
241     }
242     if #allow_label > 0 then
243         fs[#fs + 1] = ("label[0.9,0.5;%s]"):format(allow_label)
244     end
245
246     if data.color then
247         fs[#fs + 1] = get_color_buttons(data.coleft, data.lotop)
248     end
11f20d 249
5c689a 250     if data.sort then
d4609f 251         fs[#fs + 1] = ("button[%g,%g;2,0.7;sort;%s]"):format(
S 252             data.hileft, data.lotop - 1, S("Sort now"))
5c689a 253     end
d4609f 254     data.base_formspec = table.concat(fs)
78cacd 255
d4609f 256     local front = {"technic_"..lname.."_chest_front.png"}
0f6bdb 257     local locked_after_place
78cacd 258     if data.locked then
S 259         locked_after_place = function(pos, placer)
260             local meta = minetest.get_meta(pos)
261             meta:set_string("owner", placer:get_player_name() or "")
179364 262             meta:set_string("infotext",
S 263                     S("%s Locked Chest (owned by %s)")
264                     :format(name, meta:get_string("owner")))
cbfeb0 265             pipeworks.after_place(pos)
78cacd 266         end
S 267         table.insert(front, "technic_"..lname.."_chest_lock_overlay.png")
cbfeb0 268     else
VE 269         locked_after_place = pipeworks.after_place
78cacd 270     end
S 271
179364 272     local desc
S 273     if data.locked then
274         desc = S("%s Locked Chest"):format(name)
275     else
276         desc = S("%s Chest"):format(name)
277     end
278
b6d343 279     local tentry = tube_entry
VE 280     if tube_entry ~= "" then
281         if lname == "wooden" then
282             tentry = "^pipeworks_tube_connection_wooden.png"
283         elseif lname == "mithril" then
284             tentry = "^pipeworks_tube_connection_stony.png"
285         end
286     end
78cacd 287     local def = {
179364 288         description = desc,
b6d343 289         tiles = {
VE 290             "technic_"..lname.."_chest_top.png"..tentry,
291             "technic_"..lname.."_chest_top.png"..tentry,
292             "technic_"..lname.."_chest_side.png"..tentry,
293             "technic_"..lname.."_chest_side.png"..tentry,
294             "technic_"..lname.."_chest_side.png"..tentry,
295             table.concat(front, "^")
296         },
78cacd 297         paramtype2 = "facedir",
S 298         groups = self.groups,
299         tube = self.tube,
300         legacy_facedir_simple = true,
301         sounds = default.node_sound_wood_defaults(),
cbfeb0 302         after_place_node = locked_after_place,
d5db18 303         after_dig_node = pipeworks.after_dig,
VE 304
78cacd 305         on_construct = function(pos)
S 306             local meta = minetest.get_meta(pos)
179364 307             meta:set_string("infotext", S("%s Chest"):format(name))
5c689a 308             set_formspec(pos, data, "main")
78cacd 309             local inv = meta:get_inventory()
96ad67 310             inv:set_size("main", data.width * data.height)
78cacd 311         end,
S 312         can_dig = self.can_dig,
313         on_receive_fields = get_receive_fields(name, data),
314         on_metadata_inventory_move = self.on_inv_move,
315         on_metadata_inventory_put = self.on_inv_put,
930c51 316         on_metadata_inventory_take = self.on_inv_take,
6b52c7 317         on_blast = function(pos)
T 318             local drops = {}
319             default.get_inventory_drops(pos, "main", drops)
ec6354 320             drops[#drops+1] = "technic:"..name:lower()..(data.locked and "_locked" or "").."_chest"
6b52c7 321             minetest.remove_node(pos)
T 322             return drops
323         end,
78cacd 324     }
S 325     if data.locked then
326         def.allow_metadata_inventory_move = self.inv_move
327         def.allow_metadata_inventory_put = self.inv_put
328         def.allow_metadata_inventory_take = self.inv_take
35da22 329         def.on_blast = function() end
ec6354 330         def.can_dig = function(pos,player)
T 331             local meta = minetest.get_meta(pos);
332             local inv = meta:get_inventory()
333             return inv:is_empty("main") and default.can_interact_with_node(player, pos)
35da22 334         end
6b52c7 335         def.on_skeleton_key_use = function(pos, player, newsecret)
T 336             local meta = minetest.get_meta(pos)
337             local owner = meta:get_string("owner")
338             local name = player:get_player_name()
339
340             -- verify placer is owner of lockable chest
341             if owner ~= name then
342                 minetest.record_protection_violation(pos, name)
343                 minetest.chat_send_player(name, "You do not own this chest.")
344                 return nil
345             end
346
347             local secret = meta:get_string("key_lock_secret")
348             if secret == "" then
349                 secret = newsecret
350                 meta:set_string("key_lock_secret", secret)
351             end
352
353             return secret, "a locked chest", owner
35da22 354         end
78cacd 355     end
1d7cb7 356     return def
Z 357 end
78cacd 358
8a987b 359 local _TUBELIB_CALLBACKS = {
O 360     on_pull_item = function(pos, side, player_name)
361         if not minetest.is_protected(pos, player_name) then
362             local inv = minetest.get_meta(pos):get_inventory()
363             for _, stack in pairs(inv:get_list("main")) do
364                 if not stack:is_empty() then
365                     return inv:remove_item("main", stack:get_name())
366                 end
367             end
368         end
369         return nil
370     end,
371     on_push_item = function(pos, side, item, player_name)
372         local inv = minetest.get_meta(pos):get_inventory()
373         if inv:room_for_item("main", item) then
374             inv:add_item("main", item)
375             return true
376         end
377         return false
378     end,
379     on_unpull_item = function(pos, side, item, player_name)
380         local inv = minetest.get_meta(pos):get_inventory()
381         if inv:room_for_item("main", item) then
382             inv:add_item("main", item)
383             return true
384         end
385         return false
386     end,
387 }
388
1d7cb7 389 function technic.chests:register(name, data)
d4609f 390     data = table.copy(data) -- drop reference
1d7cb7 391     local def = technic.chests:definition(name, data)
78cacd 392
1d7cb7 393     local nn = "technic:"..name:lower()..(data.locked and "_locked" or "").."_chest"
78cacd 394     minetest.register_node(":"..nn, def)
d4609f 395     registered_chest_data[nn] = data
8a987b 396
O 397     if tubelib_exists then
398         tubelib.register_node(nn, {}, _TUBELIB_CALLBACKS)
399     end
78cacd 400
S 401     if data.color then
1d7cb7 402         local mk_front
Z 403         if string.find(def.tiles[6], "%^") then
404             mk_front = function (overlay) return def.tiles[6]:gsub("%^", "^"..overlay.."^") end
405         else
406             mk_front = function (overlay) return def.tiles[6].."^"..overlay end
407         end
78cacd 408         for i = 1, 15 do
S 409             local postfix = colorid_to_postfix(i)
410             local colordef = {}
411             for k, v in pairs(def) do
412                 colordef[k] = v
413             end
414             colordef.drop = nn
415             colordef.groups = self.groups_noinv
1d7cb7 416             colordef.tiles = { def.tiles[1], def.tiles[2], def.tiles[3], def.tiles[4], def.tiles[5], mk_front("technic_chest_overlay"..postfix..".png") }
d4609f 417
S 418             local new_name = nn .. postfix
419             minetest.register_node(":" .. new_name, colordef)
420             registered_chest_data[new_name] = data -- for all colors
421
8a987b 422             if tubelib_exists then
O 423                 tubelib.register_node(nn..postfix, {}, _TUBELIB_CALLBACKS)
424             end
78cacd 425         end
S 426     end
427 end
428
d4609f 429
S 430 -- Migration of chest formspecs
431 -- Group is specified in common.lua
432 minetest.register_lbm({
433     label = "technic_chests formspec upgrade",
434     name = "technic_chests:upgrade_formspec",
435     nodenames = {"group:technic_chest"},
436     run_at_every_load = false,
437     action = function(pos, node)
438         set_formspec(pos, registered_chest_data[node.name], "main")
439     end
440 })