Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemSign.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemHandler.h"
5 #include "../World.h"
6 #include "../ClientHandle.h"
7 
8 
9 
10 
11 
12 class cItemSignHandler final:
13  public cItemHandler
14 {
16 
17 public:
18 
19  using Super::Super;
20 
21 private:
22 
24  static NIBBLETYPE BlockFaceToMetaData(eBlockFace a_NeighborBlockFace)
25  {
26  switch (a_NeighborBlockFace)
27  {
28  case BLOCK_FACE_ZM: return 0x02;
29  case BLOCK_FACE_ZP: return 0x03;
30  case BLOCK_FACE_XM: return 0x04;
31  case BLOCK_FACE_XP: return 0x05;
32  case BLOCK_FACE_NONE:
33  case BLOCK_FACE_YP:
34  case BLOCK_FACE_YM:
35  {
36  break;
37  }
38  }
39  return 0x02;
40  }
41 
42 
43  virtual bool CommitPlacement(cPlayer & a_Player, const cItem & a_HeldItem, const Vector3i a_PlacePosition, const eBlockFace a_ClickedBlockFace, const Vector3i a_CursorPosition) const override
44  {
45  if (a_ClickedBlockFace == BLOCK_FACE_TOP)
46  {
47  if (!a_Player.PlaceBlock(a_PlacePosition, E_BLOCK_SIGN_POST, RotationToMetaData(a_Player.GetYaw())))
48  {
49  return false;
50  }
51  }
52  else if (!a_Player.PlaceBlock(a_PlacePosition, E_BLOCK_WALLSIGN, BlockFaceToMetaData(a_ClickedBlockFace)))
53  {
54  return false;
55  }
56 
57  // After successfully placing the sign, open the sign editor for the player:
58  a_Player.GetClientHandle()->SendEditSign(a_PlacePosition);
59  return true;
60  }
61 
62 
63  virtual bool IsPlaceable(void) const override
64  {
65  return true;
66  }
67 
68 
70  static NIBBLETYPE RotationToMetaData(double a_Rotation)
71  {
72  a_Rotation += 180 + (180.f / 16); // So it's not aligned with axis.
73  if (a_Rotation > 360)
74  {
75  a_Rotation -= 360;
76  }
77 
78  a_Rotation = (a_Rotation / 360) * 16;
79 
80  return static_cast<NIBBLETYPE>(a_Rotation) % 16;
81  }
82 } ;
83 
84 
85 
86 
@ E_BLOCK_SIGN_POST
Definition: BlockType.h:76
@ E_BLOCK_WALLSIGN
Definition: BlockType.h:82
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
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_TOP
Definition: Defines.h:49
@ 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
void SendEditSign(Vector3i a_BlockPos)
double GetYaw(void) const
Definition: Entity.h:198
Definition: Player.h:29
cClientHandle * GetClientHandle(void) const
Definition: Player.h:276
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
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: ItemSign.h:43
static NIBBLETYPE RotationToMetaData(double a_Rotation)
Converts the (player) rotation to placed-signpost block meta.
Definition: ItemSign.h:70
static NIBBLETYPE BlockFaceToMetaData(eBlockFace a_NeighborBlockFace)
Converts the block face of the neighbor to which the wallsign is attached to the wallsign block's met...
Definition: ItemSign.h:24
virtual bool IsPlaceable(void) const override
Blocks simply get placed.
Definition: ItemSign.h:63