From 9749c3aac9dbfbc46a919193c97bb9c9e5339e03 Mon Sep 17 00:00:00 2001 From: Lukas Pioch Date: Thu, 24 Sep 2015 10:48:33 +0200 Subject: Implemented brewing --- src/UI/BrewingstandWindow.cpp | 70 +++++++++++++ src/UI/BrewingstandWindow.h | 31 ++++++ src/UI/CMakeLists.txt | 2 + src/UI/SlotArea.cpp | 238 ++++++++++++++++++++++++++++++++++++++++++ src/UI/SlotArea.h | 30 ++++++ src/UI/Window.h | 1 + 6 files changed, 372 insertions(+) create mode 100644 src/UI/BrewingstandWindow.cpp create mode 100644 src/UI/BrewingstandWindow.h (limited to 'src/UI') diff --git a/src/UI/BrewingstandWindow.cpp b/src/UI/BrewingstandWindow.cpp new file mode 100644 index 000000000..2f1f3c97d --- /dev/null +++ b/src/UI/BrewingstandWindow.cpp @@ -0,0 +1,70 @@ + +// BrewingstandWindow.cpp + +// Representing the UI window for the brewing stand block + +#include "Globals.h" +#include "BrewingstandWindow.h" +#include "SlotArea.h" +#include "../BrewingRecipes.h" +#include "../Root.h" + + + + + +cBrewingstandWindow::cBrewingstandWindow(int a_BlockX, int a_BlockY, int a_BlockZ, cBrewingstandEntity * a_Brewingstand) : + cWindow(wtBrewery, "Brewingstand") +{ + m_SlotAreas.push_back(new cSlotAreaBrewingstand(a_Brewingstand, *this)); + m_SlotAreas.push_back(new cSlotAreaInventory(*this)); + m_SlotAreas.push_back(new cSlotAreaHotBar(*this)); +} + + + + + +void cBrewingstandWindow::DistributeStack(cItem & a_ItemStack, int a_Slot, cPlayer & a_Player, cSlotArea * a_ClickedArea, bool a_ShouldApply) +{ + cSlotAreas AreasInOrder; + + if (a_ClickedArea == m_SlotAreas[0]) + { + // Brewing stand Area + if ((a_Slot >= 0) && (a_Slot < 3)) + { + // Bottle slots + AreasInOrder.push_back(m_SlotAreas[2]); /* Hotbar */ + AreasInOrder.push_back(m_SlotAreas[1]); /* Inventory */ + super::DistributeStackToAreas(a_ItemStack, a_Player, AreasInOrder, a_ShouldApply, true); + } + else + { + // Ingredient slot + AreasInOrder.push_back(m_SlotAreas[1]); /* Inventory */ + AreasInOrder.push_back(m_SlotAreas[2]); /* Hotbar */ + super::DistributeStackToAreas(a_ItemStack, a_Player, AreasInOrder, a_ShouldApply, false); + } + } + else + { + cBrewingRecipes * BR = cRoot::Get()->GetBrewingRecipes(); + if ((BR->IsBottle(a_ItemStack)) || (BR->IsIngredient(a_ItemStack))) + { + AreasInOrder.push_back(m_SlotAreas[0]); /* brewing stand Area */ + } + else if (a_ClickedArea == m_SlotAreas[1]) + { + // Inventory Area + AreasInOrder.push_back(m_SlotAreas[2]); /* Hotbar */ + } + else + { + // Hotbar Area + AreasInOrder.push_back(m_SlotAreas[1]); /* Inventory */ + } + + super::DistributeStackToAreas(a_ItemStack, a_Player, AreasInOrder, a_ShouldApply, false); + } +} diff --git a/src/UI/BrewingstandWindow.h b/src/UI/BrewingstandWindow.h new file mode 100644 index 000000000..e55752187 --- /dev/null +++ b/src/UI/BrewingstandWindow.h @@ -0,0 +1,31 @@ + +// BrewingstandWindow.h + +// Representing the UI window for the brewing stand block + + + + + +#pragma once + +#include "Window.h" + + + + + +class cBrewingstandWindow : + public cWindow +{ + typedef cWindow super; + +public: + cBrewingstandWindow(int a_BlockX, int a_BlockY, int a_BlockZ, cBrewingstandEntity * a_Brewingstand); + + virtual void DistributeStack(cItem & a_ItemStack, int a_Slot, cPlayer & a_Player, cSlotArea * a_ClickedArea, bool a_ShouldApply) override; +}; + + + + diff --git a/src/UI/CMakeLists.txt b/src/UI/CMakeLists.txt index b907919c8..ad3c00f5f 100644 --- a/src/UI/CMakeLists.txt +++ b/src/UI/CMakeLists.txt @@ -9,6 +9,7 @@ SET (SRCS Window.cpp AnvilWindow.cpp BeaconWindow.cpp + BrewingstandWindow.cpp ChestWindow.cpp CraftingWindow.cpp DropSpenserWindow.cpp @@ -23,6 +24,7 @@ SET (HDRS Window.h AnvilWindow.h BeaconWindow.h + BrewingstandWindow.h ChestWindow.h CraftingWindow.h DropSpenserWindow.h diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index 478c004fb..2c0dfbe40 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -7,6 +7,7 @@ #include "SlotArea.h" #include "../Entities/Player.h" #include "../BlockEntities/BeaconEntity.h" +#include "../BlockEntities/BrewingstandEntity.h" #include "../BlockEntities/ChestEntity.h" #include "../BlockEntities/DropSpenserEntity.h" #include "../BlockEntities/EnderChestEntity.h" @@ -1934,6 +1935,242 @@ void cSlotAreaFurnace::HandleSmeltItem(const cItem & a_Result, cPlayer & a_Playe +//////////////////////////////////////////////////////////////////////////////// +// cSlotAreaBrewingstand: +cSlotAreaBrewingstand::cSlotAreaBrewingstand(cBrewingstandEntity * a_Brewingstand, cWindow & a_ParentWindow) : + cSlotArea(4, a_ParentWindow), + m_Brewingstand(a_Brewingstand) +{ + m_Brewingstand->GetContents().AddListener(*this); +} + + + + + +cSlotAreaBrewingstand::~cSlotAreaBrewingstand() +{ + m_Brewingstand->GetContents().RemoveListener(*this); +} + + + + + +void cSlotAreaBrewingstand::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) +{ + if (m_Brewingstand == nullptr) + { + LOGERROR("cSlotAreaBrewingstand::Clicked(): m_Brewingstand == nullptr"); + ASSERT(!"cSlotAreaBrewingstand::Clicked(): m_Brewingstand == nullptr"); + return; + } + + if ((a_SlotNum >= 0) && (a_SlotNum < 3)) + { + bool bAsync = false; + if (GetSlot(a_SlotNum, a_Player) == nullptr) + { + LOGWARNING("GetSlot(%d) returned nullptr! Ignoring click", a_SlotNum); + return; + } + + cItem Slot(*GetSlot(a_SlotNum, a_Player)); + if (!Slot.IsSameType(a_ClickedItem)) + { + LOGWARNING("*** Window lost sync at item %d in SlotArea with %d items ***", a_SlotNum, m_NumSlots); + LOGWARNING("My item: %s", ItemToFullString(Slot).c_str()); + LOGWARNING("Their item: %s", ItemToFullString(a_ClickedItem).c_str()); + bAsync = true; + } + + switch (a_ClickAction) + { + case caShiftLeftClick: + case caShiftRightClick: + { + HandleBrewedItem(a_Player); + ShiftClicked(a_Player, a_SlotNum, Slot); + return; + } + case caMiddleClick: + { + MiddleClicked(a_Player, a_SlotNum); + return; + } + case caDropKey: + case caCtrlDropKey: + { + DropClicked(a_Player, a_SlotNum, (a_SlotNum == caCtrlDropKey)); + Slot.m_ItemCount = Slot.m_ItemCount - GetSlot(a_SlotNum, a_Player)->m_ItemCount; + HandleBrewedItem(a_Player); + return; + } + default: + { + break; + } + } + + cItem & DraggingItem = a_Player.GetDraggingItem(); + if (!DraggingItem.IsEmpty()) + { + super::Clicked(a_Player, a_SlotNum, a_ClickAction, a_ClickedItem); + return; + } + else + { + switch (a_ClickAction) + { + case caDblClick: + { + DblClicked(a_Player, a_SlotNum); + return; + } + case caLeftClick: + { + DraggingItem = Slot; + HandleBrewedItem(a_Player); + Slot.Empty(); + break; + } + case caRightClick: + { + DraggingItem = Slot.CopyOne(); + DraggingItem.m_ItemCount = static_cast(static_cast(Slot.m_ItemCount) / 2.f + 0.5f); + Slot.m_ItemCount -= DraggingItem.m_ItemCount; + + if (Slot.m_ItemCount <= 0) + { + Slot.Empty(); + } + HandleBrewedItem(a_Player); + break; + } + default: + { + ASSERT(!"Unhandled click type!"); + } + } + } + + SetSlot(a_SlotNum, a_Player, Slot); + if (bAsync) + { + m_ParentWindow.BroadcastWholeWindow(); + } + return; + } + + super::Clicked(a_Player, a_SlotNum, a_ClickAction, a_ClickedItem); +} + + + + + +void cSlotAreaBrewingstand::HandleBrewedItem(cPlayer & a_Player) +{ + a_Player.AwardAchievement(achBrewPotion); +} + + + + + +void cSlotAreaBrewingstand::DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots, bool a_BackFill) +{ + int SlotNum = -1; + cBrewingRecipes * BR = cRoot::Get()->GetBrewingRecipes(); + if (BR->IsBottle(a_ItemStack)) + { + for (int i = 0;i < 3;i++) + { + if (GetSlot(i, a_Player)->IsEmpty()) + { + SlotNum = i; + break; + } + } + + if (SlotNum == -1) + { + // All slots are full + return; + } + } + else if (BR->IsIngredient(a_ItemStack)) + { + SlotNum = 3; + } + else + { + return; + } + + const cItem * Slot = GetSlot(SlotNum, a_Player); + if (!Slot->IsEqual(a_ItemStack) && (!Slot->IsEmpty() || a_KeepEmptySlots)) + { + // Different items + return; + } + + char NumFit = ItemHandler(Slot->m_ItemType)->GetMaxStackSize() - Slot->m_ItemCount; + if (NumFit <= 0) + { + // Full stack already + return; + } + NumFit = std::min(NumFit, a_ItemStack.m_ItemCount); + + if (a_ShouldApply) + { + cItem NewSlot(a_ItemStack); + NewSlot.m_ItemCount = Slot->m_ItemCount + NumFit; + SetSlot(SlotNum, a_Player, NewSlot); + } + a_ItemStack.m_ItemCount -= NumFit; + if (a_ItemStack.IsEmpty()) + { + return; + } +} + + + + + +const cItem * cSlotAreaBrewingstand::GetSlot(int a_SlotNum, cPlayer & a_Player) const +{ + UNUSED(a_Player); + // a_SlotNum ranges from 0 to 3, query the items from the underlying brewing stand: + return &(m_Brewingstand->GetSlot(a_SlotNum)); +} + + + + + +void cSlotAreaBrewingstand::SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) +{ + UNUSED(a_Player); + m_Brewingstand->SetSlot(a_SlotNum, a_Item); +} + + + + + +void cSlotAreaBrewingstand::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) +{ + UNUSED(a_SlotNum); + // Something has changed in the window, broadcast the entire window to all clients + ASSERT(a_ItemGrid == &(m_Brewingstand->GetContents())); + + m_ParentWindow.BroadcastWholeWindow(); +} + + //////////////////////////////////////////////////////////////////////////////// // cSlotAreaMinecartWithChest: @@ -2342,3 +2579,4 @@ cItem * cSlotAreaTemporary::GetPlayerSlots(cPlayer & a_Player) + diff --git a/src/UI/SlotArea.h b/src/UI/SlotArea.h index b150c47a1..0ff36ce50 100644 --- a/src/UI/SlotArea.h +++ b/src/UI/SlotArea.h @@ -16,6 +16,7 @@ class cWindow; class cPlayer; class cBeaconEntity; +class cBrewingstandEntity; class cChestEntity; class cEnderChestEntity; class cFurnaceEntity; @@ -456,6 +457,35 @@ protected: +class cSlotAreaBrewingstand : + public cSlotArea, + public cItemGrid::cListener +{ + typedef cSlotArea super; + +public: + cSlotAreaBrewingstand(cBrewingstandEntity * a_Brewingstand, cWindow & a_ParentWindow); + + virtual ~cSlotAreaBrewingstand(); + + virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override; + virtual void DistributeStack(cItem & a_ItemStack, cPlayer & a_Player, bool a_ShouldApply, bool a_KeepEmptySlots, bool a_BackFill) override; + virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) const override; + virtual void SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override; +protected: + cBrewingstandEntity * m_Brewingstand; + + // cItemGrid::cListener overrides: + virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) override; + + /** Called after an item has been brewed to handle statistics etc. */ + void HandleBrewedItem(cPlayer & a_Player); +} ; + + + + + class cSlotAreaMinecartWithChest : public cSlotArea { diff --git a/src/UI/Window.h b/src/UI/Window.h index cb8f40767..76d22a12c 100644 --- a/src/UI/Window.h +++ b/src/UI/Window.h @@ -18,6 +18,7 @@ class cPlayer; class cWindowOwner; class cClientHandle; +class cBrewingstandEntity; class cChestEntity; class cEnderChestEntity; class cFurnaceEntity; -- cgit v1.2.3