Cuberite
A lightweight, fast and extensible game server for Minecraft
Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Attributes | Friends | List of all members
cEnchantments Class Reference

Class that stores item enchantments or stored-enchantments The enchantments may be serialized to a stringspec and read back from such stringspec. More...

#include <Enchantments.h>

Public Types

enum  eEnchantment {
  enchProtection = 0 , enchFireProtection = 1 , enchFeatherFalling = 2 , enchBlastProtection = 3 ,
  enchProjectileProtection = 4 , enchRespiration = 5 , enchAquaAffinity = 6 , enchThorns = 7 ,
  enchDepthStrider = 8 , enchSharpness = 16 , enchSmite = 17 , enchBaneOfArthropods = 18 ,
  enchKnockback = 19 , enchFireAspect = 20 , enchLooting = 21 , enchEfficiency = 32 ,
  enchSilkTouch = 33 , enchUnbreaking = 34 , enchFortune = 35 , enchPower = 48 ,
  enchPunch = 49 , enchFlame = 50 , enchInfinity = 51 , enchLuckOfTheSea = 61 ,
  enchLure = 62
}
 Individual enchantment IDs, corresponding to their NBT IDs: https://minecraft.wiki/w/Data_values#Enchantment_IDs. More...
 

Public Member Functions

void Add (const cEnchantments &a_Other)
 Adds the enchantments contained in a_Other into this object. More...
 
void AddFromString (const AString &a_StringSpec)
 Adds enchantments in the stringspec; if a specified enchantment already exists, overwrites it. More...
 
cMap::const_iterator begin () const
 Make this class iterable. More...
 
bool CanAddEnchantment (int a_EnchantmentID) const
 Returns true if the given enchantment could be legally added to this object. More...
 
 cEnchantments (const AString &a_StringSpec)
 Creates an enchantments container filled with enchantments parsed from stringspec. More...
 
 cEnchantments (void)
 Creates an empty enchantments container. More...
 
void Clear (void)
 Removes all enchantments. More...
 
size_t Count (void) const
 Get the count of enchantments. More...
 
cMap::const_iterator end () const
 
unsigned int GetLevel (int a_EnchantmentID) const
 Returns the level for the specified enchantment; 0 if not stored. More...
 
bool IsEmpty (void) const
 Returns true if there are no enchantments. More...
 
bool operator!= (const cEnchantments &a_Other) const
 Returns true if a_Other doesn't contain exactly the same enchantments and levels. More...
 
bool operator== (const cEnchantments &a_Other) const
 Returns true if a_Other contains exactly the same enchantments and levels. More...
 
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 <= 0. More...
 
AString ToString (void) const
 Serializes all the enchantments into a string. More...
 

Static Public Member Functions

static void AddEnchantmentWeightToVector (cWeightedEnchantments &a_Enchantments, int a_Weight, int a_EnchantmentID, unsigned int a_EnchantmentLevel)
 Add a enchantment with weight to the vector. More...
 
static void AddItemEnchantmentWeights (cWeightedEnchantments &a_Enchantments, short a_ItemType, unsigned a_EnchantmentLevel)
 Add enchantment weights from item to the vector. More...
 
static void CheckEnchantmentConflictsFromVector (cWeightedEnchantments &a_Enchantments, const cEnchantments &a_FirstEnchantment)
 Check enchantment conflicts from enchantments from the vector. More...
 
static unsigned int GetLevelCap (int a_EnchantmentID)
 Get the maximum level the enchantment can have. More...
 
static cEnchantments GetRandomEnchantmentFromVector (const cWeightedEnchantments &a_Enchantments, MTRand &a_Random)
 Gets random enchantment from Vector and returns it, with randomness derived from the provided PRNG. More...
 
static int GetXPCostMultiplier (int a_EnchantmentID, bool FromBook)
 Get the XP cost multiplier for the enchantment (for anvils). More...
 
static void RemoveEnchantmentWeightFromVector (cWeightedEnchantments &a_Enchantments, const cEnchantments &a_Enchantment)
 Remove the entire enchantment (with weight) from the vector. More...
 
static void RemoveEnchantmentWeightFromVector (cWeightedEnchantments &a_Enchantments, int a_EnchantmentID)
 Remove the entire enchantment (with weight) from the vector. More...
 
static cEnchantments SelectEnchantmentFromVector (const cWeightedEnchantments &a_Enchantments, int a_Seed)
 Selects one enchantment from a Vector using cNoise. More...
 
static int StringToEnchantmentID (const AString &a_EnchantmentName)
 Converts enchantment name or ID (number in string) to the numeric representation; returns -1 if enchantment name not found; case insensitive. More...
 

Protected Types

typedef std::map< int, unsigned int > cMap
 Maps enchantment ID -> enchantment level. More...
 

Protected Attributes

cMap m_Enchantments
 Currently stored enchantments. More...
 

Friends

void EnchantmentSerializer::ParseFromNBT (cEnchantments &a_Enchantments, const cParsedNBT &a_NBT, int a_EnchListTagIdx)
 Reads the enchantments from the specified NBT list tag (ench or StoredEnchantments) More...
 
void EnchantmentSerializer::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 ("ench" or "StoredEnchantments") More...
 

Detailed Description

Class that stores item enchantments or stored-enchantments The enchantments may be serialized to a stringspec and read back from such stringspec.

The format for the stringspec is "id=lvl;id=lvl;id=lvl...", with an optional semicolon at the end, mapping each enchantment's id onto its level. ID may be either a number or the enchantment name. Level value of 0 means no such enchantment, and it will not be stored in the m_Enchantments. Serialization will never put zero-level enchantments into the stringspec and will always use numeric IDs.

Definition at line 41 of file Enchantments.h.

Member Typedef Documentation

◆ cMap

typedef std::map<int, unsigned int> cEnchantments::cMap
protected

Maps enchantment ID -> enchantment level.

Definition at line 162 of file Enchantments.h.

Member Enumeration Documentation

◆ eEnchantment

Individual enchantment IDs, corresponding to their NBT IDs: https://minecraft.wiki/w/Data_values#Enchantment_IDs.

Enumerator
enchProtection 
enchFireProtection 
enchFeatherFalling 
enchBlastProtection 
enchProjectileProtection 
enchRespiration 
enchAquaAffinity 
enchThorns 
enchDepthStrider 
enchSharpness 
enchSmite 
enchBaneOfArthropods 
enchKnockback 
enchFireAspect 
enchLooting 
enchEfficiency 
enchSilkTouch 
enchUnbreaking 
enchFortune 
enchPower 
enchPunch 
enchFlame 
enchInfinity 
enchLuckOfTheSea 
enchLure 

Definition at line 47 of file Enchantments.h.

Constructor & Destructor Documentation

◆ cEnchantments() [1/2]

cEnchantments::cEnchantments ( void  )

Creates an empty enchantments container.

Definition at line 16 of file Enchantments.cpp.

◆ cEnchantments() [2/2]

cEnchantments::cEnchantments ( const AString a_StringSpec)

Creates an enchantments container filled with enchantments parsed from stringspec.

Definition at line 25 of file Enchantments.cpp.

Member Function Documentation

◆ Add()

void cEnchantments::Add ( const cEnchantments a_Other)

Adds the enchantments contained in a_Other into this object.

Existing enchantments are preserved, unless a_Other specifies a different level, in which case the level is changed to the a_Other's one.

Definition at line 34 of file Enchantments.cpp.

◆ AddEnchantmentWeightToVector()

void cEnchantments::AddEnchantmentWeightToVector ( cWeightedEnchantments a_Enchantments,
int  a_Weight,
int  a_EnchantmentID,
unsigned int  a_EnchantmentLevel 
)
static

Add a enchantment with weight to the vector.

Definition at line 1069 of file Enchantments.cpp.

◆ AddFromString()

void cEnchantments::AddFromString ( const AString a_StringSpec)

Adds enchantments in the stringspec; if a specified enchantment already exists, overwrites it.

Definition at line 46 of file Enchantments.cpp.

◆ AddItemEnchantmentWeights()

void cEnchantments::AddItemEnchantmentWeights ( cWeightedEnchantments a_Enchantments,
short  a_ItemType,
unsigned  a_EnchantmentLevel 
)
static

Add enchantment weights from item to the vector.

Definition at line 403 of file Enchantments.cpp.

◆ begin()

cMap::const_iterator cEnchantments::begin ( ) const
inline

Make this class iterable.

Definition at line 169 of file Enchantments.h.

◆ CanAddEnchantment()

bool cEnchantments::CanAddEnchantment ( int  a_EnchantmentID) const

Returns true if the given enchantment could be legally added to this object.

Note that adding the enchantment may not actually increase the level.

Definition at line 282 of file Enchantments.cpp.

◆ CheckEnchantmentConflictsFromVector()

void cEnchantments::CheckEnchantmentConflictsFromVector ( cWeightedEnchantments a_Enchantments,
const cEnchantments a_FirstEnchantment 
)
static

Check enchantment conflicts from enchantments from the vector.

Definition at line 1115 of file Enchantments.cpp.

◆ Clear()

void cEnchantments::Clear ( void  )

Removes all enchantments.

Definition at line 152 of file Enchantments.cpp.

◆ Count()

size_t cEnchantments::Count ( void  ) const

Get the count of enchantments.

Definition at line 88 of file Enchantments.cpp.

◆ end()

cMap::const_iterator cEnchantments::end ( ) const
inline

Definition at line 170 of file Enchantments.h.

◆ GetLevel()

unsigned int cEnchantments::GetLevel ( int  a_EnchantmentID) const

Returns the level for the specified enchantment; 0 if not stored.

Definition at line 112 of file Enchantments.cpp.

◆ GetLevelCap()

unsigned int cEnchantments::GetLevelCap ( int  a_EnchantmentID)
static

Get the maximum level the enchantment can have.

Definition at line 170 of file Enchantments.cpp.

◆ GetRandomEnchantmentFromVector()

cEnchantments cEnchantments::GetRandomEnchantmentFromVector ( const cWeightedEnchantments a_Enchantments,
MTRand a_Random 
)
static

Gets random enchantment from Vector and returns it, with randomness derived from the provided PRNG.

Definition at line 1173 of file Enchantments.cpp.

◆ GetXPCostMultiplier()

int cEnchantments::GetXPCostMultiplier ( int  a_EnchantmentID,
bool  FromBook 
)
static

Get the XP cost multiplier for the enchantment (for anvils).

If FromBook is true, then this function returns the XP multiplier if the enchantment is coming from a book, otherwise it returns the normal item multiplier.

Definition at line 208 of file Enchantments.cpp.

◆ IsEmpty()

bool cEnchantments::IsEmpty ( void  ) const

Returns true if there are no enchantments.

Definition at line 161 of file Enchantments.cpp.

◆ operator!=()

bool cEnchantments::operator!= ( const cEnchantments a_Other) const

Returns true if a_Other doesn't contain exactly the same enchantments and levels.

Definition at line 394 of file Enchantments.cpp.

◆ operator==()

bool cEnchantments::operator== ( const cEnchantments a_Other) const

Returns true if a_Other contains exactly the same enchantments and levels.

Definition at line 385 of file Enchantments.cpp.

◆ RemoveEnchantmentWeightFromVector() [1/2]

void cEnchantments::RemoveEnchantmentWeightFromVector ( cWeightedEnchantments a_Enchantments,
const cEnchantments a_Enchantment 
)
static

Remove the entire enchantment (with weight) from the vector.

Definition at line 1099 of file Enchantments.cpp.

◆ RemoveEnchantmentWeightFromVector() [2/2]

void cEnchantments::RemoveEnchantmentWeightFromVector ( cWeightedEnchantments a_Enchantments,
int  a_EnchantmentID 
)
static

Remove the entire enchantment (with weight) from the vector.

Definition at line 1083 of file Enchantments.cpp.

◆ SelectEnchantmentFromVector()

cEnchantments cEnchantments::SelectEnchantmentFromVector ( const cWeightedEnchantments a_Enchantments,
int  a_Seed 
)
static

Selects one enchantment from a Vector using cNoise.

Mostly used for generators. Uses the enchantments' weights for the random distribution. If a_Enchantments is empty, returns an empty enchantment.

Definition at line 1197 of file Enchantments.cpp.

◆ SetLevel()

void cEnchantments::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 <= 0.

Definition at line 129 of file Enchantments.cpp.

◆ StringToEnchantmentID()

int cEnchantments::StringToEnchantmentID ( const AString a_EnchantmentName)
static

Converts enchantment name or ID (number in string) to the numeric representation; returns -1 if enchantment name not found; case insensitive.

Definition at line 328 of file Enchantments.cpp.

◆ ToString()

AString cEnchantments::ToString ( void  ) const

Serializes all the enchantments into a string.

Definition at line 97 of file Enchantments.cpp.

Friends And Related Function Documentation

◆ EnchantmentSerializer::ParseFromNBT

void EnchantmentSerializer::ParseFromNBT ( cEnchantments a_Enchantments,
const cParsedNBT a_NBT,
int  a_EnchListTagIdx 
)
friend

Reads the enchantments from the specified NBT list tag (ench or StoredEnchantments)

◆ EnchantmentSerializer::WriteToNBTCompound

void EnchantmentSerializer::WriteToNBTCompound ( const cEnchantments a_Enchantments,
cFastNBTWriter a_Writer,
const AString a_ListTagName 
)
friend

Writes the enchantments into the specified NBT writer; begins with the LIST tag of the specified name ("ench" or "StoredEnchantments")

Member Data Documentation

◆ m_Enchantments

cMap cEnchantments::m_Enchantments
protected

Currently stored enchantments.

Definition at line 165 of file Enchantments.h.


The documentation for this class was generated from the following files: