Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemHandler.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "../Defines.h"
5 #include "../Item.h"
6 
7 
8 
9 
10 
11 // fwd:
12 class cChunk;
13 class cWorld;
14 class cPlayer;
16 
17 
18 
19 
20 
22 {
23 public:
24 
25  friend class cItem;
26 
31  {
35  };
36 
37  constexpr cItemHandler(int a_ItemType) : m_ItemType(a_ItemType)
38  {
39  }
40 
41 
47  void OnPlayerPlace(cPlayer & a_Player, const cItem & a_HeldItem, Vector3i a_ClickedPosition, BLOCKTYPE a_ClickedBlockType, NIBBLETYPE a_ClickedBlockMeta, eBlockFace a_ClickedBlockFace, Vector3i a_CursorPosition) const;
48 
51  virtual bool OnItemUse(
52  cWorld * a_World,
53  cPlayer * a_Player,
54  cBlockPluginInterface & a_PluginInterface,
55  const cItem & a_HeldItem,
56  const Vector3i a_ClickedBlockPos,
57  eBlockFace a_ClickedBlockFace
58  ) const;
59 
61  virtual void OnItemShoot(cPlayer *, const Vector3i a_BlockPos, eBlockFace a_BlockFace) const
62  {
63  UNUSED(a_BlockPos);
64  UNUSED(a_BlockFace);
65  }
66 
68  virtual void OnUpdate(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item) const
69  {
70  UNUSED(a_World);
71  UNUSED(a_Player);
72  UNUSED(a_Item);
73  }
74 
76  virtual bool OnDiggingBlock(
77  cWorld * a_World, cPlayer * a_Player, const cItem & a_HeldItem,
78  const Vector3i a_ClickedBlockPos,
79  eBlockFace a_ClickedBlockFace
80  ) const;
81 
83  virtual void OnEntityAttack(cPlayer * a_Attacker, cEntity * a_AttackedEntity) const;
84 
86  virtual void OnFoodEaten(cWorld *a_World, cPlayer *a_Player, cItem *a_Item) const;
87 
91  virtual short GetDurabilityLossByAction(eDurabilityLostAction a_Action) const;
92 
94  virtual char GetMaxStackSize(void) const;
95 
96  struct FoodInfo
97  {
98  int FoodLevel;
99  double Saturation;
100 
101  constexpr FoodInfo(int a_FoodLevel, double a_Saturation) :
102  FoodLevel(a_FoodLevel),
103  Saturation(a_Saturation)
104  {
105  }
106  } ;
107 
109  virtual FoodInfo GetFoodInfo(const cItem * a_Item) const;
110 
112  virtual bool EatItem(cPlayer * a_Player, cItem * a_Item) const;
113 
115  virtual bool IsFood(void) const;
116 
118  virtual bool IsDrinkable(short a_ItemDamage) const;
119 
121  virtual bool IsPlaceable(void) const;
122 
124  virtual bool CanRepairWithRawMaterial(short a_ItemType) const;
125 
128  virtual bool CanHarvestBlock(BLOCKTYPE a_BlockType) const;
129 
132  virtual float GetBlockBreakingStrength(BLOCKTYPE a_Block) const;
133 
134 
135 protected:
136 
137  static const cItemHandler & For(int a_ItemType);
138 
139  ~cItemHandler() = default;
140 
141  const int m_ItemType;
142 
147  virtual bool CommitPlacement(cPlayer & a_Player, const cItem & a_HeldItem, Vector3i a_PlacePosition, eBlockFace a_ClickedBlockFace, Vector3i a_CursorPosition) const;
148 };
149 
cItemHandler::FoodInfo::FoodLevel
int FoodLevel
Definition: ItemHandler.h:98
cItemHandler::FoodInfo::FoodInfo
constexpr FoodInfo(int a_FoodLevel, double a_Saturation)
Definition: ItemHandler.h:101
cItemHandler::CanHarvestBlock
virtual bool CanHarvestBlock(BLOCKTYPE a_BlockType) const
Returns whether this tool / item can harvest a specific block (e.g.
Definition: ItemHandler.cpp:1343
cItemHandler::dlaBreakBlockInstant
@ dlaBreakBlockInstant
Definition: ItemHandler.h:34
cItemHandler::EatItem
virtual bool EatItem(cPlayer *a_Player, cItem *a_Item) const
Lets the player eat a selected item.
Definition: ItemHandler.cpp:1446
cItemHandler::For
static const cItemHandler & For(int a_ItemType)
Definition: ItemHandler.cpp:569
cItemHandler::dlaBreakBlock
@ dlaBreakBlock
Definition: ItemHandler.h:33
cItemHandler::OnItemShoot
virtual void OnItemShoot(cPlayer *, const Vector3i a_BlockPos, eBlockFace a_BlockFace) const
Called when the client sends the SHOOT status in the lclk packet (releasing the bow).
Definition: ItemHandler.h:61
cItemHandler::~cItemHandler
~cItemHandler()=default
NIBBLETYPE
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
cItemHandler::GetMaxStackSize
virtual char GetMaxStackSize(void) const
Returns the maximum stack size for a given item.
Definition: ItemHandler.cpp:1175
cItemHandler::GetBlockBreakingStrength
virtual float GetBlockBreakingStrength(BLOCKTYPE a_Block) const
Returns the strength to break a specific block.
Definition: ItemHandler.cpp:1466
cItemHandler::IsFood
virtual bool IsFood(void) const
Indicates if this item is food.
Definition: ItemHandler.cpp:1303
cWorld
Definition: World.h:47
cBlockPluginInterface
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
Definition: BlockPluginInterface.h:27
cItemHandler::IsDrinkable
virtual bool IsDrinkable(short a_ItemDamage) const
Indicates if this item is drinkable.
Definition: ItemHandler.cpp:1312
cItemHandler::OnItemUse
virtual bool OnItemUse(cWorld *a_World, cPlayer *a_Player, cBlockPluginInterface &a_PluginInterface, const cItem &a_HeldItem, const Vector3i a_ClickedBlockPos, eBlockFace a_ClickedBlockFace) const
Called when the player tries to use the item (right mouse button).
Definition: ItemHandler.cpp:1103
cItemHandler::OnPlayerPlace
void OnPlayerPlace(cPlayer &a_Player, const cItem &a_HeldItem, Vector3i a_ClickedPosition, BLOCKTYPE a_ClickedBlockType, NIBBLETYPE a_ClickedBlockMeta, eBlockFace a_ClickedBlockFace, Vector3i a_CursorPosition) const
Called when the player tries to place the item (right mouse button, IsPlaceable() == true).
Definition: ItemHandler.cpp:1045
cItemHandler::FoodInfo::Saturation
double Saturation
Definition: ItemHandler.h:99
cItem
Definition: Item.h:36
cItemHandler::dlaAttackEntity
@ dlaAttackEntity
Definition: ItemHandler.h:32
cItemHandler::eDurabilityLostAction
eDurabilityLostAction
Actions that may cause durability of an item may be lost, where the magnitude of the loss depends on ...
Definition: ItemHandler.h:30
cItemHandler::OnDiggingBlock
virtual bool OnDiggingBlock(cWorld *a_World, cPlayer *a_Player, const cItem &a_HeldItem, const Vector3i a_ClickedBlockPos, eBlockFace a_ClickedBlockFace) const
Called while the player digs a block using this item.
Definition: ItemHandler.cpp:1122
cItemHandler::GetFoodInfo
virtual FoodInfo GetFoodInfo(const cItem *a_Item) const
Returns the FoodInfo for this item.
Definition: ItemHandler.cpp:1456
cItemHandler::cItemHandler
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
cItemHandler::IsPlaceable
virtual bool IsPlaceable(void) const
Blocks simply get placed.
Definition: ItemHandler.cpp:1323
cItemHandler::GetDurabilityLossByAction
virtual short GetDurabilityLossByAction(eDurabilityLostAction a_Action) const
Get the durability lost which the item will get, when a specified action was performed.
Definition: ItemHandler.cpp:1164
cItemHandler::OnFoodEaten
virtual void OnFoodEaten(cWorld *a_World, cPlayer *a_Player, cItem *a_Item) const
Called after the player has eaten this item.
Definition: ItemHandler.cpp:1153
BLOCKTYPE
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
cItemHandler::OnEntityAttack
virtual void OnEntityAttack(cPlayer *a_Attacker, cEntity *a_AttackedEntity) const
Called when a player attacks an entity with this item in hand.
Definition: ItemHandler.cpp:1143
cItemHandler::m_ItemType
const int m_ItemType
Definition: ItemHandler.h:141
cItemHandler::CanRepairWithRawMaterial
virtual bool CanRepairWithRawMaterial(short a_ItemType) const
Can the anvil repair this item, when a_Item is the second input?
Definition: ItemHandler.cpp:1333
eBlockFace
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:37
cChunk
Definition: Chunk.h:35
cEntity
Definition: Entity.h:75
cPlayer
Definition: Player.h:27
UNUSED
#define UNUSED
Definition: Globals.h:72
cItemHandler::FoodInfo
Definition: ItemHandler.h:96
cItemHandler
Definition: ItemHandler.h:21
cItemHandler::OnUpdate
virtual void OnUpdate(cWorld *a_World, cPlayer *a_Player, const cItem &a_Item) const
Called every tick while the item is on the player's inventory (used by maps, for example) - For now,...
Definition: ItemHandler.h:68
cItemHandler::CommitPlacement
virtual bool CommitPlacement(cPlayer &a_Player, const cItem &a_HeldItem, Vector3i a_PlacePosition, eBlockFace a_ClickedBlockFace, Vector3i a_CursorPosition) const
Performs the actual placement of this placeable item.
Definition: ItemHandler.cpp:1475
Vector3
Definition: Vector3.h:10