Bookshelf: Count written and empty books
authorSmallJoker <mk939@ymail.com>
Mon, 23 Oct 2017 18:25:46 +0000 (20:25 +0200)
committerparamat <mat.gregory@virginmedia.com>
Sat, 4 Nov 2017 23:43:02 +0000 (23:43 +0000)
mods/default/nodes.lua

index 007562106121b61e120aaf4d011ed982738e4c3e..00bb2985af46787a827bdbce10bed8b15c01c751 100644 (file)
@@ -2137,23 +2137,41 @@ local bookshelf_formspec =
        "listring[current_player;main]" ..
        default.get_hotbar_bg(0,2.85)
 
-local function get_bookshelf_formspec(inv)
+local function update_bookshelf(pos)
+       local meta = minetest.get_meta(pos)
+       local inv = meta:get_inventory()
+       local invlist = inv:get_list("books")
+
        local formspec = bookshelf_formspec
-       local invlist = inv and inv:get_list("books")
        -- Inventory slots overlay
        local bx, by = 0, 0.3
+       local n_written, n_empty = 0, 0
        for i = 1, 16 do
                if i == 9 then
                        bx = 0
                        by = by + 1
                end
-               if not invlist or invlist[i]:is_empty() then
+               local stack = invlist[i]
+               if stack:is_empty() then
                        formspec = formspec ..
                                "image[" .. bx .. "," .. by .. ";1,1;default_bookshelf_slot.png]"
+               else
+                       local metatable = stack:get_meta():to_table() or {}
+                       if metatable.fields and metatable.fields.text then
+                               n_written = n_written + stack:get_count()
+                       else
+                               n_empty = n_empty + stack:get_count()
+                       end
                end
                bx = bx + 1
        end
-       return formspec
+       meta:set_string("formspec", formspec)
+       if n_written + n_empty == 0 then
+               meta:set_string("infotext", "Empty Bookshelf")
+       else
+               meta:set_string("infotext", "Bookshelf (" .. n_written ..
+                       " written, " .. n_empty .. " empty books)")
+       end
 end
 
 minetest.register_node("default:bookshelf", {
@@ -2167,9 +2185,9 @@ minetest.register_node("default:bookshelf", {
 
        on_construct = function(pos)
                local meta = minetest.get_meta(pos)
-               meta:set_string("formspec", get_bookshelf_formspec(nil))
                local inv = meta:get_inventory()
                inv:set_size("books", 8 * 2)
+               update_bookshelf(pos)
        end,
        can_dig = function(pos,player)
                local inv = minetest.get_meta(pos):get_inventory()
@@ -2184,20 +2202,17 @@ minetest.register_node("default:bookshelf", {
        on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
                minetest.log("action", player:get_player_name() ..
                        " moves stuff in bookshelf at " .. minetest.pos_to_string(pos))
-               local meta = minetest.get_meta(pos)
-               meta:set_string("formspec", get_bookshelf_formspec(meta:get_inventory()))
+               update_bookshelf(pos)
        end,
        on_metadata_inventory_put = function(pos, listname, index, stack, player)
                minetest.log("action", player:get_player_name() ..
-                       " moves stuff to bookshelf at " .. minetest.pos_to_string(pos))
-               local meta = minetest.get_meta(pos)
-               meta:set_string("formspec", get_bookshelf_formspec(meta:get_inventory()))
+                       " puts stuff to bookshelf at " .. minetest.pos_to_string(pos))
+               update_bookshelf(pos)
        end,
        on_metadata_inventory_take = function(pos, listname, index, stack, player)
                minetest.log("action", player:get_player_name() ..
                        " takes stuff from bookshelf at " .. minetest.pos_to_string(pos))
-               local meta = minetest.get_meta(pos)
-               meta:set_string("formspec", get_bookshelf_formspec(meta:get_inventory()))
+               update_bookshelf(pos)
        end,
        on_blast = function(pos)
                local drops = {}