Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemTrapdoor.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemHandler.h"
5 #include "Blocks/BlockTrapdoor.h"
6 
7 
8 
9 
10 
11 class cItemTrapdoorHandler final :
12  public cItemHandler
13 {
15 
16 public:
17 
18  using Super::Super;
19 
20 private:
21 
22  inline static NIBBLETYPE BlockFaceToMetaData(eBlockFace a_BlockFace)
23  {
24  switch (a_BlockFace)
25  {
26  case BLOCK_FACE_ZP: return 0x1;
27  case BLOCK_FACE_ZM: return 0x0;
28  case BLOCK_FACE_XP: return 0x3;
29  case BLOCK_FACE_XM: return 0x2;
30  default: UNREACHABLE("Unsupported block face");
31  }
32  }
33 
34 
35  virtual bool CommitPlacement(cPlayer & a_Player, const cItem & a_HeldItem, const Vector3i a_PlacePosition, const eBlockFace a_ClickedBlockFace, const Vector3i a_CursorPosition) const override
36  {
37  NIBBLETYPE Meta;
38 
39  if (a_ClickedBlockFace == BLOCK_FACE_YP)
40  {
41  // Trapdoor is placed on top of a block.
42  // Engage yaw rotation to determine hinge direction:
44  }
45  else if (a_ClickedBlockFace == BLOCK_FACE_YM)
46  {
47  // Trapdoor is placed on bottom of a block.
48  // Engage yaw rotation to determine hinge direction, and toggle 'Move up half-block' bit on:
49  Meta = cBlockTrapdoorHandler::YawToMetaData(a_Player.GetYaw()) | 0x8;
50  }
51  else
52  {
53  // Placement on block sides; hinge direction is determined by which side was clicked:
54  Meta = BlockFaceToMetaData(a_ClickedBlockFace);
55 
56  if (a_CursorPosition.y > 7)
57  {
58  // Trapdoor is placed on a higher half of a vertical block.
59  // Toggle 'Move up half-block' bit on:
60  Meta |= 0x8;
61  }
62  }
63 
64  return a_Player.PlaceBlock(a_PlacePosition, static_cast<BLOCKTYPE>(a_HeldItem.m_ItemType), Meta);
65  }
66 };
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
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
#define UNREACHABLE(x)
Definition: Globals.h:288
static NIBBLETYPE YawToMetaData(double a_Rotation)
Converts the rotation value as returned by cPlayer::GetYaw() to the appropriate metadata value for a ...
Definition: Mixins.h:172
double GetYaw(void) const
Definition: Entity.h:198
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
virtual bool CommitPlacement(cPlayer &a_Player, const cItem &a_HeldItem, const Vector3i a_PlacePosition, const eBlockFace a_ClickedBlockFace, const Vector3i a_CursorPosition) const override
Performs the actual placement of this placeable item.
Definition: ItemTrapdoor.h:35
static NIBBLETYPE BlockFaceToMetaData(eBlockFace a_BlockFace)
Definition: ItemTrapdoor.h:22
T y
Definition: Vector3.h:17