1
0

Empty buckets replace fluid buckets on use and vice versa (patch contributed by Mgueydan)

Fixes FS #277.
API change: added an optional parameter to cItemGrid:AddItem(), cItemGrid:AddItems(), cInventory:AddItem() and cInventory:AddItems()

git-svn-id: http://mc-server.googlecode.com/svn/trunk@1643 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft 2013-07-03 13:56:11 +00:00
parent 504f6192f7
commit e27a5db409
6 changed files with 74 additions and 29 deletions

View File

@ -12978,7 +12978,7 @@ static int tolua_AllToLua_cInventory_AddItems00(lua_State* tolua_S)
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'AddItems'", NULL);
#endif
{
int tolua_ret = (int) self->AddItems(*a_ItemStackList,a_AllowNewStacks);
int tolua_ret = (int) self->AddItems(*a_ItemStackList,a_AllowNewStacks,false);
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
@ -15821,7 +15821,7 @@ static int tolua_AllToLua_cItemGrid_AddItem00(lua_State* tolua_S)
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'AddItem'", NULL);
#endif
{
int tolua_ret = (int) self->AddItem(*a_ItemStack,a_AllowNewStacks);
int tolua_ret = (int) self->AddItem(*a_ItemStack,a_AllowNewStacks, false);
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}

View File

@ -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)
int cInventory::AddItem(const cItem & a_Item, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst)
{
cItem ToAdd(a_Item);
int res = 0;
@ -112,7 +112,7 @@ int cInventory::AddItem(const cItem & a_Item, bool a_AllowNewStacks)
}
}
res += m_HotbarSlots.AddItem(ToAdd, a_AllowNewStacks);
res += m_HotbarSlots.AddItem(ToAdd, a_AllowNewStacks, a_tryToFillEquippedFirst ? m_EquippedSlotNum : -1);
ToAdd.m_ItemCount = a_Item.m_ItemCount - res;
if (ToAdd.m_ItemCount == 0)
{
@ -127,12 +127,12 @@ int cInventory::AddItem(const cItem & a_Item, bool a_AllowNewStacks)
int cInventory::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks)
int cInventory::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst)
{
int TotalAdded = 0;
for (cItems::iterator itr = a_ItemStackList.begin(); itr != a_ItemStackList.end();)
{
int NumAdded = AddItem(*itr, a_AllowNewStacks);
int NumAdded = AddItem(*itr, a_AllowNewStacks, a_tryToFillEquippedFirst);
if (itr->m_ItemCount == NumAdded)
{
itr = a_ItemStackList.erase(itr);

View File

@ -66,17 +66,23 @@ public:
/** Adds as many items out of a_ItemStack as can fit.
If a_AllowNewStacks is set to false, only existing stacks can be topped up;
if a_AllowNewStacks is set to true, empty slots can be used for the rest.
If a_tryToFillEquippedFirst is set to true, the currently equipped slot will be used first (if empty or
compatible with added items)
if a_tryToFillEquippedFirst is set to false, the regular order applies.
Returns the number of items that fit.
*/
int AddItem(const cItem & a_ItemStack, bool a_AllowNewStacks = true);
int AddItem(const cItem & a_ItemStack, bool a_AllowNewStacks = true, bool a_tryToFillEquippedFirst = false);
/** Same as AddItem, but works on an entire list of item stacks.
The a_ItemStackList is modified to reflect the leftover items.
If a_AllowNewStacks is set to false, only existing stacks can be topped up;
if a_AllowNewStacks is set to true, empty slots can be used for the rest
if a_AllowNewStacks is set to true, empty slots can be used for the rest.
If a_tryToFillEquippedFirst is set to true, the currently equipped slot will be used first (if empty or
compatible with added items)
if a_tryToFillEquippedFirst is set to false, the regular order applies.
Returns the total number of items that fit.
*/
int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks);
int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst);
/// Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed
bool RemoveOneEquippedItem(void);

View File

@ -240,20 +240,51 @@ int cItemGrid::HowManyCanFit(const cItem & a_ItemStack)
int cItemGrid::AddItem(cItem & a_ItemStack, bool a_AllowNewStacks)
int cItemGrid::AddItemToSlot(const cItem & a_ItemStack, int a_Slot, int a_Num, int a_MaxStack)
{
int PrevCount = 0;
if (m_Slots[a_Slot].IsEmpty())
{
m_Slots[a_Slot] = a_ItemStack;
PrevCount = 0;
}
else
{
PrevCount = m_Slots[a_Slot].m_ItemCount;
}
m_Slots[a_Slot].m_ItemCount = std::min(a_MaxStack, PrevCount + a_Num);
int toReturn = m_Slots[a_Slot].m_ItemCount - PrevCount;
TriggerListeners(a_Slot);
return toReturn;
}
int cItemGrid::AddItem(cItem & a_ItemStack, bool a_AllowNewStacks, int a_PrioritarySlot)
{
int NumLeft = a_ItemStack.m_ItemCount;
int MaxStack = ItemHandler(a_ItemStack.m_ItemType)->GetMaxStackSize();
// Try prioritarySlot first:
if (
(a_PrioritarySlot != -1) &&
(
m_Slots[a_PrioritarySlot].IsEmpty() ||
m_Slots[a_PrioritarySlot].IsStackableWith(a_ItemStack)
)
)
{
NumLeft -= AddItemToSlot(a_ItemStack, a_PrioritarySlot, NumLeft, MaxStack);
}
// Scan existing stacks:
for (int i = m_NumSlots - 1; i >= 0; i--)
{
if (m_Slots[i].IsStackableWith(a_ItemStack))
{
int PrevCount = m_Slots[i].m_ItemCount;
m_Slots[i].m_ItemCount = std::min(MaxStack, PrevCount + NumLeft);
NumLeft -= m_Slots[i].m_ItemCount - PrevCount;
TriggerListeners(i);
NumLeft -= AddItemToSlot(a_ItemStack, i, NumLeft, MaxStack);
}
if (NumLeft <= 0)
{
@ -271,10 +302,7 @@ int cItemGrid::AddItem(cItem & a_ItemStack, bool a_AllowNewStacks)
{
if (m_Slots[i].IsEmpty())
{
m_Slots[i] = a_ItemStack;
m_Slots[i].m_ItemCount = std::min(MaxStack, NumLeft);
NumLeft -= m_Slots[i].m_ItemCount;
TriggerListeners(i);
NumLeft -= AddItemToSlot(a_ItemStack, i, NumLeft, MaxStack);
}
if (NumLeft <= 0)
{
@ -289,12 +317,12 @@ int cItemGrid::AddItem(cItem & a_ItemStack, bool a_AllowNewStacks)
int cItemGrid::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks)
int cItemGrid::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, int a_PrioritarySlot)
{
int TotalAdded = 0;
for (cItems::iterator itr = a_ItemStackList.begin(); itr != a_ItemStackList.end();)
{
int NumAdded = AddItem(*itr, a_AllowNewStacks);
int NumAdded = AddItem(*itr, a_AllowNewStacks, a_PrioritarySlot);
if (itr->m_ItemCount == NumAdded)
{
itr = a_ItemStackList.erase(itr);

View File

@ -76,18 +76,24 @@ public:
/** Adds as many items out of a_ItemStack as can fit.
If a_AllowNewStacks is set to false, only existing stacks can be topped up;
if a_AllowNewStacks is set to true, empty slots can be used for the rest
if a_AllowNewStacks is set to true, empty slots can be used for the rest.
If a_PrioritarySlot is set to a positive value, then the corresponding slot will be used in
first (if empty or compatible with added items)
if a_PrioritarySlot is set to -1, regular order apply
Returns the number of items that fit.
*/
int AddItem(cItem & a_ItemStack, bool a_AllowNewStacks = true);
int AddItem(cItem & a_ItemStack, bool a_AllowNewStacks = true, int a_PrioritarySlot = -1);
/** Same as AddItem, but works on an entire list of item stacks.
The a_ItemStackList is modified to reflect the leftover items.
If a_AllowNewStacks is set to false, only existing stacks can be topped up;
if a_AllowNewStacks is set to true, empty slots can be used for the rest
if a_AllowNewStacks is set to true, empty slots can be used for the rest.
If a_PrioritarySlot is set to a positive value, then the corresponding slot will be used in
first (if empty or compatible with added items)
if a_PrioritarySlot is set to -1, regular order apply
Returns the total number of items that fit.
*/
int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks = true);
int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks = true, int a_PrioritarySlot = -1);
/** Adds (or subtracts, if a_AddToCount is negative) to the count of items in the specified slot.
If the slot is empty, ignores the call.
@ -160,7 +166,7 @@ public:
void RemoveListener(cListener & a_Listener);
// tolua_begin
protected:
int m_Width;
int m_Height;
@ -173,6 +179,11 @@ protected:
/// Calls all m_Listeners for the specified slot number
void TriggerListeners(int a_SlotNum);
/** Adds up to a_Num items out of a_ItemStack, as many as can fit, in specified slot
Returns the number of items that did fit.
*/
int AddItemToSlot(const cItem & a_ItemStack, int a_Slot, int a_Num, int a_MaxStack);
} ;
// tolua_end

View File

@ -90,7 +90,7 @@ public:
// Give new bucket, filled with fluid:
cItem Item(NewItem, 1);
a_Player->GetInventory().AddItem(Item);
a_Player->GetInventory().AddItem(Item, true, true);
// Remove water / lava block
a_Player->GetWorld()->SetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_AIR, 0);
@ -136,7 +136,7 @@ public:
return false;
}
cItem Item(E_ITEM_BUCKET, 1);
if (!a_Player->GetInventory().AddItem(Item))
if (!a_Player->GetInventory().AddItem(Item,true,true))
{
return false;
}
@ -157,4 +157,4 @@ public:
return true;
}
};
};