Added protected trapdoor
[protector.git] / doors_chest.lua
1
2 -- Register Protected Doors
3
4 local function on_rightclick(pos, dir, check_name, replace, replace_dir, params)
5
6 pos.y = pos.y+dir
7
8 if not minetest.get_node(pos).name == check_name then
9 return
10 end
11
12 local p2 = minetest.get_node(pos).param2
13
14 p2 = params[p2 + 1]
15
16 minetest.swap_node(pos, {name = replace_dir, param2 = p2})
17
18 pos.y = pos.y - dir
19
20 minetest.swap_node(pos, {name = replace, param2 = p2})
21
22 local snd_1 = "doors_door_close"
23 local snd_2 = "doors_door_open"
24
25 if params[1] == 3 then
26 snd_1 = "doors_door_open"
27 snd_2 = "doors_door_close"
28 end
29
30 if minetest.get_meta(pos):get_int("right") ~= 0 then
31
32 minetest.sound_play(snd_1, {
33 pos = pos, gain = 0.3, max_hear_distance = 10})
34 else
35
36 minetest.sound_play(snd_2, {
37 pos = pos, gain = 0.3, max_hear_distance = 10})
38 end
39 end
40
41 -- Protected Wooden Door
42
43 local name = "protector:door_wood"
44
45 doors.register_door(name, {
46 description = "Protected Wooden Door",
47 inventory_image = "doors_wood.png^protector_logo.png",
48 groups = {
49 snappy = 1, choppy = 2, oddly_breakable_by_hand = 2,
50 door = 1, unbreakable = 1
51 },
52 tiles_bottom = {"doors_wood_b.png^protector_logo.png", "doors_brown.png"},
53 tiles_top = {"doors_wood_a.png", "doors_brown.png"},
54 sounds = default.node_sound_wood_defaults(),
55 sunlight = false,
56 })
57
58 minetest.override_item(name .. "_b_1", {
59
60 on_rightclick = function(pos, node, clicker)
61
62 if not minetest.is_protected(pos, clicker:get_player_name()) then
63 on_rightclick(pos, 1, name .. "_t_1",
64 name .. "_b_2", name .. "_t_2", {1, 2, 3, 0})
65 end
66 end,
67 })
68
69 minetest.override_item(name.."_t_1", {
70
71 on_rightclick = function(pos, node, clicker)
72
73 if not minetest.is_protected(pos, clicker:get_player_name()) then
74 on_rightclick(pos, -1, name .. "_b_1",
75 name .. "_t_2", name .. "_b_2", {1, 2, 3, 0})
76 end
77 end,
78 })
79
80 minetest.override_item(name.."_b_2", {
81
82 on_rightclick = function(pos, node, clicker)
83
84 if not minetest.is_protected(pos, clicker:get_player_name()) then
85 on_rightclick(pos, 1, name .. "_t_2",
86 name .. "_b_1", name .. "_t_1", {3, 0, 1, 2})
87 end
88 end,
89 })
90
91 minetest.override_item(name.."_t_2", {
92
93 on_rightclick = function(pos, node, clicker)
94
95 if not minetest.is_protected(pos, clicker:get_player_name()) then
96 on_rightclick(pos, -1, name .. "_b_2",
97 name .. "_t_1", name .. "_b_1", {3, 0, 1, 2})
98 end
99 end,
100 })
101
102 minetest.register_craft({
103 output = name,
104 recipe = {
105 {"group:wood", "group:wood"},
106 {"group:wood", "default:copper_ingot"},
107 {"group:wood", "group:wood"}
108 }
109 })
110
111 minetest.register_craft({
112 output = name,
113 recipe = {
114 {"doors:door_wood", "default:copper_ingot"}
115 }
116 })
117
118 -- Protected Steel Door
119
120 local name = "protector:door_steel"
121
122 doors.register_door(name, {
123 description = "Protected Steel Door",
124 inventory_image = "doors_steel.png^protector_logo.png",
125 groups = {
126 snappy = 1, bendy = 2, cracky = 1,
127 level = 2, door = 1, unbreakable = 1
128 },
129 tiles_bottom = {"doors_steel_b.png^protector_logo.png", "doors_grey.png"},
130 tiles_top = {"doors_steel_a.png", "doors_grey.png"},
131 sounds = default.node_sound_wood_defaults(),
132 sunlight = false,
133 })
134
135 minetest.override_item(name.."_b_1", {
136
137 on_rightclick = function(pos, node, clicker)
138
139 if not minetest.is_protected(pos, clicker:get_player_name()) then
140 on_rightclick(pos, 1, name .. "_t_1",
141 name .. "_b_2", name .. "_t_2", {1, 2, 3, 0})
142 end
143 end,
144 })
145
146 minetest.override_item(name.."_t_1", {
147
148 on_rightclick = function(pos, node, clicker)
149
150 if not minetest.is_protected(pos, clicker:get_player_name()) then
151 on_rightclick(pos, -1, name .. "_b_1",
152 name .. "_t_2", name .. "_b_2", {1, 2, 3, 0})
153 end
154 end,
155 })
156
157 minetest.override_item(name.."_b_2", {
158
159 on_rightclick = function(pos, node, clicker)
160
161 if not minetest.is_protected(pos, clicker:get_player_name()) then
162 on_rightclick(pos, 1, name .. "_t_2",
163 name .. "_b_1", name .. "_t_1", {3, 0, 1, 2})
164 end
165 end,
166 })
167
168 minetest.override_item(name.."_t_2", {
169
170 on_rightclick = function(pos, node, clicker)
171
172 if not minetest.is_protected(pos, clicker:get_player_name()) then
173 on_rightclick(pos, -1, name .. "_b_2",
174 name .. "_t_1", name .. "_b_1", {3, 0, 1, 2})
175 end
176 end,
177 })
178
179 minetest.register_craft({
180 output = name,
181 recipe = {
182 {"default:steel_ingot", "default:steel_ingot"},
183 {"default:steel_ingot", "default:copper_ingot"},
184 {"default:steel_ingot", "default:steel_ingot"}
185 }
186 })
187
188 minetest.register_craft({
189 output = name,
190 recipe = {
191 {"doors:door_steel", "default:copper_ingot"}
192 }
193 })
194
195 -- Protected Chest
196
197 minetest.register_node("protector:chest", {
198 description = "Protected Chest",
199 tiles = {
200 "default_chest_top.png", "default_chest_top.png",
201 "default_chest_side.png", "default_chest_side.png",
202 "default_chest_side.png", "default_chest_front.png^protector_logo.png"
203 },
204 paramtype2 = "facedir",
205 groups = {choppy = 2, oddly_breakable_by_hand = 2, unbreakable = 1},
206 legacy_facedir_simple = true,
207 is_ground_content = false,
208 sounds = default.node_sound_wood_defaults(),
209
210 on_construct = function(pos)
211
212 local meta = minetest.get_meta(pos)
213 local inv = meta:get_inventory()
214
215 meta:set_string("infotext", "Protected Chest")
216 meta:set_string("name", "")
217 inv:set_size("main", 8 * 4)
218 end,
219
220 can_dig = function(pos,player)
221
222 local meta = minetest.get_meta(pos)
223 local inv = meta:get_inventory()
224
225 if inv:is_empty("main") then
226
227 if not minetest.is_protected(pos, player:get_player_name()) then
228 return true
229 end
230 end
231 end,
232
233 on_metadata_inventory_put = function(pos, listname, index, stack, player)
234
235 minetest.log("action", player:get_player_name()
236 .. " moves stuff to protected chest at "
237 .. minetest.pos_to_string(pos))
238 end,
239
240 on_metadata_inventory_take = function(pos, listname, index, stack, player)
241
242 minetest.log("action", player:get_player_name()
243 .. " takes stuff from protected chest at "
244 .. minetest.pos_to_string(pos))
245 end,
246
247 on_rightclick = function(pos, node, clicker)
248
249 if minetest.is_protected(pos, clicker:get_player_name()) then
250 return
251 end
252
253 local meta = minetest.get_meta(pos)
254 local spos = pos.x .. "," .. pos.y .. "," ..pos.z
255 local formspec = "size[8,9]"
256 .. default.gui_bg
257 .. default.gui_bg_img
258 .. default.gui_slots
259 .. "list[nodemeta:".. spos .. ";main;0,0.3;8,4;]"
260 .. "button[0,4.5;2,0.25;toup;To Chest]"
261 .. "field[2.3,4.8;4,0.25;chestname;;"
262 .. meta:get_string("name") .. "]"
263 .. "button[6,4.5;2,0.25;todn;To Inventory]"
264 .. "list[current_player;main;0,5;8,1;]"
265 .. "list[current_player;main;0,6.08;8,3;8]"
266 .. "listring[nodemeta:" .. spos .. ";main]"
267 .. "listring[current_player;main]"
268 .. default.get_hotbar_bg(0,5)
269
270 minetest.show_formspec(
271 clicker:get_player_name(),
272 "protector:chest_" .. minetest.pos_to_string(pos),
273 formspec)
274 end,
275 })
276
277 -- Protected Chest formspec buttons
278
279 minetest.register_on_player_receive_fields(function(player, formname, fields)
280
281 if string.sub(formname, 0, string.len("protector:chest_")) == "protector:chest_" then
282
283 local pos_s = string.sub(formname,string.len("protector:chest_") + 1)
284 local pos = minetest.string_to_pos(pos_s)
285 local meta = minetest.get_meta(pos)
286 local chest_inv = meta:get_inventory()
287 local player_inv = player:get_inventory()
288 local leftover
289
290 if fields.toup then
291
292 -- copy contents of players inventory to chest
293 for i, v in ipairs (player_inv:get_list("main") or {}) do
294
295 if chest_inv
296 and chest_inv:room_for_item('main', v) then
297
298 leftover = chest_inv:add_item('main', v)
299
300 player_inv:remove_item("main", v)
301
302 if leftover
303 and not leftover:is_empty() then
304 player_inv:add_item("main", v)
305 end
306 end
307 end
308
309 elseif fields.todn then
310
311 -- copy contents of chest to players inventory
312 for i, v in ipairs (chest_inv:get_list('main') or {}) do
313
314 if player_inv:room_for_item("main", v) then
315
316 leftover = player_inv:add_item("main", v)
317
318 chest_inv:remove_item('main', v)
319
320 if leftover
321 and not leftover:is_empty() then
322 chest_inv:add_item('main', v)
323 end
324 end
325 end
326
327 elseif fields.chestname then
328
329 -- change chest infotext to display name
330 if fields.chestname ~= "" then
331
332 meta:set_string("name", fields.chestname)
333 meta:set_string("infotext",
334 "Protected Chest (" .. fields.chestname .. ")")
335 else
336 meta:set_string("infotext", "Protected Chest")
337 end
338
339 end
340 end
341
342 end)
343
344 -- Protected Chest recipes
345
346 minetest.register_craft({
347 output = 'protector:chest',
348 recipe = {
349 {'group:wood', 'group:wood', 'group:wood'},
350 {'group:wood', 'default:copper_ingot', 'group:wood'},
351 {'group:wood', 'group:wood', 'group:wood'},
352 }
353 })
354
355 minetest.register_craft({
356 output = 'protector:chest',
357 recipe = {
358 {'default:chest', 'default:copper_ingot', ''},
359 }
360 })
361
362 -- Protected Trapdoor
363
364 function register_trapdoor(name, def)
365 local name_closed = name
366 local name_opened = name.."_open"
367
368 def.on_rightclick = function (pos, node, clicker)
369
370 if minetest.is_protected(pos, clicker:get_player_name()) then
371 return
372 end
373
374 local newname = node.name == name_closed and name_opened or name_closed
375 local sound = false
376 if node.name == name_closed then sound = def.sound_open end
377 if node.name == name_opened then sound = def.sound_close end
378 if sound then
379 minetest.sound_play(sound, {pos = pos, gain = 0.3, max_hear_distance = 10})
380 end
381 minetest.set_node(pos, {name = newname, param1 = node.param1, param2 = node.param2})
382 end
383
384 def.on_rotate = minetest.get_modpath("screwdriver") and screwdriver.rotate_simple
385
386 -- Common trapdoor configuration
387 def.drawtype = "nodebox"
388 def.paramtype = "light"
389 def.paramtype2 = "facedir"
390 def.is_ground_content = false
391
392 local def_opened = table.copy(def)
393 local def_closed = table.copy(def)
394
395 def_closed.node_box = {
396 type = "fixed",
397 fixed = {-0.5, -0.5, -0.5, 0.5, -0.4, 0.5}
398 }
399 def_closed.selection_box = {
400 type = "fixed",
401 fixed = {-0.5, -0.5, -0.5, 0.5, -0.4, 0.5}
402 }
403 def_closed.tiles = { def.tile_front, def.tile_front, def.tile_side, def.tile_side,
404 def.tile_side, def.tile_side }
405
406 def_opened.node_box = {
407 type = "fixed",
408 fixed = {-0.5, -0.5, 0.4, 0.5, 0.5, 0.5}
409 }
410 def_opened.selection_box = {
411 type = "fixed",
412 fixed = {-0.5, -0.5, 0.4, 0.5, 0.5, 0.5}
413 }
414 def_opened.tiles = { def.tile_side, def.tile_side, def.tile_side, def.tile_side,
415 def.tile_front, def.tile_front }
416 def_opened.drop = name_closed
417 def_opened.groups.not_in_creative_inventory = 1
418
419 minetest.register_node(name_opened, def_opened)
420 minetest.register_node(name_closed, def_closed)
421 end
422
423
424
425 register_trapdoor("protector:trapdoor", {
426 description = "Protected Trapdoor",
427 inventory_image = "doors_trapdoor.png^protector_logo.png",
428 wield_image = "doors_trapdoor.png^protector_logo.png",
429 tile_front = "doors_trapdoor.png^protector_logo.png",
430 tile_side = "doors_trapdoor_side.png",
431 groups = {snappy=1, choppy=2, oddly_breakable_by_hand=2, flammable=2, door=1},
432 sounds = default.node_sound_wood_defaults(),
433 sound_open = "doors_door_open",
434 sound_close = "doors_door_close"
435 })
436
437 minetest.register_craft({
438 output = 'protector:trapdoor 2',
439 recipe = {
440 {'group:wood', 'default:copper_ingot', 'group:wood'},
441 {'group:wood', 'group:wood', 'group:wood'},
442 {'', '', ''},
443 }
444 })
445
446 minetest.register_craft({
447 output = "protector:trapdoor",
448 recipe = {
449 {"doors:trapdoor", "default:copper_ingot"}
450 }
451 })