From dbfe435abf5a79f727d7527471489ce465a4f6aa Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Mon, 23 Oct 2017 20:25:46 +0200 Subject: [PATCH] Bookshelf: Count written and empty books --- mods/default/nodes.lua | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/mods/default/nodes.lua b/mods/default/nodes.lua index 00756210..00bb2985 100644 --- a/mods/default/nodes.lua +++ b/mods/default/nodes.lua @@ -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 = {} -- 2.25.1