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 
7 
8 
9 
11  public cItemHandler
12 {
14 public:
15  cItemThrowableHandler(int a_ItemType, cProjectileEntity::eKind a_ProjectileKind, double a_SpeedCoeff) :
16  super(a_ItemType),
17  m_ProjectileKind(a_ProjectileKind),
18  m_SpeedCoeff(a_SpeedCoeff)
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  Vector3d Pos = a_Player->GetThrowStartPos();
30  Vector3d Speed = a_Player->GetLookVector() * m_SpeedCoeff;
31 
32  // Play sound
33  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));
34 
35  if (a_World->CreateProjectile(Pos.x, Pos.y, Pos.z, m_ProjectileKind, a_Player, &a_Player->GetEquippedItem(), &Speed) == cEntity::INVALID_ID)
36  {
37  return false;
38  }
39 
40  if (!a_Player->IsGameModeCreative())
41  {
42  a_Player->GetInventory().RemoveOneEquippedItem();
43  }
44 
45  return true;
46  }
47 
48 protected:
50  double m_SpeedCoeff;
51 } ;
52 
53 
54 
55 
56 
59 {
61 public:
63  super(E_ITEM_EGG, cProjectileEntity::pkEgg, 30)
64  {
65  }
66 } ;
67 
68 
69 
70 
73 {
75 
76 public:
78  super(E_ITEM_SNOWBALL, cProjectileEntity::pkSnowball, 30)
79  {
80  }
81 } ;
82 
83 
84 
85 
86 
89 {
91 
92 public:
94  super(E_ITEM_ENDER_PEARL, cProjectileEntity::pkEnderPearl, 30)
95  {
96  }
97 } ;
98 
99 
100 
101 
102 
104  public cItemThrowableHandler
105 {
107 public:
109  super(E_ITEM_BOTTLE_O_ENCHANTING, cProjectileEntity::pkExpBottle, 10)
110  {
111  }
112 };
113 
114 
115 
116 
117 
119  public cItemThrowableHandler
120 {
122 public:
124  super(E_ITEM_FIREWORK_ROCKET, cProjectileEntity::pkFirework, 0)
125  {
126  }
127 
128 
129 
130  virtual bool OnItemUse(
131  cWorld * a_World, cPlayer * a_Player, cBlockPluginInterface & a_PluginInterface, const cItem & a_Item,
132  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace
133  ) override
134  {
135  if (a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ) == E_BLOCK_AIR)
136  {
137  return false;
138  }
139 
140  if (a_World->CreateProjectile(a_BlockX + 0.5, a_BlockY + 1, a_BlockZ + 0.5, m_ProjectileKind, a_Player, &a_Player->GetEquippedItem()) == 0)
141  {
142  return false;
143  }
144 
145  if (!a_Player->IsGameModeCreative())
146  {
147  a_Player->GetInventory().RemoveOneEquippedItem();
148  }
149 
150  return true;
151  }
152 
153 };
BLOCKTYPE GetBlock(Vector3i a_BlockPos)
Returns the block type at the specified position.
Definition: World.h:416
T x
Definition: Vector3.h:17
cItemThrowableHandler(int a_ItemType, cProjectileEntity::eKind a_ProjectileKind, double a_SpeedCoeff)
Definition: ItemThrowable.h:15
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
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:20
cItemThrowableHandler super
const cItem & GetEquippedItem(void) const
Definition: Player.h:142
Definition: Player.h:27
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: ItemThrowable.h:24
eKind
The kind of the projectile.
T y
Definition: Vector3.h:17
Vector3d GetThrowStartPos(void) const
Returns the position where projectiles thrown by this player should start, player eye position + adju...
Definition: Player.cpp:1709
double GetHeight(void) const
Definition: Entity.h:204
T z
Definition: Vector3.h:17
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override
cItemThrowableHandler super
Definition: ItemThrowable.h:74
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld...
RealType RandReal(RealType a_Min, RealType a_Max)
Return a random RealType in the range [a_Min, a_Max).
Definition: FastRandom.h:123
cItemThrowableHandler super
Definition: ItemThrowable.h:90
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).
Vector3d GetLookVector(void) const
Definition: Entity.cpp:2209
Definition: World.h:65
Vector3< double > Vector3d
Definition: Vector3.h:445
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:3241
cItemThrowableHandler super
cProjectileEntity::eKind m_ProjectileKind
Definition: ItemThrowable.h:49
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:307
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
cItemThrowableHandler super
Definition: ItemThrowable.h:60
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