Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemLadder.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemHandler.h"
5 #include "Blocks/BlockLadder.h"
6 
7 
8 
9 
10 
11 class cItemLadderHandler final :
12  public cItemHandler
13 {
15 
16 public:
17 
18  using Super::Super;
19 
20 private:
21 
23  static NIBBLETYPE BlockFaceToMetaData(eBlockFace a_NeighborBlockFace)
24  {
25  switch (a_NeighborBlockFace)
26  {
27  case BLOCK_FACE_ZM: return 0x2;
28  case BLOCK_FACE_ZP: return 0x3;
29  case BLOCK_FACE_XM: return 0x4;
30  case BLOCK_FACE_XP: return 0x5;
31  case BLOCK_FACE_YM:
32  case BLOCK_FACE_YP: return 0x2;
33  default: UNREACHABLE("Unsupported neighbor block face");
34  }
35  }
36 
37 
38  virtual bool CommitPlacement(cPlayer & a_Player, const cItem & a_HeldItem, const Vector3i a_PlacePosition, eBlockFace a_ClickedBlockFace, const Vector3i a_CursorPosition) const override
39  {
40  const auto & World = *a_Player.GetWorld();
41  const auto ClickedBlockType = World.GetBlock(AddFaceDirection(a_PlacePosition, a_ClickedBlockFace, true));
42 
43  // Try finding a suitable neighbor block face for the ladder; start with the given one:
44  if (!cBlockLadderHandler::CanBePlacedOn(ClickedBlockType, a_ClickedBlockFace))
45  {
46  // Couldn't be placed on whatever face was clicked, last ditch resort - find another face:
47  a_ClickedBlockFace = FindSuitableFace(World, a_PlacePosition);
48  if (a_ClickedBlockFace == BLOCK_FACE_NONE)
49  {
50  // No attachable face found - don't place the ladder:
51  return false;
52  }
53  }
54 
55  return a_Player.PlaceBlock(a_PlacePosition, E_BLOCK_LADDER, BlockFaceToMetaData(a_ClickedBlockFace));
56  }
57 
58 
61  static eBlockFace FindSuitableFace(const cWorld & a_World, const Vector3i a_Position)
62  {
63  for (const auto Face : { BLOCK_FACE_ZM, BLOCK_FACE_XP, BLOCK_FACE_ZP, BLOCK_FACE_XM }) // Loop through all faces in specific order.
64  {
65  // The direction of Face is relative to the direction the ladder faces.
66  // This is the position, computed inverted, that such a ladder would attach to.
67  const auto NeighborPosition = AddFaceDirection(a_Position, Face, true);
68 
69  if (cBlockLadderHandler::CanBePlacedOn(a_World.GetBlock(NeighborPosition), Face))
70  {
71  return Face;
72  }
73  }
74 
75  return BLOCK_FACE_NONE;
76  }
77 };
@ E_BLOCK_LADDER
Definition: BlockType.h:78
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
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_YP
Definition: Defines.h:43
@ BLOCK_FACE_YM
Definition: Defines.h:42
@ BLOCK_FACE_ZM
Definition: Defines.h:44
@ BLOCK_FACE_ZP
Definition: Defines.h:45
@ 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(const BLOCKTYPE a_BlockType, const eBlockFace a_BlockFace)
Returns true if the ladder will be supported by the block through the given blockface.
Definition: BlockLadder.h:23
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
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
static NIBBLETYPE BlockFaceToMetaData(eBlockFace a_NeighborBlockFace)
Converts the block face of the neighbor to which the ladder is attached to the ladder block's meta.
Definition: ItemLadder.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: ItemLadder.h:38
static eBlockFace FindSuitableFace(const cWorld &a_World, const Vector3i a_Position)
Returns a suitable neighbor's blockface to place the ladder at the specified position.
Definition: ItemLadder.h:61
Definition: World.h:53
BLOCKTYPE GetBlock(Vector3i a_BlockPos) const
Returns the block type at the specified position.
Definition: World.h:363