xdecor-libre/src/mailbox.lua

210 lines
5.6 KiB
Lua
Raw Normal View History

2015-08-13 23:34:21 +03:00
local mailbox = {}
screwdriver = screwdriver or {}
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
local function get_img(img)
2019-07-23 15:03:20 +03:00
if not img then return end
local img_name = img:match("(.*)%.png")
2019-07-23 15:03:20 +03:00
if img_name then
return img_name .. ".png"
end
end
2015-11-26 14:07:35 +03:00
local function img_col(stack)
local def = minetest.registered_items[stack]
2019-07-23 15:03:20 +03:00
if not def then
return ""
end
if def.inventory_image ~= "" then
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
if def.tiles then
local tile, img = def.tiles[1]
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
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
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
-- 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 ..
-- 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,"
end
end
2015-11-26 14:07:35 +03:00
return "size[9.5,9]"
.."label[0,0;"..FS("Mailbox").."]"
.."label[6,0;"..FS("Last donators").."]"
..[[ 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;]" ..
"label[0,0;"..FS("Send your goods to\n@1",
2016-12-30 17:04:57 +03:00
(minetest.colorize and
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")
local player_name = player and player:get_player_name()
2016-02-10 15:14:14 +03:00
local inv = meta:get_inventory()
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)
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
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)))
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(),
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
function mailbox.allow_take(pos, listname, index, stack, player)
2023-07-03 17:18:52 +03:00
if listname == "drop" then
return 0
end
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
return stack:get_count()
end
function mailbox.allow_move(pos)
return 0
end
2016-02-10 15:14:14 +03:00
xdecor.register("mailbox", {
description = S("Mailbox"),
2023-07-10 20:52:38 +03:00
_tt_help = S("Lets other players give you things"),
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},
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,
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
})
-- 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"}
}
})