forked from thunderdog1138/star_wars
Show title and author of book in description
Utilizes the new key-meta (as with nodes) and the ability to set the description of an itemstack with the `description` meta key. Includes code to convert old metadata to new key-meta.
This commit is contained in:
parent
78c632ebd4
commit
c68b8274fe
|
@ -15,11 +15,19 @@ minetest.register_craftitem("default:paper", {
|
||||||
local lpp = 14 -- Lines per book's page
|
local lpp = 14 -- Lines per book's page
|
||||||
local function book_on_use(itemstack, user)
|
local function book_on_use(itemstack, user)
|
||||||
local player_name = user:get_player_name()
|
local player_name = user:get_player_name()
|
||||||
local data = minetest.deserialize(itemstack:get_metadata())
|
local meta = itemstack:get_meta()
|
||||||
local title, text, owner = "", "", player_name
|
local title, text, owner = "", "", player_name
|
||||||
local page, page_max, lines, string = 1, 1, {}, ""
|
local page, page_max, lines, string = 1, 1, {}, ""
|
||||||
|
|
||||||
if data then
|
-- Backwards compatibility
|
||||||
|
local old_data = minetest.deserialize(itemstack:get_metadata())
|
||||||
|
if old_data then
|
||||||
|
meta:from_table({ fields = old_data })
|
||||||
|
end
|
||||||
|
|
||||||
|
local data = meta:to_table().fields
|
||||||
|
|
||||||
|
if data.owner then
|
||||||
title = data.title
|
title = data.title
|
||||||
text = data.text
|
text = data.text
|
||||||
owner = data.owner
|
owner = data.owner
|
||||||
|
@ -81,35 +89,38 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
new_stack = ItemStack("default:book_written")
|
new_stack = ItemStack("default:book_written")
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
data = minetest.deserialize(stack:get_metadata())
|
data = stack:get_meta():to_table().fields
|
||||||
end
|
end
|
||||||
|
|
||||||
if not data then data = {} end
|
if not data then data = {} end
|
||||||
data.title = fields.title
|
data.title = fields.title
|
||||||
|
data.owner = player:get_player_name()
|
||||||
|
data.description = "\""..fields.title.."\" by "..data.owner
|
||||||
data.text = fields.text
|
data.text = fields.text
|
||||||
data.text_len = #data.text
|
data.text_len = #data.text
|
||||||
data.page = 1
|
data.page = 1
|
||||||
data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
|
data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
|
||||||
data.owner = player:get_player_name()
|
|
||||||
local data_str = minetest.serialize(data)
|
|
||||||
|
|
||||||
if new_stack then
|
if new_stack then
|
||||||
new_stack:set_metadata(data_str)
|
new_stack:get_meta():from_table({ fields = data })
|
||||||
if inv:room_for_item("main", new_stack) then
|
if inv:room_for_item("main", new_stack) then
|
||||||
inv:add_item("main", new_stack)
|
inv:add_item("main", new_stack)
|
||||||
else
|
else
|
||||||
minetest.add_item(player:getpos(), new_stack)
|
minetest.add_item(player:getpos(), new_stack)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
stack:set_metadata(data_str)
|
stack:get_meta():from_table({ fields = data })
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif fields.book_next or fields.book_prev then
|
elseif fields.book_next or fields.book_prev then
|
||||||
local data = minetest.deserialize(stack:get_metadata())
|
local data = stack:get_meta():to_table().fields
|
||||||
if not data or not data.page then
|
if not data or not data.page then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
data.page = tonumber(data.page)
|
||||||
|
data.page_max = tonumber(data.page_max)
|
||||||
|
|
||||||
if fields.book_next then
|
if fields.book_next then
|
||||||
data.page = data.page + 1
|
data.page = data.page + 1
|
||||||
if data.page > data.page_max then
|
if data.page > data.page_max then
|
||||||
|
@ -249,4 +260,3 @@ minetest.register_craftitem("default:flint", {
|
||||||
description = "Flint",
|
description = "Flint",
|
||||||
inventory_image = "default_flint.png"
|
inventory_image = "default_flint.png"
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue