Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockLever.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "BlockHandler.h"
4 #include "../Chunk.h"
5 #include "Mixins.h"
6 #include "BlockSlab.h"
7 
8 
10  public cMetaRotator<cBlockHandler, 0x07, 0x04, 0x01, 0x03, 0x02, false>
11 {
13 
14 public:
15 
17  super(a_BlockType)
18  {
19  }
20 
21 
22 
23 
24 
25  virtual bool OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override
26  {
27  Vector3i Coords(a_BlockX, a_BlockY, a_BlockZ);
28  // Flip the ON bit on / off using the XOR bitwise operation
29  NIBBLETYPE Meta = (a_ChunkInterface.GetBlockMeta(Coords) ^ 0x08);
30 
31  a_ChunkInterface.SetBlockMeta(Coords.x, Coords.y, Coords.z, Meta);
32  a_WorldInterface.WakeUpSimulators(Coords);
33  a_WorldInterface.GetBroadcastManager().BroadcastSoundEffect("block.lever.click", Vector3d(Coords), 0.5f, (Meta & 0x08) ? 0.6f : 0.5f);
34  return true;
35  }
36 
37 
38 
39 
40 
41  virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) override
42  {
43  // Reset meta to zero:
44  return cItem(E_BLOCK_LEVER, 1, 0);
45  }
46 
47 
48 
49 
50 
51  virtual bool IsUseable(void) override
52  {
53  return true;
54  }
55 
56 
57 
58 
59 
61  cChunkInterface & a_ChunkInterface, cPlayer & a_Player,
62  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace,
63  int a_CursorX, int a_CursorY, int a_CursorZ,
64  BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta
65  ) override
66  {
67  a_BlockType = m_BlockType;
68  a_BlockMeta = LeverDirectionToMetaData(a_BlockFace);
69  return true;
70  }
71 
72 
73 
74 
75 
77  {
78  // Determine lever direction:
79  switch (a_Dir)
80  {
81  case BLOCK_FACE_YP: return 0x6;
82  case BLOCK_FACE_XP: return 0x1;
83  case BLOCK_FACE_XM: return 0x2;
84  case BLOCK_FACE_ZP: return 0x3;
85  case BLOCK_FACE_ZM: return 0x4;
86  case BLOCK_FACE_YM: return 0x0;
87  case BLOCK_FACE_NONE: return 0x6;
88  }
89  UNREACHABLE("Unsupported block face");
90  }
91 
92 
93 
94 
95 
97  {
98  switch (a_Meta & 0x7)
99  {
100  case 0x1: return BLOCK_FACE_XP;
101  case 0x2: return BLOCK_FACE_XM;
102  case 0x3: return BLOCK_FACE_ZP;
103  case 0x4: return BLOCK_FACE_ZM;
104  case 0x5:
105  case 0x6: return BLOCK_FACE_YP;
106  case 0x7:
107  case 0x0: return BLOCK_FACE_YM;
108  default:
109  {
110  ASSERT(!"Unhandled block meta!");
111  return BLOCK_FACE_NONE;
112  }
113  }
114  }
115 
116 
117 
118 
119 
120  virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk & a_Chunk) override
121  {
122  NIBBLETYPE Meta = a_Chunk.GetMeta(a_RelX, a_RelY, a_RelZ);
123 
125 
126  AddFaceDirection(a_RelX, a_RelY, a_RelZ, Face, true);
127 
128  if ((a_RelY < 0) || (a_RelY >= cChunkDef::Height -1))
129  {
130  return false;
131  }
132 
133  BLOCKTYPE BlockIsOn;
134  a_Chunk.UnboundedRelGetBlock(a_RelX, a_RelY, a_RelZ, BlockIsOn, Meta);
135 
136 
137  if (cBlockInfo::FullyOccupiesVoxel(BlockIsOn))
138  {
139  return true;
140  }
141  else if (cBlockSlabHandler::IsAnySlabType(BlockIsOn))
142  {
143  // Check if the slab is turned up side down
144  if (((Meta & 0x08) == 0x08) && (Face == BLOCK_FACE_TOP))
145  {
146  return true;
147  }
148  }
149 
150  return false;
151  }
152 
153 
154 
155 
156 
157  virtual NIBBLETYPE MetaRotateCCW(NIBBLETYPE a_Meta) override
158  {
159  switch (a_Meta)
160  {
161  case 0x00: return 0x07; // Ceiling rotation
162  case 0x07: return 0x00;
163 
164  case 0x05: return 0x06; // Ground rotation
165  case 0x06: return 0x05;
166 
167  default: return super::MetaRotateCCW(a_Meta); // Wall Rotation
168  }
169  }
170 
171 
172 
173 
174 
175  virtual NIBBLETYPE MetaRotateCW(NIBBLETYPE a_Meta) override
176  {
177  switch (a_Meta)
178  {
179  case 0x00: return 0x07; // Ceiling rotation
180  case 0x07: return 0x00;
181 
182  case 0x05: return 0x06; // Ground rotation
183  case 0x06: return 0x05;
184 
185  default: return super::MetaRotateCW(a_Meta); // Wall Rotation
186  }
187  }
188 
189 
190 
191 
192 
193  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
194  {
195  UNUSED(a_Meta);
196  return 0;
197  }
198 
199 
200 
201 
202 
204  static bool IsLeverOn(NIBBLETYPE a_BlockMeta)
205  {
206  return ((a_BlockMeta & 0x8) == 0x8);
207  }
208 } ;
209 
210 
211 
212 
T x
Definition: Vector3.h:17
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
Definition: Player.h:27
virtual bool CanBeAt(cChunkInterface &a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk &a_Chunk) override
Checks if the block can stay at the specified relative coords in the chunk.
Definition: BlockLever.h:120
BLOCKTYPE m_BlockType
Definition: BlockHandler.h:213
virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity *a_BlockEntity, const cEntity *a_Digger, const cItem *a_Tool) override
Returns the pickups that would result if the block was mined by a_Digger using a_Tool.
Definition: BlockLever.h:41
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
Definition: Chunk.h:49
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17
static const int Height
Definition: ChunkDef.h:135
static eBlockFace BlockMetaDataToBlockFace(NIBBLETYPE a_Meta)
Definition: BlockLever.h:96
virtual bool OnUse(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override
Called if the user right clicks the block and the block is useable returns true if the use was succes...
Definition: BlockLever.h:25
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr)=0
void AddFaceDirection(int &a_BlockX, int &a_BlockY, int &a_BlockZ, eBlockFace a_BlockFace, bool a_bInverse=false)
Definition: Defines.h:859
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
Returns the base colour ID of the block, as will be represented on a map, as per documentation: https...
Definition: BlockLever.h:193
Mixin for rotations and reflections following the standard pattern of "apply mask, then use a switch".
Definition: Mixins.h:84
NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:380
virtual bool IsUseable(void) override
Checks if the block can be placed at this point.
Definition: BlockLever.h:51
static NIBBLETYPE LeverDirectionToMetaData(eBlockFace a_Dir)
Definition: BlockLever.h:76
Vector3< double > Vector3d
Definition: Vector3.h:445
static bool IsLeverOn(NIBBLETYPE a_BlockMeta)
Extracts the ON bit from metadata and returns if true if it is set.
Definition: BlockLever.h:204
virtual bool GetPlacementBlockTypeMeta(cChunkInterface &a_ChunkInterface, cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) override
Called before a block is placed into a world.
Definition: BlockLever.h:60
#define ASSERT(x)
Definition: Globals.h:335
#define UNUSED
Definition: Globals.h:152
static bool FullyOccupiesVoxel(BLOCKTYPE a_Type)
Definition: BlockInfo.h:50
virtual NIBBLETYPE MetaRotateCW(NIBBLETYPE a_Meta) override
Rotates a given block meta clockwise.
Definition: BlockLever.h:175
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
cBlockLeverHandler(BLOCKTYPE a_BlockType)
Definition: BlockLever.h:16
NIBBLETYPE GetBlockMeta(Vector3i a_Pos)
bool UnboundedRelGetBlock(Vector3i a_RelCoords, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Same as GetBlock(), but relative coords needn&#39;t be in this chunk (uses m_Neighbor-s or m_ChunkMap in ...
Definition: Chunk.cpp:997
Byte ColourID
Definition: Globals.h:118
virtual void WakeUpSimulators(Vector3i a_Block)=0
Wakes up the simulators for the specified block.
Definition: Entity.h:73
virtual NIBBLETYPE MetaRotateCW(NIBBLETYPE a_Meta) override
Rotates a given block meta clockwise.
Definition: Mixins.h:118
void SetBlockMeta(Vector3i a_BlockPos, NIBBLETYPE a_MetaData, bool a_ShouldMarkDirty=true, bool a_ShouldInformClient=true)
Sets the meta for the specified block, while keeping the blocktype.
virtual NIBBLETYPE MetaRotateCCW(NIBBLETYPE a_Meta) override
Rotates a given block meta counter-clockwise.
Definition: Mixins.h:97
virtual NIBBLETYPE MetaRotateCCW(NIBBLETYPE a_Meta) override
Rotates a given block meta counter-clockwise.
Definition: BlockLever.h:157
Definition: Item.h:36
static bool IsAnySlabType(BLOCKTYPE a_BlockType)
Returns true if the specified blocktype is one of the slabs handled by this handler.
Definition: BlockSlab.h:104
#define UNREACHABLE(x)
Use to mark code that should be impossible to reach.
Definition: Globals.h:344
virtual cBroadcastInterface & GetBroadcastManager()=0
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234