Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemLilypad.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemHandler.h"
5 #include "../Entities/Player.h"
6 #include "../LineBlockTracer.h"
7 
8 
9 
10 
11 
13  public cItemHandler
14 {
16 
17 public:
18  cItemLilypadHandler(int a_ItemType):
19  super(a_ItemType)
20  {
21 
22  }
23 
24 
25  virtual bool IsPlaceable(void) override
26  {
27  return false; // Set as not placeable so OnItemUse is called
28  }
29 
30 
31 
32  virtual bool OnItemUse(
33  cWorld * a_World, cPlayer * a_Player, cBlockPluginInterface & a_PluginInterface, const cItem & a_Item,
34  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace
35  ) override
36  {
37  if (a_BlockFace > BLOCK_FACE_NONE)
38  {
39  // Clicked on the side of a submerged block; vanilla allows placement, so should we
40  AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
41  a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_LILY_PAD, 0);
42  if (!a_Player->IsGameModeCreative())
43  {
44  a_Player->GetInventory().RemoveOneEquippedItem();
45  }
46  return true;
47  }
48 
49  class cCallbacks :
50  public cBlockTracer::cCallbacks
51  {
52  public:
53 
54  cCallbacks(void) :
55  m_HasHitFluid(false)
56  {
57  }
58 
59  virtual bool OnNextBlock(int a_CBBlockX, int a_CBBlockY, int a_CBBlockZ, BLOCKTYPE a_CBBlockType, NIBBLETYPE a_CBBlockMeta, eBlockFace a_CBEntryFace) override
60  {
61  if (IsBlockWater(a_CBBlockType))
62  {
63  if ((a_CBBlockMeta != 0) || (a_CBEntryFace == BLOCK_FACE_NONE)) // The hit block should be a source. The FACE_NONE check is clicking whilst submerged
64  {
65  return false;
66  }
67  AddFaceDirection(a_CBBlockX, a_CBBlockY, a_CBBlockZ, BLOCK_FACE_YP); // Always place pad at top of water block
68  if (
69  !IsBlockWater(a_CBBlockType) &&
70  cBlockInfo::FullyOccupiesVoxel(a_CBBlockType)
71  )
72  {
73  // Can't place lilypad on air / in another block!
74  return true;
75  }
76  m_HasHitFluid = true;
77  m_Pos.Set(a_CBBlockX, a_CBBlockY, a_CBBlockZ);
78  return true;
79  }
80  return false;
81  }
82 
83  Vector3i m_Pos;
84  bool m_HasHitFluid;
85 
86  };
87 
88  cCallbacks Callbacks;
89  cLineBlockTracer Tracer(*a_Player->GetWorld(), Callbacks);
90  Vector3d Start(a_Player->GetEyePosition() + a_Player->GetLookVector());
91  Vector3d End(a_Player->GetEyePosition() + a_Player->GetLookVector() * 5);
92 
93  Tracer.Trace(Start.x, Start.y, Start.z, End.x, End.y, End.z);
94 
95  if (Callbacks.m_HasHitFluid)
96  {
97  a_World->SetBlock(Callbacks.m_Pos.x, Callbacks.m_Pos.y, Callbacks.m_Pos.z, E_BLOCK_LILY_PAD, 0);
98  if (!a_Player->IsGameModeCreative())
99  {
100  a_Player->GetInventory().RemoveOneEquippedItem();
101  }
102  return true;
103  }
104 
105  return false;
106  }
107 };
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
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
cItemLilypadHandler(int a_ItemType)
Definition: ItemLilypad.h:18
virtual bool IsPlaceable(void) override
Blocks simply get placed.
Definition: ItemLilypad.h:25
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld...
void AddFaceDirection(int &a_BlockX, int &a_BlockY, int &a_BlockZ, eBlockFace a_BlockFace, bool a_bInverse=false)
Definition: Defines.h:859
Vector3d GetLookVector(void) const
Definition: Entity.cpp:2209
Definition: World.h:65
static bool FullyOccupiesVoxel(BLOCKTYPE a_Type)
Definition: BlockInfo.h:50
cItemHandler super
Definition: ItemLilypad.h:15
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
Vector3d GetEyePosition(void) const
Definition: Player.cpp:1251
bool IsGameModeCreative(void) const
Returns true if the player is in Creative mode, either explicitly, or by inheriting from current worl...
Definition: Player.cpp:1260
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: ItemLilypad.h:32
cInventory & GetInventory(void)
Definition: Player.h:136
void SetBlock(Vector3i a_BlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Sets the block at the specified coords to the specified value.
Definition: World.cpp:1873
Definition: Item.h:36
cWorld * GetWorld(void) const
Definition: Entity.h:201