Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemBed.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemHandler.h"
5 #include "Blocks/BlockBed.h"
7 
8 
9 
10 
11 
12 class cItemBedHandler final :
13  public cItemHandler
14 {
16 
17 public:
18 
19  using Super::Super;
20 
21 
22  virtual bool CommitPlacement(cPlayer & a_Player, const cItem & a_HeldItem, const Vector3i a_PlacePosition, const eBlockFace a_ClickedBlockFace, const Vector3i a_CursorPosition) const override
23  {
24  const auto BlockMeta = cBlockBedHandler::YawToMetaData(a_Player.GetYaw());
25  const auto HeadPosition = a_PlacePosition + cBlockBedHandler::MetaDataToDirection(BlockMeta);
26 
27  auto & World = *a_Player.GetWorld();
28  BLOCKTYPE HeadType;
29  NIBBLETYPE HeadMeta;
30  World.GetBlockTypeMeta(HeadPosition, HeadType, HeadMeta);
31 
32  // Vanilla only allows beds to be placed into air.
33  // Check if there is empty space for the "head" block:
34  if (!cBlockHandler::For(HeadType).DoesIgnoreBuildCollision(World, a_HeldItem, HeadPosition, HeadMeta, a_ClickedBlockFace, false))
35  {
36  return false;
37  }
38 
39  // The "foot", and the "head" block:
40  if (
41  !a_Player.PlaceBlocks(
42  {
43  { a_PlacePosition, E_BLOCK_BED, BlockMeta },
44  { HeadPosition, E_BLOCK_BED, static_cast<NIBBLETYPE>(BlockMeta | 0x08) }
45  })
46  )
47  {
48  return false;
49  }
50 
51  auto SetColor = [&a_HeldItem](cBlockEntity & a_BlockEntity)
52  {
53  ASSERT(a_BlockEntity.GetBlockType() == E_BLOCK_BED);
54 
55  static_cast<cBedEntity &>(a_BlockEntity).SetColor(a_HeldItem.m_ItemDamage);
56  return false;
57  };
58  World.DoWithBlockEntityAt(a_PlacePosition, SetColor);
59  World.DoWithBlockEntityAt(HeadPosition, SetColor);
60 
61  return true;
62  }
63 
64 
65  virtual bool IsPlaceable(void) const override
66  {
67  return true;
68  }
69 };
@ E_BLOCK_BED
Definition: BlockType.h:36
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
#define ASSERT(x)
Definition: Globals.h:276
Utilities to allow casting a cWorld to one of its interfaces without including World....
Definition: OpaqueWorld.h:13
static Vector3i MetaDataToDirection(NIBBLETYPE a_MetaData)
Definition: BlockBed.h:31
static const cBlockHandler & For(BLOCKTYPE a_BlockType)
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
cWorld * GetWorld(void) const
Definition: Entity.h:190
Definition: Player.h:29
bool PlaceBlocks(std::initializer_list< sSetBlock > a_Blocks)
Calls the block placement hooks and places the blocks in the world.
Definition: Player.cpp:2449
Definition: Item.h:37
virtual bool IsPlaceable(void) const override
Blocks simply get placed.
Definition: ItemBed.h:65
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: ItemBed.h:22
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37