Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemTorch.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemHandler.h"
5 #include "Blocks/BlockTorch.h"
6 
7 
8 
9 
10 
11 class cItemTorchHandler final :
12  public cItemHandler
13 {
15 
16 public:
17 
18  using Super::Super;
19 
20 private:
21 
24  {
25  switch (a_BlockFace)
26  {
32  default: UNREACHABLE("Unsupported block face");
33  }
34  }
35 
36 
37  virtual bool CommitPlacement(cPlayer & a_Player, const cItem & a_HeldItem, const Vector3i a_PlacePosition, eBlockFace a_ClickedBlockFace, const Vector3i a_CursorPosition) const override
38  {
39  const auto & World = *a_Player.GetWorld();
40  BLOCKTYPE ClickedBlockType;
41  NIBBLETYPE ClickedBlockMeta;
42  World.GetBlockTypeMeta(AddFaceDirection(a_PlacePosition, a_ClickedBlockFace, true), ClickedBlockType, ClickedBlockMeta);
43 
44  // Try finding a suitable neighbor block face for the torch; start with the given one:
45  if (!cBlockTorchHandler::CanBePlacedOn(ClickedBlockType, ClickedBlockMeta, a_ClickedBlockFace))
46  {
47  // Couldn't be placed on whatever face was clicked, last ditch resort - find another face:
48  a_ClickedBlockFace = FindSuitableFace(World, a_PlacePosition);
49  if (a_ClickedBlockFace == BLOCK_FACE_NONE)
50  {
51  // No attachable face found - don't place the torch:
52  return false;
53  }
54  }
55 
56  return a_Player.PlaceBlock(a_PlacePosition, static_cast<BLOCKTYPE>(a_HeldItem.m_ItemType), BlockFaceToMetaData(a_ClickedBlockFace));
57  }
58 
59 
62  static eBlockFace FindSuitableFace(const cWorld & a_World, const Vector3i a_Position)
63  {
64  for (const auto Face : { BLOCK_FACE_ZM, BLOCK_FACE_XP, BLOCK_FACE_ZP, BLOCK_FACE_XM, BLOCK_FACE_YP }) // Loop through all faces in specific order.
65  {
66  // The direction of Face is relative to the direction the torch faces.
67  // This is the position, computed inverted, that such a torch would attach to.
68  const auto NeighborPosition = AddFaceDirection(a_Position, Face, true);
69 
70  BLOCKTYPE NeighborBlockType;
71  NIBBLETYPE NeighborBlockMeta;
72  a_World.GetBlockTypeMeta(NeighborPosition, NeighborBlockType, NeighborBlockMeta);
73 
74  if (cBlockTorchHandler::CanBePlacedOn(NeighborBlockType, NeighborBlockMeta, Face))
75  {
76  return Face;
77  }
78  }
79 
80  return BLOCK_FACE_NONE;
81  }
82 };
@ E_META_TORCH_WEST
Definition: BlockType.h:937
@ E_META_TORCH_SOUTH
Definition: BlockType.h:938
@ E_META_TORCH_FLOOR
Definition: BlockType.h:940
@ E_META_TORCH_EAST
Definition: BlockType.h:936
@ E_META_TORCH_NORTH
Definition: BlockType.h:939
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
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
@ BLOCK_FACE_XP
Definition: Defines.h:41
@ BLOCK_FACE_EAST
Definition: Defines.h:53
@ BLOCK_FACE_YP
Definition: Defines.h:43
@ BLOCK_FACE_SOUTH
Definition: Defines.h:51
@ BLOCK_FACE_TOP
Definition: Defines.h:49
@ BLOCK_FACE_WEST
Definition: Defines.h:52
@ BLOCK_FACE_ZM
Definition: Defines.h:44
@ BLOCK_FACE_ZP
Definition: Defines.h:45
@ BLOCK_FACE_NORTH
Definition: Defines.h:50
@ BLOCK_FACE_XM
Definition: Defines.h:40
@ BLOCK_FACE_NONE
Definition: Defines.h:39
#define UNREACHABLE(x)
Definition: Globals.h:288
Utilities to allow casting a cWorld to one of its interfaces without including World....
Definition: OpaqueWorld.h:13
static bool CanBePlacedOn(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, eBlockFace a_BlockFace)
Returns true if the torch can be placed on the specified block's face.
Definition: BlockTorch.h:27
cWorld * GetWorld(void) const
Definition: Entity.h:190
Definition: Player.h:29
bool PlaceBlock(Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Attempts to place the block in the world with a call to PlaceBlocks.
Definition: Player.cpp:2440
Definition: Item.h:37
short m_ItemType
Definition: Item.h:163
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
static NIBBLETYPE BlockFaceToMetaData(eBlockFace a_BlockFace)
Converts the block face of the neighbor to which the torch is attached, to the torch block's meta.
Definition: ItemTorch.h:23
virtual bool CommitPlacement(cPlayer &a_Player, const cItem &a_HeldItem, const Vector3i a_PlacePosition, eBlockFace a_ClickedBlockFace, const Vector3i a_CursorPosition) const override
Performs the actual placement of this placeable item.
Definition: ItemTorch.h:37
static eBlockFace FindSuitableFace(const cWorld &a_World, const Vector3i a_Position)
Returns a suitable neighbor's blockface to place the torch at the specified position.
Definition: ItemTorch.h:62
Definition: World.h:53
bool GetBlockTypeMeta(Vector3i a_BlockPos, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Retrieves the block type and meta at the specified coords.
Definition: World.cpp:1779