Cuberite
A lightweight, fast and extensible game server for Minecraft
ChestWindow.cpp
Go to the documentation of this file.
1 
2 // ChestWindow.cpp
3 
4 // Representing the UI window for the chest block
5 
6 #include "Globals.h"
7 #include "ChestWindow.h"
8 #include "../BlockEntities/ChestEntity.h"
9 #include "../Entities/Player.h"
10 #include "SlotArea.h"
11 
12 
13 
14 
15 
17  cWindow(wtChest, (a_Chest->GetBlockType() == E_BLOCK_CHEST) ? "Chest" : "Trapped Chest"),
18  m_World(a_Chest->GetWorld()),
19  m_BlockPos(a_Chest->GetPos()),
20  m_PrimaryChest(a_Chest),
21  m_SecondaryChest(nullptr)
22 {
23  m_SlotAreas.push_back(new cSlotAreaChest(a_Chest, *this));
24  m_SlotAreas.push_back(new cSlotAreaInventory(*this));
25  m_SlotAreas.push_back(new cSlotAreaHotBar(*this));
26 
27  // Play the opening sound:
28  m_World->BroadcastSoundEffect("block.chest.open", m_BlockPos, 1, 1);
29 
30  // Send out the chest-open packet:
32 }
33 
34 
35 
36 
37 
38 cChestWindow::cChestWindow(cChestEntity * a_PrimaryChest, cChestEntity * a_SecondaryChest) :
39  cWindow(wtChest, (a_PrimaryChest->GetBlockType() == E_BLOCK_CHEST) ? "Double Chest" : "Double Trapped Chest"),
40  m_World(a_PrimaryChest->GetWorld()),
41  m_BlockPos(a_PrimaryChest->GetPos()),
42  m_PrimaryChest(a_PrimaryChest),
43  m_SecondaryChest(a_SecondaryChest)
44 {
45  m_SlotAreas.push_back(new cSlotAreaDoubleChest(a_PrimaryChest, a_SecondaryChest, *this));
46  m_SlotAreas.push_back(new cSlotAreaInventory(*this));
47  m_SlotAreas.push_back(new cSlotAreaHotBar(*this));
48 
49  // Play the opening sound:
50  m_World->BroadcastSoundEffect("block.chest.open", m_BlockPos, 1, 1);
51 
52  // Send out the chest-open packet:
53  m_World->BroadcastBlockAction(m_BlockPos, 1, 1, a_PrimaryChest->GetBlockType());
54 }
55 
56 
57 
58 
59 
61 {
62  // Send out the chest-close packet:
64 
65  m_World->BroadcastSoundEffect("block.chest.close", m_BlockPos, 1, 1);
66 }
67 
68 
69 
70 
71 
72 bool cChestWindow::ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse)
73 {
76 
77  if (m_SecondaryChest != nullptr)
78  {
81  }
82 
83  cWindow::ClosedByPlayer(a_Player, a_CanRefuse);
84  return true;
85 }
86 
87 
88 
89 
90 
92 {
95 
96  if (m_SecondaryChest != nullptr)
97  {
100  }
101 
102  cWindow::OpenedByPlayer(a_Player);
103 }
104 
105 
106 
107 
108 
109 void cChestWindow::DistributeStack(cItem & a_ItemStack, int a_Slot, cPlayer & a_Player, cSlotArea * a_ClickedArea, bool a_ShouldApply)
110 {
111  cSlotAreas AreasInOrder;
112 
113  if (a_ClickedArea == m_SlotAreas[0])
114  {
115  // Chest Area
116  AreasInOrder.push_back(m_SlotAreas[2]); /* Hotbar */
117  AreasInOrder.push_back(m_SlotAreas[1]); /* Inventory */
118  Super::DistributeStackToAreas(a_ItemStack, a_Player, AreasInOrder, a_ShouldApply, true);
119  }
120  else
121  {
122  // Hotbar or Inventory
123  AreasInOrder.push_back(m_SlotAreas[0]); /* Chest */
124  Super::DistributeStackToAreas(a_ItemStack, a_Player, AreasInOrder, a_ShouldApply, false);
125  }
126 }
127 
128 
129 
130 
@ E_BLOCK_CHEST
Definition: BlockType.h:64
std::vector< cSlotArea * > cSlotAreas
Definition: Window.h:34
cWorld * GetWorld() const
Definition: BlockEntity.h:99
Vector3i GetPos() const
Definition: BlockEntity.h:90
BLOCKTYPE GetBlockType() const
Definition: BlockEntity.h:97
int GetNumberOfPlayers(void) const
Gets the number of players who currently have this chest open.
Definition: ChestEntity.h:40
void SetNumberOfPlayers(int a_NumActivePlayers)
Sets the number of players who currently have this chest open.
Definition: ChestEntity.h:53
Definition: Player.h:29
Definition: Item.h:37
virtual void OpenedByPlayer(cPlayer &a_Player) override
Definition: ChestWindow.cpp:91
virtual bool ClosedByPlayer(cPlayer &a_Player, bool a_CanRefuse) override
Called when a player closes this window; notifies all slot areas.
Definition: ChestWindow.cpp:72
cWorld * m_World
Definition: ChestWindow.h:38
Vector3i m_BlockPos
Definition: ChestWindow.h:39
cChestWindow(cChestEntity *a_Chest)
Definition: ChestWindow.cpp:16
cChestEntity * m_PrimaryChest
Definition: ChestWindow.h:40
cChestEntity * m_SecondaryChest
Definition: ChestWindow.h:41
virtual void DistributeStack(cItem &a_ItemStack, int a_Slot, cPlayer &a_Player, cSlotArea *a_ClickedArea, bool a_ShouldApply) override
Called on shift-clicking to distribute the stack into other areas; Modifies a_ItemStack as it is dist...
virtual ~cChestWindow() override
Definition: ChestWindow.cpp:60
Handles the main inventory of each player, excluding the armor and hotbar.
Definition: SlotArea.h:120
Handles the hotbar of each player.
Definition: SlotArea.h:138
Represents a UI window.
Definition: Window.h:54
virtual void OpenedByPlayer(cPlayer &a_Player)
Definition: Window.cpp:284
virtual bool ClosedByPlayer(cPlayer &a_Player, bool a_CanRefuse)
Called when a player closes this window; notifies all slot areas.
Definition: Window.cpp:306
void DistributeStackToAreas(cItem &a_ItemStack, cPlayer &a_Player, cSlotAreas &a_AreasInOrder, bool a_ShouldApply, bool a_BackFill)
Called from DistributeStack() to distribute the stack into a_AreasInOrder; Modifies a_ItemStack as it...
Definition: Window.cpp:467
cSlotAreas m_SlotAreas
Definition: Window.h:179
virtual void WakeUpSimulators(Vector3i a_Block) override
Wakes up the simulators for the specified block.
Definition: World.cpp:1357
virtual void BroadcastBlockAction(Vector3i a_BlockPos, Byte a_Byte1, Byte a_Byte2, BLOCKTYPE a_BlockType, const cClientHandle *a_Exclude=nullptr) override
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override