Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemArmor.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemHandler.h"
5 #include "../World.h"
6 
7 
8 
9 
10 
11 class cItemArmorHandler final :
12  public cItemHandler
13 {
15 
16 public:
17 
18  using Super::Super;
19 
20 
21 
22 
23 
25  virtual bool OnItemUse(
26  cWorld * a_World,
27  cPlayer * a_Player,
28  cBlockPluginInterface & a_PluginInterface,
29  const cItem & a_HeldItem,
30  const Vector3i a_ClickedBlockPos,
31  eBlockFace a_ClickedBlockFace
32  ) const override
33  {
34  int SlotNum;
35  if (ItemCategory::IsHelmet(a_HeldItem.m_ItemType))
36  {
37  SlotNum = 0;
38  }
39  else if (ItemCategory::IsChestPlate(a_HeldItem.m_ItemType))
40  {
41  SlotNum = 1;
42  }
43  else if (ItemCategory::IsLeggings(a_HeldItem.m_ItemType))
44  {
45  SlotNum = 2;
46  }
47  else if (ItemCategory::IsBoots(a_HeldItem.m_ItemType))
48  {
49  SlotNum = 3;
50  }
51  else
52  {
53  LOGWARNING("Used unknown armor: %i", a_HeldItem.m_ItemType);
54  return false;
55  }
56 
57  if (!a_Player->GetInventory().GetArmorSlot(SlotNum).IsEmpty())
58  {
59  return false;
60  }
61 
62  a_Player->GetInventory().SetArmorSlot(SlotNum, a_HeldItem.CopyOne());
63  a_Player->GetInventory().RemoveOneEquippedItem();
64  return true;
65  }
66 
67 
68 
69 
70 
71  virtual bool CanRepairWithRawMaterial(short a_ItemType) const override
72  {
73  switch (m_ItemType)
74  {
75  case E_ITEM_CHAIN_BOOTS:
79  {
80  return (a_ItemType == E_ITEM_IRON);
81  }
86  {
87  return (a_ItemType == E_ITEM_DIAMOND);
88  }
89  case E_ITEM_IRON_BOOTS:
91  case E_ITEM_IRON_HELMET:
93  {
94  return (a_ItemType == E_ITEM_IRON);
95  }
96  case E_ITEM_GOLD_BOOTS:
98  case E_ITEM_GOLD_HELMET:
100  {
101  return (a_ItemType == E_ITEM_GOLD);
102  }
103  case E_ITEM_ELYTRA: // TODO: require Phantom Membrane instead of leather starting from protocol version 369 or 1.13 release
105  case E_ITEM_LEATHER_CAP:
108  {
109  return (a_ItemType == E_ITEM_LEATHER);
110  }
111  }
112  return false;
113  }
114 
115 } ;
116 
117 
118 
119 
@ E_ITEM_IRON
Definition: BlockType.h:309
@ E_ITEM_GOLD_BOOTS
Definition: BlockType.h:361
@ E_ITEM_LEATHER_PANTS
Definition: BlockType.h:344
@ E_ITEM_CHAIN_BOOTS
Definition: BlockType.h:349
@ E_ITEM_IRON_LEGGINGS
Definition: BlockType.h:352
@ E_ITEM_DIAMOND_BOOTS
Definition: BlockType.h:357
@ E_ITEM_GOLD
Definition: BlockType.h:310
@ E_ITEM_CHAIN_HELMET
Definition: BlockType.h:346
@ E_ITEM_GOLD_HELMET
Definition: BlockType.h:358
@ E_ITEM_GOLD_CHESTPLATE
Definition: BlockType.h:359
@ E_ITEM_IRON_HELMET
Definition: BlockType.h:350
@ E_ITEM_IRON_CHESTPLATE
Definition: BlockType.h:351
@ E_ITEM_GOLD_LEGGINGS
Definition: BlockType.h:360
@ E_ITEM_DIAMOND_CHESTPLATE
Definition: BlockType.h:355
@ E_ITEM_DIAMOND_HELMET
Definition: BlockType.h:354
@ E_ITEM_IRON_BOOTS
Definition: BlockType.h:353
@ E_ITEM_DIAMOND
Definition: BlockType.h:308
@ E_ITEM_LEATHER_TUNIC
Definition: BlockType.h:343
@ E_ITEM_ELYTRA
Definition: BlockType.h:490
@ E_ITEM_LEATHER
Definition: BlockType.h:378
@ E_ITEM_DIAMOND_LEGGINGS
Definition: BlockType.h:356
@ E_ITEM_LEATHER_CAP
Definition: BlockType.h:342
@ E_ITEM_CHAIN_CHESTPLATE
Definition: BlockType.h:347
@ E_ITEM_LEATHER_BOOTS
Definition: BlockType.h:345
@ E_ITEM_CHAIN_LEGGINGS
Definition: BlockType.h:348
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
void LOGWARNING(std::string_view a_Format, const Args &... args)
Definition: LoggerSimple.h:67
bool IsHelmet(short a_ItemType)
Definition: Defines.cpp:512
bool IsLeggings(short a_ItemType)
Definition: Defines.cpp:543
bool IsChestPlate(short a_ItemType)
Definition: Defines.cpp:527
bool IsBoots(short a_ItemType)
Definition: Defines.cpp:558
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
Definition: Player.h:29
cInventory & GetInventory(void)
Definition: Player.h:156
const cItem & GetArmorSlot(int a_ArmorSlotNum) const
Returns current item in a_ArmorSlotNum in armor slots.
Definition: Inventory.cpp:412
void SetArmorSlot(int a_ArmorSlotNum, const cItem &a_Item)
Puts a_Item item in a_ArmorSlotNum slot number in armor slots.
Definition: Inventory.cpp:335
bool RemoveOneEquippedItem(void)
Removes one item out of the currently equipped item stack, returns true if successful,...
Definition: Inventory.cpp:232
Definition: Item.h:37
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
short m_ItemType
Definition: Item.h:163
cItem CopyOne(void) const
Returns a copy of this item with m_ItemCount set to 1.
Definition: Item.cpp:93
virtual bool CanRepairWithRawMaterial(short a_ItemType) const override
Can the anvil repair this item, when a_Item is the second input?
Definition: ItemArmor.h:71
virtual bool OnItemUse(cWorld *a_World, cPlayer *a_Player, cBlockPluginInterface &a_PluginInterface, const cItem &a_HeldItem, const Vector3i a_ClickedBlockPos, eBlockFace a_ClickedBlockFace) const override
Move the armor to the armor slot of the player's inventory.
Definition: ItemArmor.h:25
const int m_ItemType
Definition: ItemHandler.h:141
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
Definition: World.h:53