X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=mailbox.lua;h=cd9a03c4845abce370fb999775974a250b799502;hb=75ca6f64f5b7116ff0ab3993c26fad1999ad18e8;hp=94658cae0f40eb4cbacb56620c6c3d1993be7c8c;hpb=d038cb158efa066d27b0c83922497371d3c34cda;p=xdecor.git diff --git a/mailbox.lua b/mailbox.lua index 94658ca..cd9a03c 100644 --- a/mailbox.lua +++ b/mailbox.lua @@ -1,132 +1,154 @@ local mailbox = {} screwdriver = screwdriver or {} -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", - }, - groups = {cracky=3, oddly_breakable_by_hand=1}, - on_rotate = screwdriver.rotate_simple, - after_place_node = function(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", player_name.."'s Mailbox") - - local inv = meta:get_inventory() - inv:set_size("mailbox", 6*4) - inv:set_size("drop", 1) - end, - on_rightclick = function(pos, _, clicker, _) - local meta = minetest.get_meta(pos) - local player = clicker:get_player_name() - local owner = meta:get_string("owner") - - if player == owner then - minetest.show_formspec(player, "", mailbox.formspec(pos, owner, 1)) - else - minetest.show_formspec(player, "", mailbox.formspec(pos, owner, 0)) - end - end, - can_dig = function(pos, player) - local meta = minetest.get_meta(pos) - local owner = meta:get_string("owner") - local player_name = player:get_player_name() - local inv = meta:get_inventory() - - return inv:is_empty("mailbox") and player and player_name == owner - end, - on_metadata_inventory_put = function(pos, listname, _, stack, player) - local inv = minetest.get_meta(pos):get_inventory() - local player_name = player:get_player_name() - local meta = minetest.get_meta(pos) - local stack_name = stack:get_name().." "..stack:get_count() +local function get_img(img) + local img_name = img:match("(.*)%.png") + if img_name then return img_name..".png" end +end - if listname == "drop" and inv:room_for_item("mailbox", stack) then - inv:remove_item("drop", stack) - inv:add_item("mailbox", stack) +local function img_col(stack) + local def = minetest.registered_items[stack] + if not def then return "" end - for i = 7, 2, -1 do - meta:set_string("giver"..i, meta:get_string("giver"..(i-1))) - meta:set_string("stack"..i, meta:get_string("stack"..(i-1))) - end - meta:set_string("giver1", player_name) - meta:set_string("stack1", stack_name) - end - end, - allow_metadata_inventory_put = function(pos, listname, _, stack, _) - if listname == "drop" then - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - if inv:room_for_item("mailbox", stack) then return -1 end - end - return 0 + if def.inventory_image ~= "" then + local img = get_img(def.inventory_image) + if img then return img end end -}) -local function img_col(stack) - if not stack then return "" end - if stack.type == "node" then - if stack.inventory_image ~= "" then - return string.match(stack.inventory_image, "([%w_]+)")..".png" - else - return string.match(stack.tiles[1], "([%w_]+)")..".png" + if def.tiles then + local img + local tile = def.tiles[1] + + if type(tile) == "table" then + img = get_img(tile.name) + elseif type(tile) == "string" then + img = get_img(tile) end - elseif stack.type == "tool" or stack.type == "craft" then - return string.match(stack.inventory_image, "([%w_]+)")..".png" - else return "" end + + if img then return img end + end + + return "" end -function mailbox.formspec(pos, owner, num) - local xbg = default.gui_bg..default.gui_bg_img..default.gui_slots +function mailbox:formspec(pos, owner, num) local spos = pos.x..","..pos.y..","..pos.z local meta = minetest.get_meta(pos) - local giver = "" - local def_stack1 = minetest.registered_items[meta:get_string("stack1"):match("([%w_]+:[%w_]+)")] - local def_stack2 = minetest.registered_items[meta:get_string("stack2"):match("([%w_]+:[%w_]+)")] - local def_stack3 = minetest.registered_items[meta:get_string("stack3"):match("([%w_]+:[%w_]+)")] - local def_stack4 = minetest.registered_items[meta:get_string("stack4"):match("([%w_]+:[%w_]+)")] - local def_stack5 = minetest.registered_items[meta:get_string("stack5"):match("([%w_]+:[%w_]+)")] - local def_stack6 = minetest.registered_items[meta:get_string("stack6"):match("([%w_]+:[%w_]+)")] - local def_stack7 = minetest.registered_items[meta:get_string("stack7"):match("([%w_]+:[%w_]+)")] + local giver, img = "", "" if num == 1 then for i = 1, 7 do - if meta:get_string("giver"..i) ~= "" then - giver = giver.."#FFFF00,"..meta:get_string("giver"..i):sub(1, 12).. - ","..i..",#FFFFFF,x "..meta:get_string("stack"..i):sub(-3):match("%d+").."," + local giving = meta:get_string("giver"..i) + if giving ~= "" then + local stack = meta:get_string("stack"..i) + local giver_name = giving:sub(1,12) + local stack_name = stack:match("[%w_:]+") + local stack_count = stack:match("%s(%d+)") or 1 + + giver = giver.."#FFFF00,"..giver_name..","..i..",#FFFFFF,x "..stack_count.."," + -- Hack to force using a 16px resolution for images in formspec's tablecolumn. + -- The engine doesn't scale them automatically yet. + img = img..i.."="..img_col(stack_name).."^\\[resize:16x16," end end - return "size[9.5,9]"..xbg.. - default.get_hotbar_bg(0.75,5.25).. - "label[0,0;Mailbox :]".. - "label[6,0;Last donators :]".. - "box[6,0.72;3.3,3.5;#555555]".. - "tablecolumns[color;text;image,".. - "1="..img_col(def_stack1)..",".. - "2="..img_col(def_stack2)..",".. - "3="..img_col(def_stack3)..",".. - "4="..img_col(def_stack4)..",".. - "5="..img_col(def_stack5)..",".. - "6="..img_col(def_stack6)..",".. - "7="..img_col(def_stack7)..";color;text]".. - "tableoptions[background=#00000000;highlight=#00000000;border=false]".. + return [[ size[9.5,9] + label[0,0;Mailbox] + label[6,0;Last donators] + box[6,0.72;3.3,3.5;#555555] + listring[current_player;main] + list[current_player;main;0.75,5.25;8,4;] + 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;]".. - "list[current_player;main;0.75,5.25;8,4;]".. "listring[nodemeta:"..spos..";mailbox]".. - "listring[current_player;main]" + xbg..default.get_hotbar_bg(0.75,5.25) else - return "size[8,5]"..xbg.. - default.get_hotbar_bg(0,1.25).. - "label[0.5,0;Send your goods\nto "..owner.." :]".. + return [[ size[8,5] + list[current_player;main;0,1.25;8,4;] + tablecolumns[color;text;color;text] + tableoptions[background=#00000000;highlight=#00000000;border=false] ]] + .."table[0,0;3,1;sendform;#FFFFFF,Send your goods to,,,#FFFF00,"..owner.."]".. "list[nodemeta:"..spos..";drop;3.5,0;1,1;]".. - "list[current_player;main;0,1.25;8,4;]" + xbg..default.get_hotbar_bg(0,1.25) + end +end + +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() + local inv = meta:get_inventory() + + return inv:is_empty("mailbox") and player_name == owner +end + +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", player_name.."'s Mailbox") + + local inv = meta:get_inventory() + inv:set_size("mailbox", 6*4) + inv:set_size("drop", 1) +end + +function mailbox.rightclick(pos, node, clicker, itemstack, pointed_thing) + local meta = minetest.get_meta(pos) + local player = clicker:get_player_name() + local owner = meta:get_string("owner") + + if player == owner then + minetest.show_formspec(player, "xdecor:mailbox", mailbox:formspec(pos, owner, 1)) + else + minetest.show_formspec(player, "xdecor:mailbox", mailbox:formspec(pos, owner, 0)) + end + return itemstack +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 + minetest.chat_send_player(player:get_player_name(), "The mailbox is full") + end end + 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) + + for i = 7, 2, -1 do + meta:set_string("giver"..i, meta:get_string("giver"..(i-1))) + meta:set_string("stack"..i, meta:get_string("stack"..(i-1))) + end + + meta:set_string("giver1", player:get_player_name()) + meta:set_string("stack1", stack:to_string()) + end +end + +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"}, + groups = {cracky=3, oddly_breakable_by_hand=1}, + on_rotate = screwdriver.rotate_simple, + can_dig = mailbox.dig, + on_rightclick = mailbox.rightclick, + on_metadata_inventory_put = mailbox.on_put, + allow_metadata_inventory_put = mailbox.put, + after_place_node = mailbox.after_place_node +})