]> git.lizzy.rs Git - xdecor.git/blob - mailbox.lua
Shortening some naming
[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") then return false end
40                 return player:get_player_name() == owner
41         end,
42         on_metadata_inventory_put = function(pos, listname, _, stack, _)
43                 local inv = minetest.get_meta(pos):get_inventory()
44                 if listname == "drop" and inv:room_for_item("main", stack) then
45                         inv:remove_item("drop", stack)
46                         inv:add_item("main", stack)
47                 end
48         end,
49         allow_metadata_inventory_put = function(pos, listname, _, stack, _)
50                 if listname == "main" then return 0 end
51                 if listname == "drop" then
52                         local meta = minetest.get_meta(pos)
53                         local inv = meta:get_inventory()
54                         if inv:room_for_item("main", stack) then return -1
55                         else return 0 end
56                 end
57         end
58 })
59
60 function mailbox.get_formspec(pos)
61         local spos = pos.x..","..pos.y..","..pos.z
62         local formspec = "size[8,9]"..xbg..
63                 "label[0,0;You received...]"..
64                 "list[nodemeta:"..spos..";main;0,0.75;8,4;]"..
65                 "list[current_player;main;0,5.25;8,4;]"
66         return formspec
67 end
68
69 function mailbox.get_insert_formspec(pos, owner)
70         local spos = pos.x..","..pos.y..","..pos.z
71         local formspec = "size[8,5]"..xbg..
72                 "label[0.5,0;Send your goods\nto "..owner.." :]"..
73                 "list[nodemeta:"..spos..";drop;3.5,0;1,1;]"..
74                 "list[current_player;main;0,1.25;8,4;]"
75         return formspec
76 end