diff options
author | Tiger Wang <ziwei.tiger@hotmail.co.uk> | 2015-05-18 15:30:16 +0200 |
---|---|---|
committer | Tiger Wang <ziwei.tiger@hotmail.co.uk> | 2015-05-18 15:30:16 +0200 |
commit | 0dbba305b6b96f81fe5f9b457dfb4f63d4a420c9 (patch) | |
tree | dc5046599788117404826792986eb073ef3b27db /src/Inventory.cpp | |
parent | Merge branch 'master' of https://github.com/mc-server/MCServer (diff) | |
download | cuberite-0dbba305b6b96f81fe5f9b457dfb4f63d4a420c9.tar cuberite-0dbba305b6b96f81fe5f9b457dfb4f63d4a420c9.tar.gz cuberite-0dbba305b6b96f81fe5f9b457dfb4f63d4a420c9.tar.bz2 cuberite-0dbba305b6b96f81fe5f9b457dfb4f63d4a420c9.tar.lz cuberite-0dbba305b6b96f81fe5f9b457dfb4f63d4a420c9.tar.xz cuberite-0dbba305b6b96f81fe5f9b457dfb4f63d4a420c9.tar.zst cuberite-0dbba305b6b96f81fe5f9b457dfb4f63d4a420c9.zip |
Diffstat (limited to 'src/Inventory.cpp')
-rw-r--r-- | src/Inventory.cpp | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/src/Inventory.cpp b/src/Inventory.cpp index c595da5b0..f08dd1896 100644 --- a/src/Inventory.cpp +++ b/src/Inventory.cpp @@ -98,7 +98,7 @@ int cInventory::HowManyCanFit(const cItem & a_ItemStack, int a_BeginSlotNum, int -int cInventory::AddItem(const cItem & a_Item, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst) +int cInventory::AddItem(const cItem & a_Item, bool a_AllowNewStacks) { cItem ToAdd(a_Item); int res = 0; @@ -116,8 +116,30 @@ int cInventory::AddItem(const cItem & a_Item, bool a_AllowNewStacks, bool a_tryT } } - res += m_HotbarSlots.AddItem(ToAdd, a_AllowNewStacks, a_tryToFillEquippedFirst ? m_EquippedSlotNum : -1); + for (int SlotIdx = 0; SlotIdx < m_InventorySlots.GetNumSlots(); ++SlotIdx) + { + auto & Slot = m_InventorySlots.GetSlot(SlotIdx); + if (Slot.IsEqual(a_Item)) + { + cItemHandler Handler(Slot.m_ItemType); + int AmountToAdd = std::min(static_cast<char>(Handler.GetMaxStackSize() - Slot.m_ItemCount), ToAdd.m_ItemCount); + res += AmountToAdd; + + cItem SlotAdjusted(Slot); + SlotAdjusted.m_ItemCount += AmountToAdd; + m_InventorySlots.SetSlot(SlotIdx, SlotAdjusted); + + ToAdd.m_ItemCount -= AmountToAdd; + if (ToAdd.m_ItemCount == 0) + { + return res; + } + } + } + + res += m_HotbarSlots.AddItem(ToAdd, a_AllowNewStacks); ToAdd.m_ItemCount = a_Item.m_ItemCount - res; + if (ToAdd.m_ItemCount == 0) { return res; @@ -131,12 +153,12 @@ int cInventory::AddItem(const cItem & a_Item, bool a_AllowNewStacks, bool a_tryT -int cInventory::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst) +int cInventory::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks) { int TotalAdded = 0; for (cItems::iterator itr = a_ItemStackList.begin(); itr != a_ItemStackList.end();) { - int NumAdded = AddItem(*itr, a_AllowNewStacks, a_tryToFillEquippedFirst); + int NumAdded = AddItem(*itr, a_AllowNewStacks); if (itr->m_ItemCount == NumAdded) { itr = a_ItemStackList.erase(itr); |