2 screwdriver = screwdriver or {}
3 local xbg = default.gui_bg..default.gui_bg_img..default.gui_slots
5 xdecor.register("mailbox", {
6 description = "Mailbox",
8 "xdecor_mailbox_top.png", "xdecor_mailbox_bottom.png",
9 "xdecor_mailbox_side.png", "xdecor_mailbox_side.png",
10 "xdecor_mailbox.png", "xdecor_mailbox.png",
12 groups = {cracky=3, oddly_breakable_by_hand=1},
13 on_rotate = screwdriver.rotate_simple,
14 after_place_node = function(pos, placer, _)
15 local meta = minetest.get_meta(pos)
16 local owner = placer:get_player_name()
18 meta:set_string("owner", owner)
19 meta:set_string("infotext", owner.."'s Mailbox")
21 local inv = meta:get_inventory()
22 inv:set_size("main", 8*4)
23 inv:set_size("drop", 1)
25 on_rightclick = function(pos, _, clicker, _)
26 local meta = minetest.get_meta(pos)
27 local player = clicker:get_player_name()
28 local owner = meta:get_string("owner")
30 if owner == player then
31 minetest.show_formspec(player, "", mailbox.get_formspec(pos))
32 else minetest.show_formspec(player, "",
33 mailbox.get_insert_formspec(pos, owner))
36 can_dig = function(pos, player)
37 local meta = minetest.get_meta(pos)
38 local owner = meta:get_string("owner")
39 local inv = meta:get_inventory()
41 return inv:is_empty("main") and player and player:get_player_name() == owner
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)
50 allow_metadata_inventory_put = function(pos, listname, _, stack, _)
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 end
60 function mailbox.get_formspec(pos)
61 local spos = pos.x..","..pos.y..","..pos.z
62 return "size[8,9]"..xbg..default.get_hotbar_bg(0,5.25)..
63 "label[0,0;You received...]list[nodemeta:"..spos..";main;0,0.75;8,4;]list[current_player;main;0,5.25;8,4;]"
66 function mailbox.get_insert_formspec(pos, owner)
67 local spos = pos.x..","..pos.y..","..pos.z
68 return "size[8,5]"..xbg..default.get_hotbar_bg(0,1.25)..
69 "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;]"