From e81ed836dc36aeaa812c95372745b7fd4122d819 Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Mon, 12 Jul 2021 22:37:43 -0700 Subject: [PATCH] Support updating items in cornernote's bags mod with chat command... Homepage: http://cornernote.github.io/minetest-bags/ Git repo: https://github.com/cornernote/minetest-bags --- TODO.txt | 1 - chat.lua | 28 ++++++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/TODO.txt b/TODO.txt index af34581..493aa05 100644 --- a/TODO.txt +++ b/TODO.txt @@ -2,7 +2,6 @@ TODO: - update world file when chat commands are used - update player inventories when items are replaced: - - bags - 3d_armor - update storage inventories when items are replaced - update player inventories on login diff --git a/chat.lua b/chat.lua index 3af053e..25542cd 100644 --- a/chat.lua +++ b/chat.lua @@ -180,6 +180,22 @@ core.register_chatcommand(cmd_repo.node.cmd_rem, { end, }) +local function update_list(inv, listname, src, tgt) + local list = inv:get_list(listname) + if not list then + cleaner.log("warning", "unknown player list: " .. listname) + return + end + + for idx, stack in pairs(list) do + if stack:get_name() == src then + local new_stack = ItemStack(tgt) + new_stack:set_count(stack:get_count()) + inv:set_stack(listname, idx, new_stack) + end + end +end + local function replace_item(src, tgt) if not core.registered_items[tgt] then return false, S('Cannot use unknown item "@1" as replacement.', tgt) @@ -191,16 +207,16 @@ local function replace_item(src, tgt) core.register_alias(src, tgt) + local bags = core.get_modpath("bags") ~= nil + -- update player inventories for _, player in ipairs(core.get_connected_players()) do local pinv = player:get_inventory() + update_list(pinv, "main", src, tgt) - for idx, stack in pairs(pinv:get_list("main")) do - if stack:get_name() == src then - local new_stack = ItemStack(tgt) - new_stack:set_count(stack:get_count()) - - pinv:set_stack("main", idx, new_stack) + if bags then + for i = 1, 4 do + update_list(pinv, "bag" .. i .. "contents", src, tgt) end end end