Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemThrowable.h
Go to the documentation of this file.
1 
2 // Declares the itemhandlers for throwable items: eggs, snowballs, ender pearls, and eyes of ender.
3 
4 #pragma once
5 
6 #include "ItemHandler.h"
8 
9 
10 
11 
12 
14  public cItemHandler
15 {
17 
18 public:
19 
20  constexpr cItemThrowableHandler(int a_ItemType, cProjectileEntity::eKind a_ProjectileKind, double a_SpeedCoeff):
21  Super(a_ItemType),
22  m_ProjectileKind(a_ProjectileKind),
23  m_SpeedCoeff(a_SpeedCoeff)
24  {
25  }
26 
27 
28 
29 
30 
31  virtual bool OnItemUse(
32  cWorld * a_World,
33  cPlayer * a_Player,
34  cBlockPluginInterface & a_PluginInterface,
35  const cItem & a_HeldItem,
36  const Vector3i a_ClickedBlockPos,
37  eBlockFace a_ClickedBlockFace
38  ) const override
39  {
40  auto Pos = a_Player->GetThrowStartPos();
41  auto Speed = a_Player->GetLookVector() * m_SpeedCoeff;
42 
43  // Create the projectile:
44  if (a_World->CreateProjectile(Pos.x, Pos.y, Pos.z, m_ProjectileKind, a_Player, &a_Player->GetEquippedItem(), &Speed) == cEntity::INVALID_ID)
45  {
46  return false;
47  }
48 
49  // Play sound:
50  a_World->BroadcastSoundEffect("entity.arrow.shoot", a_Player->GetPosition() - Vector3d(0, a_Player->GetHeight(), 0), 0.5f, 0.4f / GetRandomProvider().RandReal(0.8f, 1.2f));
51 
52  // Remove from inventory
53  if (!a_Player->IsGameModeCreative())
54  {
55  a_Player->GetInventory().RemoveOneEquippedItem();
56  }
57 
58  return true;
59  }
60 
61 protected:
62 
65 
67  double m_SpeedCoeff;
68 
70 } ;
71 
72 
73 
74 
75 
76 class cItemEggHandler final:
78 {
80 
81 public:
82 
83  constexpr cItemEggHandler(int a_ItemType):
84  Super(a_ItemType, cProjectileEntity::pkEgg, 30)
85  {
86  }
87 } ;
88 
89 
90 
91 
94 {
96 
97 public:
98 
99  constexpr cItemSnowballHandler(int a_ItemType):
100  Super(a_ItemType, cProjectileEntity::pkSnowball, 30)
101  {
102  }
103 } ;
104 
105 
106 
107 
108 
110  public cItemThrowableHandler
111 {
113 
114 public:
115 
116  constexpr cItemEnderPearlHandler(int a_ItemType):
117  Super(a_ItemType, cProjectileEntity::pkEnderPearl, 30)
118  {
119  }
120 } ;
121 
122 
123 
124 
125 
127  public cItemThrowableHandler
128 {
130 
131 public:
132 
133  constexpr cItemBottleOEnchantingHandler(int a_ItemType):
134  Super(a_ItemType, cProjectileEntity::pkExpBottle, 14)
135  {
136  }
137 };
138 
139 
140 
141 
142 
144  public cItemThrowableHandler
145 {
147 
148 public:
149 
150  constexpr cItemFireworkHandler(int a_ItemType):
151  Super(a_ItemType, cProjectileEntity::pkFirework, 0)
152  {
153  }
154 
155 
156 
157 
158 
159  virtual bool OnItemUse(
160  cWorld * a_World,
161  cPlayer * a_Player,
162  cBlockPluginInterface & a_PluginInterface,
163  const cItem & a_HeldItem,
164  const Vector3i a_ClickedBlockPos,
165  eBlockFace a_ClickedBlockFace
166  ) const override
167  {
168  if (a_World->GetBlock(a_ClickedBlockPos) == E_BLOCK_AIR)
169  {
170  return false;
171  }
172 
173  if (a_World->CreateProjectile(Vector3d(a_ClickedBlockPos) + Vector3d(0.5, 1, 0.5), m_ProjectileKind, a_Player, &a_Player->GetEquippedItem()) == 0)
174  {
175  return false;
176  }
177 
178  if (!a_Player->IsGameModeCreative())
179  {
180  a_Player->GetInventory().RemoveOneEquippedItem();
181  }
182 
183  return true;
184  }
185 };
@ E_BLOCK_AIR
Definition: BlockType.h:10
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:12
Vector3< double > Vector3d
Definition: Vector3.h:485
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
static const UInt32 INVALID_ID
Special ID that is considered an "invalid value", signifying no entity.
Definition: Entity.h:128
float GetHeight(void) const
Definition: Entity.h:193
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
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 GetThrowStartPos(void) const
Returns the position where projectiles thrown by this player should start, player eye position + adju...
Definition: Player.cpp:1501
eKind
The kind of the projectile.
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
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
constexpr cItemThrowableHandler(int a_ItemType, cProjectileEntity::eKind a_ProjectileKind, double a_SpeedCoeff)
Definition: ItemThrowable.h:20
cProjectileEntity::eKind m_ProjectileKind
The kind of projectile to create when shooting.
Definition: ItemThrowable.h:64
~cItemThrowableHandler()=default
double m_SpeedCoeff
The speed multiplier (to the player's normalized look vector) to set for the new projectile.
Definition: ItemThrowable.h:67
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: ItemThrowable.h:31
constexpr cItemEggHandler(int a_ItemType)
Definition: ItemThrowable.h:83
constexpr cItemSnowballHandler(int a_ItemType)
Definition: ItemThrowable.h:99
constexpr cItemEnderPearlHandler(int a_ItemType)
constexpr cItemBottleOEnchantingHandler(int a_ItemType)
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).
constexpr cItemFireworkHandler(int a_ItemType)
Definition: World.h:53
BLOCKTYPE GetBlock(Vector3i a_BlockPos) const
Returns the block type at the specified position.
Definition: World.h:363
UInt32 CreateProjectile(Vector3d a_Pos, cProjectileEntity::eKind a_Kind, cEntity *a_Creator, const cItem *a_Item, const Vector3d *a_Speed=nullptr)
Creates a projectile of the specified type.
Definition: World.cpp:2948
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override