2015-08-13 23:34:21 +03:00
|
|
|
|
local mailbox = {}
|
2015-11-15 22:35:20 +03:00
|
|
|
|
screwdriver = screwdriver or {}
|
2020-09-09 18:05:44 +03:00
|
|
|
|
local S = minetest.get_translator("xdecor")
|
|
|
|
|
local FS = function(...) return minetest.formspec_escape(S(...)) end
|
2015-08-13 23:34:21 +03:00
|
|
|
|
|
2023-07-03 17:18:52 +03:00
|
|
|
|
-- Max. length of the list of givers in mailbox formspec
|
|
|
|
|
local GIVER_LIST_LENGTH = 7
|
|
|
|
|
|
2016-06-07 00:43:14 +03:00
|
|
|
|
local function get_img(img)
|
2019-07-23 15:03:20 +03:00
|
|
|
|
if not img then return end
|
2016-06-07 00:43:14 +03:00
|
|
|
|
local img_name = img:match("(.*)%.png")
|
2019-07-23 15:03:20 +03:00
|
|
|
|
|
|
|
|
|
if img_name then
|
|
|
|
|
return img_name .. ".png"
|
|
|
|
|
end
|
2016-06-07 00:43:14 +03:00
|
|
|
|
end
|
|
|
|
|
|
2015-11-26 14:07:35 +03:00
|
|
|
|
local function img_col(stack)
|
2016-03-28 14:39:09 +03:00
|
|
|
|
local def = minetest.registered_items[stack]
|
2019-07-23 15:03:20 +03:00
|
|
|
|
if not def then
|
|
|
|
|
return ""
|
|
|
|
|
end
|
2016-03-28 14:39:09 +03:00
|
|
|
|
|
|
|
|
|
if def.inventory_image ~= "" then
|
2016-06-07 00:43:14 +03:00
|
|
|
|
local img = get_img(def.inventory_image)
|
2019-07-23 15:03:20 +03:00
|
|
|
|
if img then
|
|
|
|
|
return img
|
|
|
|
|
end
|
2015-11-26 18:36:02 +03:00
|
|
|
|
end
|
2016-05-31 17:37:32 +03:00
|
|
|
|
|
2016-06-07 00:43:14 +03:00
|
|
|
|
if def.tiles then
|
2016-12-03 14:52:47 +03:00
|
|
|
|
local tile, img = def.tiles[1]
|
2016-06-07 00:43:14 +03:00
|
|
|
|
if type(tile) == "table" then
|
|
|
|
|
img = get_img(tile.name)
|
|
|
|
|
elseif type(tile) == "string" then
|
|
|
|
|
img = get_img(tile)
|
|
|
|
|
end
|
2019-07-23 15:03:20 +03:00
|
|
|
|
|
|
|
|
|
if img then
|
|
|
|
|
return img
|
|
|
|
|
end
|
2016-05-31 17:37:32 +03:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return ""
|
2015-11-26 14:07:35 +03:00
|
|
|
|
end
|
|
|
|
|
|
2016-12-05 14:58:35 +03:00
|
|
|
|
function mailbox:formspec(pos, owner, is_owner)
|
2019-07-23 15:03:20 +03:00
|
|
|
|
local spos = pos.x .. "," .. pos.y .. "," .. pos.z
|
2015-11-25 02:10:33 +03:00
|
|
|
|
local meta = minetest.get_meta(pos)
|
2015-11-26 20:07:19 +03:00
|
|
|
|
local giver, img = "", ""
|
2015-07-02 16:26:19 +03:00
|
|
|
|
|
2016-12-05 14:58:35 +03:00
|
|
|
|
if is_owner then
|
2023-07-03 17:18:52 +03:00
|
|
|
|
for i = 1, GIVER_LIST_LENGTH do
|
2019-07-23 15:03:20 +03:00
|
|
|
|
local giving = meta:get_string("giver" .. i)
|
2016-04-05 15:51:03 +03:00
|
|
|
|
if giving ~= "" then
|
2019-07-23 15:03:20 +03:00
|
|
|
|
local stack = meta:get_string("stack" .. i)
|
2016-04-05 15:51:03 +03:00
|
|
|
|
local giver_name = giving:sub(1,12)
|
|
|
|
|
local stack_name = stack:match("[%w_:]+")
|
|
|
|
|
local stack_count = stack:match("%s(%d+)") or 1
|
2015-11-26 20:07:19 +03:00
|
|
|
|
|
2023-06-30 09:47:41 +03:00
|
|
|
|
-- List of donors. A line looks like this:
|
|
|
|
|
-- <donor name> <item icon> × <item count>
|
2019-07-23 15:03:20 +03:00
|
|
|
|
giver = giver .. "#FFFF00," .. giver_name .. "," .. i ..
|
2023-06-30 09:47:41 +03:00
|
|
|
|
-- Times a certain item count; used for the mailbox donor list
|
|
|
|
|
",#FFFFFF," .. FS("× @1", stack_count) .. ","
|
2019-07-23 15:03:20 +03:00
|
|
|
|
|
|
|
|
|
img = img .. i .. "=" ..
|
|
|
|
|
img_col(stack_name) .. "^\\[resize:16x16,"
|
2015-11-25 02:10:33 +03:00
|
|
|
|
end
|
|
|
|
|
end
|
2015-11-26 14:07:35 +03:00
|
|
|
|
|
2020-09-09 18:05:44 +03:00
|
|
|
|
return "size[9.5,9]"
|
|
|
|
|
.."label[0,0;"..FS("Mailbox").."]"
|
|
|
|
|
.."label[6,0;"..FS("Last donators").."]"
|
2023-07-03 17:06:24 +03:00
|
|
|
|
..[[ box[6,0.72;3.3,3.9;#555555]
|
2015-12-30 22:08:39 +03:00
|
|
|
|
listring[current_player;main]
|
|
|
|
|
list[current_player;main;0.75,5.25;8,4;]
|
2019-07-23 15:03:20 +03:00
|
|
|
|
tableoptions[background=#00000000;highlight=#00000000;border=false] ]] ..
|
|
|
|
|
"tablecolumns[color;text;image," .. img .. "0;color;text]" ..
|
|
|
|
|
"table[6,0.75;3.3,4;givers;" .. giver .. "]" ..
|
|
|
|
|
"list[nodemeta:" .. spos .. ";mailbox;0,0.75;6,4;]" ..
|
|
|
|
|
"listring[nodemeta:" .. spos .. ";mailbox]" ..
|
|
|
|
|
xbg .. default.get_hotbar_bg(0.75, 5.25)
|
2015-11-25 00:23:43 +03:00
|
|
|
|
end
|
2019-07-23 15:03:20 +03:00
|
|
|
|
|
|
|
|
|
return "size[8,5]" ..
|
|
|
|
|
"list[current_player;main;0,1.25;8,4;]" ..
|
2020-09-09 18:05:44 +03:00
|
|
|
|
"label[0,0;"..FS("Send your goods to\n@1",
|
2016-12-30 17:04:57 +03:00
|
|
|
|
(minetest.colorize and
|
2020-09-09 18:05:44 +03:00
|
|
|
|
minetest.colorize("#FFFF00", owner) or owner)) .. "]" ..
|
2019-07-23 15:03:20 +03:00
|
|
|
|
"list[nodemeta:" .. spos .. ";drop;3.5,0;1,1;]" ..
|
2023-07-03 17:15:07 +03:00
|
|
|
|
"listring[]" ..
|
2019-07-23 15:03:20 +03:00
|
|
|
|
xbg .. default.get_hotbar_bg(0, 1.25)
|
2015-07-02 16:26:19 +03:00
|
|
|
|
end
|
2015-11-25 00:23:43 +03:00
|
|
|
|
|
2016-02-10 15:14:14 +03:00
|
|
|
|
function mailbox.dig(pos, player)
|
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
|
local owner = meta:get_string("owner")
|
2016-08-05 23:17:15 +03:00
|
|
|
|
local player_name = player and player:get_player_name()
|
2016-02-10 15:14:14 +03:00
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
|
|
2016-08-05 23:17:15 +03:00
|
|
|
|
return inv:is_empty("mailbox") and player_name == owner
|
2016-02-10 15:14:14 +03:00
|
|
|
|
end
|
|
|
|
|
|
2023-07-01 10:34:49 +03:00
|
|
|
|
function mailbox.blast(pos)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2016-02-10 15:14:14 +03:00
|
|
|
|
function mailbox.after_place_node(pos, placer)
|
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
|
local player_name = placer:get_player_name()
|
|
|
|
|
|
|
|
|
|
meta:set_string("owner", player_name)
|
2020-09-09 18:05:44 +03:00
|
|
|
|
meta:set_string("infotext", S("@1's Mailbox", player_name))
|
2016-02-10 15:14:14 +03:00
|
|
|
|
|
|
|
|
|
local inv = meta:get_inventory()
|
2019-07-23 15:03:20 +03:00
|
|
|
|
inv:set_size("mailbox", 6 * 4)
|
2016-02-10 15:14:14 +03:00
|
|
|
|
inv:set_size("drop", 1)
|
|
|
|
|
end
|
|
|
|
|
|
2016-08-05 22:33:53 +03:00
|
|
|
|
function mailbox.rightclick(pos, node, clicker, itemstack, pointed_thing)
|
2016-02-10 15:14:14 +03:00
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
|
local player = clicker:get_player_name()
|
|
|
|
|
local owner = meta:get_string("owner")
|
|
|
|
|
|
2019-07-23 15:03:20 +03:00
|
|
|
|
minetest.show_formspec(player, "xdecor:mailbox",
|
|
|
|
|
mailbox:formspec(pos, owner, (player == owner)))
|
|
|
|
|
|
2016-08-05 22:33:53 +03:00
|
|
|
|
return itemstack
|
2016-02-10 15:14:14 +03:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function mailbox.put(pos, listname, _, stack, player)
|
|
|
|
|
if listname == "drop" then
|
|
|
|
|
local inv = minetest.get_meta(pos):get_inventory()
|
|
|
|
|
if inv:room_for_item("mailbox", stack) then
|
|
|
|
|
return -1
|
|
|
|
|
else
|
2016-12-05 14:58:35 +03:00
|
|
|
|
minetest.chat_send_player(player:get_player_name(),
|
2020-09-09 18:05:44 +03:00
|
|
|
|
S("The mailbox is full."))
|
2016-02-10 15:14:14 +03:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-07-23 15:03:20 +03:00
|
|
|
|
|
2016-02-10 15:14:14 +03:00
|
|
|
|
return 0
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function mailbox.on_put(pos, listname, _, stack, player)
|
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
|
|
|
|
|
|
if listname == "drop" and inv:room_for_item("mailbox", stack) then
|
|
|
|
|
inv:set_list("drop", {})
|
|
|
|
|
inv:add_item("mailbox", stack)
|
|
|
|
|
|
2023-07-03 17:18:52 +03:00
|
|
|
|
for i = GIVER_LIST_LENGTH, 2, -1 do
|
2019-07-23 15:03:20 +03:00
|
|
|
|
meta:set_string("giver" .. i, meta:get_string("giver" .. (i - 1)))
|
|
|
|
|
meta:set_string("stack" .. i, meta:get_string("stack" .. (i - 1)))
|
2016-02-10 15:14:14 +03:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
meta:set_string("giver1", player:get_player_name())
|
|
|
|
|
meta:set_string("stack1", stack:to_string())
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-09 06:47:59 +03:00
|
|
|
|
function mailbox.allow_take(pos, listname, index, stack, player)
|
2023-07-03 17:18:52 +03:00
|
|
|
|
if listname == "drop" then
|
|
|
|
|
return 0
|
|
|
|
|
end
|
2017-04-09 06:47:59 +03:00
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
|
|
|
|
|
|
if player:get_player_name() ~= meta:get_string("owner") then
|
|
|
|
|
return 0
|
|
|
|
|
end
|
2019-07-23 15:03:20 +03:00
|
|
|
|
|
2017-04-09 06:47:59 +03:00
|
|
|
|
return stack:get_count()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function mailbox.allow_move(pos)
|
|
|
|
|
return 0
|
|
|
|
|
end
|
|
|
|
|
|
2016-02-10 15:14:14 +03:00
|
|
|
|
xdecor.register("mailbox", {
|
2020-09-09 18:05:44 +03:00
|
|
|
|
description = S("Mailbox"),
|
2023-07-10 20:52:38 +03:00
|
|
|
|
_tt_help = S("Lets other players give you things"),
|
2016-02-15 21:32:49 +03:00
|
|
|
|
tiles = {"xdecor_mailbox_top.png", "xdecor_mailbox_bottom.png",
|
|
|
|
|
"xdecor_mailbox_side.png", "xdecor_mailbox_side.png",
|
|
|
|
|
"xdecor_mailbox.png", "xdecor_mailbox.png"},
|
2019-07-23 15:03:20 +03:00
|
|
|
|
groups = {cracky = 3, oddly_breakable_by_hand = 1},
|
2023-06-30 02:42:50 +03:00
|
|
|
|
sounds = default.node_sound_metal_defaults(),
|
2016-02-10 15:14:14 +03:00
|
|
|
|
on_rotate = screwdriver.rotate_simple,
|
|
|
|
|
can_dig = mailbox.dig,
|
2023-07-01 10:34:49 +03:00
|
|
|
|
on_blast = mailbox.blast,
|
2016-02-10 15:14:14 +03:00
|
|
|
|
on_rightclick = mailbox.rightclick,
|
2017-04-09 06:47:59 +03:00
|
|
|
|
allow_metadata_inventory_take = mailbox.allow_take,
|
|
|
|
|
allow_metadata_inventory_move = mailbox.allow_move,
|
2016-02-10 15:14:14 +03:00
|
|
|
|
on_metadata_inventory_put = mailbox.on_put,
|
|
|
|
|
allow_metadata_inventory_put = mailbox.put,
|
|
|
|
|
after_place_node = mailbox.after_place_node
|
|
|
|
|
})
|
2017-08-28 08:41:14 +03:00
|
|
|
|
|
|
|
|
|
-- Recipes
|
|
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
|
output = "xdecor:mailbox",
|
|
|
|
|
recipe = {
|
|
|
|
|
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
|
|
|
|
|
{"dye:red", "default:paper", "dye:red"},
|
|
|
|
|
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}
|
|
|
|
|
}
|
2020-09-09 18:05:44 +03:00
|
|
|
|
})
|