From 4a253721e8457ae1e52a768d72f07f3f3a85c071 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Fri, 24 Nov 2023 14:06:02 -0300 Subject: [PATCH] fix: create item talkaction for stackable items (#1898) --- data/scripts/talkactions/god/create_item.lua | 22 +++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/data/scripts/talkactions/god/create_item.lua b/data/scripts/talkactions/god/create_item.lua index 7a6285ef101..723a7f047dd 100644 --- a/data/scripts/talkactions/god/create_item.lua +++ b/data/scripts/talkactions/god/create_item.lua @@ -23,16 +23,22 @@ function createItem.onSay(player, words, param) local count = tonumber(split[2]) if count then if itemType:isStackable() then - local item = Game.createItem(itemType:getId(), count) - if not item then - player:sendCancelMessage("Cannot create item") - return true - end + local stackSize = itemType:getStackSize() + local container = Game.createItem(2854) + local remainingCount = count - local ret = player:addItemEx(item, INDEX_WHEREEVER, FLAG_NOLIMIT) - if ret ~= RETURNVALUE_NOERROR then - player:sendCancelMessage(ret) + while remainingCount > 0 do + local countToAdd = math.min(remainingCount, stackSize) + local tmpItem = container:addItem(itemType:getId(), countToAdd, INDEX_WHEREEVER, FLAG_NOLIMIT) + if tmpItem then + remainingCount = remainingCount - countToAdd + else + logger.warn("Failed to add item: {}, to container", itemType:getName()) + end end + + player:addItemEx(container) + player:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN) return true elseif not itemType:isFluidContainer() then local min = 100