Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bookshelf: Count written and empty books #1924

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 27 additions & 12 deletions mods/default/nodes.lua
Expand Up @@ -2145,23 +2145,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", {
Expand All @@ -2175,9 +2193,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()
Expand All @@ -2192,20 +2210,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 = {}
Expand Down