Cuberite
A lightweight, fast and extensible game server for Minecraft
Item.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
3 
4 #include "Item.h"
5 #include "ItemGrid.h"
6 #include "json/json.h"
7 #include "Items/ItemHandler.h"
8 
9 #include "FastRandom.h"
10 
11 
12 
13 
14 
15 cItem cItem::CopyOne(void) const
16 {
17  cItem res(*this);
18  res.m_ItemCount = 1;
19  return res;
20 }
21 
22 
23 
24 
25 
26 cItem & cItem::AddCount(char a_AmountToAdd)
27 {
28  m_ItemCount += a_AmountToAdd;
29  if (m_ItemCount <= 0)
30  {
31  Empty();
32  }
33  return *this;
34 }
35 
36 
37 
38 
39 
40 short cItem::GetMaxDamage(void) const
41 {
42  switch (m_ItemType)
43  {
44  case E_ITEM_BOW: return 384;
45  case E_ITEM_CHAIN_BOOTS: return 196;
46  case E_ITEM_CHAIN_CHESTPLATE:return 241;
47  case E_ITEM_CHAIN_HELMET: return 166;
48  case E_ITEM_CHAIN_LEGGINGS: return 226;
49  case E_ITEM_DIAMOND_AXE: return 1561;
50  case E_ITEM_DIAMOND_BOOTS: return 430;
51  case E_ITEM_DIAMOND_CHESTPLATE: return 529;
52  case E_ITEM_DIAMOND_HELMET: return 364;
53  case E_ITEM_DIAMOND_HOE: return 1561;
54  case E_ITEM_DIAMOND_LEGGINGS:return 496;
55  case E_ITEM_DIAMOND_PICKAXE: return 1561;
56  case E_ITEM_DIAMOND_SHOVEL: return 1561;
57  case E_ITEM_DIAMOND_SWORD: return 1561;
58  case E_ITEM_FLINT_AND_STEEL: return 64;
59  case E_ITEM_FISHING_ROD: return 65;
60  case E_ITEM_GOLD_AXE: return 32;
61  case E_ITEM_GOLD_BOOTS: return 92;
62  case E_ITEM_GOLD_CHESTPLATE: return 113;
63  case E_ITEM_GOLD_HELMET: return 78;
64  case E_ITEM_GOLD_HOE: return 32;
65  case E_ITEM_GOLD_LEGGINGS: return 106;
66  case E_ITEM_GOLD_PICKAXE: return 32;
67  case E_ITEM_GOLD_SHOVEL: return 32;
68  case E_ITEM_GOLD_SWORD: return 32;
69  case E_ITEM_IRON_AXE: return 250;
70  case E_ITEM_IRON_BOOTS: return 196;
71  case E_ITEM_IRON_CHESTPLATE: return 241;
72  case E_ITEM_IRON_HELMET: return 166;
73  case E_ITEM_IRON_HOE: return 250;
74  case E_ITEM_IRON_LEGGINGS: return 226;
75  case E_ITEM_IRON_PICKAXE: return 250;
76  case E_ITEM_IRON_SHOVEL: return 250;
77  case E_ITEM_IRON_SWORD: return 250;
78  case E_ITEM_LEATHER_BOOTS: return 66;
79  case E_ITEM_LEATHER_CAP: return 55;
80  case E_ITEM_LEATHER_PANTS: return 76;
81  case E_ITEM_LEATHER_TUNIC: return 81;
82  case E_ITEM_SHEARS: return 250;
83  case E_ITEM_STONE_AXE: return 131;
84  case E_ITEM_STONE_HOE: return 131;
85  case E_ITEM_STONE_PICKAXE: return 131;
86  case E_ITEM_STONE_SHOVEL: return 131;
87  case E_ITEM_STONE_SWORD: return 131;
88  case E_ITEM_WOODEN_AXE: return 59;
89  case E_ITEM_WOODEN_HOE: return 59;
90  case E_ITEM_WOODEN_PICKAXE: return 59;
91  case E_ITEM_WOODEN_SHOVEL: return 59;
92  case E_ITEM_WOODEN_SWORD: return 59;
93 
94  default: return 0;
95  }
96 }
97 
98 
99 
100 
101 
102 bool cItem::DamageItem(short a_Amount)
103 {
104  short MaxDamage = GetMaxDamage();
105  if (MaxDamage == 0)
106  {
107  // Item doesn't have damage
108  return false;
109  }
110 
111  m_ItemDamage += a_Amount;
112  return (m_ItemDamage > MaxDamage);
113 }
114 
115 
116 
117 
118 
119 bool cItem::IsFullStack(void) const
120 {
122 }
123 
124 
125 
126 
127 
128 char cItem::GetMaxStackSize(void) const
129 {
131 }
132 
133 
134 
135 
136 
138 {
139  return ItemHandler(m_ItemType);
140 }
141 
142 
143 
144 
145 
146 void cItem::GetJson(Json::Value & a_OutValue) const
147 {
148  a_OutValue["ID"] = m_ItemType;
149  if (m_ItemType > 0)
150  {
151  a_OutValue["Count"] = m_ItemCount;
152  a_OutValue["Health"] = m_ItemDamage;
153  AString Enchantments(m_Enchantments.ToString());
154  if (!Enchantments.empty())
155  {
156  a_OutValue["ench"] = Enchantments;
157  }
158  if (!IsCustomNameEmpty())
159  {
160  a_OutValue["Name"] = m_CustomName;
161  }
162  if (!IsLoreEmpty())
163  {
164  auto & LoreArray = (a_OutValue["Lore"] = Json::Value(Json::arrayValue));
165 
166  for (const auto & Line : m_LoreTable)
167  {
168  LoreArray.append(Line);
169  }
170  }
171 
172  if (m_ItemColor.IsValid())
173  {
174  a_OutValue["Color_Red"] = m_ItemColor.GetRed();
175  a_OutValue["Color_Green"] = m_ItemColor.GetGreen();
176  a_OutValue["Color_Blue"] = m_ItemColor.GetBlue();
177  }
178 
180  {
181  a_OutValue["Flicker"] = m_FireworkItem.m_HasFlicker;
182  a_OutValue["Trail"] = m_FireworkItem.m_HasTrail;
183  a_OutValue["Type"] = m_FireworkItem.m_Type;
184  a_OutValue["FlightTimeInTicks"] = m_FireworkItem.m_FlightTimeInTicks;
185  a_OutValue["Colours"] = m_FireworkItem.ColoursToString(m_FireworkItem);
186  a_OutValue["FadeColours"] = m_FireworkItem.FadeColoursToString(m_FireworkItem);
187  }
188 
189  a_OutValue["RepairCost"] = m_RepairCost;
190  }
191 }
192 
193 
194 
195 
196 
197 void cItem::FromJson(const Json::Value & a_Value)
198 {
199  m_ItemType = static_cast<ENUM_ITEM_ID>(a_Value.get("ID", -1).asInt());
200  if (m_ItemType > 0)
201  {
202  m_ItemCount = static_cast<char>(a_Value.get("Count", -1).asInt());
203  m_ItemDamage = static_cast<short>(a_Value.get("Health", -1).asInt());
205  m_Enchantments.AddFromString(a_Value.get("ench", "").asString());
206  m_CustomName = a_Value.get("Name", "").asString();
207  auto Lore = a_Value.get("Lore", Json::arrayValue);
208  for (auto & Line : Lore)
209  {
210  m_LoreTable.push_back(Line.asString());
211  }
212 
213  int red = a_Value.get("Color_Red", -1).asInt();
214  int green = a_Value.get("Color_Green", -1).asInt();
215  int blue = a_Value.get("Color_Blue", -1).asInt();
216  if ((red > -1) && (red < static_cast<int>(cColor::COLOR_LIMIT)) && (green > -1) && (green < static_cast<int>(cColor::COLOR_LIMIT)) && (blue > -1) && (blue < static_cast<int>(cColor::COLOR_LIMIT)))
217  {
218  m_ItemColor.SetColor(static_cast<unsigned char>(red), static_cast<unsigned char>(green), static_cast<unsigned char>(blue));
219  }
220  else if ((red != -1) || (blue != -1) || (green != -1))
221  {
222  LOGWARNING("Item with invalid red, green, and blue values read in from json file.");
223  }
224 
226  {
227  m_FireworkItem.m_HasFlicker = a_Value.get("Flicker", false).asBool();
228  m_FireworkItem.m_HasTrail = a_Value.get("Trail", false).asBool();
229  m_FireworkItem.m_Type = static_cast<NIBBLETYPE>(a_Value.get("Type", 0).asInt());
230  m_FireworkItem.m_FlightTimeInTicks = static_cast<short>(a_Value.get("FlightTimeInTicks", 0).asInt());
231  m_FireworkItem.ColoursFromString(a_Value.get("Colours", "").asString(), m_FireworkItem);
232  m_FireworkItem.FadeColoursFromString(a_Value.get("FadeColours", "").asString(), m_FireworkItem);
233  }
234 
235  m_RepairCost = a_Value.get("RepairCost", 0).asInt();
236  }
237 }
238 
239 
240 
241 
242 
243 bool cItem::IsEnchantable(short a_ItemType, bool a_FromBook)
244 {
245  if (
246  ItemCategory::IsAxe(a_ItemType) ||
247  ItemCategory::IsSword(a_ItemType) ||
248  ItemCategory::IsShovel(a_ItemType) ||
249  ItemCategory::IsPickaxe(a_ItemType) ||
250  (a_FromBook && ItemCategory::IsHoe(a_ItemType)) ||
251  ItemCategory::IsArmor(a_ItemType)
252  )
253  {
254  return true;
255  }
256 
257  switch (a_ItemType)
258  {
259  case E_ITEM_BOOK:
260  case E_ITEM_BOW:
261  case E_ITEM_FISHING_ROD:
262  {
263  return true;
264  }
265 
267  case E_ITEM_SHEARS:
269  {
270  return a_FromBook;
271  }
272  }
273 
274  return false;
275 }
276 
277 
278 
279 
280 
282 {
283  int Enchantability = 0;
284 
285  switch (m_ItemType)
286  {
287  case E_ITEM_WOODEN_SWORD: Enchantability = 15; break;
288  case E_ITEM_WOODEN_PICKAXE: Enchantability = 15; break;
289  case E_ITEM_WOODEN_SHOVEL: Enchantability = 15; break;
290  case E_ITEM_WOODEN_AXE: Enchantability = 15; break;
291  case E_ITEM_WOODEN_HOE: Enchantability = 15; break;
292 
293  case E_ITEM_LEATHER_CAP: Enchantability = 15; break;
294  case E_ITEM_LEATHER_TUNIC: Enchantability = 15; break;
295  case E_ITEM_LEATHER_PANTS: Enchantability = 15; break;
296  case E_ITEM_LEATHER_BOOTS: Enchantability = 15; break;
297 
298  case E_ITEM_STONE_SWORD: Enchantability = 5; break;
299  case E_ITEM_STONE_PICKAXE: Enchantability = 5; break;
300  case E_ITEM_STONE_SHOVEL: Enchantability = 5; break;
301  case E_ITEM_STONE_AXE: Enchantability = 5; break;
302  case E_ITEM_STONE_HOE: Enchantability = 5; break;
303 
304  case E_ITEM_IRON_HELMET: Enchantability = 9; break;
305  case E_ITEM_IRON_CHESTPLATE: Enchantability = 9; break;
306  case E_ITEM_IRON_LEGGINGS: Enchantability = 9; break;
307  case E_ITEM_IRON_BOOTS: Enchantability = 9; break;
308 
309  case E_ITEM_IRON_SWORD: Enchantability = 14; break;
310  case E_ITEM_IRON_PICKAXE: Enchantability = 14; break;
311  case E_ITEM_IRON_SHOVEL: Enchantability = 14; break;
312  case E_ITEM_IRON_AXE: Enchantability = 14; break;
313  case E_ITEM_IRON_HOE: Enchantability = 14; break;
314 
315  case E_ITEM_CHAIN_HELMET: Enchantability = 12; break;
316  case E_ITEM_CHAIN_CHESTPLATE: Enchantability = 12; break;
317  case E_ITEM_CHAIN_LEGGINGS: Enchantability = 12; break;
318  case E_ITEM_CHAIN_BOOTS: Enchantability = 12; break;
319 
320  case E_ITEM_DIAMOND_HELMET: Enchantability = 10; break;
321  case E_ITEM_DIAMOND_CHESTPLATE: Enchantability = 10; break;
322  case E_ITEM_DIAMOND_LEGGINGS: Enchantability = 10; break;
323  case E_ITEM_DIAMOND_BOOTS: Enchantability = 10; break;
324 
325  case E_ITEM_DIAMOND_SWORD: Enchantability = 10; break;
326  case E_ITEM_DIAMOND_PICKAXE: Enchantability = 10; break;
327  case E_ITEM_DIAMOND_SHOVEL: Enchantability = 10; break;
328  case E_ITEM_DIAMOND_AXE: Enchantability = 10; break;
329  case E_ITEM_DIAMOND_HOE: Enchantability = 10; break;
330 
331  case E_ITEM_GOLD_HELMET: Enchantability = 25; break;
332  case E_ITEM_GOLD_CHESTPLATE: Enchantability = 25; break;
333  case E_ITEM_GOLD_LEGGINGS: Enchantability = 25; break;
334  case E_ITEM_GOLD_BOOTS: Enchantability = 25; break;
335 
336  case E_ITEM_GOLD_SWORD: Enchantability = 22; break;
337  case E_ITEM_GOLD_PICKAXE: Enchantability = 22; break;
338  case E_ITEM_GOLD_SHOVEL: Enchantability = 22; break;
339  case E_ITEM_GOLD_AXE: Enchantability = 22; break;
340  case E_ITEM_GOLD_HOE: Enchantability = 22; break;
341 
342  case E_ITEM_FISHING_ROD: Enchantability = 1; break;
343  case E_ITEM_BOW: Enchantability = 1; break;
344  case E_ITEM_BOOK: Enchantability = 1; break;
345  }
346 
347  return Enchantability;
348 }
349 
350 
351 
352 
353 
354 bool cItem::EnchantByXPLevels(int a_NumXPLevels)
355 {
357  {
358  return false;
359  }
360 
361  int Enchantability = GetEnchantability();
362  if (Enchantability == 0)
363  {
364  return false;
365  }
366 
367  auto & Random = GetRandomProvider();
368  int ModifiedEnchantmentLevel = a_NumXPLevels + Random.RandInt(Enchantability / 4) + Random.RandInt(Enchantability / 4) + 1;
369  float RandomBonus = 1.0F + (Random.RandReal() + Random.RandReal() - 1.0F) * 0.15F;
370  int FinalEnchantmentLevel = static_cast<int>(ModifiedEnchantmentLevel * RandomBonus + 0.5F);
371 
372  cWeightedEnchantments Enchantments;
373  cEnchantments::AddItemEnchantmentWeights(Enchantments, m_ItemType, FinalEnchantmentLevel);
374 
375  if (m_ItemType == E_ITEM_BOOK)
376  {
378  }
379 
381  m_Enchantments.AddFromString(Enchantment1.ToString());
382  cEnchantments::RemoveEnchantmentWeightFromVector(Enchantments, Enchantment1);
383 
384  // Checking for conflicting enchantments
385  cEnchantments::CheckEnchantmentConflictsFromVector(Enchantments, Enchantment1);
386 
387  // Next Enchantment (Second)
388  float NewEnchantmentLevel = a_NumXPLevels / 2.0f;
389  float SecondEnchantmentChance = (NewEnchantmentLevel + 1) / 50.0f;
390  if (Enchantments.empty() || !Random.RandBool(SecondEnchantmentChance))
391  {
392  return true;
393  }
394 
396  m_Enchantments.AddFromString(Enchantment2.ToString());
397  cEnchantments::RemoveEnchantmentWeightFromVector(Enchantments, Enchantment2);
398 
399  // Checking for conflicting enchantments
400  cEnchantments::CheckEnchantmentConflictsFromVector(Enchantments, Enchantment2);
401 
402  // Next Enchantment (Third)
403  NewEnchantmentLevel = NewEnchantmentLevel / 2.0f;
404  float ThirdEnchantmentChance = (NewEnchantmentLevel + 1) / 50.0f;
405  if (Enchantments.empty() || !Random.RandBool(ThirdEnchantmentChance))
406  {
407  return true;
408  }
409 
411  m_Enchantments.AddFromString(Enchantment3.ToString());
412  cEnchantments::RemoveEnchantmentWeightFromVector(Enchantments, Enchantment3);
413 
414  // Checking for conflicting enchantments
415  cEnchantments::CheckEnchantmentConflictsFromVector(Enchantments, Enchantment3);
416 
417  // Next Enchantment (Fourth)
418  NewEnchantmentLevel = NewEnchantmentLevel / 2.0f;
419  float FourthEnchantmentChance = (NewEnchantmentLevel + 1) / 50.0f;
420  if (Enchantments.empty() || !Random.RandBool(FourthEnchantmentChance))
421  {
422  return true;
423  }
425  m_Enchantments.AddFromString(Enchantment4.ToString());
426 
427  return true;
428 }
429 
430 
431 
432 
433 
434 int cItem::AddEnchantment(int a_EnchantmentID, unsigned int a_Level, bool a_FromBook)
435 {
436  unsigned int OurLevel = m_Enchantments.GetLevel(a_EnchantmentID);
437  int Multiplier = cEnchantments::GetXPCostMultiplier(a_EnchantmentID, a_FromBook);
438  unsigned int NewLevel = 0;
439  if (OurLevel > a_Level)
440  {
441  // They don't add anything to us
442  NewLevel = OurLevel;
443  }
444  else if (OurLevel == a_Level)
445  {
446  // Bump it by 1
447  NewLevel = OurLevel + 1;
448  }
449  else
450  {
451  // Take the sacrifice's level
452  NewLevel = a_Level;
453  }
454  unsigned int LevelCap = cEnchantments::GetLevelCap(a_EnchantmentID);
455  if (NewLevel > LevelCap)
456  {
457  NewLevel = LevelCap;
458  }
459 
460  m_Enchantments.SetLevel(a_EnchantmentID, NewLevel);
461  return static_cast<int>(NewLevel) * Multiplier;
462 }
463 
464 
465 
466 
467 
468 bool cItem::CanHaveEnchantment(int a_EnchantmentID)
469 {
471  {
472  // Enchanted books can take anything
473  return true;
474  }
475 
476  // The organization here is based on the summary at:
477  // https://minecraft.gamepedia.com/Enchanting
478  // as of July 2017 (Minecraft 1.12).
479 
480  // Hand tool enchantments
481  static const std::set<int> SwordEnchantments =
482  {
490  };
491  static const std::set<int> AxeEnchantments =
492  {
499  cEnchantments::enchUnbreaking
500  };
501  static const std::set<int> ToolEnchantments =
502  {
506  cEnchantments::enchUnbreaking
507  };
508  static const std::set<int> ShearEnchantments =
509  {
511  cEnchantments::enchUnbreaking
512  };
513  static const std::set<int> BowEnchantments =
514  {
519  };
520  static const std::set<int> FishingEnchantments =
521  {
524  };
525  static const std::set<int> MiscEnchantments =
526  {
527  cEnchantments::enchUnbreaking
528  };
529 
531  {
532  return SwordEnchantments.count(a_EnchantmentID) > 0;
533  }
535  {
536  return AxeEnchantments.count(a_EnchantmentID) > 0;
537  }
539  {
540  return ToolEnchantments.count(a_EnchantmentID) > 0;
541  }
542  if (m_ItemType == E_ITEM_SHEARS)
543  {
544  return ShearEnchantments.count(a_EnchantmentID) > 0;
545  }
546  if (m_ItemType == E_ITEM_BOW)
547  {
548  return BowEnchantments.count(a_EnchantmentID) > 0;
549  }
551  {
552  return FishingEnchantments.count(a_EnchantmentID) > 0;
553  }
555  {
556  return MiscEnchantments.count(a_EnchantmentID) > 0;
557  }
558 
559  // Armor enchantments
560  static const std::set<int> ArmorEnchantments =
561  {
567  cEnchantments::enchUnbreaking
568  };
569  static const std::set<int> HatOnlyEnchantments =
570  {
573  };
574  static const std::set<int> BootOnlyEnchantments =
575  {
578  };
579 
581  {
582  return (BootOnlyEnchantments.count(a_EnchantmentID) > 0) || (ArmorEnchantments.count(a_EnchantmentID) > 0);
583  }
585  {
586  return (HatOnlyEnchantments.count(a_EnchantmentID) > 0) || (ArmorEnchantments.count(a_EnchantmentID) > 0);
587  }
589  {
590  return ArmorEnchantments.count(a_EnchantmentID) > 0;
591  }
592  return false;
593 }
594 
595 
596 
597 
598 
600 {
601  bool FromBook = (a_Other.m_ItemType == E_ITEM_ENCHANTED_BOOK);
602 
603  // Consider each enchantment seperately
604  int EnchantingCost = 0;
605  for (auto & Enchantment : a_Other.m_Enchantments)
606  {
607  if (CanHaveEnchantment(Enchantment.first))
608  {
609  if (!m_Enchantments.CanAddEnchantment(Enchantment.first))
610  {
611  // Cost of incompatible enchantments
612  EnchantingCost += 1;
613  }
614  else
615  {
616  EnchantingCost += AddEnchantment(Enchantment.first, Enchantment.second, FromBook);
617  }
618  }
619  }
620  return EnchantingCost;
621 }
622 
623 
624 
625 
626 
628 // cItems:
629 
630 cItems::cItems(cItem && a_InitialItem)
631 {
632  push_back(std::move(a_InitialItem));
633 }
634 
635 
636 
637 
638 
639 cItem * cItems::Get(int a_Idx)
640 {
641  if ((a_Idx < 0) || (a_Idx >= static_cast<int>(size())))
642  {
643  LOGWARNING("cItems: Attempt to get an out-of-bounds item at index %d; there are currently %zu items. Returning a nil.", a_Idx, size());
644  return nullptr;
645  }
646  return &at(static_cast<size_t>(a_Idx));
647 }
648 
649 
650 
651 
652 
653 void cItems::Set(int a_Idx, const cItem & a_Item)
654 {
655  if ((a_Idx < 0) || (a_Idx >= static_cast<int>(size())))
656  {
657  LOGWARNING("cItems: Attempt to set an item at an out-of-bounds index %d; there are currently %zu items. Not setting.", a_Idx, size());
658  return;
659  }
660  at(static_cast<size_t>(a_Idx)) = a_Item;
661 }
662 
663 
664 
665 
666 
667 void cItems::Delete(int a_Idx)
668 {
669  if ((a_Idx < 0) || (a_Idx >= static_cast<int>(size())))
670  {
671  LOGWARNING("cItems: Attempt to delete an item at an out-of-bounds index %d; there are currently %zu items. Ignoring.", a_Idx, size());
672  return;
673  }
674  erase(begin() + a_Idx);
675 }
676 
677 
678 
679 
680 
681 void cItems::Set(int a_Idx, short a_ItemType, char a_ItemCount, short a_ItemDamage)
682 {
683  if ((a_Idx < 0) || (a_Idx >= static_cast<int>(size())))
684  {
685  LOGWARNING("cItems: Attempt to set an item at an out-of-bounds index %d; there are currently %zu items. Not setting.", a_Idx, size());
686  return;
687  }
688  at(static_cast<size_t>(a_Idx)) = cItem(a_ItemType, a_ItemCount, a_ItemDamage);
689 }
690 
691 
692 
693 
694 
695 bool cItems::Contains(const cItem & a_Item)
696 {
697  for (auto itr : *this)
698  {
699  if (a_Item.IsEqual(itr))
700  {
701  return true;
702  }
703  }
704  return false;
705 }
706 
707 
708 
709 
710 
711 bool cItems::ContainsType(const cItem & a_Item)
712 {
713  for (auto itr : *this)
714  {
715  if (a_Item.IsSameType(itr))
716  {
717  return true;
718  }
719  }
720  return false;
721 }
722 
723 
724 
725 
726 
727 void cItems::AddItemGrid(const cItemGrid & a_ItemGrid)
728 {
729  auto numSlots = a_ItemGrid.GetNumSlots();
730  for (int i = 0; i < numSlots; ++i)
731  {
732  Add(a_ItemGrid.GetSlot(i));
733  }
734 }
cItemHandler * GetHandler(void) const
Returns the cItemHandler responsible for this item type.
Definition: Item.cpp:137
short m_ItemDamage
Definition: Item.h:211
bool IsSword(short a_ItemID)
Definition: Defines.h:1043
bool Contains(const cItem &a_Item)
Definition: Item.cpp:695
bool IsFullStack(void) const
Returns true if the item is stacked up to its maximum stacking.
Definition: Item.cpp:119
int m_RepairCost
Definition: Item.h:221
bool DamageItem(short a_Amount=1)
Damages a weapon / tool.
Definition: Item.cpp:102
MTRand & GetRandomProvider()
Returns the current thread&#39;s random number source.
Definition: FastRandom.cpp:20
cItem & AddCount(char a_AmountToAdd)
Adds the specified count to this object and returns the reference to self (useful for chaining) ...
Definition: Item.cpp:26
bool IsHelmet(short a_ItemType)
Definition: Defines.h:1087
cColor m_ItemColor
Definition: Item.h:223
virtual char GetMaxStackSize(void)
Returns the maximum stack size for a given item.
static void RemoveEnchantmentWeightFromVector(cWeightedEnchantments &a_Enchantments, int a_EnchantmentID)
Remove the entire enchantment (with weight) from the vector.
cFireworkItem m_FireworkItem
Definition: Item.h:222
static bool IsEnchantable(short a_ItemType, bool a_FromBook=false)
Returns true if the specified item type is enchantable.
Definition: Item.cpp:243
void Clear(void)
Removes all enchantments.
bool IsHoe(short a_ItemID)
Definition: Defines.h:1054
bool IsValid() const
Returns whether the color is a valid color.
Definition: Color.h:28
unsigned int GetLevel(int a_EnchantmentID) const
Returns the level for the specified enchantment; 0 if not stored.
bool IsEqual(const cItem &a_Item) const
Definition: Item.h:123
static void AddItemEnchantmentWeights(cWeightedEnchantments &a_Enchantments, short a_ItemType, int a_EnchantmentLevel)
Add enchantment weights from item to the vector.
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
void AddFromString(const AString &a_StringSpec)
Adds enchantments in the stringspec; if a specified enchantment already exists, overwrites it...
cItem CopyOne(void) const
Returns a copy of this item with m_ItemCount set to 1.
Definition: Item.cpp:15
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 ...
static void ColoursFromString(const AString &a_String, cFireworkItem &a_FireworkItem)
Parses a string containing encoded firework colours and populates a FireworkItem with it...
void SetColor(unsigned char a_Red, unsigned char a_Green, unsigned char a_Blue)
Changes the color.
Definition: Color.cpp:19
cEnchantments m_Enchantments
Definition: Item.h:212
static void FadeColoursFromString(const AString &a_String, cFireworkItem &a_FireworkItem)
Parses a string containing encoded firework fade colours and populates a FireworkItem with it...
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:599
std::vector< cWeightedEnchantment > cWeightedEnchantments
Definition: Enchantments.h:23
void Set(int a_Idx, const cItem &a_Item)
Definition: Item.cpp:653
AString m_CustomName
Definition: Item.h:213
bool IsLoreEmpty(void) const
Definition: Item.h:149
short GetMaxDamage(void) const
Returns the maximum damage value that this item can have; zero if damage is not applied.
Definition: Item.cpp:40
void Delete(int a_Idx)
Definition: Item.cpp:667
char m_ItemCount
Definition: Item.h:210
unsigned char GetRed() const
Returns the red value of the color.
Definition: Color.cpp:55
bool IsSameType(const cItem &a_Item) const
Definition: Item.h:136
unsigned char GetBlue() const
Returns the blue value of the color.
Definition: Color.cpp:73
void LOGWARNING(const char *a_Format, fmt::ArgList a_ArgList)
Definition: Logger.cpp:174
bool ContainsType(const cItem &a_Item)
Definition: Item.cpp:711
void Empty(void)
Definition: Item.h:92
bool IsArmor(short a_ItemType)
Definition: Defines.h:1152
int GetEnchantability()
Returns the enchantability of the item.
Definition: Item.cpp:281
std::string AString
Definition: StringUtils.h:13
cItems(void)
Need a Lua-accessible constructor.
Definition: Item.h:250
cItem * Get(int a_Idx)
Definition: Item.cpp:639
bool IsAxe(short a_ItemID)
Definition: Defines.h:1032
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:434
unsigned char GetGreen() const
Returns the green value of the color.
Definition: Color.cpp:64
static unsigned int GetLevelCap(int a_EnchantmentID)
Get the maximum level the enchantment can have.
char GetMaxStackSize(void) const
Returns the maximum amount of stacked items of this type.
Definition: Item.cpp:128
static cEnchantments GetRandomEnchantmentFromVector(cWeightedEnchantments &a_Enchantments)
Gets random enchantment from Vector and returns it.
ENUM_ITEM_ID
Definition: BlockID.h:293
static int GetXPCostMultiplier(int a_EnchantmentID, bool FromBook)
Get the XP cost multiplier for the enchantment (for anvils).
bool IsShovel(short a_ItemID)
Definition: Defines.h:1065
void GetJson(Json::Value &a_OutValue) const
Saves the item data into JSON representation.
Definition: Item.cpp:146
AString ToString(void) const
Serializes all the enchantments into a string.
static AString ColoursToString(const cFireworkItem &a_FireworkItem)
Converts the firework&#39;s vector of colours into a string of values separated by a semicolon.
static AString FadeColoursToString(const cFireworkItem &a_FireworkItem)
Converts the firework&#39;s vector of fade colours into a string of values separated by a semicolon...
bool EnchantByXPLevels(int a_NumXPLevels)
Randomly enchants the item using the specified number of XP levels.
Definition: Item.cpp:354
AStringVector m_LoreTable
Definition: Item.h:217
Class that stores item enchantments or stored-enchantments The enchantments may be serialized to a st...
Definition: Enchantments.h:40
short m_ItemType
Definition: Item.h:209
const cItem & GetSlot(int a_X, int a_Y) const
Definition: ItemGrid.cpp:96
cItem(void)
Creates an empty item.
Definition: Item.h:40
int GetNumSlots(void) const
Definition: ItemGrid.h:40
void FromJson(const Json::Value &a_Value)
Loads the item data from JSON representation.
Definition: Item.cpp:197
bool CanHaveEnchantment(int a_EnchantmentID)
Returns whether or not this item is allowed to have the given enchantment.
Definition: Item.cpp:468
static void CheckEnchantmentConflictsFromVector(cWeightedEnchantments &a_Enchantments, cEnchantments a_FirstEnchantment)
Check enchantment conflicts from enchantments from the vector.
Definition: Item.h:36
bool IsBoots(short a_ItemType)
Definition: Defines.h:1126
cItemHandler * ItemHandler(int a_ItemType)
Definition: ItemHandler.h:174
bool IsCustomNameEmpty(void) const
Definition: Item.h:148
bool IsPickaxe(short a_ItemID)
Definition: Defines.h:1021
void AddItemGrid(const cItemGrid &a_ItemGrid)
Adds a copy of all items in a_ItemGrid.
Definition: Item.cpp:727
bool CanAddEnchantment(int a_EnchantmentID) const
Returns true if the given enchantment could be legally added to this object.