From a4e9da19ca8417836b540b220259ebd3b9cb05c7 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 6 Jun 2017 02:00:36 +0200 Subject: [PATCH] Refactor book copy code, gray out author --- mods/ITEMS/mcl_books/init.lua | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/mods/ITEMS/mcl_books/init.lua b/mods/ITEMS/mcl_books/init.lua index 9c05bc9a9e..7ba9e98d57 100644 --- a/mods/ITEMS/mcl_books/init.lua +++ b/mods/ITEMS/mcl_books/init.lua @@ -42,6 +42,21 @@ local get_text = function(itemstack) return text 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) -- Call on_rightclick if the pointed node defines it 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("author", name) 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, … 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 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 if generation < 1 then generation = 1 end + local desc = make_description(title, author, generation) + imeta:set_string("description", desc) imeta:set_int("generation", generation) end