]> git.lizzy.rs Git - xdecor.git/blob - mailbox.lua
Fix some rare cases where the player is null and dig the itemframe or mailbox
[xdecor.git] / mailbox.lua
1 local mailbox = {}
2 local xbg = default.gui_bg..default.gui_bg_img..default.gui_slots
3
4 xdecor.register("mailbox", {
5         description = "Mailbox",
6         tiles = {
7                 "xdecor_mailbox_top.png", "xdecor_mailbox_bottom.png",
8                 "xdecor_mailbox_side.png", "xdecor_mailbox_side.png",
9                 "xdecor_mailbox.png", "xdecor_mailbox.png",
10         },
11         groups = {cracky=3},
12         after_place_node = function(pos, placer, _)
13                 local meta = minetest.get_meta(pos)
14                 local owner = placer:get_player_name()
15
16                 meta:set_string("owner", owner)
17                 meta:set_string("infotext", owner.."'s Mailbox")
18
19                 local inv = meta:get_inventory()
20                 inv:set_size("main", 8*4)
21                 inv:set_size("drop", 1)
22         end,
23         on_rightclick = function(pos, _, clicker, _)
24                 local meta = minetest.get_meta(pos)
25                 local player = clicker:get_player_name()
26                 local owner = meta:get_string("owner")
27
28                 if owner == player then
29                         minetest.show_formspec(player, "", mailbox.get_formspec(pos))
30                 else minetest.show_formspec(player, "",
31                                 mailbox.get_insert_formspec(pos, owner))
32                 end
33         end,
34         can_dig = function(pos, player)
35                 local meta = minetest.get_meta(pos)
36                 local owner = meta:get_string("owner")
37                 local inv = meta:get_inventory()
38
39                 if not inv:is_empty("main") or not player or
40                         player:get_player_name() ~= owner then return false end
41                 return true
42         end,
43         on_metadata_inventory_put = function(pos, listname, _, stack, _)
44                 local inv = minetest.get_meta(pos):get_inventory()
45                 if listname == "drop" and inv:room_for_item("main", stack) then
46                         inv:remove_item("drop", stack)
47                         inv:add_item("main", stack)
48                 end
49         end,
50         allow_metadata_inventory_put = function(pos, listname, _, stack, _)
51                 if listname == "main" then return 0 end
52                 if listname == "drop" then
53                         local meta = minetest.get_meta(pos)
54                         local inv = meta:get_inventory()
55                         if inv:room_for_item("main", stack) then return -1
56                         else return 0 end
57                 end
58         end
59 })
60
61 function mailbox.get_formspec(pos)
62         local spos = pos.x..","..pos.y..","..pos.z
63         local formspec = "size[8,9]"..xbg..
64                 "label[0,0;You received...]list[nodemeta:"..spos..";main;0,0.75;8,4;]list[current_player;main;0,5.25;8,4;]"
65         return formspec
66 end
67
68 function mailbox.get_insert_formspec(pos, owner)
69         local spos = pos.x..","..pos.y..","..pos.z
70         local formspec = "size[8,5]"..xbg..
71                 "label[0.5,0;Send your goods\nto "..owner.." :]list[nodemeta:"..spos..";drop;3.5,0;1,1;]list[current_player;main;0,1.25;8,4;]"
72         return formspec
73 end