default: Fix removing content from written books.

Fixes https://github.com/minetest/minetest_game/issues/1743
This commit is contained in:
orbea 2020-04-11 22:20:15 -07:00
parent 695f98f213
commit 49836ea077

View file

@ -19,8 +19,8 @@ local function book_on_use(itemstack, user)
local data = meta:to_table().fields local data = meta:to_table().fields
if data.owner then if data.owner then
title = data.title title = data.title or ""
text = data.text text = data.text or ""
owner = data.owner owner = data.owner
for str in (text .. "\n"):gmatch("([^\n]*)[\n]") do for str in (text .. "\n"):gmatch("([^\n]*)[\n]") do
@ -40,7 +40,7 @@ local function book_on_use(itemstack, user)
local formspec local formspec
local esc = minetest.formspec_escape local esc = minetest.formspec_escape
if owner == player_name then if owner == player_name or (title == "" and text == "") then
formspec = "size[8,8]" .. formspec = "size[8,8]" ..
"field[0.5,1;7.5,0;title;" .. esc(S("Title:")) .. ";" .. "field[0.5,1;7.5,0;title;" .. esc(S("Title:")) .. ";" ..
esc(title) .. "]" .. esc(title) .. "]" ..
@ -72,8 +72,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
local inv = player:get_inventory() local inv = player:get_inventory()
local stack = player:get_wielded_item() local stack = player:get_wielded_item()
if fields.save and fields.title and fields.text if fields.save and fields.title and fields.text then
and fields.title ~= "" and fields.text ~= "" then
local new_stack, data local new_stack, data
if stack:get_name() ~= "default:book_written" then if stack:get_name() ~= "default:book_written" then
local count = stack:get_count() local count = stack:get_count()
@ -87,7 +86,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
data = stack:get_meta():to_table().fields data = stack:get_meta():to_table().fields
end end
if data and data.owner and data.owner ~= player:get_player_name() then if data and data.owner and data.owner ~= player:get_player_name()
and fields.title ~= "" and fields.text ~= "" then
return return
end end
@ -95,8 +95,10 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
data.title = fields.title:sub(1, max_title_size) data.title = fields.title:sub(1, max_title_size)
data.owner = player:get_player_name() data.owner = player:get_player_name()
local short_title = data.title local short_title = data.title
if short_title == "" then
short_title = "Book"
-- Don't bother triming the title if the trailing dots would make it longer -- Don't bother triming the title if the trailing dots would make it longer
if #short_title > short_title_size + 3 then elseif #short_title > short_title_size + 3 then
short_title = short_title:sub(1, short_title_size) .. "..." short_title = short_title:sub(1, short_title_size) .. "..."
end end
data.description = S("\"@1\" by @2", short_title, data.owner) data.description = S("\"@1\" by @2", short_title, data.owner)