From 9592b1df73320f5a78654455a1c614a3969f4087 Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Mon, 12 Jul 2021 22:11:00 -0700 Subject: [PATCH] Update player inventories when item is replaced with chat command --- TODO.txt | 6 +++++- chat.lua | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/TODO.txt b/TODO.txt index 89f5e98..af34581 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,5 +1,9 @@ TODO: - update world file when chat commands are used -- update player inventories when items are replaced +- update player inventories when items are replaced: + - bags + - 3d_armor +- update storage inventories when items are replaced +- update player inventories on login - fix top nodes disappearing with /replace_node command diff --git a/chat.lua b/chat.lua index a229bef..3af053e 100644 --- a/chat.lua +++ b/chat.lua @@ -190,13 +190,26 @@ local function replace_item(src, tgt) end core.register_alias(src, tgt) + + -- update player inventories + for _, player in ipairs(core.get_connected_players()) do + local pinv = player:get_inventory() + + 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) + end + end + end + return true end --- Replaces an item. -- --- FIXME: inventory icons not updated --- -- @chatcmd replace_item -- @param old_item Technical name of item to replace. -- @param new_item Technical name of item to be used in place.