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 
12  public cItemHandler
13 {
15 
16 public:
17  cItemBoatHandler(int a_ItemType) :
18  super(a_ItemType)
19  {
20  }
21 
22 
23 
24  virtual bool OnItemUse(
25  cWorld * a_World, cPlayer * a_Player, cBlockPluginInterface & a_PluginInterface, const cItem & a_Item,
26  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace
27  ) override
28  {
29  if ((a_BlockFace != BLOCK_FACE_YM) && (a_BlockFace != BLOCK_FACE_NONE))
30  {
31  return false;
32  }
33 
34  class cCallbacks :
35  public cBlockTracer::cCallbacks
36  {
37  public:
38  Vector3d m_Pos;
39  bool m_HasFound;
40 
41  cCallbacks(void) :
42  m_HasFound(false)
43  {
44  }
45 
46  virtual bool OnNextBlock(int a_CBBlockX, int a_CBBlockY, int a_CBBlockZ, BLOCKTYPE a_CBBlockType, NIBBLETYPE a_CBBlockMeta, eBlockFace a_CBEntryFace) override
47  {
48  if (a_CBBlockType != E_BLOCK_AIR)
49  {
50  m_Pos.Set(a_CBBlockX, a_CBBlockY, a_CBBlockZ);
51  m_HasFound = true;
52  return true;
53  }
54  return false;
55  }
56  } Callbacks;
57 
58  cLineBlockTracer Tracer(*a_World, Callbacks);
59  Vector3d Start(a_Player->GetEyePosition() + a_Player->GetLookVector());
60  Vector3d End(a_Player->GetEyePosition() + a_Player->GetLookVector() * 5);
61 
62  Tracer.Trace(Start.x, Start.y, Start.z, End.x, End.y, End.z);
63 
64  if (!Callbacks.m_HasFound)
65  {
66  return false;
67  }
68 
69  auto x = Callbacks.m_Pos.x;
70  auto y = Callbacks.m_Pos.y;
71  auto z = Callbacks.m_Pos.z;
72  auto bx = FloorC(x);
73  auto by = FloorC(y);
74  auto bz = FloorC(z);
75 
76  // Verify that block type for spawn point is water
77  BLOCKTYPE SpawnBlock = a_World->GetBlock(bx, by, bz);
78  if (!IsBlockWater(SpawnBlock))
79  {
80  return false;
81  }
82 
83  // Block above must be air to spawn a boat (prevents spawning a boat underwater)
84  BLOCKTYPE BlockAbove = a_World->GetBlock(bx, by + 1, bz);
85  if (BlockAbove != E_BLOCK_AIR)
86  {
87  return false;
88  }
89 
90  // Spawn block at water level
91  if (a_World->SpawnBoat(Callbacks.m_Pos + Vector3d(0.5, 0.5, 0.5), cBoat::ItemToMaterial(a_Player->GetEquippedItem())) == cEntity::INVALID_ID)
92  {
93  return false;
94  }
95 
96  // Remove boat from players hand
97  if (!a_Player->IsGameModeCreative())
98  {
99  a_Player->GetInventory().RemoveOneEquippedItem();
100  }
101 
102  return true;
103  }
104 } ;
BLOCKTYPE GetBlock(Vector3i a_BlockPos)
Returns the block type at the specified position.
Definition: World.h:416
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
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: ItemBoat.h:24
const cItem & GetEquippedItem(void) const
Definition: Player.h:142
Definition: Player.h:27
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
UInt32 SpawnBoat(double a_X, double a_Y, double a_Z, cBoat::eMaterial a_Material)
Definition: World.h:590
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld...
Vector3d GetLookVector(void) const
Definition: Entity.cpp:2209
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:179
Definition: World.h:65
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...
cItemBoatHandler(int a_ItemType)
Definition: ItemBoat.h:17
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
std::enable_if< std::is_arithmetic< T >::value, C >::type FloorC(T a_Value)
Floors a value, then casts it to C (an int by default)
Definition: Globals.h:362
cInventory & GetInventory(void)
Definition: Player.h:136
static const UInt32 INVALID_ID
Special ID that is considered an "invalid value", signifying no entity.
Definition: Entity.h:156
Definition: Item.h:36
cItemHandler super
Definition: ItemBoat.h:14