Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemBow.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "../Entities/ArrowEntity.h"
5 #include "ItemHandler.h"
6 
7 
8 
9 
10 
11 class cItemBowHandler 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  ASSERT(a_Player != nullptr);
34 
35  // Check if the player has an arrow in the inventory, or is in Creative:
36  if (!(a_Player->IsGameModeCreative() || a_Player->GetInventory().HasItems(cItem(E_ITEM_ARROW))))
37  {
38  return false;
39  }
40 
41  a_Player->StartChargingBow();
42  return true;
43  }
44 
45 
46 
47  virtual void OnItemShoot(cPlayer * a_Player, const Vector3i a_BlockPos, eBlockFace a_BlockFace) const override
48  {
49  // Actual shot - produce the arrow with speed based on the number of ticks that the bow was charged
50  UNUSED(a_BlockPos);
51  ASSERT(a_Player != nullptr);
52 
53  int BowCharge = a_Player->FinishChargingBow();
54  double Force = static_cast<double>(BowCharge) / 20.0;
55  Force = (Force * Force + 2.0 * Force) / 3.0; // This formula is used by the 1.6.2 client
56  if (Force < 0.1)
57  {
58  // Too little force, ignore the shot
59  return;
60  }
61  Force = std::min(Force, 1.0);
62 
63  // Does the player have an arrow?
64  if (!a_Player->IsGameModeCreative() && !a_Player->GetInventory().HasItems(cItem(E_ITEM_ARROW)))
65  {
66  return;
67  }
68 
69  // Create the arrow entity:
70  auto Arrow = std::make_unique<cArrowEntity>(*a_Player, Force * 2);
71  auto ArrowPtr = Arrow.get();
72  if (!ArrowPtr->Initialize(std::move(Arrow), *a_Player->GetWorld()))
73  {
74  return;
75  }
76  a_Player->GetWorld()->BroadcastSoundEffect(
77  "entity.arrow.shoot",
78  a_Player->GetPosition(),
79  0.5,
80  static_cast<float>(Force)
81  );
82  if (!a_Player->IsGameModeCreative())
83  {
85  {
87  }
88  else
89  {
90  ArrowPtr->SetPickupState(cArrowEntity::psNoPickup);
91  }
92 
93  a_Player->UseEquippedItem();
94  }
96  {
97  ArrowPtr->StartBurning(100);
98  }
99  }
100 } ;
@ E_ITEM_ARROW
Definition: BlockType.h:306
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
#define ASSERT(x)
Definition: Globals.h:276
#define UNUSED
Definition: Globals.h:72
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
unsigned int GetLevel(int a_EnchantmentID) const
Returns the level for the specified enchantment; 0 if not stored.
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
cWorld * GetWorld(void) const
Definition: Entity.h:190
Definition: Player.h:29
const cItem & GetEquippedItem(void) const
Definition: Player.h:162
int FinishChargingBow(void)
Finishes charging the current bow.
Definition: Player.cpp:324
void StartChargingBow(void)
Starts charging the equipped bow.
Definition: Player.cpp:312
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
void UseEquippedItem(short a_Damage=1)
Damage the player's equipped item by a_Damage, possibly less if the equipped item is enchanted.
Definition: Player.cpp:2033
bool HasItems(const cItem &a_ItemStack)
Returns true if there are at least as many items of type a_ItemStack as in a_ItemStack.
Definition: Inventory.cpp:303
int RemoveItem(const cItem &a_ItemStack)
Removes the specified item from the inventory, as many as possible, up to a_ItemStack....
Definition: Inventory.cpp:189
Definition: Item.h:37
cEnchantments m_Enchantments
Definition: Item.h:166
virtual void OnItemShoot(cPlayer *a_Player, const Vector3i a_BlockPos, eBlockFace a_BlockFace) const override
Called when the client sends the SHOOT status in the lclk packet (releasing the bow).
Definition: ItemBow.h:47
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: ItemBow.h:24
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
friend class cItem
Definition: ItemHandler.h:25
Definition: World.h:53
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override