Bookshelf: Count written and empty books

This commit is contained in:
SmallJoker 2017-10-23 20:25:46 +02:00 committed by paramat
parent 08c60cfc30
commit dbfe435abf

View File

@ -2137,23 +2137,41 @@ local bookshelf_formspec =
"listring[current_player;main]" .. "listring[current_player;main]" ..
default.get_hotbar_bg(0,2.85) 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 formspec = bookshelf_formspec
local invlist = inv and inv:get_list("books")
-- Inventory slots overlay -- Inventory slots overlay
local bx, by = 0, 0.3 local bx, by = 0, 0.3
local n_written, n_empty = 0, 0
for i = 1, 16 do for i = 1, 16 do
if i == 9 then if i == 9 then
bx = 0 bx = 0
by = by + 1 by = by + 1
end end
if not invlist or invlist[i]:is_empty() then local stack = invlist[i]
if stack:is_empty() then
formspec = formspec .. formspec = formspec ..
"image[" .. bx .. "," .. by .. ";1,1;default_bookshelf_slot.png]" "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 end
bx = bx + 1 bx = bx + 1
end 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 end
minetest.register_node("default:bookshelf", { minetest.register_node("default:bookshelf", {
@ -2167,9 +2185,9 @@ minetest.register_node("default:bookshelf", {
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("formspec", get_bookshelf_formspec(nil))
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size("books", 8 * 2) inv:set_size("books", 8 * 2)
update_bookshelf(pos)
end, end,
can_dig = function(pos,player) can_dig = function(pos,player)
local inv = minetest.get_meta(pos):get_inventory() 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) on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
minetest.log("action", player:get_player_name() .. minetest.log("action", player:get_player_name() ..
" moves stuff in bookshelf at " .. minetest.pos_to_string(pos)) " moves stuff in bookshelf at " .. minetest.pos_to_string(pos))
local meta = minetest.get_meta(pos) update_bookshelf(pos)
meta:set_string("formspec", get_bookshelf_formspec(meta:get_inventory()))
end, end,
on_metadata_inventory_put = function(pos, listname, index, stack, player) on_metadata_inventory_put = function(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name() .. minetest.log("action", player:get_player_name() ..
" moves stuff to bookshelf at " .. minetest.pos_to_string(pos)) " puts stuff to bookshelf at " .. minetest.pos_to_string(pos))
local meta = minetest.get_meta(pos) update_bookshelf(pos)
meta:set_string("formspec", get_bookshelf_formspec(meta:get_inventory()))
end, end,
on_metadata_inventory_take = function(pos, listname, index, stack, player) on_metadata_inventory_take = function(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name() .. minetest.log("action", player:get_player_name() ..
" takes stuff from bookshelf at " .. minetest.pos_to_string(pos)) " takes stuff from bookshelf at " .. minetest.pos_to_string(pos))
local meta = minetest.get_meta(pos) update_bookshelf(pos)
meta:set_string("formspec", get_bookshelf_formspec(meta:get_inventory()))
end, end,
on_blast = function(pos) on_blast = function(pos)
local drops = {} local drops = {}