Replace deprecated itemstack metadata methods with up to date ones and add book description naming

This commit is contained in:
KaadmY 2017-07-04 11:42:22 -07:00
parent 476a723aab
commit a0a1a61692

View File

@ -7,44 +7,46 @@
minetest.register_craftitem( minetest.register_craftitem(
":default:book", ":default:book",
{ {
description = "Book", description = "Unnamed Book",
inventory_image = "default_book.png", inventory_image = "default_book.png",
stack_max = 1, stack_max = 1,
on_use = function(itemstack, player, pointed_thing) on_use = function(itemstack, player, pointed_thing)
local name = player:get_player_name() local name = player:get_player_name()
local data = minetest.deserialize(itemstack:get_metadata()) local data = minetest.deserialize(itemstack:get_metadata())
local title = "" local title = ""
local text = "" local text = ""
if data then if data then
text = data.text text = data.text
title = data.title title = data.title
end end
local form = default.ui.get_page("default:notabs") local form = default.ui.get_page("default:notabs")
form = form .. "field[0.5,1.25;8,0;title;Title:;"..minetest.formspec_escape(title).."]" form = form .. "field[0.5,1.25;8,0;title;Title:;"..minetest.formspec_escape(title).."]"
form = form .. "textarea[0.5,1.75;8,6.75;text;Contents:;"..minetest.formspec_escape(text).."]" form = form .. "textarea[0.5,1.75;8,6.75;text;Contents:;"..minetest.formspec_escape(text).."]"
form = form .. default.ui.button_exit(2.75, 7.75, 3, 1, "write", "Write") form = form .. default.ui.button_exit(2.75, 7.75, 3, 1, "write", "Write")
minetest.show_formspec(name, "book:book", form) minetest.show_formspec(name, "book:book", form)
end, end,
}) })
minetest.register_on_player_receive_fields( minetest.register_on_player_receive_fields(
function(player, form_name, fields) function(player, form_name, fields)
if form_name ~= "book:book" or not fields.write then return end if form_name ~= "book:book" or not fields.write then return end
local itemstack = player:get_wielded_item() local itemstack = player:get_wielded_item()
local data = minetest.deserialize(itemstack:get_metadata())
if not data then data = {} end local meta = itemstack:get_meta()
data.text = fields.text
data.title = fields.title meta:set_string("description", fields.title) -- Set the item description
meta:set_string("book:title", fields.title)
meta:set_string("book:text", fields.text)
itemstack:set_metadata(minetest.serialize(data))
player:set_wielded_item(itemstack) player:set_wielded_item(itemstack)
end) end)
-- Achievements -- Achievements
@ -55,6 +57,6 @@ achievements.register_achievement(
description = "Craft a book", description = "Craft a book",
times = 1, times = 1,
craftitem = "default:book", craftitem = "default:book",
}) })
default.log("mod:book", "loaded") default.log("mod:book", "loaded")