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 cWorld;
13 class cPlayer;
15 
16 
17 
18 
19 
21 {
22 public:
23 
28  {
32  };
33 
34  cItemHandler(int a_ItemType);
35 
37  virtual ~cItemHandler() {}
38 
39 
46  virtual bool OnPlayerPlace(
47  cWorld & a_World, cPlayer & a_Player, const cItem & a_EquippedItem,
48  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace,
49  int a_CursorX, int a_CursorY, int a_CursorZ
50  );
51 
52 
58  virtual bool GetBlocksToPlace(
59  cWorld & a_World, cPlayer & a_Player, const cItem & a_EquippedItem,
60  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace,
61  int a_CursorX, int a_CursorY, int a_CursorZ,
62  sSetBlockVector & a_BlocksToSet
63  );
64 
65 
69  virtual bool GetPlacementBlockTypeMeta(
70  cWorld * a_World, cPlayer * a_Player,
71  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace,
72  int a_CursorX, int a_CursorY, int a_CursorZ,
73  BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta
74  );
75 
76 
79  virtual bool OnItemUse(
80  cWorld * a_World, cPlayer * a_Player, cBlockPluginInterface & a_PluginInterface, const cItem & a_Item,
81  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace
82  );
83 
84 
86  virtual void OnItemShoot(cPlayer *, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace)
87  {
88  UNUSED(a_BlockX);
89  UNUSED(a_BlockY);
90  UNUSED(a_BlockZ);
91  UNUSED(a_BlockFace);
92  }
93 
95  virtual void OnUpdate(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item)
96  {
97  UNUSED(a_World);
98  UNUSED(a_Player);
99  UNUSED(a_Item);
100  }
101 
103  virtual bool OnDiggingBlock(cWorld * a_World, cPlayer * a_Player, const cItem & a_HeldItem, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace);
104 
106  virtual void OnEntityAttack(cPlayer * a_Attacker, cEntity * a_AttackedEntity);
107 
109  virtual void OnFoodEaten(cWorld *a_World, cPlayer *a_Player, cItem *a_Item);
110 
114  virtual short GetDurabilityLossByAction(eDurabilityLostAction a_Action);
115 
117  virtual char GetMaxStackSize(void);
118 
119  struct FoodInfo
120  {
122  double Saturation;
123 
124  FoodInfo(int a_FoodLevel, double a_Saturation) :
125  FoodLevel(a_FoodLevel),
126  Saturation(a_Saturation)
127  {
128  }
129  } ;
130 
132  virtual FoodInfo GetFoodInfo(const cItem * a_Item);
133 
135  virtual bool EatItem(cPlayer * a_Player, cItem * a_Item);
136 
138  virtual bool IsTool(void);
139 
141  virtual bool IsFood(void);
142 
144  virtual bool IsDrinkable(short a_ItemDamage);
145 
147  virtual bool IsPlaceable(void);
148 
150  virtual bool CanRepairWithRawMaterial(short a_ItemType);
151 
154  virtual bool CanHarvestBlock(BLOCKTYPE a_BlockType);
155 
158  virtual float GetBlockBreakingStrength(BLOCKTYPE a_Block);
159 
160  static cItemHandler * GetItemHandler(int a_ItemType);
161  static cItemHandler * GetItemHandler(const cItem & a_Item) { return GetItemHandler(a_Item.m_ItemType); }
162 
163  static void Deinit();
164 
165 protected:
167  static cItemHandler * CreateItemHandler(int m_ItemType);
168 
170  static bool m_HandlerInitialized; // used to detect if the itemhandlers are initialized
171 };
172 
173 // Short function
174 inline cItemHandler *ItemHandler(int a_ItemType) { return cItemHandler::GetItemHandler(a_ItemType); }
static bool m_HandlerInitialized
Definition: ItemHandler.h:170
cItemHandler(int a_ItemType)
virtual bool CanHarvestBlock(BLOCKTYPE a_BlockType)
Returns whether this tool / item can harvest a specific block (e.g.
static cItemHandler * GetItemHandler(const cItem &a_Item)
Definition: ItemHandler.h:161
virtual bool OnItemUse(cWorld *a_World, cPlayer *a_Player, cBlockPluginInterface &a_PluginInterface, const cItem &a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace)
Called when the player tries to use the item (right mouse button).
virtual void OnItemShoot(cPlayer *, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace)
Called when the client sends the SHOOT status in the lclk packet.
Definition: ItemHandler.h:86
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
static cItemHandler * CreateItemHandler(int m_ItemType)
virtual float GetBlockBreakingStrength(BLOCKTYPE a_Block)
Returns the strength to break a specific block.
virtual char GetMaxStackSize(void)
Returns the maximum stack size for a given item.
Definition: Player.h:27
virtual bool CanRepairWithRawMaterial(short a_ItemType)
Can the anvil repair this item, when a_Item is the second input?
static cItemHandler * GetItemHandler(int a_ItemType)
Definition: ItemHandler.cpp:74
virtual short GetDurabilityLossByAction(eDurabilityLostAction a_Action)
Get the durability lost which the item will get, when a specified action was performed.
virtual bool GetBlocksToPlace(cWorld &a_World, cPlayer &a_Player, const cItem &a_EquippedItem, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, sSetBlockVector &a_BlocksToSet)
Called from OnPlayerPlace() to determine the blocks that the current placement operation should set...
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
virtual bool OnDiggingBlock(cWorld *a_World, cPlayer *a_Player, const cItem &a_HeldItem, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace)
Called while the player digs a block using this item.
virtual void OnFoodEaten(cWorld *a_World, cPlayer *a_Player, cItem *a_Item)
Called after the player has eaten this item.
virtual ~cItemHandler()
Force virtual destructor.
Definition: ItemHandler.h:37
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld...
Definition: World.h:65
virtual FoodInfo GetFoodInfo(const cItem *a_Item)
Returns the FoodInfo for this item.
static cItemHandler * m_ItemHandler[E_ITEM_LAST+1]
Definition: ItemHandler.h:169
static void Deinit()
virtual bool IsTool(void)
Indicates if this item is a tool.
virtual bool OnPlayerPlace(cWorld &a_World, cPlayer &a_Player, const cItem &a_EquippedItem, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ)
Called when the player tries to place the item (right mouse button, IsPlaceable() == true)...
virtual void OnUpdate(cWorld *a_World, cPlayer *a_Player, const cItem &a_Item)
Called every tick while the item is on the player's inventory (used by maps, for example) - For now...
Definition: ItemHandler.h:95
virtual bool GetPlacementBlockTypeMeta(cWorld *a_World, cPlayer *a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta)
Called when the player right-clicks with this item and IsPlaceable() == true, and OnPlayerPlace() is ...
#define UNUSED
Definition: Globals.h:152
Maximum valid ItemType.
Definition: BlockID.h:523
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
virtual bool IsPlaceable(void)
Blocks simply get placed.
virtual bool IsDrinkable(short a_ItemDamage)
Indicates if this item is drinkable.
Definition: Entity.h:73
short m_ItemType
Definition: Item.h:209
virtual void OnEntityAttack(cPlayer *a_Attacker, cEntity *a_AttackedEntity)
Called when a player attacks a other entity.
Definition: Item.h:36
eDurabilityLostAction
Actions that may cause durability of an item may be lost, where the magnitude of the loss depends on ...
Definition: ItemHandler.h:27
virtual bool IsFood(void)
Indicates if this item is food.
FoodInfo(int a_FoodLevel, double a_Saturation)
Definition: ItemHandler.h:124
cItemHandler * ItemHandler(int a_ItemType)
Definition: ItemHandler.h:174
std::vector< sSetBlock > sSetBlockVector
Definition: ChunkDef.h:564
virtual bool EatItem(cPlayer *a_Player, cItem *a_Item)
Lets the player eat a selected item.