Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemPotion.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "../Entities/EntityEffect.h"
5 
6 
7 class cItemPotionHandler final:
8  public cItemHandler
9 {
11 
12 public:
13 
14  using Super::Super;
15 
16 
17 
18 
19 
20  // cItemHandler overrides:
21  virtual bool IsDrinkable(short a_ItemDamage) const override
22  {
23  // Drinkable potion if 13th lowest bit is set
24  // Ref.: https://minecraft.wiki/w/Potions#Data_value_table
25  return cEntityEffect::IsPotionDrinkable(a_ItemDamage);
26  }
27 
28 
29 
30 
31 
32  virtual bool OnItemUse(
33  cWorld * a_World,
34  cPlayer * a_Player,
35  cBlockPluginInterface & a_PluginInterface,
36  const cItem & a_HeldItem,
37  const Vector3i a_ClickedBlockPos,
38  eBlockFace a_ClickedBlockFace
39  ) const override
40  {
41  short PotionDamage = a_HeldItem.m_ItemDamage;
42 
43  // Do not throw non-splash potions:
44  if (cEntityEffect::IsPotionDrinkable(PotionDamage))
45  {
46  return false;
47  }
48 
49  Vector3d Pos = a_Player->GetThrowStartPos();
50  Vector3d Speed = a_Player->GetLookVector() * 14;
51 
52  // Play sound
53  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));
54 
55  if (a_World->CreateProjectile(Pos.x, Pos.y, Pos.z, cProjectileEntity::pkSplashPotion, a_Player, &a_Player->GetEquippedItem(), &Speed) == cEntity::INVALID_ID)
56  {
57  return false;
58  }
59 
60  if (!a_Player->IsGameModeCreative())
61  {
62  a_Player->GetInventory().RemoveOneEquippedItem();
63  }
64 
65  return true;
66  }
67 
68 
69 
70 
71 
72  virtual bool EatItem(cPlayer * a_Player, cItem * a_Item) const override
73  {
74  short PotionDamage = a_Item->m_ItemDamage;
75 
76  // Do not drink undrinkable potions:
78  {
79  return false;
80  }
81 
82  a_Player->AddEntityEffect(
86  );
87 
88  if (!a_Player->IsGameModeCreative())
89  {
91  }
92  return true;
93  }
94 };
95 
96 
97 
98 
@ E_ITEM_GLASS_BOTTLE
Definition: BlockType.h:420
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
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
static int GetPotionEffectDuration(short a_ItemDamage)
Returns the effect duration, in ticks, based on the potion's damage value.
static bool IsPotionDrinkable(short a_ItemDamage)
Returns true if the potion with the given damage is drinkable.
static short GetPotionEffectIntensity(short a_ItemDamage)
Retrieves the intensity level from the potion's damage value.
static cEntityEffect::eType GetPotionEffectType(short a_ItemDamage)
Translates the potion's damage value into the entity effect that the potion gives.
void AddEntityEffect(cEntityEffect::eType a_EffectType, int a_EffectDurationTicks, short a_EffectIntensity, double a_DistanceModifier=1)
Applies an entity effect.
Definition: Pawn.cpp:186
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
void ReplaceOneEquippedItemTossRest(const cItem &)
Removes one item from the the current equipped item stack, and attempts to add the specified item sta...
Definition: Player.cpp:1745
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
short m_ItemDamage
Definition: Item.h:165
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
friend class cItem
Definition: ItemHandler.h:25
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: ItemPotion.h:32
virtual bool EatItem(cPlayer *a_Player, cItem *a_Item) const override
Lets the player eat a selected item.
Definition: ItemPotion.h:72
virtual bool IsDrinkable(short a_ItemDamage) const override
Indicates if this item is drinkable.
Definition: ItemPotion.h:21
T x
Definition: Vector3.h:17
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17
Definition: World.h:53
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