xdecor-libre/mailbox.lua

83 lines
2.4 KiB
Lua
Raw Normal View History

2015-08-13 23:34:21 +03:00
local mailbox = {}
2015-07-02 16:26:19 +03:00
xdecor.register("mailbox", {
description = "Mailbox",
tiles = {
"xdecor_mailbox_top.png", "xdecor_mailbox_bottom.png",
"xdecor_mailbox_side.png", "xdecor_mailbox_side.png",
"xdecor_mailbox.png", "xdecor_mailbox.png",
},
2015-08-03 00:47:02 +03:00
groups = {cracky=2},
2015-08-12 21:49:07 +03:00
after_place_node = function(pos, placer, _)
2015-07-02 16:26:19 +03:00
local meta = minetest.get_meta(pos)
local owner = placer:get_player_name()
meta:set_string("owner", owner)
meta:set_string("infotext", owner.."'s Mailbox")
local inv = meta:get_inventory()
inv:set_size("main", 8*4)
inv:set_size("drop", 1)
end,
2015-08-12 21:49:07 +03:00
on_rightclick = function(pos, _, clicker, _)
2015-07-02 16:26:19 +03:00
local meta = minetest.get_meta(pos)
2015-08-05 11:39:57 +03:00
local player = clicker:get_player_name()
local owner = meta:get_string("owner")
2015-07-02 16:26:19 +03:00
if owner == player then
2015-08-05 11:39:57 +03:00
minetest.show_formspec(player, "default:chest_locked",
2015-08-13 23:34:21 +03:00
mailbox.get_formspec(pos))
2015-08-05 11:39:57 +03:00
else minetest.show_formspec(player, "default:chest_locked",
2015-08-13 23:34:21 +03:00
mailbox.get_insert_formspec(pos, owner))
2015-07-02 16:26:19 +03:00
end
end,
2015-08-03 13:08:30 +03:00
can_dig = function(pos, player)
2015-07-02 16:26:19 +03:00
local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner")
local inv = meta:get_inventory()
if not inv:is_empty("main") then return false end
return player:get_player_name() == owner
2015-07-02 16:26:19 +03:00
end,
2015-08-12 21:49:07 +03:00
on_metadata_inventory_put = function(pos, listname, _, stack, _)
2015-07-02 16:26:19 +03:00
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if listname == "drop" and inv:room_for_item("main", stack) then
inv:remove_item("drop", stack)
inv:add_item("main", stack)
end
end,
2015-08-12 21:49:07 +03:00
allow_metadata_inventory_put = function(pos, listname, _, stack, _)
2015-08-03 13:08:30 +03:00
if listname == "main" then return 0 end
2015-07-02 16:26:19 +03:00
if listname == "drop" then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2015-08-03 13:08:30 +03:00
if inv:room_for_item("main", stack) then return -1
2015-08-05 11:39:57 +03:00
else return 0 end
2015-07-02 16:26:19 +03:00
end
2015-08-03 13:08:30 +03:00
end
2015-07-02 16:26:19 +03:00
})
2015-08-16 22:57:17 +03:00
local xbg = default.gui_bg..default.gui_bg_img..default.gui_slots
2015-08-13 23:34:21 +03:00
function mailbox.get_formspec(pos)
2015-07-02 16:26:19 +03:00
local spos = pos.x..","..pos.y..","..pos.z
local formspec =
2015-08-16 22:14:01 +03:00
"size[8,9]"..xbg..
2015-07-02 16:26:19 +03:00
"label[0,0;You received...]"..
"list[nodemeta:"..spos..";main;0,0.75;8,4;]"..
"list[current_player;main;0,5.25;8,4;]"
return formspec
end
2015-08-13 23:34:21 +03:00
function mailbox.get_insert_formspec(pos, owner)
2015-07-02 16:26:19 +03:00
local spos = pos.x..","..pos.y..","..pos.z
local formspec =
2015-08-16 22:14:01 +03:00
"size[8,5]"..xbg..
"label[0.5,0;Send your goods\nto "..owner.." :]"..
2015-07-02 16:26:19 +03:00
"list[nodemeta:"..spos..";drop;3.5,0;1,1;]"..
"list[current_player;main;0,1.25;8,4;]"
return formspec
end