From b4392aeb1f73bb7e4fe71e7b910a69ae1f660262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 1 Jun 2022 20:54:16 +0200 Subject: [PATCH] Fix book duplication: Remember wield index (#2957) --- mods/default/craftitems.lua | 33 ++++++++++++++++++++++++++++---- mods/default/locale/template.txt | 1 + 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/mods/default/craftitems.lua b/mods/default/craftitems.lua index 46d78049..d2e827f4 100644 --- a/mods/default/craftitems.lua +++ b/mods/default/craftitems.lua @@ -42,6 +42,12 @@ local function formspec_string(lpp, page, lines, string) return string end +local book_writers = {} + +minetest.register_on_leaveplayer(function(player) + book_writers[player:get_player_name()] = nil +end) + local tab_number local lpp = 14 -- Lines per book's page local function book_on_use(itemstack, user) @@ -90,6 +96,8 @@ local function book_on_use(itemstack, user) end minetest.show_formspec(player_name, "default:book", formspec_size .. formspec) + -- Store the wield index in case the user accidentally switches before the formspec is shown + book_writers[player_name] = {wield_index = user:get_wield_index()} return itemstack end @@ -97,10 +105,23 @@ local max_text_size = 10000 local max_title_size = 80 local short_title_size = 35 minetest.register_on_player_receive_fields(function(player, formname, fields) - if formname ~= "default:book" then return end + if formname ~= "default:book" then + return + end local player_name = player:get_player_name() local inv = player:get_inventory() - local stack = player:get_wielded_item() + if not book_writers[player_name] then + return + end + local wield_index = book_writers[player_name].wield_index + local wield_list = player:get_wield_list() + local stack = inv:get_stack(wield_list, wield_index) + local written = stack:get_name() == "default:book_written" + if stack:get_name() ~= "default:book" and not written then + -- No book in the wield slot, abort & inform the player + minetest.chat_send_player(player_name, S("The book you were writing to mysteriously disappeared.")) + return + end local data = stack:get_meta():to_table().fields local title = data.title or "" @@ -127,9 +148,13 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) return end + if fields.close then + book_writers[player_name] = nil + end + if fields.save and fields.title and fields.text then local new_stack - if stack:get_name() ~= "default:book_written" then + if not written then local count = stack:get_count() if count == 1 then stack:set_name("default:book_written") @@ -193,7 +218,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end -- Update stack - player:set_wielded_item(stack) + inv:set_stack(wield_list, wield_index, stack) end) diff --git a/mods/default/locale/template.txt b/mods/default/locale/template.txt index 13808e9d..f214d837 100644 --- a/mods/default/locale/template.txt +++ b/mods/default/locale/template.txt @@ -161,6 +161,7 @@ Empty Bookshelf= Bookshelf (@1 written, @2 empty books)= Bookshelf= Text too long= +The book you were writing to mysteriously disappeared.= "@1"= Wooden Sign= Steel Sign=