From cdc452916e3ec7e61f4a1ad822666192593b4b08 Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Thu, 2 Apr 2020 12:42:15 +0000 Subject: Replace buckets to the selected hotbar slot, rather than the first available. (#4580) * Replace buckets to the selected hotbar slot, rather than the first available. Replicates vanilla behaviour, as well as being more logical. * Refactor cInventory::AddItem. Behaviour is now documented * Add new cInventory::ReplaceOneEquippedItem and ::SetEquippedItem methods * Return empty potion to the same slot after drinking * Replace buckets correctly in other situations, not simply water and lava Uses the new ReplaceOneEquippedItem method * Correct collecting water from source block with bottle * Add cPlayer::ReplaceOneEquippedItemTossRest method * Handle stacked filled buckets (in theory) Use new cPlayer::ReplaceOneEquippedItemTossRest method --- src/Items/ItemArmor.h | 2 +- src/Items/ItemBottle.h | 7 +++++-- src/Items/ItemBucket.h | 32 ++++++-------------------------- src/Items/ItemPotion.h | 3 +-- 4 files changed, 13 insertions(+), 31 deletions(-) (limited to 'src/Items') diff --git a/src/Items/ItemArmor.h b/src/Items/ItemArmor.h index 906993a5b..d218c22b3 100644 --- a/src/Items/ItemArmor.h +++ b/src/Items/ItemArmor.h @@ -61,7 +61,7 @@ public: { Item.Empty(); } - a_Player->GetInventory().SetHotbarSlot(a_Player->GetInventory().GetEquippedSlotNum(), Item); + a_Player->GetInventory().SetEquippedItem(Item); return true; } diff --git a/src/Items/ItemBottle.h b/src/Items/ItemBottle.h index b261937e5..18767dcde 100644 --- a/src/Items/ItemBottle.h +++ b/src/Items/ItemBottle.h @@ -83,8 +83,11 @@ public: return false; // Nothing in range. } - a_Player->GetInventory().RemoveOneEquippedItem(); - a_Player->GetInventory().AddItem(cItem(E_ITEM_POTION)); + // Give back a filled water bottle if gamemode is not creative: + if (!a_Player->IsGameModeCreative()) + { + a_Player->ReplaceOneEquippedItemTossRest(cItem(E_ITEM_POTION)); + } return true; } } ; diff --git a/src/Items/ItemBucket.h b/src/Items/ItemBucket.h index 7a91149d1..8affff6ca 100644 --- a/src/Items/ItemBucket.h +++ b/src/Items/ItemBucket.h @@ -70,15 +70,15 @@ public: } BLOCKTYPE Block = a_World->GetBlock(BlockPos.x, BlockPos.y, BlockPos.z); - ENUM_ITEM_ID NewItem; + ENUM_ITEM_ID NewItemType; if (IsBlockWater(Block)) { - NewItem = E_ITEM_WATER_BUCKET; + NewItemType = E_ITEM_WATER_BUCKET; } else if (IsBlockLava(Block)) { - NewItem = E_ITEM_LAVA_BUCKET; + NewItemType = E_ITEM_LAVA_BUCKET; } else { @@ -101,18 +101,7 @@ public: // Give new bucket, filled with fluid when the gamemode is not creative: if (!a_Player->IsGameModeCreative()) { - // Remove the bucket from the inventory - if (!a_Player->GetInventory().RemoveOneEquippedItem()) - { - LOG("Clicked with an empty bucket, but cannot remove one from the inventory? WTF?"); - ASSERT(!"Inventory bucket mismatch"); - return true; - } - if (a_Player->GetInventory().AddItem(cItem(NewItem)) != 1) - { - // The bucket didn't fit, toss it as a pickup: - a_Player->TossPickup(cItem(NewItem)); - } + a_Player->ReplaceOneEquippedItemTossRest(cItem(NewItemType)); } return true; @@ -154,19 +143,10 @@ public: return false; } + // Give back an empty bucket if the gamemode is not creative: if (!a_Player->IsGameModeCreative()) { - // Remove fluid bucket, add empty bucket: - if (!a_Player->GetInventory().RemoveOneEquippedItem()) - { - LOG("Clicked with a full bucket, but cannot remove one from the inventory? WTF?"); - ASSERT(!"Inventory bucket mismatch"); - return false; - } - if (!a_Player->GetInventory().AddItem(cItem(E_ITEM_BUCKET))) - { - return false; - } + a_Player->ReplaceOneEquippedItemTossRest(cItem(E_ITEM_BUCKET)); } // Wash away anything that was there prior to placing: diff --git a/src/Items/ItemPotion.h b/src/Items/ItemPotion.h index 470a7c67f..86330d1c0 100644 --- a/src/Items/ItemPotion.h +++ b/src/Items/ItemPotion.h @@ -77,8 +77,7 @@ public: if (!a_Player->IsGameModeCreative()) { - a_Player->GetInventory().RemoveOneEquippedItem(); - a_Player->GetInventory().AddItem(cItem(E_ITEM_GLASS_BOTTLE)); + a_Player->ReplaceOneEquippedItemTossRest(cItem(E_ITEM_GLASS_BOTTLE)); } return true; } -- cgit v1.2.3