Cuberite
A lightweight, fast and extensible game server for Minecraft
Window.h
Go to the documentation of this file.
1 
2 // Window.h
3 
4 // Interfaces to the cWindow class representing a UI window for a specific block
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include "../FunctionRef.h"
13 #include "../ItemGrid.h"
14 
15 
16 
17 
18 
19 class cPlayer;
20 class cWindowOwner;
21 class cClientHandle;
23 class cChestEntity;
24 class cEnderChestEntity;
25 class cFurnaceEntity;
26 class cHopperEntity;
27 class cMinecartWithChest;
28 class cBeaconEntity;
29 class cSlotArea;
30 class cSlotAreaAnvil;
31 class cWorld;
32 
33 typedef std::list<cPlayer *> cPlayerList;
34 typedef std::vector<cSlotArea *> cSlotAreas;
37 
38 
39 
40 
41 // tolua_begin
42 
53 class cWindow
54 {
55 public:
57  {
58  wtInventory = -1, // This value is never actually sent to a client
59  wtChest = 0,
61  wtFurnace = 2,
62  wtDropSpenser = 3, // Dropper or Dispenser
64  wtBrewery = 5,
66  wtBeacon = 7,
67  wtAnvil = 8,
68  wtHopper = 9,
69  wtDropper = 10,
71  };
72 
73  // tolua_end
74 
75  static const int c_NumInventorySlots = 36;
76 
77  cWindow(WindowType a_WindowType, const AString & a_WindowTitle);
78  virtual ~cWindow();
79 
80  char GetWindowID(void) const { return m_WindowID; } // tolua_export
81  int GetWindowType(void) const { return m_WindowType; } // tolua_export
82 
84  const AString GetWindowTypeName(void) const; // tolua_export
85 
86  cWindowOwner * GetOwner(void) { return m_Owner; }
87  void SetOwner( cWindowOwner * a_Owner) { m_Owner = a_Owner; }
88 
90  int GetNumSlots(void) const;
91 
94 
95  // tolua_begin
96 
98  const cItem * GetSlot(cPlayer & a_Player, int a_SlotNum) const;
99 
101  void SetSlot(cPlayer & a_Player, int a_SlotNum, const cItem & a_Item);
102 
104  bool IsSlotInPlayerMainInventory(int a_SlotNum) const;
105 
107  bool IsSlotInPlayerHotbar(int a_SlotNum) const;
108 
110  bool IsSlotInPlayerInventory(int a_SlotNum) const;
111 
112  // tolua_end
113 
115  void GetSlots(cPlayer & a_Player, cItems & a_Slots) const;
116 
118  virtual void Clicked(
119  cPlayer & a_Player, int a_WindowID,
120  short a_SlotNum, eClickAction a_ClickAction,
121  const cItem & a_ClickedItem
122  );
123 
124  virtual void OpenedByPlayer(cPlayer & a_Player);
125 
127  virtual bool ClosedByPlayer(cPlayer & a_Player, bool a_CanRefuse);
128 
130  void BroadcastSlot(cSlotArea * a_Area, int a_LocalSlotNum);
131 
133  void SendWholeWindow(cClientHandle & a_Client);
134 
136  void BroadcastWholeWindow(void);
137 
138  // tolua_begin
139 
140  const AString & GetWindowTitle() const { return m_WindowTitle; }
141  void SetWindowTitle(const AString & a_WindowTitle) { m_WindowTitle = a_WindowTitle; }
142 
145  virtual void SetProperty(size_t a_Property, short a_Value);
146 
147  // tolua_end
148 
149  void OwnerDestroyed(void);
150 
152  bool ForEachPlayer(cPlayerListCallback a_Callback);
153 
155  bool ForEachClient(cClientHandleCallback a_Callback);
156 
160  virtual void DistributeStack(cItem & a_ItemStack, int a_Slot, cPlayer & a_Player, cSlotArea * a_ClickedArea, bool a_ShouldApply) = 0;
161 
166  void DistributeStackToAreas(cItem & a_ItemStack, cPlayer & a_Player, cSlotAreas & a_AreasInOrder, bool a_ShouldApply, bool a_BackFill);
167 
172  bool CollectItemsToHand(cItem & a_Dragging, cSlotArea & a_Area, cPlayer & a_Player, bool a_CollectFullStacks);
173 
175  void SendSlot(cPlayer & a_Player, cSlotArea * a_SlotArea, int a_RelativeSlotNum);
176 
177 protected:
178 
180 
184 
187 
189 
191 
193 
195  virtual void Destroy(void);
196 
200  cSlotArea * GetSlotArea(int a_GlobalSlotNum, int & a_LocalSlotNum);
201 
206  const cSlotArea * GetSlotArea(int a_GlobalSlotNum, int & a_LocalSlotNum) const;
207 
209  void OnPaintBegin(cPlayer & a_Player);
210 
212  void OnPaintProgress(cPlayer & a_Player, int a_SlotNum);
213 
215  void OnLeftPaintEnd(cPlayer & a_Player);
216 
218  void OnRightPaintEnd(cPlayer & a_Player);
219 
221  void OnMiddlePaintEnd(cPlayer & a_Player);
222 
225  char DistributeItemToSlots(cPlayer & a_Player, const cItem & a_Item, char a_NumToEachSlot, const cSlotNums & a_SlotNums, bool a_LimitItems = true);
226 } ; // tolua_export
eClickAction
Individual actions sent in the WindowClick packet.
Definition: Defines.h:82
std::vector< int > cSlotNums
List of slot numbers, used for inventory-painting.
Definition: Defines.h:9
unsigned char Byte
Definition: Globals.h:161
std::string AString
Definition: StringUtils.h:11
std::vector< cSlotArea * > cSlotAreas
Definition: Window.h:34
std::list< cPlayer * > cPlayerList
Definition: Window.h:31
Definition: Player.h:29
Definition: Item.h:37
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
Represents a UI window.
Definition: Window.h:54
virtual void OpenedByPlayer(cPlayer &a_Player)
Definition: Window.cpp:284
void OnPaintBegin(cPlayer &a_Player)
Prepares the internal structures for inventory painting from the specified player.
Definition: Window.cpp:612
virtual void SetProperty(size_t a_Property, short a_Value)
Updates a numerical property associated with the window.
Definition: Window.cpp:406
bool m_IsDestroyed
Definition: Window.h:188
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 SetSlot(cPlayer &a_Player, int a_SlotNum, const cItem &a_Item)
Sets the item to the specified slot for the specified player.
Definition: Window.cpp:121
void OnLeftPaintEnd(cPlayer &a_Player)
Processes the entire action stored in the internal structures for inventory painting; distributes as ...
Definition: Window.cpp:632
static Byte m_WindowIDCounter
Definition: Window.h:192
cWindowOwner * m_Owner
Definition: Window.h:190
void BroadcastWholeWindow(void)
Sends the contents of the whole window to all clients of this window.
Definition: Window.cpp:393
WindowType
Definition: Window.h:57
@ wtDropSpenser
Definition: Window.h:62
@ wtNPCTrade
Definition: Window.h:65
@ wtDropper
Definition: Window.h:69
@ wtWorkbench
Definition: Window.h:60
@ wtFurnace
Definition: Window.h:61
@ wtAnvil
Definition: Window.h:67
@ wtHopper
Definition: Window.h:68
@ wtBeacon
Definition: Window.h:66
@ wtAnimalChest
Definition: Window.h:70
@ wtInventory
Definition: Window.h:58
@ wtEnchantment
Definition: Window.h:63
@ wtBrewery
Definition: Window.h:64
@ wtChest
Definition: Window.h:59
char DistributeItemToSlots(cPlayer &a_Player, const cItem &a_Item, char a_NumToEachSlot, const cSlotNums &a_SlotNums, bool a_LimitItems=true)
Distributes a_NumToEachSlot items into the slots specified in a_SlotNums; returns the total number of...
Definition: Window.cpp:713
virtual void DistributeStack(cItem &a_ItemStack, int a_Slot, cPlayer &a_Player, cSlotArea *a_ClickedArea, bool a_ShouldApply)=0
Called on shift-clicking to distribute the stack into other areas; Modifies a_ItemStack as it is dist...
const cItem * GetSlot(cPlayer &a_Player, int a_SlotNum) const
Returns the item at the specified slot for the specified player.
Definition: Window.cpp:104
void GetSlots(cPlayer &a_Player, cItems &a_Slots) const
Fills a_Slots with the slots read from m_SlotAreas[], for the specified player.
Definition: Window.cpp:171
AString m_WindowTitle
Definition: Window.h:183
virtual ~cWindow()
Definition: Window.cpp:49
cPlayerList m_OpenedBy
Definition: Window.h:186
bool CollectItemsToHand(cItem &a_Dragging, cSlotArea &a_Area, cPlayer &a_Player, bool a_CollectFullStacks)
Called on DblClicking to collect all stackable items from all areas into hand.
Definition: Window.cpp:490
bool ForEachPlayer(cPlayerListCallback a_Callback)
Calls the callback safely for each player that has this window open; returns true if all players have...
Definition: Window.cpp:433
int GetNumNonInventorySlots(void) const
Returns the number of slots, excluding the player's inventory (used for network protocols)
Definition: Window.h:93
int GetNumSlots(void) const
Returns the total number of slots.
Definition: Window.cpp:90
void OnMiddlePaintEnd(cPlayer &a_Player)
Processes the entire action stored in the internal structures for inventory painting; distributes a f...
Definition: Window.cpp:689
cWindowOwner * GetOwner(void)
Definition: Window.h:86
bool IsSlotInPlayerHotbar(int a_SlotNum) const
Returns true if the specified slot is in the Player Hotbar slotarea.
Definition: Window.cpp:149
char m_WindowID
Definition: Window.h:181
int m_WindowType
Definition: Window.h:182
static const int c_NumInventorySlots
Definition: Window.h:75
bool ForEachClient(cClientHandleCallback a_Callback)
Calls the callback safely for each client that has this window open; returns true if all clients have...
Definition: Window.cpp:450
bool IsSlotInPlayerMainInventory(int a_SlotNum) const
Returns true if the specified slot is in the Player Main Inventory slotarea.
Definition: Window.cpp:138
void OwnerDestroyed(void)
Definition: Window.cpp:419
void SendSlot(cPlayer &a_Player, cSlotArea *a_SlotArea, int a_RelativeSlotNum)
Used by cSlotAreas to send individual slots to clients, a_RelativeSlotNum is the slot number relative...
Definition: Window.cpp:507
cSlotArea * GetSlotArea(int a_GlobalSlotNum, int &a_LocalSlotNum)
Returns the correct slot area for the specified window-global SlotNum Also returns the area-local Slo...
Definition: Window.cpp:550
virtual void Destroy(void)
Sets the internal flag as "destroyed"; notifies the owner that the window is destroying.
Definition: Window.cpp:536
char GetWindowID(void) const
Definition: Window.h:80
virtual void Clicked(cPlayer &a_Player, int a_WindowID, short a_SlotNum, eClickAction a_ClickAction, const cItem &a_ClickedItem)
Handles a click event from a player.
Definition: Window.cpp:197
void OnRightPaintEnd(cPlayer &a_Player)
Processes the entire action stored in the internal structures for inventory painting; distributes one...
Definition: Window.cpp:661
const AString & GetWindowTitle() const
Definition: Window.h:140
int GetWindowType(void) const
Definition: Window.h:81
void SendWholeWindow(cClientHandle &a_Client)
Sends the contents of the whole window to the specified client.
Definition: Window.cpp:384
void SetWindowTitle(const AString &a_WindowTitle)
Definition: Window.h:141
bool IsSlotInPlayerInventory(int a_SlotNum) const
Returns true if the specified slot is in the Player Main Inventory or Hotbar slotareas.
Definition: Window.cpp:160
cCriticalSection m_CS
Definition: Window.h:185
cWindow(WindowType a_WindowType, const AString &a_WindowTitle)
Definition: Window.cpp:30
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
const AString GetWindowTypeName(void) const
Returns the textual representation of the window's type, such as "minecraft:chest".
Definition: Window.cpp:62
void OnPaintProgress(cPlayer &a_Player, int a_SlotNum)
Adds the slot to the internal structures for inventory painting by the specified player.
Definition: Window.cpp:622
void BroadcastSlot(cSlotArea *a_Area, int a_LocalSlotNum)
Sends the specified slot's contents to all clients of this window; the slot is specified as local in ...
Definition: Window.cpp:350
cSlotAreas m_SlotAreas
Definition: Window.h:179
void SetOwner(cWindowOwner *a_Owner)
Definition: Window.h:87
Base class for the window owning.
Definition: WindowOwner.h:18
Definition: World.h:53