]> git.lizzy.rs Git - xdecor.git/blobdiff - mailbox.lua
Don't crash on nil-player in can_dig and check the right players for attachment in...
[xdecor.git] / mailbox.lua
index 984ce3e3e1e38d7ab392034b1802230942f60fbe..cd9a03c4845abce370fb999775974a250b799502 100644 (file)
@@ -1,13 +1,34 @@
 local mailbox = {}
 screwdriver = screwdriver or {}
 
+local function get_img(img)
+       local img_name = img:match("(.*)%.png")
+       if img_name then return img_name..".png" end
+end
+
 local function img_col(stack)
-       if not stack then return "" end
-       if stack.inventory_image ~= "" then
-               return stack.inventory_image:match("(.*)%.png")..".png"
-       else
-               return stack.tiles[1]:match("(.*)%.png")..".png"
+       local def = minetest.registered_items[stack]
+       if not def then return "" end
+
+       if def.inventory_image ~= "" then
+               local img = get_img(def.inventory_image)
+               if img then return img end
+       end
+
+       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
+
+               if img then return img end
        end
+
+       return ""
 end
 
 function mailbox:formspec(pos, owner, num)
@@ -17,12 +38,17 @@ function mailbox:formspec(pos, owner, num)
 
        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):match("%s(%d+)")..","
-
-                               img = img..i.."="..img_col(minetest.registered_items[
-                                       meta:get_string("stack"..i):match("(.*)%s")])..","
+                       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
 
@@ -52,10 +78,10 @@ end
 function mailbox.dig(pos, player)
        local meta = minetest.get_meta(pos)
        local owner = meta:get_string("owner")
-       local player_name = player:get_player_name()
+       local player_name = player and player:get_player_name()
        local inv = meta:get_inventory()
 
-       return inv:is_empty("mailbox") and player and player_name == owner
+       return inv:is_empty("mailbox") and player_name == owner
 end
 
 function mailbox.after_place_node(pos, placer)
@@ -70,7 +96,7 @@ function mailbox.after_place_node(pos, placer)
        inv:set_size("drop", 1)
 end
 
-function mailbox.rightclick(pos, _, clicker)
+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")
@@ -80,6 +106,7 @@ function mailbox.rightclick(pos, _, clicker)
        else
                minetest.show_formspec(player, "xdecor:mailbox", mailbox:formspec(pos, owner, 0))
        end
+       return itemstack
 end
 
 function mailbox.put(pos, listname, _, stack, player)
@@ -88,7 +115,7 @@ function mailbox.put(pos, listname, _, stack, player)
                if inv:room_for_item("mailbox", stack) then
                        return -1
                else
-                       minetest.chat_send_player(player:get_player_name(), "[!] The mailbox is full")
+                       minetest.chat_send_player(player:get_player_name(), "The mailbox is full")
                end
        end
        return 0
@@ -125,4 +152,3 @@ xdecor.register("mailbox", {
        allow_metadata_inventory_put = mailbox.put,
        after_place_node = mailbox.after_place_node
 })
-