Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemLighter.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemHandler.h"
5 #include "../World.h"
6 #include "../Entities/Player.h"
7 
8 
9 
10 
11 
12 class cItemLighterHandler final:
13  public cItemHandler
14 {
16 
17 public:
18 
19  using Super::Super;
20 
21 
22 
23 
24 
25  virtual bool OnItemUse(
26  cWorld * a_World,
27  cPlayer * a_Player,
28  cBlockPluginInterface & a_PluginInterface,
29  const cItem & a_HeldItem,
30  const Vector3i a_ClickedBlockPos,
31  eBlockFace a_ClickedBlockFace
32  ) const override
33  {
34  if (a_ClickedBlockFace < 0)
35  {
36  return false;
37  }
38 
39  if (!a_Player->IsGameModeCreative())
40  {
42  {
43  a_Player->UseEquippedItem();
44  }
45  else // Fire charge.
46  {
47  a_Player->GetInventory().RemoveOneEquippedItem();
48  }
49  }
50 
51  // Activate TNT if we clicked on it while not crouched:
52  if ((a_World->GetBlock(a_ClickedBlockPos) == E_BLOCK_TNT) && !a_Player->IsCrouched())
53  {
54  a_World->DigBlock(a_ClickedBlockPos, a_Player);
55  a_World->SpawnPrimedTNT(Vector3d(a_ClickedBlockPos) + Vector3d(0.5, 0.5, 0.5)); // 80 ticks to boom
56  return false;
57  }
58 
59  const auto FirePos = AddFaceDirection(a_ClickedBlockPos, a_ClickedBlockFace);
60  if (!cChunkDef::IsValidHeight(FirePos))
61  {
62  return false;
63  }
64 
65  // Light a fire next to / on top of the block if air:
66  if (a_World->GetBlock(FirePos) == E_BLOCK_AIR)
67  {
68  a_World->PlaceBlock(FirePos, E_BLOCK_FIRE, 0);
69 
70  // The client plays flint and steel sounds, only need to handle fire charges:
72  {
73  a_World->BroadcastSoundEffect("item.firecharge.use", FirePos, 1.0f, 1.04f);
74  }
75  }
76 
77  return false;
78  }
79 } ;
@ E_BLOCK_AIR
Definition: BlockType.h:10
@ E_BLOCK_FIRE
Definition: BlockType.h:61
@ E_BLOCK_TNT
Definition: BlockType.h:56
@ E_ITEM_FIRE_CHARGE
Definition: BlockType.h:431
@ E_ITEM_FLINT_AND_STEEL
Definition: BlockType.h:303
void AddFaceDirection(int &a_BlockX, int &a_BlockY, int &a_BlockZ, eBlockFace a_BlockFace, bool a_bInverse)
Modifies the specified coords so that they point to the block adjacent to the one specified through i...
Definition: Defines.cpp:378
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
Vector3< double > Vector3d
Definition: Vector3.h:485
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
static bool IsValidHeight(Vector3i a_BlockPosition)
Validates a height-coordinate.
Definition: ChunkDef.h:185
Definition: Player.h:29
virtual bool IsCrouched(void) const override
Definition: Player.cpp:2949
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 RemoveOneEquippedItem(void)
Removes one item out of the currently equipped item stack, returns true if successful,...
Definition: Inventory.cpp:232
Definition: Item.h:37
const int m_ItemType
Definition: ItemHandler.h:141
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
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: ItemLighter.h:25
Definition: World.h:53
UInt32 SpawnPrimedTNT(double a_X, double a_Y, double a_Z, int a_FuseTimeInSec=80, double a_InitialVelocityCoeff=1, bool a_ShouldPlayFuseSound=true)
Definition: World.h:519
BLOCKTYPE GetBlock(Vector3i a_BlockPos) const
Returns the block type at the specified position.
Definition: World.h:363
bool DigBlock(Vector3i a_BlockPos, const cEntity *a_Digger=nullptr)
Replaces the specified block with air, and calls the OnBroken block handler.
Definition: World.cpp:2069
void PlaceBlock(const Vector3i a_Position, const BLOCKTYPE a_BlockType, const NIBBLETYPE a_BlockMeta)
Replaces the specified block with another, and calls the OnPlaced block handler.
Definition: World.cpp:2043
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override