Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemBottle.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 
12  public cItemHandler
13 {
14 public:
17  {
18  }
19 
20 
21  bool GetBlockFromTrace(cWorld * a_World, cPlayer * a_Player, Vector3i & a_BlockPos)
22  {
23  class cCallbacks :
24  public cBlockTracer::cCallbacks
25  {
26  public:
27  Vector3i m_Pos;
28  bool m_HasHitFluid;
29 
30 
31  cCallbacks(void) :
32  m_HasHitFluid(false)
33  {
34  }
35 
36  virtual bool OnNextBlock(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, eBlockFace a_EntryFace) override
37  {
38  if (IsBlockWater(a_BlockType))
39  {
40  if (a_BlockMeta != 0) // we're only looking for source blocks
41  {
42  return false;
43  }
44  m_HasHitFluid = true;
45  m_Pos.Set(a_BlockX, a_BlockY, a_BlockZ);
46  return true;
47  }
48  return false;
49  }
50  } Callbacks;
51 
52  cLineBlockTracer Tracer(*a_World, Callbacks);
53  Vector3d Start(a_Player->GetEyePosition() + a_Player->GetLookVector());
54  Vector3d End(a_Player->GetEyePosition() + a_Player->GetLookVector() * 5);
55 
56  Tracer.Trace(Start.x, Start.y, Start.z, End.x, End.y, End.z);
57 
58  if (!Callbacks.m_HasHitFluid)
59  {
60  return false;
61  }
62 
63 
64  a_BlockPos = Callbacks.m_Pos;
65  return true;
66  }
67 
68 
69 
70  virtual bool OnItemUse(
71  cWorld * a_World, cPlayer * a_Player, cBlockPluginInterface & a_PluginInterface, const cItem & a_Item,
72  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace
73  ) override
74  {
75  if (a_BlockFace != BLOCK_FACE_NONE)
76  {
77  return false;
78  }
79 
80  Vector3i BlockPos;
81  if (!GetBlockFromTrace(a_World, a_Player, BlockPos))
82  {
83  return false; // Nothing in range.
84  }
85 
86  a_Player->GetInventory().RemoveOneEquippedItem();
87  cItem NewItem(E_ITEM_POTION, 1, 0);
88  a_Player->GetInventory().AddItem(NewItem);
89  return true;
90  }
91 } ;
92 
93 
94 
95 
bool IsBlockWater(BLOCKTYPE a_BlockType)
Definition: Defines.h:436
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
bool RemoveOneEquippedItem(void)
Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed.
Definition: Inventory.cpp:207
Definition: Player.h:27
bool GetBlockFromTrace(cWorld *a_World, cPlayer *a_Player, Vector3i &a_BlockPos)
Definition: ItemBottle.h:21
void Set(T a_x, T a_y, T a_z)
Definition: Vector3.h:37
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
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) override
Called when the player tries to use the item (right mouse button).
Definition: ItemBottle.h:70
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld...
Vector3d GetLookVector(void) const
Definition: Entity.cpp:2209
Definition: World.h:65
int AddItem(const cItem &a_ItemStack, bool a_AllowNewStacks=true)
Adds as many items out of a_ItemStack as can fit.
Definition: Inventory.cpp:106
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
bool Trace(double a_StartX, double a_StartY, double a_StartZ, double a_EndX, double a_EndY, double a_EndZ)
Traces one line between Start and End; returns true if the entire line was traced (until OnNoMoreHits...
Vector3d GetEyePosition(void) const
Definition: Player.cpp:1251
cInventory & GetInventory(void)
Definition: Player.h:136
Definition: Item.h:36