From 103127ee96f6b038601ca22ad462242caac99c54 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Fri, 17 Jul 2020 23:45:51 +0200 Subject: [PATCH] [LuaMod] 'giveItemStack' now tries to use inventory before hotbar. --- source/common/inventory/Inventory.cpp | 4 ++-- source/common/inventory/Inventory.hpp | 2 +- source/server/lua/LuaMod.cpp | 5 ++++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/source/common/inventory/Inventory.cpp b/source/common/inventory/Inventory.cpp index 65dbe5052..95c7267e5 100644 --- a/source/common/inventory/Inventory.cpp +++ b/source/common/inventory/Inventory.cpp @@ -32,9 +32,9 @@ void Inventory::setStack(u16 x, u16 y, const std::string &stringID, u16 amount) m_hasChanged = true; } -bool Inventory::addStack(const std::string &stringID, u16 amount, u16 offset, u16 size) { +bool Inventory::addStack(const std::string &stringID, u16 amount, u16 offset, u16 size, bool mergeOnly) { for (std::size_t i = offset ; i < (size ? offset + size : m_items.size()) ; ++i) { - if (m_items[i].item().id() == 0) { + if (m_items[i].item().id() == 0 && !mergeOnly) { m_items[i] = ItemStack(stringID, amount); m_hasChanged = true; return true; diff --git a/source/common/inventory/Inventory.hpp b/source/common/inventory/Inventory.hpp index a6c7cf3e3..fe4ea70bf 100644 --- a/source/common/inventory/Inventory.hpp +++ b/source/common/inventory/Inventory.hpp @@ -43,7 +43,7 @@ class Inventory : public gk::ISerializable { const ItemStack &getStack(u16 x, u16 y) const { return m_items.at(x + y * m_width); } ItemStack &getStackRef(u16 x, u16 y) { return m_items.at(x + y * m_width); } void setStack(u16 x, u16 y, const std::string &stringID, u16 amount = 1); - bool addStack(const std::string &stringID, u16 amount = 1, u16 offset = 0, u16 size = 0); + bool addStack(const std::string &stringID, u16 amount = 1, u16 offset = 0, u16 size = 0, bool mergeOnly = false); bool addStack2(const std::string &stringID, u16 amount = 1); // Needed for Lua void clearStack(u16 x, u16 y); diff --git a/source/server/lua/LuaMod.cpp b/source/server/lua/LuaMod.cpp index 4d57a6601..eaf8f669d 100644 --- a/source/server/lua/LuaMod.cpp +++ b/source/server/lua/LuaMod.cpp @@ -108,7 +108,10 @@ void LuaMod::despawnEntity(EntityWrapper &entity) { void LuaMod::giveItemStack(ServerPlayer &player, ItemStack *itemStack) { if (itemStack) { - player.inventory().addStack(itemStack->item().stringID(), itemStack->amount()); + // FIXME: This should probably be moved to a mod + if (!player.inventory().addStack(itemStack->item().stringID(), itemStack->amount(), 9, 24, true)) + player.inventory().addStack(itemStack->item().stringID(), itemStack->amount(), 0, 9); + m_worldController.server()->sendPlayerInvUpdate(player.clientID(), player.client()); } else