Maciej Kasatkin
2012-09-11 86bb776f7848f26435723f671ef7d238ace19f76
commit | author | age
167434 1 local chest_mark_colors = {
MK 2     '', -- regular chest, without color bar
3     '_black',
4     '_blue', 
5     '_brown',
6     '_cyan',
7     '_dark_green',
8     '_dark_grey',
9     '_green',
10     '_grey',
11     '_magenta',
12     '_orange',
13     '_pink',
14     '_red',
15     '_violet',
16     '_white',
17     '_yellow',
18 }
19
20 minetest.register_craft({
21     output = 'technic:gold_chest 1',
22     recipe = {
23         {'moreores:gold_ingot','moreores:gold_ingot','moreores:gold_ingot'},
24         {'moreores:gold_ingot','technic:silver_chest','moreores:gold_ingot'},
25         {'moreores:gold_ingot','moreores:gold_ingot','moreores:gold_ingot'},
26     }
27 })
28
29 minetest.register_craft({
30     output = 'technic:gold_locked_chest 1',
31     recipe = {
32         {'moreores:gold_ingot','moreores:gold_ingot','moreores:gold_ingot'},
33         {'moreores:gold_ingot','technic:silver_locked_chest','moreores:gold_ingot'},
34         {'moreores:gold_ingot','moreores:gold_ingot','moreores:gold_ingot'},
35     }
36 })
37
38 minetest.register_craft({
39     output = 'technic:gold_locked_chest 1',
40     recipe = {
41         {'default:steel_ingot'},
42         {'technic:gold_chest'},
43     }
44 })
45
46 minetest.register_craftitem("technic:gold_chest", {
47     description = "Gold Chest",
48     stack_max = 99,
49 })
50 minetest.register_craftitem("technic:gold_locked_chest", {
51     description = "Gold Locked Chest",
52     stack_max = 99,
53 })
54
55 for i, state in ipairs(chest_mark_colors) do
56 minetest.register_node("technic:gold_chest".. state, {
57     description = "Gold Chest",
58     tiles = {"technic_gold_chest_top.png", "technic_gold_chest_top.png", "technic_gold_chest_side.png",
59         "technic_gold_chest_side.png", "technic_gold_chest_side.png", "technic_gold_chest_front"..state..".png"},
60     paramtype2 = "facedir",
61     groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
62     legacy_facedir_simple = true,
63     sounds = default.node_sound_wood_defaults(),
64     on_construct = function(pos)
65         local meta = minetest.env:get_meta(pos)
66         meta:set_string("formspec",
67                 "invsize[12,9;]"..
68                 "list[current_name;main;0,0;12,4;]"..
69                 "list[current_player;main;0,5;8,4;]")
70         meta:set_string("infotext", "Gold Chest")
71         local inv = meta:get_inventory()
72         inv:set_size("main", 12*4)
73     end,
74     
75     can_dig = function(pos,player)
76         local meta = minetest.env:get_meta(pos);
77         local inv = meta:get_inventory()
78         return inv:is_empty("main")
79     end,
80     
81     on_punch = function (pos, node, puncher)
82     chest_punched (pos,node,puncher);
83     end,
84     
85     on_receive_fields = function(pos, formname, fields, sender)
86         local meta = minetest.env:get_meta(pos);
87               fields.text = fields.text or ""
88         meta:set_string("text", fields.text)
89         meta:set_string("infotext", '"'..fields.text..'"')
90
91         meta:set_string("formspec",
92                 "invsize[12,9;]"..
93                 "list[current_name;main;0,0;12,4;]"..
94                 "list[current_player;main;0,5;8,4;]")
95     end,
96
97     on_metadata_inventory_move = function(pos, from_list, from_index,
98             to_list, to_index, count, player)
99         minetest.log("action", player:get_player_name()..
100                 " moves stuff in chest at "..minetest.pos_to_string(pos))
101         return minetest.node_metadata_inventory_move_allow_all(
102                 pos, from_list, from_index, to_list, to_index, count, player)
103     end,
104     on_metadata_inventory_offer = function(pos, listname, index, stack, player)
105         minetest.log("action", player:get_player_name()..
106                 " moves stuff to chest at "..minetest.pos_to_string(pos))
107         return minetest.node_metadata_inventory_offer_allow_all(
108                 pos, listname, index, stack, player)
109     end,
110     on_metadata_inventory_take = function(pos, listname, index, stack, player)
111         minetest.log("action", player:get_player_name()..
112                 " takes stuff from chest at "..minetest.pos_to_string(pos))
113     end,
114 })
115 end
116
117
118 local function has_locked_chest_privilege(meta, player)
119     if player:get_player_name() ~= meta:get_string("owner") then
120         return false
121     end
122     return true
123 end
124
125 for i, state in ipairs(chest_mark_colors) do
126 minetest.register_node("technic:gold_locked_chest".. state, {
127     description = "Gold Locked Chest",
128     tiles = {"technic_gold_chest_top.png", "technic_gold_chest_top.png", "technic_gold_chest_side.png",
129         "technic_gold_chest_side.png", "technic_gold_chest_side.png", "technic_gold_chest_locked"..state..".png"},
130     paramtype2 = "facedir",
131     groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
132     legacy_facedir_simple = true,
133     sounds = default.node_sound_wood_defaults(),
134     after_place_node = function(pos, placer)
135         local meta = minetest.env:get_meta(pos)
136         meta:set_string("owner", placer:get_player_name() or "")
137         meta:set_string("infotext", "Gold Locked Chest (owned by "..
138                 meta:get_string("owner")..")")
139     end,
140     on_construct = function(pos)
141         local meta = minetest.env:get_meta(pos)
142         meta:set_string("formspec",
143                 "invsize[12,9;]"..
144                 "list[current_name;main;0,0;12,4;]"..
145                 "list[current_player;main;0,5;8,4;]")
146         meta:set_string("infotext", "Gold Locked Chest")
147         meta:set_string("owner", "")
148         local inv = meta:get_inventory()
149         inv:set_size("main", 12*4)
150     end,
151     can_dig = function(pos,player)
152         local meta = minetest.env:get_meta(pos);
153         local inv = meta:get_inventory()
154         return inv:is_empty("main")
155     end,
156
157     on_punch = function (pos, node, puncher)
158             local meta = minetest.env:get_meta(pos);
159         if (has_locked_chest_privilege(meta, puncher)) then
160         locked_chest_punched (pos,node,puncher);
161         end
162        end,
163     
164     on_receive_fields = function(pos, formname, fields, sender)
165         local meta = minetest.env:get_meta(pos);
166               fields.text = fields.text or ""
167         meta:set_string("text", fields.text)
168         meta:set_string("infotext", '"'..fields.text..'"')
169
170         meta:set_string("formspec",
171                 "invsize[12,9;]"..
172                 "list[current_name;main;0,0;12,4;]"..
173                 "list[current_player;main;0,5;8,4;]")
174     end,
175
176     allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
177         local meta = minetest.env:get_meta(pos)
178         if not has_locked_chest_privilege(meta, player) then
179             minetest.log("action", player:get_player_name()..
180                     " tried to access a locked chest belonging to "..
181                     meta:get_string("owner").." at "..
182                     minetest.pos_to_string(pos))
183             return 0
184         end
185         return count
186     end,
187     allow_metadata_inventory_put = function(pos, listname, index, stack, player)
188         local meta = minetest.env:get_meta(pos)
189         if not has_locked_chest_privilege(meta, player) then
190             minetest.log("action", player:get_player_name()..
191                     " tried to access a locked chest belonging to "..
192                     meta:get_string("owner").." at "..
193                     minetest.pos_to_string(pos))
194             return 0
195         end
196         return stack:get_count()
197     end,
198     allow_metadata_inventory_take = function(pos, listname, index, stack, player)
199         local meta = minetest.env:get_meta(pos)
200         if not has_locked_chest_privilege(meta, player) then
201             minetest.log("action", player:get_player_name()..
202                     " tried to access a locked chest belonging to "..
203                     meta:get_string("owner").." at "..
204                     minetest.pos_to_string(pos))
205             return 0
206         end
207         return stack:get_count()
208     end,
209     on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
210         minetest.log("action", player:get_player_name()..
211                 " moves stuff in locked chest at "..minetest.pos_to_string(pos))
212     end,
213     on_metadata_inventory_put = function(pos, listname, index, stack, player)
214         minetest.log("action", player:get_player_name()..
215                 " moves stuff to locked chest at "..minetest.pos_to_string(pos))
216     end,
217     on_metadata_inventory_take = function(pos, listname, index, stack, player)
218         minetest.log("action", player:get_player_name()..
219                 " takes stuff from locked chest at "..minetest.pos_to_string(pos))
220     end,
221 })
222 end
223
224 function chest_punched (pos,node,puncher)
225     
226     local player_tool = puncher:get_wielded_item();
227     local item=player_tool:get_name();
228     if item == "dye:black" then
229         if (hacky_swap_node(pos,"technic:gold_chest_black")) then
230             player_tool:take_item(1);
231             puncher:set_wielded_item(player_tool);
232             return
233            end
234         end
235     if item == "dye:blue" then
236         if (hacky_swap_node(pos,"technic:gold_chest_blue")) then
237             player_tool:take_item(1);
238             puncher:set_wielded_item(player_tool);
239             return
240            end
241         end
242     if item == "dye:brown" then
243         if (hacky_swap_node(pos,"technic:gold_chest_brown")) then
244             player_tool:take_item(1);
245             puncher:set_wielded_item(player_tool);
246             return
247            end
248         end
249     if item == "dye:cyan" then
250         if (hacky_swap_node(pos,"technic:gold_chest_cyan")) then
251             player_tool:take_item(1);
252             puncher:set_wielded_item(player_tool);
253             return
254            end
255         end
256     if item == "dye:dark_green" then
257         if (hacky_swap_node(pos,"technic:gold_chest_dark_green")) then
258             player_tool:take_item(1);
259             puncher:set_wielded_item(player_tool);
260             return
261            end
262         end
263     if item == "dye:dark_grey" then
264         if (hacky_swap_node(pos,"technic:gold_chest_dark_grey")) then
265             player_tool:take_item(1);
266             puncher:set_wielded_item(player_tool);
267             return
268            end
269         end
270     if item == "dye:green" then
271         if (hacky_swap_node(pos,"technic:gold_chest_green")) then
272             player_tool:take_item(1);
273             puncher:set_wielded_item(player_tool);
274             return
275            end
276         end
277     if item == "dye:grey" then
278         if (hacky_swap_node(pos,"technic:gold_chest_grey")) then
279             player_tool:take_item(1);
280             puncher:set_wielded_item(player_tool);
281             return
282            end
283         end
284     if item == "dye:magenta" then
285         if (hacky_swap_node(pos,"technic:gold_chest_magenta")) then
286             player_tool:take_item(1);
287             puncher:set_wielded_item(player_tool);
288             return
289            end
290         end
291     if item == "dye:orange" then
292         if (hacky_swap_node(pos,"technic:gold_chest_orange")) then
293             player_tool:take_item(1);
294             puncher:set_wielded_item(player_tool);
295             return
296            end
297         end
298     if item == "dye:pink" then
299         if (hacky_swap_node(pos,"technic:gold_chest_pink")) then
300             player_tool:take_item(1);
301             puncher:set_wielded_item(player_tool);
302             return
303            end
304         end
305     if item == "dye:red" then
306         if (hacky_swap_node(pos,"technic:gold_chest_red")) then
307             player_tool:take_item(1);
308             puncher:set_wielded_item(player_tool);
309             return
310            end
311         end
312     if item == "dye:violet" then
313         if (hacky_swap_node(pos,"technic:gold_chest_violet")) then
314             player_tool:take_item(1);
315             puncher:set_wielded_item(player_tool);
316             return
317            end
318         end
319     if item == "dye:white" then
320         if (hacky_swap_node(pos,"technic:gold_chest_white")) then
321             player_tool:take_item(1);
322             puncher:set_wielded_item(player_tool);
323             return
324            end
325         end
326     if item == "dye:yellow" then
327         if (hacky_swap_node(pos,"technic:gold_chest_yellow")) then
328             player_tool:take_item(1);
329             puncher:set_wielded_item(player_tool);
330             return
331            end
332         end
333
334         local meta = minetest.env:get_meta(pos);
335                 meta:set_string("formspec", "hack:sign_text_input")
336     end
337
338
339 function locked_chest_punched (pos,node,puncher)
340     
341     local player_tool = puncher:get_wielded_item();
342     local item=player_tool:get_name();
343     if item == "dye:black" then
344         if (hacky_swap_node(pos,"technic:gold_locked_chest_black")) then
345             player_tool:take_item(1);
346             puncher:set_wielded_item(player_tool);
347             return
348            end
349         end
350     if item == "dye:blue" then
351         if (hacky_swap_node(pos,"technic:gold_locked_chest_blue")) then
352             player_tool:take_item(1);
353             puncher:set_wielded_item(player_tool);
354             return
355            end
356         end
357     if item == "dye:brown" then
358         if (hacky_swap_node(pos,"technic:gold_locked_chest_brown")) then
359             player_tool:take_item(1);
360             puncher:set_wielded_item(player_tool);
361             return
362            end
363         end
364     if item == "dye:cyan" then
365         if (hacky_swap_node(pos,"technic:gold_locked_chest_cyan")) then
366             player_tool:take_item(1);
367             puncher:set_wielded_item(player_tool);
368             return
369            end
370         end
371     if item == "dye:dark_green" then
372         if (hacky_swap_node(pos,"technic:gold_locked_chest_dark_green")) then
373             player_tool:take_item(1);
374             puncher:set_wielded_item(player_tool);
375             return
376            end
377         end
378     if item == "dye:dark_grey" then
379         if (hacky_swap_node(pos,"technic:gold_locked_chest_dark_grey")) then
380             player_tool:take_item(1);
381             puncher:set_wielded_item(player_tool);
382             return
383            end
384         end
385     if item == "dye:green" then
386         if (hacky_swap_node(pos,"technic:gold_locked_chest_green")) then
387             player_tool:take_item(1);
388             puncher:set_wielded_item(player_tool);
389             return
390            end
391         end
392     if item == "dye:grey" then
393         if (hacky_swap_node(pos,"technic:gold_locked_chest_grey")) then
394             player_tool:take_item(1);
395             puncher:set_wielded_item(player_tool);
396             return
397            end
398         end
399     if item == "dye:magenta" then
400         if (hacky_swap_node(pos,"technic:gold_locked_chest_magenta")) then
401             player_tool:take_item(1);
402             puncher:set_wielded_item(player_tool);
403             return
404            end
405         end
406     if item == "dye:orange" then
407         if (hacky_swap_node(pos,"technic:gold_locked_chest_orange")) then
408             player_tool:take_item(1);
409             puncher:set_wielded_item(player_tool);
410             return
411            end
412         end
413     if item == "dye:pink" then
414         if (hacky_swap_node(pos,"technic:gold_locked_chest_pink")) then
415             player_tool:take_item(1);
416             puncher:set_wielded_item(player_tool);
417             return
418            end
419         end
420     if item == "dye:red" then
421         if (hacky_swap_node(pos,"technic:gold_locked_chest_red")) then
422             player_tool:take_item(1);
423             puncher:set_wielded_item(player_tool);
424             return
425            end
426         end
427     if item == "dye:violet" then
428         if (hacky_swap_node(pos,"technic:gold_locked_chest_violet")) then
429             player_tool:take_item(1);
430             puncher:set_wielded_item(player_tool);
431             return
432            end
433         end
434     if item == "dye:white" then
435         if (hacky_swap_node(pos,"technic:gold_locked_chest_white")) then
436             player_tool:take_item(1);
437             puncher:set_wielded_item(player_tool);
438             return
439            end
440         end
441     if item == "dye:yellow" then
442         if (hacky_swap_node(pos,"technic:gold_locked_chest_yellow")) then
443             player_tool:take_item(1);
444             puncher:set_wielded_item(player_tool);
445             return
446            end
447         end
448
449         local meta = minetest.env:get_meta(pos);
450                 meta:set_string("formspec", "hack:sign_text_input")
451     end
452