Cuberite
A lightweight, fast and extensible game server for Minecraft
Enchantments.h
Go to the documentation of this file.
1 // Enchantments.h
2 
3 // Declares the cEnchantments class representing a storage for item enchantments and stored-enchantments
4 
5 
6 
7 
8 
9 #pragma once
10 
11 #include "Defines.h"
13 
14 
15 
16 
17 // fwd: "WorldStorage/FastNBT.h"
18 class cFastNBTWriter;
19 class cParsedNBT;
20 
21 
22 // fwd:
24 
25 typedef std::vector<cWeightedEnchantment> cWeightedEnchantments;
26 
27 
28 
29 
39 // tolua_begin
41 {
42 public:
47  {
48  // Currently missing: Frost walker, curse of binding, sweeping edge, mending, and curse of vanishing.
59  enchSmite = 17,
68  enchPower = 48,
69  enchPunch = 49,
70  enchFlame = 50,
73  enchLure = 62,
74  } ;
75 
77  cEnchantments(void);
78 
80  cEnchantments(const AString & a_StringSpec);
81 
84  void Add(const cEnchantments & a_Other);
85 
87  void AddFromString(const AString & a_StringSpec);
88 
90  size_t Count(void);
91 
93  AString ToString(void) const;
94 
96  unsigned int GetLevel(int a_EnchantmentID) const;
97 
99  void SetLevel(int a_EnchantmentID, unsigned int a_Level);
100 
102  void Clear(void);
103 
105  bool IsEmpty(void) const;
106 
108  bool CanAddEnchantment(int a_EnchantmentID) const;
109 
111  static int StringToEnchantmentID(const AString & a_EnchantmentName);
112 
114  bool operator ==(const cEnchantments & a_Other) const;
115 
116  // tolua_end
117 
122  static int GetXPCostMultiplier(int a_EnchantmentID, bool FromBook);
123 
125  static unsigned int GetLevelCap(int a_EnchantmentID);
126 
128  static void AddItemEnchantmentWeights(cWeightedEnchantments & a_Enchantments, short a_ItemType, int a_EnchantmentLevel);
129 
131  static void AddEnchantmentWeightToVector(cWeightedEnchantments & a_Enchantments, int a_Weight, int a_EnchantmentID, unsigned int a_EnchantmentLevel);
132 
134  static void RemoveEnchantmentWeightFromVector(cWeightedEnchantments & a_Enchantments, int a_EnchantmentID);
135 
137  static void RemoveEnchantmentWeightFromVector(cWeightedEnchantments & a_Enchantments, const cEnchantments & a_Enchantment);
138 
140  static void CheckEnchantmentConflictsFromVector(cWeightedEnchantments & a_Enchantments, cEnchantments a_FirstEnchantment);
141 
144 
148  static cEnchantments SelectEnchantmentFromVector(const cWeightedEnchantments & a_Enchantments, int a_Seed);
149 
151  bool operator !=(const cEnchantments & a_Other) const;
152 
154  friend void EnchantmentSerializer::WriteToNBTCompound(const cEnchantments & a_Enchantments, cFastNBTWriter & a_Writer, const AString & a_ListTagName);
155 
157  friend void EnchantmentSerializer::ParseFromNBT(cEnchantments & a_Enchantments, const cParsedNBT & a_NBT, int a_EnchListTagIdx);
158 
159 protected:
161  typedef std::map<int, unsigned int> cMap;
162 
165 
166 public:
168  cMap::const_iterator begin() const { return m_Enchantments.begin(); }
169  cMap::const_iterator end() const { return m_Enchantments.end(); }
170 }; // tolua_export
171 
172 
173 
174 
175 // Define the cWeightedEnchantment struct for the Enchanting System to store the EnchantmentWeights:
177 {
178  int m_Weight;
180 };
181 
182 
183 
void WriteToNBTCompound(const cEnchantments &a_Enchantments, cFastNBTWriter &a_Writer, const AString &a_ListTagName)
Writes the enchantments into the specified NBT writer; begins with the LIST tag of the specified name...
cEnchantments m_Enchantments
Definition: Enchantments.h:179
cEnchantments(void)
Creates an empty enchantments container.
cMap::const_iterator end() const
Definition: Enchantments.h:169
Parses and contains the parsed data Also implements data accessor functions for tree traversal and va...
Definition: FastNBT.h:152
static void RemoveEnchantmentWeightFromVector(cWeightedEnchantments &a_Enchantments, int a_EnchantmentID)
Remove the entire enchantment (with weight) from the vector.
void Clear(void)
Removes all enchantments.
eEnchantment
Individual enchantment IDs, corresponding to their NBT IDs: https://minecraft.gamepedia.com/Data_values#Enchantment_IDs.
Definition: Enchantments.h:46
unsigned int GetLevel(int a_EnchantmentID) const
Returns the level for the specified enchantment; 0 if not stored.
cMap m_Enchantments
Currently stored enchantments.
Definition: Enchantments.h:164
static void AddItemEnchantmentWeights(cWeightedEnchantments &a_Enchantments, short a_ItemType, int a_EnchantmentLevel)
Add enchantment weights from item to the vector.
void AddFromString(const AString &a_StringSpec)
Adds enchantments in the stringspec; if a specified enchantment already exists, overwrites it...
void SetLevel(int a_EnchantmentID, unsigned int a_Level)
Sets the level for the specified enchantment, adding it if not stored before or removing it if level ...
std::vector< cWeightedEnchantment > cWeightedEnchantments
Definition: Enchantments.h:23
void Add(const cEnchantments &a_Other)
Adds the enchantments contained in a_Other into this object.
bool operator!=(const cEnchantments &a_Other) const
Returns true if a_Other doesn&#39;t contain exactly the same enchantments and levels. ...
size_t Count(void)
Get the count of enchantments.
static void AddEnchantmentWeightToVector(cWeightedEnchantments &a_Enchantments, int a_Weight, int a_EnchantmentID, unsigned int a_EnchantmentLevel)
Add a enchantment with weight to the vector.
std::string AString
Definition: StringUtils.h:13
bool operator==(const cEnchantments &a_Other) const
Returns true if a_Other contains exactly the same enchantments and levels.
cMap::const_iterator begin() const
Make this class iterable.
Definition: Enchantments.h:168
static unsigned int GetLevelCap(int a_EnchantmentID)
Get the maximum level the enchantment can have.
static cEnchantments GetRandomEnchantmentFromVector(cWeightedEnchantments &a_Enchantments)
Gets random enchantment from Vector and returns it.
static int GetXPCostMultiplier(int a_EnchantmentID, bool FromBook)
Get the XP cost multiplier for the enchantment (for anvils).
std::map< int, unsigned int > cMap
Maps enchantment ID -> enchantment level.
Definition: Enchantments.h:161
AString ToString(void) const
Serializes all the enchantments into a string.
Class that stores item enchantments or stored-enchantments The enchantments may be serialized to a st...
Definition: Enchantments.h:40
static int StringToEnchantmentID(const AString &a_EnchantmentName)
Converts enchantment name or ID (number in string) to the numeric representation; returns -1 if encha...
static void CheckEnchantmentConflictsFromVector(cWeightedEnchantments &a_Enchantments, cEnchantments a_FirstEnchantment)
Check enchantment conflicts from enchantments from the vector.
bool IsEmpty(void) const
Returns true if there are no enchantments.
void ParseFromNBT(cEnchantments &a_Enchantments, const cParsedNBT &a_NBT, int a_EnchListTagIdx)
Reads the enchantments from the specified NBT list tag (ench or StoredEnchantments) ...
static cEnchantments SelectEnchantmentFromVector(const cWeightedEnchantments &a_Enchantments, int a_Seed)
Selects one enchantment from a Vector using cNoise.
bool CanAddEnchantment(int a_EnchantmentID) const
Returns true if the given enchantment could be legally added to this object.