Cuberite
A lightweight, fast and extensible game server for Minecraft
EnchantmentSerializer.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h"
3 
5 #include "FastNBT.h"
6 #include "../Enchantments.h"
7 
8 void EnchantmentSerializer::WriteToNBTCompound(const cEnchantments & a_Enchantments, cFastNBTWriter & a_Writer, const AString & a_ListTagName)
9 {
10  // Write the enchantments into the specified NBT writer
11  // begin with the LIST tag of the specified name ("ench" or "StoredEnchantments")
12 
13  a_Writer.BeginList(a_ListTagName, TAG_Compound);
14  for (cEnchantments::cMap::const_iterator itr = a_Enchantments.m_Enchantments.begin(), end = a_Enchantments.m_Enchantments.end(); itr != end; ++itr)
15  {
16  a_Writer.BeginCompound("");
17  a_Writer.AddShort("id", static_cast<Int16>(itr->first));
18  a_Writer.AddShort("lvl", static_cast<Int16>(itr->second));
19  a_Writer.EndCompound();
20  } // for itr - m_Enchantments[]
21  a_Writer.EndList();
22 }
23 
24 
25 
26 
27 
28 void EnchantmentSerializer::ParseFromNBT(cEnchantments & a_Enchantments, const cParsedNBT & a_NBT, int a_EnchListTagIdx)
29 {
30  // Read the enchantments from the specified NBT list tag (ench or StoredEnchantments)
31 
32  // Verify that the tag is a list:
33  if (a_NBT.GetType(a_EnchListTagIdx) != TAG_List)
34  {
35  LOGWARNING("%s: Invalid EnchListTag type: exp %d, got %d. Enchantments not parsed",
36  __FUNCTION__, TAG_List, a_NBT.GetType(a_EnchListTagIdx)
37  );
38  ASSERT(!"Bad EnchListTag type");
39  return;
40  }
41 
42  // Verify that the list is of Compounds:
43  if (a_NBT.GetChildrenType(a_EnchListTagIdx) != TAG_Compound)
44  {
45  LOGWARNING("%s: Invalid NBT list children type: exp %d, got %d. Enchantments not parsed",
46  __FUNCTION__, TAG_Compound, a_NBT.GetChildrenType(a_EnchListTagIdx)
47  );
48  ASSERT(!"Bad EnchListTag children type");
49  return;
50  }
51 
52  a_Enchantments.Clear();
53 
54  // Iterate over all the compound children, parse an enchantment from each:
55  for (int tag = a_NBT.GetFirstChild(a_EnchListTagIdx); tag >= 0; tag = a_NBT.GetNextSibling(tag))
56  {
57  // tag is the compound inside the "ench" list tag
58  ASSERT(a_NBT.GetType(tag) == TAG_Compound);
59 
60  // Search for the id and lvl tags' values:
61  int id = -1, lvl = -1;
62  for (int ch = a_NBT.GetFirstChild(tag); ch >= 0; ch = a_NBT.GetNextSibling(ch))
63  {
64  if (a_NBT.GetType(ch) != TAG_Short)
65  {
66  continue;
67  }
68  if (a_NBT.GetName(ch) == "id")
69  {
70  id = a_NBT.GetShort(ch);
71  }
72  else if (a_NBT.GetName(ch) == "lvl")
73  {
74  lvl = a_NBT.GetShort(ch);
75  }
76  } // for ch - children of the compound tag
77 
78  if ((id == -1) || (lvl <= 0))
79  {
80  // Failed to parse either the id or the lvl, skip this compound
81  continue;
82  }
83 
84  // Store the enchantment:
85  a_Enchantments.m_Enchantments[id] = static_cast<unsigned int>(lvl);
86  } // for tag - children of the ench list tag
87 }
88 
signed short Int16
Definition: Globals.h:153
#define ASSERT(x)
Definition: Globals.h:276
void LOGWARNING(std::string_view a_Format, const Args &... args)
Definition: LoggerSimple.h:67
std::string AString
Definition: StringUtils.h:11
@ TAG_List
Definition: FastNBT.h:41
@ TAG_Short
Definition: FastNBT.h:34
@ TAG_Compound
Definition: FastNBT.h:42
void ParseFromNBT(cEnchantments &a_Enchantments, const cParsedNBT &a_NBT, int a_EnchListTagIdx)
Reads the enchantments from the specified NBT list tag (ench or StoredEnchantments)
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...
Class that stores item enchantments or stored-enchantments The enchantments may be serialized to a st...
Definition: Enchantments.h:42
cMap m_Enchantments
Currently stored enchantments.
Definition: Enchantments.h:165
void Clear(void)
Removes all enchantments.
Parses and contains the parsed data Also implements data accessor functions for tree traversal and va...
Definition: FastNBT.h:153
int GetNextSibling(int a_Tag) const
Returns the next sibling of the specified tag, or -1 if none.
Definition: FastNBT.h:175
Int16 GetShort(int a_Tag) const
Returns the value stored in a Short tag.
Definition: FastNBT.h:227
int GetFirstChild(int a_Tag) const
Returns the first child of the specified tag, or -1 if none / not applicable.
Definition: FastNBT.h:169
AString GetName(int a_Tag) const
Returns the tag's name.
Definition: FastNBT.h:293
eTagType GetType(int a_Tag) const
Definition: FastNBT.h:210
eTagType GetChildrenType(int a_Tag) const
Returns the children type for a List tag; undefined on other tags.
Definition: FastNBT.h:213
void AddShort(const AString &a_Name, Int16 a_Value)
Definition: FastNBT.cpp:561
void EndList(void)
Definition: FastNBT.cpp:536
void BeginList(const AString &a_Name, eTagType a_ChildrenType)
Definition: FastNBT.cpp:512
void EndCompound(void)
Definition: FastNBT.cpp:499
void BeginCompound(const AString &a_Name)
Definition: FastNBT.cpp:481