]> git.lizzy.rs Git - xdecor.git/blob - mailbox.lua
Merge pull request #67 from Napiophelios/master
[xdecor.git] / mailbox.lua
1 local mailbox = {}
2 screwdriver = screwdriver or {}
3
4 local function get_img(img)
5         local img_name = img:match("(.*)%.png")
6         if img_name then return img_name..".png" end
7 end
8
9 local function img_col(stack)
10         local def = minetest.registered_items[stack]
11         if not def then return "" end
12
13         if def.inventory_image ~= "" then
14                 local img = get_img(def.inventory_image)
15                 if img then return img end
16         end
17
18         if def.tiles then
19                 local img
20                 local tile = def.tiles[1]
21
22                 if type(tile) == "table" then
23                         img = get_img(tile.name)
24                 elseif type(tile) == "string" then
25                         img = get_img(tile)
26                 end
27
28                 if img then return img end
29         end
30
31         return ""
32 end
33
34 function mailbox:formspec(pos, owner, num)
35         local spos = pos.x..","..pos.y..","..pos.z
36         local meta = minetest.get_meta(pos)
37         local giver, img = "", ""
38
39         if num == 1 then
40                 for i = 1, 7 do
41                         local giving = meta:get_string("giver"..i)
42                         if giving ~= "" then
43                                 local stack = meta:get_string("stack"..i)
44                                 local giver_name = giving:sub(1,12)
45                                 local stack_name = stack:match("[%w_:]+")
46                                 local stack_count = stack:match("%s(%d+)") or 1
47
48                                 giver = giver.."#FFFF00,"..giver_name..","..i..",#FFFFFF,x "..stack_count..","
49                                 -- Hack to force using a 16px resolution for images in formspec's tablecolumn.
50                                 -- The engine doesn't scale them automatically yet.
51                                 img = img..i.."="..img_col(stack_name).."^\\[resize:16x16,"
52                         end
53                 end
54
55                 return [[ size[9.5,9]
56                         label[0,0;Mailbox]
57                         label[6,0;Last donators]
58                         box[6,0.72;3.3,3.5;#555555]
59                         listring[current_player;main]
60                         list[current_player;main;0.75,5.25;8,4;]
61                         tableoptions[background=#00000000;highlight=#00000000;border=false] ]]
62                         .."tablecolumns[color;text;image,"..img.."0;color;text]"..
63                         "table[6,0.75;3.3,4;givers;"..giver.."]"..
64                         "list[nodemeta:"..spos..";mailbox;0,0.75;6,4;]"..
65                         "listring[nodemeta:"..spos..";mailbox]"..
66                         xbg..default.get_hotbar_bg(0.75,5.25)
67         else
68                 return [[ size[8,5]
69                         list[current_player;main;0,1.25;8,4;]
70                         tablecolumns[color;text;color;text]
71                         tableoptions[background=#00000000;highlight=#00000000;border=false] ]]
72                         .."table[0,0;3,1;sendform;#FFFFFF,Send your goods to,,,#FFFF00,"..owner.."]"..
73                         "list[nodemeta:"..spos..";drop;3.5,0;1,1;]"..
74                         xbg..default.get_hotbar_bg(0,1.25)
75         end
76 end
77
78 function mailbox.dig(pos, player)
79         local meta = minetest.get_meta(pos)
80         local owner = meta:get_string("owner")
81         local player_name = player and player:get_player_name()
82         local inv = meta:get_inventory()
83
84         return inv:is_empty("mailbox") and player_name == owner
85 end
86
87 function mailbox.after_place_node(pos, placer)
88         local meta = minetest.get_meta(pos)
89         local player_name = placer:get_player_name()
90
91         meta:set_string("owner", player_name)
92         meta:set_string("infotext", player_name.."'s Mailbox")
93
94         local inv = meta:get_inventory()
95         inv:set_size("mailbox", 6*4)
96         inv:set_size("drop", 1)
97 end
98
99 function mailbox.rightclick(pos, node, clicker, itemstack, pointed_thing)
100         local meta = minetest.get_meta(pos)
101         local player = clicker:get_player_name()
102         local owner = meta:get_string("owner")
103
104         if player == owner then
105                 minetest.show_formspec(player, "xdecor:mailbox", mailbox:formspec(pos, owner, 1))
106         else
107                 minetest.show_formspec(player, "xdecor:mailbox", mailbox:formspec(pos, owner, 0))
108         end
109         return itemstack
110 end
111
112 function mailbox.put(pos, listname, _, stack, player)
113         if listname == "drop" then
114                 local inv = minetest.get_meta(pos):get_inventory()
115                 if inv:room_for_item("mailbox", stack) then
116                         return -1
117                 else
118                         minetest.chat_send_player(player:get_player_name(), "The mailbox is full")
119                 end
120         end
121         return 0
122 end
123
124 function mailbox.on_put(pos, listname, _, stack, player)
125         local meta = minetest.get_meta(pos)
126         local inv = meta:get_inventory()
127
128         if listname == "drop" and inv:room_for_item("mailbox", stack) then
129                 inv:set_list("drop", {})
130                 inv:add_item("mailbox", stack)
131
132                 for i = 7, 2, -1 do
133                         meta:set_string("giver"..i, meta:get_string("giver"..(i-1)))
134                         meta:set_string("stack"..i, meta:get_string("stack"..(i-1)))
135                 end
136
137                 meta:set_string("giver1", player:get_player_name())
138                 meta:set_string("stack1", stack:to_string())
139         end
140 end
141
142 xdecor.register("mailbox", {
143         description = "Mailbox",
144         tiles = {"xdecor_mailbox_top.png", "xdecor_mailbox_bottom.png",
145                  "xdecor_mailbox_side.png", "xdecor_mailbox_side.png",
146                  "xdecor_mailbox.png", "xdecor_mailbox.png"},
147         groups = {cracky=3, oddly_breakable_by_hand=1},
148         on_rotate = screwdriver.rotate_simple,
149         can_dig = mailbox.dig,
150         on_rightclick = mailbox.rightclick,
151         on_metadata_inventory_put = mailbox.on_put,
152         allow_metadata_inventory_put = mailbox.put,
153         after_place_node = mailbox.after_place_node
154 })