Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemChest.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemHandler.h"
5 #include "Blocks/BlockChest.h"
6 
7 
8 
9 
10 
11 class cItemChestHandler final :
12  public cItemHandler
13 {
15 
16 public:
17 
18  using Super::Super;
19 
21 
22 private:
23 
24  virtual bool CommitPlacement(cPlayer & a_Player, const cItem & a_HeldItem, const Vector3i a_PlacePosition, const eBlockFace a_ClickedBlockFace, const Vector3i a_CursorPosition) const override
25  {
26  // Check that there is at most one single neighbor of the same chest type:
27  static const Vector3i CrossCoords[] =
28  {
29  {-1, 0, 0},
30  { 0, 0, -1},
31  { 1, 0, 0},
32  { 0, 0, 1},
33  };
34 
35  auto & World = *a_Player.GetWorld();
36  int NeighborIdx = -1;
37 
38  for (size_t i = 0; i < ARRAYCOUNT(CrossCoords); i++)
39  {
40  const auto NeighborPos = a_PlacePosition + CrossCoords[i];
41  if (World.GetBlock(NeighborPos) != m_ItemType)
42  {
43  continue;
44  }
45  if (NeighborIdx >= 0)
46  {
47  // Can't place here, there are already two neighbors, this would form a 3-block chest
48  return false;
49  }
50  NeighborIdx = static_cast<int>(i);
51 
52  // Check that this neighbor is a single chest:
53  for (size_t j = 0; j < ARRAYCOUNT(CrossCoords); j++)
54  {
55  if (World.GetBlock(NeighborPos + CrossCoords[j]) == m_ItemType)
56  {
57  // Trying to place next to a dblchest
58  return false;
59  }
60  } // for j
61  } // for i
62 
63  // Get the meta of the placed chest; take existing neighbors into account:
64  BLOCKTYPE ChestBlockType = static_cast<BLOCKTYPE>(m_ItemType);
65  NIBBLETYPE Meta;
66  const auto yaw = a_Player.GetYaw();
67  switch (NeighborIdx)
68  {
69  case 0:
70  case 2:
71  {
72  // The neighbor is in the X axis, form a X-axis-aligned dblchest:
73  Meta = ((yaw >= -90) && (yaw < 90)) ? E_META_CHEST_FACING_ZM : E_META_CHEST_FACING_ZP;
74  break;
75  }
76  case 1:
77  case 3:
78  {
79  // The neighbor is in the Z axis, form a Z-axis-aligned dblchest:
81  break;
82  }
83  default:
84  {
85  // No neighbor, place based on yaw:
87  break;
88  }
89  } // switch (NeighborIdx)
90 
91  // Place the new chest:
92  if (!a_Player.PlaceBlock(a_PlacePosition, ChestBlockType, Meta))
93  {
94  return false;
95  }
96 
97  // Adjust the existing chest, if any:
98  if (NeighborIdx != -1)
99  {
100  World.FastSetBlock(a_PlacePosition + CrossCoords[NeighborIdx], ChestBlockType, Meta);
101  }
102 
103  return true;
104  }
105 };
@ E_META_CHEST_FACING_XP
Definition: BlockType.h:600
@ E_META_CHEST_FACING_ZM
Definition: BlockType.h:597
@ E_META_CHEST_FACING_XM
Definition: BlockType.h:599
@ E_META_CHEST_FACING_ZP
Definition: BlockType.h:598
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 ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:231
Utilities to allow casting a cWorld to one of its interfaces without including World....
Definition: OpaqueWorld.h:13
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 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
cItemChestHandler(const cItemChestHandler &)=delete
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: ItemChest.h:24
const int m_ItemType
Definition: ItemHandler.h:141
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37