Cuberite
A lightweight, fast and extensible game server for Minecraft
Item.h
Go to the documentation of this file.
1 
2 // Item.h
3 
4 // Declares the cItem class representing an item (in the inventory sense)
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include "Defines.h"
13 #include "Enchantments.h"
15 #include "Color.h"
16 
17 
18 
19 
20 
21 // fwd:
22 class cItemHandler;
23 class cItemGrid;
24 class cColor;
25 
26 namespace Json
27 {
28  class Value;
29 }
30 
31 
32 
33 
34 
35 // tolua_begin
36 class cItem
37 {
38 public:
40  cItem(void);
41 
43  cItem(
44  short a_ItemType,
45  char a_ItemCount = 1,
46  short a_ItemDamage = 0,
47  const AString & a_Enchantments = "",
48  const AString & a_CustomName = "",
49  const AStringVector & a_LoreTable = {}
50  );
51 
52  // The constructor is disabled in code, because the compiler generates it anyway,
53  // but it needs to stay because ToLua needs to generate the binding for it
54  #ifdef TOLUA_EXPOSITION
55 
57  cItem(const cItem & a_CopyFrom);
58 
59  #endif
60 
62  void Empty(void);
63 
66  void Clear(void);
67 
69  bool IsEmpty(void) const
70  {
71  return ((m_ItemType <= 0) || (m_ItemCount <= 0));
72  }
73 
74  /* Returns true if this itemstack can stack with the specified stack (types match, enchantments etc.)
75  ItemCounts are ignored. */
76  bool IsEqual(const cItem & a_Item) const
77  {
78  return (
79  IsSameType(a_Item) &&
80  (m_ItemDamage == a_Item.m_ItemDamage) &&
81  (m_Enchantments == a_Item.m_Enchantments) &&
82  (m_CustomName == a_Item.m_CustomName) &&
83  (m_LoreTable == a_Item.m_LoreTable) &&
85  );
86  }
87 
88 
89  bool IsSameType(const cItem & a_Item) const
90  {
91  return (m_ItemType == a_Item.m_ItemType) || (IsEmpty() && a_Item.IsEmpty());
92  }
93 
94 
95  bool IsBothNameAndLoreEmpty(void) const
96  {
97  return (m_CustomName.empty() && m_LoreTable.empty());
98  }
99 
100 
101  bool IsCustomNameEmpty(void) const { return (m_CustomName.empty()); }
102  bool IsLoreEmpty(void) const { return (m_LoreTable.empty()); }
103 
105  cItem CopyOne(void) const;
106 
108  cItem & AddCount(char a_AmountToAdd);
109 
111  short GetMaxDamage(void) const;
112 
114  bool DamageItem(short a_Amount = 1);
115 
116  inline bool IsDamageable(void) const { return (GetMaxDamage() > 0); }
117 
119  bool IsFullStack(void) const;
120 
122  char GetMaxStackSize(void) const;
123 
124  // tolua_end
125 
127  const cItemHandler & GetHandler(void) const;
128 
130  void GetJson(Json::Value & a_OutValue) const;
131 
133  void FromJson(const Json::Value & a_Value);
134 
138  static bool IsEnchantable(short a_ItemType, bool a_FromBook = false); // tolua_export
139 
141  unsigned GetEnchantability(); // tolua_export
142 
146  bool EnchantByXPLevels(unsigned a_NumXPLevels, MTRand & a_Random); // Exported in ManualBindings.cpp
147 
152  int AddEnchantment(int a_EnchantmentID, unsigned int a_Level, bool a_FromBook); // tolua_export
153 
156  int AddEnchantmentsFromItem(const cItem & a_Other); // tolua_export
157 
159  bool CanHaveEnchantment(int a_EnchantmentID);
160 
161  // tolua_begin
162 
163  short m_ItemType;
168 
169  // tolua_end
170 
171  AStringVector m_LoreTable; // Exported in ManualBindings.cpp
172 
183  {
184  bool operator() (const cItem & a_Lhs, const cItem & a_Rhs) const
185  {
186  if (a_Lhs.m_ItemType != a_Rhs.m_ItemType)
187  {
188  return (a_Lhs.m_ItemType < a_Rhs.m_ItemType);
189  }
190  if ((a_Lhs.m_ItemDamage == -1) || (a_Rhs.m_ItemDamage == -1))
191  {
192  return false; // -1 is a wildcard, damage of -1 alway compares equal
193  }
194  return (a_Lhs.m_ItemDamage < a_Rhs.m_ItemDamage);
195  }
196  };
197 
198  // tolua_begin
199 
203 };
204 // tolua_end
205 
206 
207 
208 
209 
213 class cItems // tolua_export
214  : public std::vector<cItem>
215 { // tolua_export
216 public:
217 
218  cItems(const cItems &) = default;
219  cItems(cItems &&) = default;
220  cItems & operator = (const cItems &) = default;
221  cItems & operator = (cItems &&) = default;
222 
224  cItems(cItem && a_InitialItem);
225 
226  // tolua_begin
227 
229  cItems(void) {}
230 
231  cItem * Get (int a_Idx);
232  void Set (int a_Idx, const cItem & a_Item);
233  void Add (const cItem & a_Item) {push_back(a_Item); }
234  void Add (short a_ItemType) { emplace_back(a_ItemType); }
235  void Add (short a_ItemType, char a_ItemCount) { emplace_back(a_ItemType, a_ItemCount); }
236  void Delete(int a_Idx);
237  void Clear (void) {clear(); }
238  size_t Size (void) const { return size(); }
239  void Set (int a_Idx, short a_ItemType, char a_ItemCount, short a_ItemDamage);
240  bool Contains(const cItem & a_Item);
241  bool ContainsType(const cItem & a_Item);
242 
243  void Add (short a_ItemType, char a_ItemCount, short a_ItemDamage)
244  {
245  emplace_back(a_ItemType, a_ItemCount, a_ItemDamage);
246  }
247 
249  void AddItemGrid(const cItemGrid & a_ItemGrid);
250 
251  // tolua_end
252 } ; // tolua_export
253 
254 
255 
256 
257 
260 {
261 public:
265  int m_Weight;
266 } ;
std::vector< AString > AStringVector
Definition: StringUtils.h:12
std::string AString
Definition: StringUtils.h:11
Definition: Inventory.h:11
Definition: Color.h:14
Class that stores item enchantments or stored-enchantments The enchantments may be serialized to a st...
Definition: Enchantments.h:42
Class to wrap any random engine to provide a more convenient interface.
Definition: FastRandom.h:58
Definition: Item.h:37
void FromJson(const Json::Value &a_Value)
Loads the item data from JSON representation.
Definition: Item.cpp:276
cItem & AddCount(char a_AmountToAdd)
Adds the specified count to this object and returns the reference to self (useful for chaining)
Definition: Item.cpp:104
const cItemHandler & GetHandler(void) const
Returns the cItemHandler responsible for this item type.
Definition: Item.cpp:216
bool IsBothNameAndLoreEmpty(void) const
Definition: Item.h:95
bool CanHaveEnchantment(int a_EnchantmentID)
Returns whether or not this item is allowed to have the given enchantment.
Definition: Item.cpp:544
static bool IsEnchantable(short a_ItemType, bool a_FromBook=false)
Returns true if the specified item type is enchantable.
Definition: Item.cpp:322
cEnchantments m_Enchantments
Definition: Item.h:166
bool IsCustomNameEmpty(void) const
Definition: Item.h:101
bool EnchantByXPLevels(unsigned a_NumXPLevels, MTRand &a_Random)
Randomly enchants the item using the specified number of XP levels.
Definition: Item.cpp:431
char m_ItemCount
Definition: Item.h:164
bool DamageItem(short a_Amount=1)
Damages a weapon / tool.
Definition: Item.cpp:181
AString m_CustomName
Definition: Item.h:167
unsigned GetEnchantability()
Returns the enchantability of the item.
Definition: Item.cpp:360
void GetJson(Json::Value &a_OutValue) const
Saves the item data into JSON representation.
Definition: Item.cpp:225
char GetMaxStackSize(void) const
Returns the maximum amount of stacked items of this type.
Definition: Item.cpp:207
bool IsEmpty(void) const
Returns true if the item represents an empty stack - either the type is invalid, or count is zero.
Definition: Item.h:69
cColor m_ItemColor
Definition: Item.h:202
bool IsLoreEmpty(void) const
Definition: Item.h:102
bool IsEqual(const cItem &a_Item) const
Definition: Item.h:76
int AddEnchantment(int a_EnchantmentID, unsigned int a_Level, bool a_FromBook)
Adds this specific enchantment to this item, returning the cost.
Definition: Item.cpp:510
AStringVector m_LoreTable
Definition: Item.h:171
short m_ItemType
Definition: Item.h:163
void Clear(void)
Empties the item and frees up any dynamic storage used by the internals.
Definition: Item.cpp:80
int AddEnchantmentsFromItem(const cItem &a_Other)
Adds the enchantments on a_Other to this item, returning the XP cost of the transfer.
Definition: Item.cpp:675
int m_RepairCost
Definition: Item.h:200
void Empty(void)
Empties the item and frees up any dynamic storage used by the internals.
Definition: Item.cpp:63
bool IsDamageable(void) const
Definition: Item.h:116
short GetMaxDamage(void) const
Returns the maximum damage value that this item can have; zero if damage is not applied.
Definition: Item.cpp:118
bool IsFullStack(void) const
Returns true if the item is stacked up to its maximum stacking.
Definition: Item.cpp:198
cFireworkItem m_FireworkItem
Definition: Item.h:201
cItem CopyOne(void) const
Returns a copy of this item with m_ItemCount set to 1.
Definition: Item.cpp:93
cItem(void)
Creates an empty item.
Definition: Item.cpp:16
short m_ItemDamage
Definition: Item.h:165
bool IsSameType(const cItem &a_Item) const
Definition: Item.h:89
Compares two items for the same type or category.
Definition: Item.h:183
bool operator()(const cItem &a_Lhs, const cItem &a_Rhs) const
Definition: Item.h:184
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
cItems & operator=(const cItems &)=default
void Clear(void)
Definition: Item.h:237
cItems(cItems &&)=default
size_t Size(void) const
Definition: Item.h:238
bool Contains(const cItem &a_Item)
Definition: Item.cpp:771
cItems(void)
Need a Lua-accessible constructor.
Definition: Item.h:229
cItems(const cItems &)=default
void Delete(int a_Idx)
Definition: Item.cpp:743
cItem * Get(int a_Idx)
Definition: Item.cpp:715
void Add(short a_ItemType, char a_ItemCount)
Definition: Item.h:235
void Add(short a_ItemType)
Definition: Item.h:234
bool ContainsType(const cItem &a_Item)
Definition: Item.cpp:787
void AddItemGrid(const cItemGrid &a_ItemGrid)
Adds a copy of all items in a_ItemGrid.
Definition: Item.cpp:803
void Add(const cItem &a_Item)
Definition: Item.h:233
void Add(short a_ItemType, char a_ItemCount, short a_ItemDamage)
Definition: Item.h:243
void Set(int a_Idx, const cItem &a_Item)
Definition: Item.cpp:729
Used to store loot probability tables.
Definition: Item.h:260
int m_MinAmount
Definition: Item.h:263
cItem m_Item
Definition: Item.h:262
int m_Weight
Definition: Item.h:265
int m_MaxAmount
Definition: Item.h:264
bool IsEqualTo(const cFireworkItem &a_Item) const