Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemBoat.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "../Entities/Boat.h"
5 #include "../LineBlockTracer.h"
6 
7 
8 
9 
10 
11 class cItemBoatHandler final:
12  public cItemHandler
13 {
15 
16 public:
17 
18  using Super::Super;
19 
20 
21 
22 
23 
24  virtual bool OnItemUse(
25  cWorld * a_World,
26  cPlayer * a_Player,
27  cBlockPluginInterface & a_PluginInterface,
28  const cItem & a_HeldItem,
29  const Vector3i a_ClickedBlockPos,
30  eBlockFace a_ClickedBlockFace
31  ) const override
32  {
33  // Only allow placing blocks on top of blocks, or when not in range of dest block:
34  if ((a_ClickedBlockFace != BLOCK_FACE_YM) && (a_ClickedBlockFace != BLOCK_FACE_NONE))
35  {
36  return false;
37  }
38 
39  // Find the actual placement position by tracing line of sight until non-air block:
40  class cCallbacks:
41  public cBlockTracer::cCallbacks
42  {
43  public:
44  Vector3d m_Pos;
45  bool m_HasFound;
46 
47  cCallbacks():
48  m_HasFound(false)
49  {
50  }
51 
52  virtual bool OnNextBlock(Vector3i a_CBBlockPos, BLOCKTYPE a_CBBlockType, NIBBLETYPE a_CBBlockMeta, eBlockFace a_CBEntryFace) override
53  {
54  if (a_CBBlockType != E_BLOCK_AIR)
55  {
56  m_Pos = a_CBBlockPos;
57  m_HasFound = true;
58  return true;
59  }
60  return false;
61  }
62  } Callbacks;
63  auto Start = a_Player->GetEyePosition() + a_Player->GetLookVector();
64  auto End = a_Player->GetEyePosition() + a_Player->GetLookVector() * 5;
65  cLineBlockTracer::Trace(*a_World, Callbacks, Start, End);
66  if (!Callbacks.m_HasFound)
67  {
68  return false;
69  }
70 
71  // Block above must be air to spawn a boat (prevents spawning a boat underwater)
72  auto PosAbove = Callbacks.m_Pos.Floor().addedY(1);
73  if (!cChunkDef::IsValidHeight(PosAbove))
74  {
75  return false;
76  }
77  BLOCKTYPE BlockAbove = a_World->GetBlock(PosAbove);
78  if (BlockAbove != E_BLOCK_AIR)
79  {
80  return false;
81  }
82 
83  // Spawn block at water level
84  if (a_World->SpawnBoat(Callbacks.m_Pos + Vector3d(0.5, 1, 0.5), cBoat::ItemToMaterial(a_Player->GetEquippedItem())) == cEntity::INVALID_ID)
85  {
86  return false;
87  }
88 
89  // Remove boat from players hand
90  if (!a_Player->IsGameModeCreative())
91  {
92  a_Player->GetInventory().RemoveOneEquippedItem();
93  }
94 
95  return true;
96  }
97 } ;
@ E_BLOCK_AIR
Definition: BlockType.h:10
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_YM
Definition: Defines.h:42
@ BLOCK_FACE_NONE
Definition: Defines.h:39
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
static bool IsValidHeight(Vector3i a_BlockPosition)
Validates a height-coordinate.
Definition: ChunkDef.h:185
static eMaterial ItemToMaterial(const cItem &a_Item)
Returns the eMaterial that should be used for a boat created from the specified item.
Definition: Boat.cpp:253
static const UInt32 INVALID_ID
Special ID that is considered an "invalid value", signifying no entity.
Definition: Entity.h:128
Vector3d GetLookVector(void) const
Definition: Entity.cpp:2267
Definition: Player.h:29
const cItem & GetEquippedItem(void) const
Definition: Player.h:162
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
cInventory & GetInventory(void)
Definition: Player.h:156
Vector3d GetEyePosition(void) const
Definition: Player.cpp:1001
bool RemoveOneEquippedItem(void)
Removes one item out of the currently equipped item stack, returns true if successful,...
Definition: Inventory.cpp:232
Definition: Item.h:37
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: ItemBoat.h:24
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
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
UInt32 SpawnBoat(double a_X, double a_Y, double a_Z, cBoat::eMaterial a_Material)
Definition: World.h:480
BLOCKTYPE GetBlock(Vector3i a_BlockPos) const
Returns the block type at the specified position.
Definition: World.h:363