Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockComparator.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "BlockHandler.h"
6 #include "Mixins.h"
7 
8 
9 
10 
11 
13  public cYawRotator<cBlockHandler, 0x03, 0x00, 0x01, 0x02, 0x03>
14 {
16 
17 public:
18 
19  using Super::Super;
20 
21  inline static bool IsInSubtractionMode(NIBBLETYPE a_Meta)
22  {
23  return ((a_Meta & 0x4) == 0x4);
24  }
25 
26  inline static Vector3i GetFrontCoordinate(Vector3i a_Position, NIBBLETYPE a_Meta)
27  {
28  switch (a_Meta)
29  {
30  case 0x0: a_Position.z--; break;
31  case 0x1: a_Position.x++; break;
32  case 0x2: a_Position.z++; break;
33  case 0x3: a_Position.x--; break;
34  default:
35  {
36  LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta);
37  ASSERT(!"Unknown metadata while determining orientation of comparator!");
38  break;
39  }
40  }
41 
42  return a_Position;
43  }
44 
45  inline static Vector3i GetSideCoordinate(Vector3i a_Position, NIBBLETYPE a_Meta, bool a_bInverse)
46  {
47  if (!a_bInverse)
48  {
49  switch (a_Meta)
50  {
51  case 0x0: a_Position.x++; break;
52  case 0x1: a_Position.z--; break;
53  case 0x2: a_Position.x--; break;
54  case 0x3: a_Position.z++; break;
55  default:
56  {
57  LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta);
58  ASSERT(!"Unknown metadata while determining orientation of comparator!");
59  break;
60  }
61  }
62  }
63  else
64  {
65  switch (a_Meta)
66  {
67  case 0x0: a_Position.x--; break;
68  case 0x1: a_Position.z++; break;
69  case 0x2: a_Position.x++; break;
70  case 0x3: a_Position.z--; break;
71  default:
72  {
73  LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta);
74  ASSERT(!"Unknown metadata while determining orientation of comparator!");
75  break;
76  }
77  }
78  }
79 
80  return a_Position;
81  }
82 
83  inline static Vector3i GetRearCoordinate(Vector3i a_Position, NIBBLETYPE a_Meta)
84  {
85  switch (a_Meta)
86  {
87  case 0x0: a_Position.z++; break;
88  case 0x1: a_Position.x--; break;
89  case 0x2: a_Position.z--; break;
90  case 0x3: a_Position.x++; break;
91  default:
92  {
93  LOGWARNING("%s: Unknown metadata: %d", __FUNCTION__, a_Meta);
94  ASSERT(!"Unknown metadata while determining orientation of comparator!");
95  break;
96  }
97  }
98 
99  return a_Position;
100  }
101 
102 private:
103 
104  virtual bool OnUse(
105  cChunkInterface & a_ChunkInterface,
106  cWorldInterface & a_WorldInterface,
107  cPlayer & a_Player,
108  const Vector3i a_BlockPos,
109  eBlockFace a_BlockFace,
110  const Vector3i a_CursorPos
111  ) const override
112  {
113  const auto Meta = a_ChunkInterface.GetBlockMeta(a_BlockPos);
114 
115  // Toggle the 3rd bit (addition / subtraction):
116  a_ChunkInterface.SetBlockMeta(a_BlockPos, Meta ^ 0x04);
117 
118  // Update simulators:
119  a_WorldInterface.WakeUpSimulators(a_BlockPos);
120  return true;
121  }
122 
123 
124 
125 
126 
127  virtual void OnCancelRightClick(
128  cChunkInterface & a_ChunkInterface,
129  cWorldInterface & a_WorldInterface,
130  cPlayer & a_Player,
131  const Vector3i a_BlockPos,
132  eBlockFace a_BlockFace
133  ) const override
134  {
135  UNUSED(a_ChunkInterface);
136  UNUSED(a_BlockFace);
137 
138  a_WorldInterface.WakeUpSimulators(a_BlockPos);
139  a_WorldInterface.SendBlockTo(a_BlockPos, a_Player);
140  }
141 
142 
143 
144 
145 
146  virtual bool IsUseable(void) const override
147  {
148  return true;
149  }
150 
151 
152 
153 
154 
155  virtual bool CanBeAt(const cChunk & a_Chunk, const Vector3i a_Position, const NIBBLETYPE a_Meta) const override
156  {
157  if (a_Position.y <= 0)
158  {
159  return false;
160  }
161 
162  BLOCKTYPE BelowBlock;
163  NIBBLETYPE BelowBlockMeta;
164  a_Chunk.GetBlockTypeMeta(a_Position.addedY(-1), BelowBlock, BelowBlockMeta);
165 
166  if (cBlockInfo::FullyOccupiesVoxel(BelowBlock))
167  {
168  return true;
169  }
170 
171  // upside down slabs
172  if (cBlockSlabHandler::IsAnySlabType(BelowBlock))
173  {
174  return BelowBlockMeta & E_META_WOODEN_SLAB_UPSIDE_DOWN;
175  }
176 
177  // upside down stairs
178  if (cBlockStairsHandler::IsAnyStairType(BelowBlock))
179  {
180  return BelowBlockMeta & E_BLOCK_STAIRS_UPSIDE_DOWN;
181  }
182 
183  return false;
184  }
185 
186 
187 
188 
189 
190  virtual cItems ConvertToPickups(const NIBBLETYPE a_BlockMeta, const cItem * const a_Tool) const override
191  {
192  return cItem(E_ITEM_COMPARATOR, 1, 0);
193  }
194 
195 
196 
197 
198 
199  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
200  {
201  UNUSED(a_Meta);
202  return 11;
203  }
204 } ;
205 
206 
207 
208 
@ E_META_WOODEN_SLAB_UPSIDE_DOWN
Definition: BlockType.h:989
@ E_BLOCK_STAIRS_UPSIDE_DOWN
Definition: BlockType.h:902
@ E_ITEM_COMPARATOR
Definition: BlockType.h:450
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
Byte ColourID
Definition: Globals.h:162
#define ASSERT(x)
Definition: Globals.h:276
#define UNUSED
Definition: Globals.h:72
void LOGWARNING(std::string_view a_Format, const Args &... args)
Definition: LoggerSimple.h:67
static bool FullyOccupiesVoxel(BLOCKTYPE Block)
Does this block fully occupy its voxel - is it a 'full' block?
Definition: BlockInfo.cpp:606
static Vector3i GetRearCoordinate(Vector3i a_Position, NIBBLETYPE a_Meta)
virtual cItems ConvertToPickups(const NIBBLETYPE a_BlockMeta, const cItem *const a_Tool) const override
static Vector3i GetSideCoordinate(Vector3i a_Position, NIBBLETYPE a_Meta, bool a_bInverse)
static Vector3i GetFrontCoordinate(Vector3i a_Position, NIBBLETYPE a_Meta)
virtual void OnCancelRightClick(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, cPlayer &a_Player, const Vector3i a_BlockPos, eBlockFace a_BlockFace) const override
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
virtual bool OnUse(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, cPlayer &a_Player, const Vector3i a_BlockPos, eBlockFace a_BlockFace, const Vector3i a_CursorPos) const override
virtual bool CanBeAt(const cChunk &a_Chunk, const Vector3i a_Position, const NIBBLETYPE a_Meta) const override
static bool IsInSubtractionMode(NIBBLETYPE a_Meta)
virtual bool IsUseable(void) const override
static bool IsAnySlabType(BLOCKTYPE a_BlockType)
Returns true if the specified blocktype is one of the slabs handled by this handler.
Definition: BlockSlab.h:30
static bool IsAnyStairType(BLOCKTYPE a_Block)
Definition: BlockStairs.h:19
void SetBlockMeta(Vector3i a_BlockPos, NIBBLETYPE a_MetaData)
Sets the meta for the specified block, while keeping the blocktype.
NIBBLETYPE GetBlockMeta(Vector3i a_Pos)
Mixin for blocks whose meta on placement depends on the yaw of the player placing the block.
Definition: Mixins.h:162
cMetaRotator< cBlockHandler, BitMask, North, East, South, West, false > Super
Definition: Mixins.h:163
virtual void WakeUpSimulators(Vector3i a_Block)=0
Wakes up the simulators for the specified block.
virtual void SendBlockTo(int a_BlockX, int a_BlockY, int a_BlockZ, const cPlayer &a_Player)=0
Sends the block on those coords to the player.
Definition: Chunk.h:36
void GetBlockTypeMeta(Vector3i a_RelPos, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Definition: Chunk.cpp:1757
Definition: Player.h:29
Definition: Item.h:37
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
Vector3< T > addedY(T a_AddY) const
Returns a copy of this vector moved by the specified amount on the y axis.
Definition: Vector3.h:314
T x
Definition: Vector3.h:17
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17