Refactor book copy code, gray out author

This commit is contained in:
Wuzzy 2017-06-06 02:00:36 +02:00
parent 79576e37ea
commit a4e9da19ca
1 changed files with 18 additions and 12 deletions

View File

@ -42,6 +42,21 @@ local get_text = function(itemstack)
return text return text
end end
local make_description = function(title, author, generation)
local desc
if generation == 0 then
desc = string.format("“%s”", title)
elseif generation == 1 then
desc = string.format("Copy of “%s”", title)
elseif generation == 2 then
desc = string.format("Copy of Copy of “%s”", title)
else
desc = "Tattered Book"
end
desc = desc .. "\n" .. core.colorize("#AAAAAA", string.format("by %s", author))
return desc
end
local write = function(itemstack, user, pointed_thing) local write = function(itemstack, user, pointed_thing)
-- Call on_rightclick if the pointed node defines it -- Call on_rightclick if the pointed node defines it
if pointed_thing.type == "node" then if pointed_thing.type == "node" then
@ -129,7 +144,7 @@ minetest.register_on_player_receive_fields(function ( player, formname, fields )
meta:set_string("title", fields.title) meta:set_string("title", fields.title)
meta:set_string("author", name) meta:set_string("author", name)
meta:set_string("text", text) meta:set_string("text", text)
meta:set_string("description", string.format("“%s”",fields.title) .. "\n" .. string.format("by %s", name)) meta:set_string("description", make_description(fields.title, name, 0))
-- The book copy counter. 0 = original, 1 = copy of original, 2 = copy of copy of original, … -- The book copy counter. 0 = original, 1 = copy of original, 2 = copy of copy of original, …
meta:set_int("generation", 0) meta:set_int("generation", 0)
@ -207,22 +222,13 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv
-- Increase book generation and update description -- Increase book generation and update description
local generation = ometa:get_int("generation") local generation = ometa:get_int("generation")
local desc
if generation == 0 then
desc = string.format("Copy of “%s”", title)
elseif generation == 1 then
desc = string.format("Copy of Copy of “%s”", title)
else
desc = "Tattered Book"
end
desc = desc .. "\n" .. string.format("by %s", author)
generation = generation + 1 generation = generation + 1
if generation < 1 then if generation < 1 then
generation = 1 generation = 1
end end
local desc = make_description(title, author, generation)
imeta:set_string("description", desc) imeta:set_string("description", desc)
imeta:set_int("generation", generation) imeta:set_int("generation", generation)
end end