-
Notifications
You must be signed in to change notification settings - Fork 2
/
chests.lua
380 lines (338 loc) · 10.7 KB
/
chests.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
default.chest = {}
-- support for MT game translation.
local S = default.get_translator
function default.chest.get_chest_formspec(pos)
local spos = pos.x .. "," .. pos.y .. "," .. pos.z
local formspec =
"size[8,9]" ..
"list[nodemeta:" .. spos .. ";main;0,0.3;8,4;]" ..
"list[current_player;main;0,4.85;8,1;]" ..
"list[current_player;main;0,6.08;8,3;8]" ..
"listring[nodemeta:" .. spos .. ";main]" ..
"listring[current_player;main]" ..
default.get_hotbar_bg(0,4.85)
return formspec
end
function default.chest.chest_lid_obstructed(pos)
local above = {x = pos.x, y = pos.y + 1, z = pos.z}
local def = minetest.registered_nodes[minetest.get_node(above).name]
-- allow ladders, signs, wallmounted things and torches to not obstruct
if def and
(def.drawtype == "airlike" or
def.drawtype == "signlike" or
def.drawtype == "torchlike" or
(def.drawtype == "nodebox" and def.paramtype2 == "wallmounted")) then
return false
end
return true
end
function default.chest.chest_lid_close(pn)
local chest_open_info = default.chest.open_chests[pn]
local pos = chest_open_info.pos
local sound = chest_open_info.sound
local swap = chest_open_info.swap
default.chest.open_chests[pn] = nil
for k, v in pairs(default.chest.open_chests) do
if vector.equals(v.pos, pos) then
-- another player is also looking at the chest
return true
end
end
local node = minetest.get_node(pos)
minetest.after(0.2, function()
local current_node = minetest.get_node(pos)
if current_node.name ~= swap .. "_open" then
-- the chest has already been replaced, don't try to replace what's there.
return
end
minetest.swap_node(pos, {name = swap, param2 = node.param2})
minetest.sound_play(sound, {gain = 0.3, pos = pos,
max_hear_distance = 10}, true)
end)
end
default.chest.open_chests = {}
minetest.register_on_player_receive_fields(function(player, formname, fields)
local pn = player:get_player_name()
if formname ~= "default:chest" then
if default.chest.open_chests[pn] then
default.chest.chest_lid_close(pn)
end
return
end
if not (fields.quit and default.chest.open_chests[pn]) then
return
end
default.chest.chest_lid_close(pn)
return true
end)
minetest.register_on_leaveplayer(function(player)
local pn = player:get_player_name()
if default.chest.open_chests[pn] then
default.chest.chest_lid_close(pn)
end
end)
function default.chest.register_chest(prefixed_name, d)
local name = prefixed_name:sub(1,1) == ':' and prefixed_name:sub(2,-1) or prefixed_name
local def = table.copy(d)
def.drawtype = "mesh"
def.visual = "mesh"
def.paramtype = "light"
def.paramtype2 = "facedir"
def.legacy_facedir_simple = true
def.is_ground_content = false
if def.protected then
def.on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", S("Locked Chest"))
meta:set_string("owner", "")
local inv = meta:get_inventory()
inv:set_size("main", 8*4)
end
def.after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name() or "")
meta:set_string("infotext", S("Locked Chest (owned by @1)", meta:get_string("owner")))
end
def.can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main") and
default.can_interact_with_node(player, pos)
end
def.allow_metadata_inventory_move = function(pos, from_list, from_index,
to_list, to_index, count, player)
if not default.can_interact_with_node(player, pos) then
return 0
end
return count
end
def.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if not default.can_interact_with_node(player, pos) then
return 0
end
return stack:get_count()
end
def.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if not default.can_interact_with_node(player, pos) then
return 0
end
return stack:get_count()
end
def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
if not default.can_interact_with_node(clicker, pos) then
return itemstack
end
local cn = clicker:get_player_name()
if default.chest.open_chests[cn] then
default.chest.chest_lid_close(cn)
end
minetest.sound_play(def.sound_open, {gain = 0.3,
pos = pos, max_hear_distance = 10}, true)
if not default.chest.chest_lid_obstructed(pos) then
minetest.swap_node(pos,
{ name = name .. "_open",
param2 = node.param2 })
end
minetest.after(0.2, minetest.show_formspec, cn,
"default:chest", default.chest.get_chest_formspec(pos))
default.chest.open_chests[cn] = { pos = pos,
sound = def.sound_close, swap = name }
end
def.on_blast = function() end
def.on_key_use = function(pos, player)
local secret = minetest.get_meta(pos):get_string("key_lock_secret")
local itemstack = player:get_wielded_item()
local key_meta = itemstack:get_meta()
if itemstack:get_meta():get_string("") == "" then
return
end
if key_meta:get_string("secret") == "" then
key_meta:set_string("secret", minetest.parse_json(itemstack:get_meta():get_string("")).secret)
itemstack:set_metadata("")
end
if secret ~= key_meta:get_string("secret") then
return
end
minetest.show_formspec(
player:get_player_name(),
"default:chest_locked",
default.chest.get_chest_formspec(pos)
)
end
def.on_skeleton_key_use = function(pos, player, newsecret)
local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner")
local pn = player:get_player_name()
-- verify placer is owner of lockable chest
if owner ~= pn then
minetest.record_protection_violation(pos, pn)
minetest.chat_send_player(pn, S("You do not own this chest."))
return nil
end
local secret = meta:get_string("key_lock_secret")
if secret == "" then
secret = newsecret
meta:set_string("key_lock_secret", secret)
end
return secret, S("a locked chest"), owner
end
else
def.on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", S("Chest"))
local inv = meta:get_inventory()
inv:set_size("main", 8*4)
end
def.can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main")
end
def.on_rightclick = function(pos, node, clicker)
local cn = clicker:get_player_name()
if default.chest.open_chests[cn] then
default.chest.chest_lid_close(cn)
end
minetest.sound_play(def.sound_open, {gain = 0.3, pos = pos,
max_hear_distance = 10}, true)
if not default.chest.chest_lid_obstructed(pos) then
minetest.swap_node(pos, {
name = name .. "_open",
param2 = node.param2 })
end
minetest.after(0.2, minetest.show_formspec,
cn,
"default:chest", default.chest.get_chest_formspec(pos))
default.chest.open_chests[cn] = { pos = pos,
sound = def.sound_close, swap = name }
end
def.on_blast = function(pos)
local drops = {}
default.get_inventory_drops(pos, "main", drops)
drops[#drops+1] = name
minetest.remove_node(pos)
return drops
end
end
default.set_inventory_action_loggers(def, "chest")
local def_opened = table.copy(def)
local def_closed = table.copy(def)
def_opened.mesh = "chest_open.obj"
for i = 1, #def_opened.tiles do
if type(def_opened.tiles[i]) == "string" then
def_opened.tiles[i] = {name = def_opened.tiles[i], backface_culling = true}
elseif def_opened.tiles[i].backface_culling == nil then
def_opened.tiles[i].backface_culling = true
end
end
def_opened.drop = name
def_opened.groups.not_in_creative_inventory = 1
def_opened.selection_box = {
type = "fixed",
fixed = { -1/2, -1/2, -1/2, 1/2, 3/16, 1/2 },
}
def_opened.can_dig = function()
return false
end
def_opened.on_blast = function() end
def_closed.mesh = nil
def_closed.drawtype = nil
def_closed.tiles[6] = def.tiles[5] -- swap textures around for "normal"
def_closed.tiles[5] = def.tiles[3] -- drawtype to make them match the mesh
def_closed.tiles[3] = def.tiles[3].."^[transformFX"
minetest.register_node(prefixed_name, def_closed)
minetest.register_node(prefixed_name .. "_open", def_opened)
-- convert old chests to this new variant
if name == "default:chest" or name == "default:chest_locked" then
minetest.register_lbm({
label = "update chests to opening chests",
name = "default:upgrade_" .. name:sub(9,-1) .. "_v2",
nodenames = {name},
action = function(pos, node)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", "")
local inv = meta:get_inventory()
local list = inv:get_list("default:chest")
if list then
inv:set_size("main", 8*4)
inv:set_list("main", list)
inv:set_list("default:chest", nil)
end
end
})
end
-- close opened chests on load
local modname, chestname = prefixed_name:match("^(:?.-):(.*)$")
minetest.register_lbm({
label = "close opened chests on load",
name = modname .. ":close_" .. chestname .. "_open",
nodenames = {prefixed_name .. "_open"},
run_at_every_load = true,
action = function(pos, node)
node.name = prefixed_name
minetest.swap_node(pos, node)
end
})
end
default.chest.register_chest("default:chest", {
description = S("Chest"),
tiles = {
"default_chest_top.png",
"default_chest_top.png",
"default_chest_side.png",
"default_chest_side.png",
"default_chest_front.png",
"default_chest_inside.png"
},
sounds = default.node_sound_wood_defaults(),
sound_open = "default_chest_open",
sound_close = "default_chest_close",
groups = {choppy = 2, oddly_breakable_by_hand = 2},
})
default.chest.register_chest("default:chest_locked", {
description = S("Locked Chest"),
tiles = {
"default_chest_top.png",
"default_chest_top.png",
"default_chest_side.png",
"default_chest_side.png",
"default_chest_lock.png",
"default_chest_inside.png"
},
sounds = default.node_sound_wood_defaults(),
sound_open = "default_chest_open",
sound_close = "default_chest_close",
groups = {choppy = 2, oddly_breakable_by_hand = 2},
protected = true,
})
minetest.register_craft({
output = "default:chest",
recipe = {
{"group:wood", "group:wood", "group:wood"},
{"group:wood", "", "group:wood"},
{"group:wood", "group:wood", "group:wood"},
}
})
minetest.register_craft({
output = "default:chest_locked",
recipe = {
{"group:wood", "group:wood", "group:wood"},
{"group:wood", "default:steel_ingot", "group:wood"},
{"group:wood", "group:wood", "group:wood"},
}
})
minetest.register_craft( {
type = "shapeless",
output = "default:chest_locked",
recipe = {"default:chest", "default:steel_ingot"},
})
minetest.register_craft({
type = "fuel",
recipe = "default:chest",
burntime = 30,
})
minetest.register_craft({
type = "fuel",
recipe = "default:chest_locked",
burntime = 30,
})