Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemGrid.h
Go to the documentation of this file.
1 
2 // ItemGrid.h
3 
4 // Declares the cItemGrid class representing a storage for items in a XY grid (chests, dispensers, inventory etc.)
5 
6 
7 
8 
9 #pragma once
10 
11 #include "Item.h"
12 #include "LazyArray.h"
13 
14 
15 
16 
17 
18 // tolua_begin
19 class cItemGrid
20 {
21 public:
22  // tolua_end
23 
25  class cListener
26  {
27  public:
28  virtual ~cListener() {}
29 
31  virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) = 0;
32  } ;
33  typedef std::vector<cListener *> cListeners;
34 
35  cItemGrid(int a_Width, int a_Height);
36 
37  // tolua_begin
38  int GetWidth (void) const { return m_Width; }
39  int GetHeight (void) const { return m_Height; }
40  int GetNumSlots(void) const { return m_Slots.size(); }
41 
43  int GetSlotNum(int a_X, int a_Y) const;
44 
45  // tolua_end
46 
48  void GetSlotCoords(int a_SlotNum, int & a_X, int & a_Y) const;
49 
52  void CopyFrom(const cItemGrid & a_Src);
53 
54  // tolua_begin
55 
56  // Retrieve slots by coords or slot number; Logs warning and returns the first slot on invalid coords / slotnum
57  const cItem & GetSlot(int a_X, int a_Y) const;
58  const cItem & GetSlot(int a_SlotNum) const;
59 
60  // Set slot by coords or slot number; Logs warning and doesn't set on invalid coords / slotnum
61  void SetSlot(int a_X, int a_Y, const cItem & a_Item);
62  void SetSlot(int a_X, int a_Y, short a_ItemType, char a_ItemCount, short a_ItemDamage);
63  void SetSlot(int a_SlotNum, const cItem & a_Item);
64  void SetSlot(int a_SlotNum, short a_ItemType, char a_ItemCount, short a_ItemDamage);
65 
66  // Empty the specified slot; Logs warning and doesn't set on invalid coords / slotnum
67  void EmptySlot(int a_X, int a_Y);
68  void EmptySlot(int a_SlotNum);
69 
71  bool IsSlotEmpty(int a_SlotNum) const;
72 
74  bool IsSlotEmpty(int a_X, int a_Y) const;
75 
77  void Clear(void);
78 
80  int HowManyCanFit(const cItem & a_ItemStack, bool a_AllowNewStacks = true);
81 
89  char AddItem(cItem & a_ItemStack, bool a_AllowNewStacks = true, int a_PrioritySlot = -1);
90 
99  char AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks = true, int a_PrioritySlot = -1);
100 
103  char RemoveItem(const cItem & a_ItemStack);
104 
106  cItem * FindItem(const cItem & a_RecipeItem);
107 
112  char ChangeSlotCount(int a_SlotNum, char a_AddToCount);
113 
118  char ChangeSlotCount(int a_X, int a_Y, char a_AddToCount);
119 
123  cItem RemoveOneItem(int a_SlotNum);
124 
128  cItem RemoveOneItem(int a_X, int a_Y);
129 
131  int HowManyItems(const cItem & a_Item);
132 
134  bool HasItems(const cItem & a_ItemStack);
135 
137  int GetFirstEmptySlot(void) const;
138 
140  int GetFirstUsedSlot(void) const;
141 
143  int GetLastEmptySlot(void) const;
144 
146  int GetLastUsedSlot(void) const;
147 
149  int GetNextEmptySlot(int a_StartFrom) const;
150 
152  int GetNextUsedSlot(int a_StartFrom) const;
153 
155  void CopyToItems(cItems & a_Items) const;
156 
158  bool DamageItem(int a_SlotNum, short a_Amount);
159 
161  bool DamageItem(int a_X, int a_Y, short a_Amount);
162 
163  // tolua_end
164 
165 
167  bool IsValidSlotCoords(int a_X, int a_Y) const;
168 
170  bool IsValidSlotNum(int a_SlotNum) const;
171 
176  void GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, size_t a_CountLootProbabs, int a_NumSlots, int a_Seed);
177 
179  void AddListener(cListener & a_Listener);
180 
182  void RemoveListener(cListener & a_Listener);
183 
184  // tolua_begin
185 
186 protected:
187  int m_Width;
188  int m_Height;
190 
194 
196  void TriggerListeners(int a_SlotNum);
197 
201  char AddItemToSlot(const cItem & a_ItemStack, int a_Slot, int a_Num, int a_MaxStack);
202 } ;
203 // tolua_end
Definition: Item.h:37
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
Used to store loot probability tables.
Definition: Item.h:260
int GetFirstEmptySlot(void) const
Returns the index of the first empty slot; -1 if all full.
Definition: ItemGrid.cpp:595
void GetSlotCoords(int a_SlotNum, int &a_X, int &a_Y) const
Converts slot number into XY coords; sets coords to -1 on invalid slot number.
Definition: ItemGrid.cpp:64
char AddItems(cItems &a_ItemStackList, bool a_AllowNewStacks=true, int a_PrioritySlot=-1)
Same as AddItem, but works on an entire list of item stacks.
Definition: ItemGrid.cpp:381
bool IsSlotEmpty(int a_SlotNum) const
Returns true if the specified slot is empty or the slot doesn't exist.
Definition: ItemGrid.cpp:203
bool IsValidSlotCoords(int a_X, int a_Y) const
Returns true if slot coordinates lie within the grid.
Definition: ItemGrid.cpp:36
void CopyFrom(const cItemGrid &a_Src)
Copies all items from a_Src to this grid.
Definition: ItemGrid.cpp:83
char ChangeSlotCount(int a_SlotNum, char a_AddToCount)
Adds (or subtracts, if a_AddToCount is negative) to the count of items in the specified slot.
Definition: ItemGrid.cpp:468
cItemGrid(int a_Width, int a_Height)
Definition: ItemGrid.cpp:15
cCriticalSection m_CSListeners
CS that guards the m_Listeners against multi-thread access.
Definition: ItemGrid.h:192
void TriggerListeners(int a_SlotNum)
Calls all m_Listeners for the specified slot number.
Definition: ItemGrid.cpp:838
int GetLastEmptySlot(void) const
Returns the index of the last empty slot; -1 if all full.
Definition: ItemGrid.cpp:613
void SetSlot(int a_X, int a_Y, const cItem &a_Item)
Definition: ItemGrid.cpp:121
int GetFirstUsedSlot(void) const
Returns the index of the first non-empty slot; -1 if all empty.
Definition: ItemGrid.cpp:604
std::vector< cListener * > cListeners
Definition: ItemGrid.h:33
int GetLastUsedSlot(void) const
Returns the index of the last used slot; -1 if all empty.
Definition: ItemGrid.cpp:629
char AddItemToSlot(const cItem &a_ItemStack, int a_Slot, int a_Num, int a_MaxStack)
Adds up to a_Num items out of a_ItemStack, as many as can fit, in specified slot Returns the number o...
Definition: ItemGrid.cpp:283
cLazyArray< cItem > m_Slots
Definition: ItemGrid.h:189
cItem * FindItem(const cItem &a_RecipeItem)
Finds an item based on ItemType and ItemDamage (<- defines the itemType, too)
Definition: ItemGrid.cpp:443
const cItem & GetSlot(int a_X, int a_Y) const
Definition: ItemGrid.cpp:96
int GetNextUsedSlot(int a_StartFrom) const
Returns the index of the first used slot following a_StartFrom (a_StartFrom is not checked)
Definition: ItemGrid.cpp:674
void Clear(void)
Sets all items as empty.
Definition: ItemGrid.cpp:228
void EmptySlot(int a_X, int a_Y)
Definition: ItemGrid.cpp:169
bool HasItems(const cItem &a_ItemStack)
Returns true if there are at least as many items of type a_ItemStack as in a_ItemStack.
Definition: ItemGrid.cpp:585
void GenerateRandomLootWithBooks(const cLootProbab *a_LootProbabs, size_t a_CountLootProbabs, int a_NumSlots, int a_Seed)
Generates random loot from the specified loot probability table, with a chance of enchanted books add...
Definition: ItemGrid.cpp:752
int m_Width
Definition: ItemGrid.h:187
int GetNumSlots(void) const
Definition: ItemGrid.h:40
int GetNextEmptySlot(int a_StartFrom) const
Returns the index of the first empty slot following a_StartFrom (a_StartFrom is not checked)
Definition: ItemGrid.cpp:650
bool IsValidSlotNum(int a_SlotNum) const
Returns true if slot number is within the grid.
Definition: ItemGrid.cpp:27
bool m_IsInTriggerListeners
Set to true while TriggerListeners is running, to detect attempts to manipulate listener list while t...
Definition: ItemGrid.h:193
cListeners m_Listeners
Listeners which should be notified on slot changes; the pointers are not owned by this object.
Definition: ItemGrid.h:191
void AddListener(cListener &a_Listener)
Adds a callback that gets called whenever a slot changes.
Definition: ItemGrid.cpp:809
int GetSlotNum(int a_X, int a_Y) const
Converts XY coords into slot number; returns -1 on invalid coords.
Definition: ItemGrid.cpp:48
int HowManyItems(const cItem &a_Item)
Returns the number of items of type a_Item that are stored.
Definition: ItemGrid.cpp:563
int GetWidth(void) const
Definition: ItemGrid.h:38
int HowManyCanFit(const cItem &a_ItemStack, bool a_AllowNewStacks=true)
Returns number of items out of a_ItemStack that can fit in the storage.
Definition: ItemGrid.cpp:246
char RemoveItem(const cItem &a_ItemStack)
Removes the specified item from the grid, as many as possible, up to a_ItemStack.m_ItemCount.
Definition: ItemGrid.cpp:405
void RemoveListener(cListener &a_Listener)
Removes a slot-change-callback.
Definition: ItemGrid.cpp:820
bool DamageItem(int a_SlotNum, short a_Amount)
Adds the specified damage to the specified item; returns true if the item broke (but the item is left...
Definition: ItemGrid.cpp:723
cItem RemoveOneItem(int a_SlotNum)
Removes one item from the stack in the specified slot, and returns it.
Definition: ItemGrid.cpp:516
char AddItem(cItem &a_ItemStack, bool a_AllowNewStacks=true, int a_PrioritySlot=-1)
Adds as many items out of a_ItemStack as can fit.
Definition: ItemGrid.cpp:313
void CopyToItems(cItems &a_Items) const
Copies the contents into a cItems object; preserves the original a_Items contents.
Definition: ItemGrid.cpp:703
int m_Height
Definition: ItemGrid.h:188
int GetHeight(void) const
Definition: ItemGrid.h:39
This class is used as a callback for when a slot changes.
Definition: ItemGrid.h:26
virtual ~cListener()
Definition: ItemGrid.h:28
virtual void OnSlotChanged(cItemGrid *a_ItemGrid, int a_SlotNum)=0
Called whenever a slot changes.
size_type size() const noexcept
Definition: LazyArray.h:80