]> git.lizzy.rs Git - Crafter.git/blob - mods/utility/chest.lua
e61d404e9efd2d8f2cc2146c9e0f50f58bfd633e
[Crafter.git] / mods / utility / chest.lua
1 local chest = {}
2
3 function chest.get_chest_formspec(pos)
4         local spos = pos.x .. "," .. pos.y .. "," .. pos.z
5         local formspec =
6                 "size[9,8.75]" ..
7                 "listcolors[#8b8a89;#c9c3c6;#3e3d3e;#000000;#FFFFFF]"..
8                 "background[-0.19,-0.25;9.41,9.49;gui_hb_bg.png]"..
9                 "list[nodemeta:" .. spos .. ";main;0,0.3;9,4;]" ..
10                 "list[current_player;main;0,4.5;9,1;]" ..
11                 "list[current_player;main;0,6.08;9,3;8]" ..
12                 "listring[nodemeta:" .. spos .. ";main]" ..
13                 "listring[current_player;main]" --..
14                 --default.get_hotbar_bg(0,4.85)
15         return formspec
16 end
17
18 function chest.chest_lid_close(pn)
19         local chest_open_info = chest.open_chests[pn]
20         local pos = chest_open_info.pos
21         local sound = chest_open_info.sound
22         local swap = chest_open_info.swap
23
24         chest.open_chests[pn] = nil
25         for k, v in pairs(chest.open_chests) do
26                 if v.pos.x == pos.x and v.pos.y == pos.y and v.pos.z == pos.z then
27                         return true
28                 end
29         end
30
31         local node = minetest.get_node(pos)
32         minetest.after(0.2, function(pos,swap,node)
33                 if minetest.get_node(pos).name == "utility:chest_open" then
34                         minetest.swap_node(pos,{name = "utility:"..swap,param2=node.param2})
35                         minetest.sound_play(sound, {gain = 0.3, pos = pos, max_hear_distance = 10},true)
36                 end
37         end,pos,swap,node)
38 end
39
40 chest.open_chests = {}
41
42 minetest.register_on_player_receive_fields(function(player, formname, fields)
43         if formname ~= "utility:chest" then
44                 return
45         end
46         if not player or not fields.quit then
47                 return
48         end
49         local pn = player:get_player_name()
50
51         if not chest.open_chests[pn] then
52                 return
53         end
54
55         chest.chest_lid_close(pn)
56         return true
57 end)
58
59 minetest.register_on_leaveplayer(function(player)
60         local pn = player:get_player_name()
61         if chest.open_chests[pn] then
62                 chest.chest_lid_close(pn)
63         end
64 end)
65
66 local function destroy_chest(pos)
67         local meta = minetest.get_meta(pos)
68         local inv = meta:get_inventory()
69         local lists = inv:get_lists()
70         for listname,_ in pairs(lists) do
71                 local size = inv:get_size(listname)
72                 for i = 1,size do
73                         local stack = inv:get_stack(listname, i)
74                         minetest.add_item(pos, stack)
75                 end
76         end
77 end
78
79 function chest.register_chest(name, d)
80         local def = table.copy(d)
81         def.drawtype = "mesh"
82         def.visual = "mesh"
83         def.paramtype = "light"
84         def.paramtype2 = "facedir"
85         def.legacy_facedir_simple = true
86         def.is_ground_content = false
87
88         if def.protected then
89                 def.on_construct = function(pos)
90                         local meta = minetest.get_meta(pos)
91                         --meta:set_string("infotext", S("Locked Chest"))
92                         meta:set_string("owner", "")
93                         local inv = meta:get_inventory()
94                         inv:set_size("main", 9*4)
95                 end
96                 def.after_place_node = function(pos, placer)
97                         local meta = minetest.get_meta(pos)
98                         meta:set_string("owner", placer:get_player_name() or "")
99                         --meta:set_string("infotext", S("Locked Chest (owned by @1)", meta:get_string("owner")))
100                 end
101
102                 def.allow_metadata_inventory_move = function(pos, from_list, from_index,
103                                 to_list, to_index, count, player)
104                         if not default.can_interact_with_node(player, pos) then
105                                 return 0
106                         end
107                         return count
108                 end
109                 def.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
110                         if not default.can_interact_with_node(player, pos) then
111                                 return 0
112                         end
113                         return stack:get_count()
114                 end
115                 def.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
116                         if not default.can_interact_with_node(player, pos) then
117                                 return 0
118                         end
119                         return stack:get_count()
120                 end
121                 def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
122                         if not default.can_interact_with_node(clicker, pos) then
123                                 return itemstack
124                         end
125
126                         minetest.sound_play(def.sound_open, {gain = 0.3,
127                                         pos = pos, max_hear_distance = 10}, true)
128                         
129                                 minetest.swap_node(pos,
130                                                 { name = "utility:" .. name .. "_open",
131                                                 param2 = node.param2 })
132                          minetest.show_formspec(clicker:get_player_name(),"utility:chest", chest.get_chest_formspec(pos))
133                         chest.open_chests[clicker:get_player_name()] = { pos = pos,
134                                         sound = def.sound_close, swap = name }
135                 end
136                 def.on_blast = function() end
137                 def.on_key_use = function(pos, player)
138                         local secret = minetest.get_meta(pos):get_string("key_lock_secret")
139                         local itemstack = player:get_wielded_item()
140                         local key_meta = itemstack:get_meta()
141
142                         if itemstack:get_metadata() == "" then
143                                 return
144                         end
145
146                         if key_meta:get_string("secret") == "" then
147                                 key_meta:set_string("secret", minetest.parse_json(itemstack:get_metadata()).secret)
148                                 itemstack:set_metadata("")
149                         end
150
151                         if secret ~= key_meta:get_string("secret") then
152                                 return
153                         end
154
155                         minetest.show_formspec(
156                                 player:get_player_name(),
157                                 "utility:chest_locked",
158                                 chest.get_chest_formspec(pos)
159                         )
160                 end
161                 def.on_skeleton_key_use = function(pos, player, newsecret)
162                         local meta = minetest.get_meta(pos)
163                         local owner = meta:get_string("owner")
164                         local pn = player:get_player_name()
165
166                         -- verify placer is owner of lockable chest
167                         if owner ~= pn then
168                                 minetest.record_protection_violation(pos, pn)
169                                 --minetest.chat_send_player(pn, S("You do not own this chest."))
170                                 return nil
171                         end
172
173                         local secret = meta:get_string("key_lock_secret")
174                         if secret == "" then
175                                 secret = newsecret
176                                 meta:set_string("key_lock_secret", secret)
177                         end
178
179                         --return secret, S("a locked chest"), owner
180                 end
181         else
182                 def.on_construct = function(pos)
183                         local meta = minetest.get_meta(pos)
184                         --meta:set_string("infotext", S("Chest"))
185                         local inv = meta:get_inventory()
186                         inv:set_size("main", 9*4)
187                 end
188                 
189                 def.on_rightclick = function(pos, node, clicker)
190                         if minetest.get_node(pos).name ~= "utility:chest" and  minetest.get_node(pos).name ~= "utility:chest_open" then
191                                 return
192                         end
193                         minetest.sound_play(def.sound_open, {gain = 0.3, pos = pos, max_hear_distance = 10}, true)      
194                         minetest.swap_node(pos, {name = "utility:" .. name .. "_open", param2 = node.param2 })
195                                 
196                         minetest.show_formspec(clicker:get_player_name(),"utility:chest", chest.get_chest_formspec(pos))
197                         chest.open_chests[clicker:get_player_name()] = { pos = pos,sound = def.sound_close, swap = name }
198                 end
199                 def.on_blast = function(pos)
200                         local drops = {}
201                         default.get_inventory_drops(pos, "main", drops)
202                         drops[#drops+1] = "utility:" .. name
203                         minetest.remove_node(pos)
204                         return drops
205                 end
206         end
207         
208         def.on_destruct = function(pos)
209                 destroy_chest(pos)
210         end
211
212         local def_opened = table.copy(def)
213         local def_closed = table.copy(def)
214
215         def_opened.mesh = "chest_open.obj"
216         for i = 1, #def_opened.tiles do
217                 if type(def_opened.tiles[i]) == "string" then
218                         def_opened.tiles[i] = {name = def_opened.tiles[i], backface_culling = true}
219                 elseif def_opened.tiles[i].backface_culling == nil then
220                         def_opened.tiles[i].backface_culling = true
221                 end
222         end
223         def_opened.drop = "utility:" .. name
224         def_opened.groups.not_in_creative_inventory = 1
225         def_opened.selection_box = {
226                 type = "fixed",
227                 fixed = { -1/2, -1/2, -1/2, 1/2, 3/16, 1/2 },
228         }
229
230         def_opened.on_blast = function() end
231
232         def_closed.mesh = nil
233         def_closed.drawtype = nil
234         def_closed.tiles[6] = def.tiles[5] -- swap textures around for "normal"
235         def_closed.tiles[5] = def.tiles[3] -- drawtype to make them match the mesh
236         def_closed.tiles[3] = def.tiles[3].."^[transformFX"
237
238         minetest.register_node("utility:" .. name, def_closed)
239         minetest.register_node("utility:" .. name .. "_open", def_opened)
240
241 end
242
243 chest.register_chest("chest", {
244         description = "Chest",
245         tiles = {
246                 "chest_top.png",
247                 "chest_top.png",
248                 "chest_side.png",
249                 "chest_side.png",
250                 "chest_front.png",
251                 "chest_inside.png"
252         },
253         sounds = main.woodSound(),
254         sound_open = "default_chest_open",
255         sound_close = "default_chest_close",
256         groups = {wood = 2,  hard = 1, axe = 1, hand = 3,pathable = 1},
257 })
258
259 minetest.register_craft({
260         output = "utility:chest",
261         recipe = {
262                 {"group:wood", "group:wood", "group:wood"},
263                 {"group:wood", "",                          "group:wood"},
264                 {"group:wood", "group:wood", "group:wood"},
265         }
266 })
267
268
269 minetest.register_craft({
270         type = "fuel",
271         recipe = "utility:chest",
272         burntime = 5,
273 })