]> git.lizzy.rs Git - xdecor.git/blob - mailbox.lua
Some preparative cleanup in Work Table's code
[xdecor.git] / mailbox.lua
1 local mailbox = {}
2 screwdriver = screwdriver or {}
3
4 local function img_col(stack)
5         if not stack then return "" end
6         if stack.inventory_image ~= "" then
7                 return stack.inventory_image:match("(.*)%.png")..".png"
8         else
9                 return stack.tiles[1]:match("(.*)%.png")..".png"
10         end
11 end
12
13 function mailbox:formspec(pos, owner, num)
14         local spos = pos.x..","..pos.y..","..pos.z
15         local meta = minetest.get_meta(pos)
16         local giver, img = "", ""
17
18         if num == 1 then
19                 for i = 1, 7 do
20                         if meta:get_string("giver"..i) ~= "" then
21                                 giver = giver.."#FFFF00,"..meta:get_string("giver"..i):sub(1, 12)..
22                                         ","..i..",#FFFFFF,x "..meta:get_string("stack"..i):match("%s(%d+)")..","
23
24                                 img = img..i.."="..img_col(minetest.registered_items[
25                                         meta:get_string("stack"..i):match("(.*)%s")])..","
26                         end
27                 end
28
29                 return [[ size[9.5,9]
30                         label[0,0;Mailbox]
31                         label[6,0;Last donators]
32                         box[6,0.72;3.3,3.5;#555555]
33                         listring[current_player;main]
34                         list[current_player;main;0.75,5.25;8,4;]
35                         tableoptions[background=#00000000;highlight=#00000000;border=false] ]]
36                         .."tablecolumns[color;text;image,"..img.."0;color;text]"..
37                         "table[6,0.75;3.3,4;givers;"..giver.."]"..
38                         "list[nodemeta:"..spos..";mailbox;0,0.75;6,4;]"..
39                         "listring[nodemeta:"..spos..";mailbox]"..
40                         xbg..default.get_hotbar_bg(0.75,5.25)
41         else
42                 return [[ size[8,5]
43                         list[current_player;main;0,1.25;8,4;]
44                         tablecolumns[color;text;color;text]
45                         tableoptions[background=#00000000;highlight=#00000000;border=false] ]]
46                         .."table[0,0;3,1;sendform;#FFFFFF,Send your goods to,,,#FFFF00,"..owner.."]"..
47                         "list[nodemeta:"..spos..";drop;3.5,0;1,1;]"..
48                         xbg..default.get_hotbar_bg(0,1.25)
49         end
50 end
51
52 function mailbox.dig(pos, player)
53         local meta = minetest.get_meta(pos)
54         local owner = meta:get_string("owner")
55         local player_name = player:get_player_name()
56         local inv = meta:get_inventory()
57
58         return inv:is_empty("mailbox") and player and player_name == owner
59 end
60
61 function mailbox.after_place_node(pos, placer)
62         local meta = minetest.get_meta(pos)
63         local player_name = placer:get_player_name()
64
65         meta:set_string("owner", player_name)
66         meta:set_string("infotext", player_name.."'s Mailbox")
67
68         local inv = meta:get_inventory()
69         inv:set_size("mailbox", 6*4)
70         inv:set_size("drop", 1)
71 end
72
73 function mailbox.rightclick(pos, _, clicker)
74         local meta = minetest.get_meta(pos)
75         local player = clicker:get_player_name()
76         local owner = meta:get_string("owner")
77
78         if player == owner then
79                 minetest.show_formspec(player, "", mailbox:formspec(pos, owner, 1))
80         else
81                 minetest.show_formspec(player, "", mailbox:formspec(pos, owner, 0))
82         end
83 end
84
85 function mailbox.put(pos, listname, _, stack, player)
86         if listname == "drop" then
87                 local inv = minetest.get_meta(pos):get_inventory()
88                 if inv:room_for_item("mailbox", stack) then
89                         return -1
90                 else
91                         minetest.chat_send_player(player:get_player_name(), "[!] The mailbox is full")
92                 end
93         end
94         return 0
95 end
96
97 function mailbox.on_put(pos, listname, _, stack, player)
98         local meta = minetest.get_meta(pos)
99         local inv = meta:get_inventory()
100
101         if listname == "drop" and inv:room_for_item("mailbox", stack) then
102                 inv:set_list("drop", {})
103                 inv:add_item("mailbox", stack)
104
105                 for i = 7, 2, -1 do
106                         meta:set_string("giver"..i, meta:get_string("giver"..(i-1)))
107                         meta:set_string("stack"..i, meta:get_string("stack"..(i-1)))
108                 end
109
110                 meta:set_string("giver1", player:get_player_name())
111                 meta:set_string("stack1", stack:to_string())
112         end
113 end
114
115 xdecor.register("mailbox", {
116         description = "Mailbox",
117         tiles = {
118                 "xdecor_mailbox_top.png", "xdecor_mailbox_bottom.png",
119                 "xdecor_mailbox_side.png", "xdecor_mailbox_side.png",
120                 "xdecor_mailbox.png", "xdecor_mailbox.png",
121         },
122         groups = {cracky=3, oddly_breakable_by_hand=1},
123         on_rotate = screwdriver.rotate_simple,
124         can_dig = mailbox.dig,
125         on_rightclick = mailbox.rightclick,
126         on_metadata_inventory_put = mailbox.on_put,
127         allow_metadata_inventory_put = mailbox.put,
128         after_place_node = mailbox.after_place_node
129 })
130