From 7adb8429ba14ecc4ed7ff0a9085bc2c20dec1a3a Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Thu, 20 Oct 2022 00:07:44 +0200 Subject: [PATCH] Refactor books --- DEV_GROUPS.md | 1 + mods/rp_book/init.lua | 135 ++++++++++++++----- mods/rp_book/mod.conf | 2 +- mods/rp_book/textures/default_book.png | Bin 197 -> 139 bytes mods/rp_book/textures/rp_book_book_empty.png | Bin 0 -> 131 bytes mods/rp_default/crafting.lua | 19 +-- mods/rp_gold/init.lua | 6 +- mods/rp_gold/textures/gold_book.png | Bin 0 -> 139 bytes mods/rp_goodies/init.lua | 2 +- 9 files changed, 112 insertions(+), 53 deletions(-) create mode 100644 mods/rp_book/textures/rp_book_book_empty.png create mode 100644 mods/rp_gold/textures/gold_book.png diff --git a/DEV_GROUPS.md b/DEV_GROUPS.md index a0472c21..1f03c7c8 100644 --- a/DEV_GROUPS.md +++ b/DEV_GROUPS.md @@ -53,6 +53,7 @@ These groups are mainly used for a better item sorting in Creative Mode. * `food`: Can be eaten by player. Rating: 2 = eatable, 3 = drinkable, 1 = unknown food type * `nav_compass`: Compass. Rating: 1 = normal compass, 2 = magnocompass * `spawn_egg`: Item that spawns mobs +* `book`: Book. Rating: 1 = unspecific book, 2 = writable book diff --git a/mods/rp_book/init.lua b/mods/rp_book/init.lua index dac1210e..ed0634a4 100644 --- a/mods/rp_book/init.lua +++ b/mods/rp_book/init.lua @@ -19,33 +19,51 @@ local F = minetest.formspec_escape local BOOK_MAX_TITLE_LENGTH = 64 local BOOK_MAX_TEXT_LENGTH = 4500 +local on_use = function(itemstack, player, pointed_thing) + local name = player:get_player_name() + local data = itemstack:get_meta() + + local title = "" + local text = "" + + if data then + text = data:get_string("book:text") + title = data:get_string("book:title") + end + + local form = rp_formspec.get_page("rp_formspec:default") + form = form .. "field[0.5,1.25;8,0;title;"..F(S("Title:"))..";"..F(title).."]" + form = form .. "textarea[0.5,1.75;8,6.75;text;"..F(S("Contents:"))..";"..F(text).."]" + form = form .. rp_formspec.button_exit(2.75, 7.75, 3, 1, "write", S("Write")) + + minetest.show_formspec(name, "rp_book:book", form) +end + +-- Unwritten book (stackable) +minetest.register_craftitem( + ":rp_default:book_empty", + { + description = S("Book"), + _tt_help = S("Write down some notes"), + inventory_image = "rp_book_book_empty.png", + groups = { book = 2, tool = 1 }, + + on_use = function(itemstack, player, pointed_thing) + on_use(itemstack, player, pointed_thing) + end, +}) + +-- Writable book (not stackable) minetest.register_craftitem( ":rp_default:book", { description = S("Unnamed Book"), - _tt_help = S("Write down some notes"), inventory_image = "default_book.png", stack_max = 1, - groups = { tool = 1 }, + groups = { book = 2, tool = 1, not_in_creative_inventory = 1 }, on_use = function(itemstack, player, pointed_thing) - local name = player:get_player_name() - local data = itemstack:get_meta() - - local title = "" - local text = "" - - if data then - text = data:get_string("book:text") - title = data:get_string("book:title") - end - - local form = rp_formspec.get_page("rp_formspec:default") - form = form .. "field[0.5,1.25;8,0;title;"..F(S("Title:"))..";"..F(title).."]" - form = form .. "textarea[0.5,1.75;8,6.75;text;"..F(S("Contents:"))..";"..F(text).."]" - form = form .. rp_formspec.button_exit(2.75, 7.75, 3, 1, "write", S("Write")) - - minetest.show_formspec(name, "rp_book:book", form) + on_use(itemstack, player, pointed_thing) end, }) @@ -55,22 +73,79 @@ minetest.register_on_player_receive_fields( return end - local itemstack = player:get_wielded_item() + local wieldstack = player:get_wielded_item() + local iname = wieldstack:get_name() + if minetest.get_item_group(iname, "book") ~= 2 then + return + end - local meta = itemstack:get_meta() + local title = fields.title + local text = fields.text -- Limit title and text length - if string.len(fields.title) > BOOK_MAX_TITLE_LENGTH then - fields.title = string.sub(fields.title, 1, BOOK_MAX_TITLE_LENGTH) + if string.len(title) > BOOK_MAX_TITLE_LENGTH then + title = string.sub(title, 1, BOOK_MAX_TITLE_LENGTH) end - if string.len(fields.text) > BOOK_MAX_TEXT_LENGTH then - fields.text= string.sub(fields.text, 1, BOOK_MAX_TEXT_LENGTH) + if string.len(text) > BOOK_MAX_TEXT_LENGTH then + text= string.sub(text, 1, BOOK_MAX_TEXT_LENGTH) end - meta:set_string("description", S("Book: “@1”", minetest.colorize("#ffff00", fields.title))) -- Set the item description + local function set_book_meta(item, title, text) + local meta = item:get_meta() + if title ~= "" then + meta:set_string("description", S("Book: “@1”", minetest.colorize("#ffff00", title))) -- Set the item description + else + meta:set_string("description", "") + end + meta:set_string("book:title", title) + meta:set_string("book:text", text) + end - meta:set_string("book:title", fields.title) - meta:set_string("book:text", fields.text) - - player:set_wielded_item(itemstack) + if wieldstack:get_name() ~= "rp_default:book" then + if wieldstack:get_count() == 1 then + wieldstack:set_name("rp_default:book") + set_book_meta(wieldstack, title, text) + player:set_wielded_item(wieldstack) + else + local newstack = wieldstack:take_item() + newstack:set_count(1) + newstack:set_name("rp_default:book") + set_book_meta(newstack, title, text) + local inv = player:get_inventory() + if inv:room_for_item("main", newstack) then + inv:add_item("main", newstack) + else + minetest.add_item(player:get_pos(), newstack) + end + player:set_wielded_item(wieldstack) + end + else + set_book_meta(wieldstack, title, text) + player:set_wielded_item(wieldstack) + end end) + +minetest.register_craft( + { + type = "fuel", + recipe = "rp_default:book_empty", + burntime = 2, +}) + +minetest.register_craft( + { + type = "fuel", + recipe = "rp_default:book", + burntime = 2, +}) + +crafting.register_craft( + { + output = "rp_default:book_empty", + items = { + "rp_default:paper 3", + "rp_default:stick", + "rp_default:fiber", + } +}) + diff --git a/mods/rp_book/mod.conf b/mods/rp_book/mod.conf index a23c81a2..d25452c5 100644 --- a/mods/rp_book/mod.conf +++ b/mods/rp_book/mod.conf @@ -1,2 +1,2 @@ name = rp_book -depends = rp_default, rp_formspec +depends = rp_crafting, rp_default, rp_formspec diff --git a/mods/rp_book/textures/default_book.png b/mods/rp_book/textures/default_book.png index 612b40dabe4c3e888d3c56c016f70ccce2b8f9f3..ac64a466f43ffb6a892a40df251e5edb1f158a2a 100644 GIT binary patch delta 121 zcmX@g*v&XWvWSU+fnmbL3^O3bRubeF45Wb|ZyEbUAm!lc;uuoFn7lyW;28IY6=B~d z9(Qplv|#RLTk9ce!Xh%?vRKWI_uJ8F4lT2!`tqJ U)5;>RVnJqly85}Sb4q9e09x!O6aWAK delta 180 zcmV;l089Ui0mT838Gi-<001BJ|6u?C0E|gQK~#7FWBC97KSh}O1{fO~|0f2+)MIG& zF_XuMk>ufqFzlZb#;~m28;oHJBJEVLVvzjInKR)Afb@cF25U}p*F-U(ILs1+@frXG z2m>N#`rvT^VFN&3!E^xv6J-F+Tma(J)&-PABgF{`)qqWt0xif)$_N8sDHBAavqAEB iGZRb^E)0^#FaQ81gBJb@jSV9J0000l%VIS