Remove lots of redundant code in kerova
[minetest_hades/hades_revisited.git] / mods / default / chests.lua
blobf8e32836349783b2718e9c994828e7d60e859db7
1 default.chest_formspec =
2 "size[8,9]"..
3 "list[current_name;main;0,0;8,4;]"..
4 "list[current_player;main;0,5;8,4;]"..
5 "listring[]"..
6 "background[-0.5,-0.65;9,10.35;".."chestui.png".."]"
8 function default.get_locked_chest_formspec(pos)
9 local spos = pos.x .. "," .. pos.y .. "," ..pos.z
10 local formspec =
11 "size[8,9]"..
12 "list[nodemeta:".. spos .. ";main;0,0;8,4;]"..
13 "list[current_player;main;0,5;8,4;]"..
14 "listring[]"..
15 "background[-0.5,-0.65;9,10.35;".."chestui.png".."]"
16 return formspec
17 end
19 minetest.register_node("default:chest", {
20 description = "Chest",
21 tiles = {"default_chest_top.png", "default_chest_top.png", "default_chest_side.png",
22 "default_chest_side.png", "default_chest_side.png", "default_chest_front.png"},
23 paramtype2 = "facedir",
24 groups = {choppy=2,oddly_breakable_by_hand=2, chest=1},
25 legacy_facedir_simple = true,
26 is_ground_content = false,
27 sounds = default.node_sound_wood_defaults(),
28 on_construct = function(pos)
29 local meta = minetest.get_meta(pos)
30 meta:set_string("formspec",default.chest_formspec)
31 meta:set_string("infotext", "Chest")
32 local inv = meta:get_inventory()
33 inv:set_size("main", 8*4)
34 end,
35 can_dig = function(pos,player)
36 local meta = minetest.get_meta(pos);
37 local inv = meta:get_inventory()
38 return inv:is_empty("main")
39 end,
40 on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
41 minetest.log("action", player:get_player_name()..
42 " moves stuff in chest at "..minetest.pos_to_string(pos))
43 end,
44 on_metadata_inventory_put = function(pos, listname, index, stack, player)
45 minetest.log("action", player:get_player_name()..
46 " moves stuff to chest at "..minetest.pos_to_string(pos))
47 end,
50 local function has_locked_chest_privilege(meta, player)
51 if player:get_player_name() ~= meta:get_string("owner") then
52 return false
53 end
54 return true
55 end
57 minetest.register_node("default:chest_locked", {
58 description = "Locked Chest",
59 tiles = {"default_chest_top.png", "default_chest_top.png", "default_chest_side.png",
60 "default_chest_side.png", "default_chest_side.png", "default_chest_lock.png"},
61 paramtype2 = "facedir",
62 groups = {choppy=2,oddly_breakable_by_hand=2, locked_chest=1},
63 legacy_facedir_simple = true,
64 is_ground_content = false,
65 sounds = default.node_sound_wood_defaults(),
66 after_place_node = function(pos, placer)
67 local meta = minetest.get_meta(pos)
68 meta:set_string("owner", placer:get_player_name() or "")
69 meta:set_string("infotext", "Locked Chest (owned by "..
70 meta:get_string("owner")..")")
71 end,
72 on_construct = function(pos)
73 local meta = minetest.get_meta(pos)
74 meta:set_string("infotext", "Locked Chest")
75 meta:set_string("owner", "")
76 local inv = meta:get_inventory()
77 inv:set_size("main", 8*4)
78 end,
79 can_dig = function(pos,player)
80 local meta = minetest.get_meta(pos);
81 local inv = meta:get_inventory()
82 return inv:is_empty("main") and has_locked_chest_privilege(meta, player)
83 end,
84 allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
85 local meta = minetest.get_meta(pos)
86 if not has_locked_chest_privilege(meta, player) then
87 minetest.log("action", player:get_player_name()..
88 " tried to access a locked chest belonging to "..
89 meta:get_string("owner").." at "..
90 minetest.pos_to_string(pos))
91 return 0
92 end
93 return count
94 end,
95 allow_metadata_inventory_put = function(pos, listname, index, stack, player)
96 local meta = minetest.get_meta(pos)
97 if not has_locked_chest_privilege(meta, player) then
98 minetest.log("action", player:get_player_name()..
99 " tried to access a locked chest belonging to "..
100 meta:get_string("owner").." at "..
101 minetest.pos_to_string(pos))
102 return 0
104 return stack:get_count()
105 end,
106 allow_metadata_inventory_take = function(pos, listname, index, stack, player)
107 local meta = minetest.get_meta(pos)
108 if not has_locked_chest_privilege(meta, player) then
109 minetest.log("action", player:get_player_name()..
110 " tried to access a locked chest belonging to "..
111 meta:get_string("owner").." at "..
112 minetest.pos_to_string(pos))
113 return 0
115 return stack:get_count()
116 end,
117 on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
118 minetest.log("action", player:get_player_name()..
119 " moves stuff in locked chest at "..minetest.pos_to_string(pos))
120 end,
121 on_metadata_inventory_put = function(pos, listname, index, stack, player)
122 minetest.log("action", player:get_player_name()..
123 " moves stuff to locked chest at "..minetest.pos_to_string(pos))
124 end,
125 on_metadata_inventory_take = function(pos, listname, index, stack, player)
126 minetest.log("action", player:get_player_name()..
127 " takes stuff from locked chest at "..minetest.pos_to_string(pos))
128 end,
129 on_rightclick = function(pos, node, clicker)
130 local meta = minetest.get_meta(pos)
131 if has_locked_chest_privilege(meta, clicker) then
132 minetest.show_formspec(
133 clicker:get_player_name(),
134 "default:chest_locked",
135 default.get_locked_chest_formspec(pos)
138 end,