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 "../BlockInfo.h"
6 #include "../World.h"
7 
8 
9 
10 
11 
12 class cItemBottleHandler final:
13  public cItemHandler
14 {
16 
17 public:
18 
19  using Super::Super;
20 
21 
22 
23 
24 
28  bool GetBlockFromTrace(cWorld * a_World, cPlayer * a_Player, Vector3i & a_BlockPos) const
29  {
30  class cCallbacks:
31  public cBlockTracer::cCallbacks
32  {
33  public:
34  Vector3i m_Pos;
35  bool m_HasHitFluid;
36 
37 
38  cCallbacks():
39  m_HasHitFluid(false)
40  {
41  }
42 
43  virtual bool OnNextBlock(Vector3i a_BlockPosition, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, eBlockFace a_EntryFace) override
44  {
45  if (IsBlockWater(a_BlockType))
46  {
47  if (a_BlockMeta != 0) // we're only looking for source blocks
48  {
49  return false;
50  }
51  m_HasHitFluid = true;
52  m_Pos = a_BlockPosition;
53  return true;
54  }
55  return false;
56  }
57  } Callbacks;
58  auto Start = a_Player->GetEyePosition() + a_Player->GetLookVector();
59  auto End = a_Player->GetEyePosition() + a_Player->GetLookVector() * 5;
60  cLineBlockTracer::Trace(*a_World, Callbacks, Start, End);
61  if (!Callbacks.m_HasHitFluid)
62  {
63  return false;
64  }
65 
66  a_BlockPos = Callbacks.m_Pos;
67  return true;
68  }
69 
70 
71 
72 
73 
74  virtual bool OnItemUse(
75  cWorld * a_World,
76  cPlayer * a_Player,
77  cBlockPluginInterface & a_PluginInterface,
78  const cItem & a_HeldItem,
79  const Vector3i a_ClickedBlockPos,
80  eBlockFace a_ClickedBlockFace
81  ) const override
82  {
83  if (a_ClickedBlockFace != BLOCK_FACE_NONE)
84  {
85  return false;
86  }
87 
88  Vector3i BlockPos;
89  if (!GetBlockFromTrace(a_World, a_Player, BlockPos))
90  {
91  return false; // Nothing in range.
92  }
93 
94  // Give back a filled water bottle if gamemode is not creative:
95  if (!a_Player->IsGameModeCreative())
96  {
98  }
99  return true;
100  }
101 } ;
102 
103 
104 
105 
bool IsBlockWater(BLOCKTYPE a_BlockType)
Definition: BlockInfo.cpp:10
@ E_ITEM_POTION
Definition: BlockType.h:418
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
@ BLOCK_FACE_NONE
Definition: Defines.h:39
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
Vector3d GetLookVector(void) const
Definition: Entity.cpp:2267
Definition: Player.h:29
bool IsGameModeCreative(void) const
Returns true if the player is in Creative mode, either explicitly, or by inheriting from current worl...
Definition: Player.cpp:1025
Vector3d GetEyePosition(void) const
Definition: Player.cpp:1001
void ReplaceOneEquippedItemTossRest(const cItem &)
Removes one item from the the current equipped item stack, and attempts to add the specified item sta...
Definition: Player.cpp:1745
Definition: Item.h:37
bool GetBlockFromTrace(cWorld *a_World, cPlayer *a_Player, Vector3i &a_BlockPos) const
Searches for a water source block in the line of sight.
Definition: ItemBottle.h:28
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
Called when the player tries to use the item (right mouse button).
Definition: ItemBottle.h:74
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
friend class cItem
Definition: ItemHandler.h:25
bool Trace(Vector3d a_Start, Vector3d a_End)
Traces one line between Start and End; returns true if the entire line was traced (until OnNoMoreHits...
Definition: World.h:53