diff --git a/docs/lua-api-mod.md b/docs/lua-api-mod.md index 7bb241e7..62ac10e2 100644 --- a/docs/lua-api-mod.md +++ b/docs/lua-api-mod.md @@ -109,9 +109,11 @@ Give an item stack to a player. Example: ```lua -mod:give_item_stack(player, item_stack) +local items_left = mod:give_item_stack(player, item_stack) ``` +This function returns an `ItemStack` containing the items it couldn't place into player's inventory. + ### `spawn_entity` Spawn an entity. diff --git a/source/common/inventory/Inventory.cpp b/source/common/inventory/Inventory.cpp index fd91daa7..4f3cf50a 100644 --- a/source/common/inventory/Inventory.cpp +++ b/source/common/inventory/Inventory.cpp @@ -35,7 +35,9 @@ void Inventory::setStack(u16 x, u16 y, const std::string &stringID, u16 amount) ItemStack Inventory::addStack(const std::string &stringID, u16 amount, u16 offset, u16 size, bool mergeOnly) { if (stringID.empty() || stringID == BLOCK_AIR || amount == 0) - return {BLOCK_AIR, 0}; + return ItemStack::Empty; + + bool hasChanged = false; ItemStack ret{stringID, amount}; for (std::size_t i = offset ; ret.amount() && i < (size ? offset + size : m_items.size()) ; ++i) { @@ -49,7 +51,8 @@ ItemStack Inventory::addStack(const std::string &stringID, u16 amount, u16 offse m_items[i] = ItemStack(stringID, ret.amount()); ret.setAmount(0); } - m_hasChanged = true; + + hasChanged = true; } else if (item.stringID() == stringID) { u16 sum = m_items[i].amount() + ret.amount(); @@ -62,12 +65,14 @@ ItemStack Inventory::addStack(const std::string &stringID, u16 amount, u16 offse ret.setAmount(0); } - m_hasChanged = true; + hasChanged = true; } } - if (!m_hasChanged && mergeOnly && ret.amount()) + if (!hasChanged && mergeOnly && ret.amount()) return addStack(ret.item().stringID(), ret.amount(), offset, size); + else if (hasChanged) + m_hasChanged = hasChanged; return ret; } diff --git a/source/common/inventory/ItemStack.cpp b/source/common/inventory/ItemStack.cpp index ea2ddab5..a3818fee 100644 --- a/source/common/inventory/ItemStack.cpp +++ b/source/common/inventory/ItemStack.cpp @@ -27,6 +27,8 @@ #include "ItemStack.hpp" #include "Registry.hpp" +const ItemStack ItemStack::Empty{BLOCK_AIR, 0}; + const Item &ItemStack::item() const { return Registry::getInstance().getItemFromStringID(m_stringID); } diff --git a/source/common/inventory/ItemStack.hpp b/source/common/inventory/ItemStack.hpp index e7c13f8d..292ac852 100644 --- a/source/common/inventory/ItemStack.hpp +++ b/source/common/inventory/ItemStack.hpp @@ -46,6 +46,8 @@ class ItemStack { static void initUsertype(sol::state &lua); + static const ItemStack Empty; + private: std::string m_stringID{BLOCK_AIR}; diff --git a/source/server/lua/LuaMod.cpp b/source/server/lua/LuaMod.cpp index a24eac9e..507e7107 100644 --- a/source/server/lua/LuaMod.cpp +++ b/source/server/lua/LuaMod.cpp @@ -106,16 +106,19 @@ void LuaMod::despawnEntity(EntityWrapper &entity) { gkError() << "In mod '" + m_id + "': Failed to despawn entity: Missing position and network components"; } -void LuaMod::giveItemStack(ServerPlayer &player, ItemStack *itemStack) { - if (itemStack) { - // FIXME: This should probably be moved to a mod - ItemStack stackRet = player.inventory().addStack(itemStack->item().stringID(), itemStack->amount(), 9, 24, true); - if (stackRet.amount() != 0) - player.inventory().addStack(stackRet.item().stringID(), stackRet.amount(), 0, 9, true); - - m_worldController.server()->sendPlayerInvUpdate(player.clientID(), player.client()); - } - else +ItemStack LuaMod::giveItemStack(ServerPlayer &player, ItemStack *itemStack) { + if (!itemStack) { gkError() << "In mod '" + m_id + "': Failed to add stack to player"; + return ItemStack::Empty; + } + + // FIXME: This should probably be moved to a mod + ItemStack stackRet = player.inventory().addStack(itemStack->item().stringID(), itemStack->amount(), 9, 24, true); + if (stackRet.amount() != 0) + stackRet = player.inventory().addStack(stackRet.item().stringID(), stackRet.amount(), 0, 9, true); + + m_worldController.server()->sendPlayerInvUpdate(player.clientID(), player.client()); + + return stackRet; } diff --git a/source/server/lua/LuaMod.hpp b/source/server/lua/LuaMod.hpp index fe5a1f0a..2de1faf4 100644 --- a/source/server/lua/LuaMod.hpp +++ b/source/server/lua/LuaMod.hpp @@ -67,7 +67,7 @@ class LuaMod { void spawnEntity(const std::string &entityID, const sol::table &table); void despawnEntity(EntityWrapper &entity); - void giveItemStack(ServerPlayer &player, ItemStack *itemStack); + ItemStack giveItemStack(ServerPlayer &player, ItemStack *itemStack); enum class DefinitionType { Block,