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 } ;
Enchantments.h
cItem::m_FireworkItem
cFireworkItem m_FireworkItem
Definition: Item.h:201
cItems::Clear
void Clear(void)
Definition: Item.h:237
cItem::IsSameType
bool IsSameType(const cItem &a_Item) const
Definition: Item.h:89
cItem::IsEnchantable
static bool IsEnchantable(short a_ItemType, bool a_FromBook=false)
Returns true if the specified item type is enchantable.
Definition: Item.cpp:322
cLootProbab::m_Item
cItem m_Item
Definition: Item.h:262
cItems::Size
size_t Size(void) const
Definition: Item.h:238
cItems::Add
void Add(const cItem &a_Item)
Definition: Item.h:233
cItem::FromJson
void FromJson(const Json::Value &a_Value)
Loads the item data from JSON representation.
Definition: Item.cpp:276
cItem::Clear
void Clear(void)
Empties the item and frees up any dynamic storage used by the internals.
Definition: Item.cpp:80
cItem::IsEqual
bool IsEqual(const cItem &a_Item) const
Definition: Item.h:76
cItem::AddCount
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
cItem::m_RepairCost
int m_RepairCost
Definition: Item.h:200
cItem::IsDamageable
bool IsDamageable(void) const
Definition: Item.h:116
cItem::CopyOne
cItem CopyOne(void) const
Returns a copy of this item with m_ItemCount set to 1.
Definition: Item.cpp:93
cItems::cItems
cItems(void)
Need a Lua-accessible constructor.
Definition: Item.h:229
Json
Definition: Inventory.h:10
cItem::m_Enchantments
cEnchantments m_Enchantments
Definition: Item.h:166
cItem::AddEnchantment
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
cItem::sItemCompare
Compares two items for the same type or category.
Definition: Item.h:182
cItems::operator=
cItems & operator=(const cItems &)=default
Color.h
FireworksSerializer.h
cItem::IsLoreEmpty
bool IsLoreEmpty(void) const
Definition: Item.h:102
cItem::GetEnchantability
unsigned GetEnchantability()
Returns the enchantability of the item.
Definition: Item.cpp:360
cFireworkItem::IsEqualTo
bool IsEqualTo(const cFireworkItem &a_Item) const
Definition: FireworksSerializer.h:57
cLootProbab
Used to store loot probability tables.
Definition: Item.h:259
cItem::GetHandler
const cItemHandler & GetHandler(void) const
Returns the cItemHandler responsible for this item type.
Definition: Item.cpp:216
cItems::Set
void Set(int a_Idx, const cItem &a_Item)
Definition: Item.cpp:729
cItem
Definition: Item.h:36
cFireworkItem
Definition: FireworksSerializer.h:26
cItem::m_ItemCount
char m_ItemCount
Definition: Item.h:164
cItem::m_CustomName
AString m_CustomName
Definition: Item.h:167
cLootProbab::m_MinAmount
int m_MinAmount
Definition: Item.h:263
cItem::m_ItemDamage
short m_ItemDamage
Definition: Item.h:165
cItem::IsCustomNameEmpty
bool IsCustomNameEmpty(void) const
Definition: Item.h:101
cItems::Get
cItem * Get(int a_Idx)
Definition: Item.cpp:715
cItem::GetMaxDamage
short GetMaxDamage(void) const
Returns the maximum damage value that this item can have; zero if damage is not applied.
Definition: Item.cpp:118
cItems::AddItemGrid
void AddItemGrid(const cItemGrid &a_ItemGrid)
Adds a copy of all items in a_ItemGrid.
Definition: Item.cpp:803
cItem::Empty
void Empty(void)
Empties the item and frees up any dynamic storage used by the internals.
Definition: Item.cpp:63
cItems
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:213
cItem::IsEmpty
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
Defines.h
cItem::AddEnchantmentsFromItem
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
cItem::CanHaveEnchantment
bool CanHaveEnchantment(int a_EnchantmentID)
Returns whether or not this item is allowed to have the given enchantment.
Definition: Item.cpp:544
cItem::DamageItem
bool DamageItem(short a_Amount=1)
Damages a weapon / tool.
Definition: Item.cpp:181
cItems::Delete
void Delete(int a_Idx)
Definition: Item.cpp:743
cItem::IsFullStack
bool IsFullStack(void) const
Returns true if the item is stacked up to its maximum stacking.
Definition: Item.cpp:198
cItems::Add
void Add(short a_ItemType, char a_ItemCount)
Definition: Item.h:235
cItem::m_ItemColor
cColor m_ItemColor
Definition: Item.h:202
cEnchantments
Class that stores item enchantments or stored-enchantments The enchantments may be serialized to a st...
Definition: Enchantments.h:41
cItem::GetMaxStackSize
char GetMaxStackSize(void) const
Returns the maximum amount of stacked items of this type.
Definition: Item.cpp:207
cItemGrid
Definition: ItemGrid.h:19
cItem::cItem
cItem(void)
Creates an empty item.
Definition: Item.cpp:16
cItems::Contains
bool Contains(const cItem &a_Item)
Definition: Item.cpp:771
cLootProbab::m_MaxAmount
int m_MaxAmount
Definition: Item.h:264
cItem::m_ItemType
short m_ItemType
Definition: Item.h:163
cItems::Add
void Add(short a_ItemType)
Definition: Item.h:234
cItem::IsBothNameAndLoreEmpty
bool IsBothNameAndLoreEmpty(void) const
Definition: Item.h:95
cLootProbab::m_Weight
int m_Weight
Definition: Item.h:265
cItem::GetJson
void GetJson(Json::Value &a_OutValue) const
Saves the item data into JSON representation.
Definition: Item.cpp:225
cItem::m_LoreTable
AStringVector m_LoreTable
Definition: Item.h:171
cItemHandler
Definition: ItemHandler.h:21
cColor
Definition: Color.h:13
cItem::EnchantByXPLevels
bool EnchantByXPLevels(unsigned a_NumXPLevels, MTRand &a_Random)
Randomly enchants the item using the specified number of XP levels.
Definition: Item.cpp:431
AString
std::string AString
Definition: StringUtils.h:11
cRandomWrapper
Class to wrap any random engine to provide a more convenient interface.
Definition: FastRandom.h:57
cItems::ContainsType
bool ContainsType(const cItem &a_Item)
Definition: Item.cpp:787
cItem::sItemCompare::operator()
bool operator()(const cItem &a_Lhs, const cItem &a_Rhs) const
Definition: Item.h:184
cItems::Add
void Add(short a_ItemType, char a_ItemCount, short a_ItemDamage)
Definition: Item.h:243
AStringVector
std::vector< AString > AStringVector
Definition: StringUtils.h:12