Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockTrapdoor.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "BlockHandler.h"
5 #include "Mixins.h"
6 #include "../EffectID.h"
7 
8 
9 
10 
12  public cClearMetaOnDrop<cMetaRotator<cBlockHandler, 0x03, 0x01, 0x02, 0x00, 0x03, false>>
13 {
15 
16 public:
17 
19  super(a_BlockType)
20  {
21  }
22 
23 
24 
25 
26  virtual bool IsUseable(void) override
27  {
28  return true;
29  }
30 
31 
32 
33 
34 
35  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
36  {
38  {
39  // Iron doors can only be toggled by redstone, not by right-clicking
40  return false;
41  }
42 
43  // Flip the ON bit on / off using the XOR bitwise operation
44  NIBBLETYPE Meta = (a_ChunkInterface.GetBlockMeta({a_BlockX, a_BlockY, a_BlockZ}) ^ 0x04);
45  a_ChunkInterface.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, Meta);
46  a_WorldInterface.GetBroadcastManager().BroadcastSoundParticleEffect(EffectID::SFX_RANDOM_FENCE_GATE_OPEN, { a_BlockX, a_BlockY, a_BlockZ }, 0, a_Player.GetClientHandle());
47 
48  return true;
49  }
50 
51  virtual void OnCancelRightClick(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace) override
52  {
53  UNUSED(a_ChunkInterface);
54  a_WorldInterface.SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, a_Player);
55  }
56 
58  cChunkInterface & a_ChunkInterface, cPlayer & a_Player,
59  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace,
60  int a_CursorX, int a_CursorY, int a_CursorZ,
61  BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta
62  ) override
63  {
64  a_BlockType = m_BlockType;
65  a_BlockMeta = BlockFaceToMetaData(a_BlockFace);
66 
67  if (a_CursorY > 7)
68  {
69  a_BlockMeta |= 0x8;
70  }
71  return true;
72  }
73 
74  inline static NIBBLETYPE BlockFaceToMetaData(eBlockFace a_BlockFace)
75  {
76  switch (a_BlockFace)
77  {
78  case BLOCK_FACE_ZP: return 0x1;
79  case BLOCK_FACE_ZM: return 0x0;
80  case BLOCK_FACE_XP: return 0x3;
81  case BLOCK_FACE_XM: return 0x2;
82  case BLOCK_FACE_NONE:
83  case BLOCK_FACE_YM:
84  case BLOCK_FACE_YP:
85  {
86  ASSERT(!"Unhandled block face!");
87  return 0;
88  }
89  }
90  UNREACHABLE("Unsupported block face");
91  }
92 
94  {
95  switch (a_Meta & 0x3)
96  {
97  case 0x0: return BLOCK_FACE_ZM;
98  case 0x1: return BLOCK_FACE_ZP;
99  case 0x2: return BLOCK_FACE_XM;
100  case 0x3: return BLOCK_FACE_XP;
101  default:
102  {
103  ASSERT(!"Unhandled block meta!");
104  return BLOCK_FACE_NONE;
105  }
106  }
107  }
108 
109  virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk & a_Chunk) override
110  {
111  NIBBLETYPE Meta;
112  a_Chunk.UnboundedRelGetBlockMeta(a_RelX, a_RelY, a_RelZ, Meta);
113 
114  AddFaceDirection(a_RelX, a_RelY, a_RelZ, BlockMetaDataToBlockFace(Meta), true);
115  BLOCKTYPE BlockIsOn;
116  a_Chunk.UnboundedRelGetBlockType(a_RelX, a_RelY, a_RelZ, BlockIsOn);
117 
118  return ((a_RelY > 0) && cBlockInfo::IsSolid(BlockIsOn));
119  }
120 
121  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
122  {
123  UNUSED(a_Meta);
124  switch (m_BlockType)
125  {
126  case E_BLOCK_TRAPDOOR: return 13;
127  case E_BLOCK_IRON_TRAPDOOR: return 6;
128  default:
129  {
130  ASSERT(!"Unhandled blocktype in trapdoor handler!");
131  return 0;
132  }
133  }
134  }
135 };
136 
137 
138 
139 
static bool IsSolid(BLOCKTYPE a_Type)
Definition: BlockInfo.h:48
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
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: BlockTrapdoor.h:57
bool UnboundedRelGetBlockType(Vector3i a_RelCoords, BLOCKTYPE &a_BlockType) const
Same as GetBlockType(), but relative coords needn&#39;t be in this chunk (uses m_Neighbor-s or m_ChunkMap...
Definition: Chunk.cpp:1018
Definition: Player.h:27
BLOCKTYPE m_BlockType
Definition: BlockHandler.h:213
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: BlockTrapdoor.h:35
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
Definition: Chunk.h:49
bool UnboundedRelGetBlockMeta(Vector3i a_RelPos, NIBBLETYPE &a_BlockMeta) const
Same as GetBlockMeta(), but relative coords needn&#39;t be in this chunk (uses m_Neighbor-s or m_ChunkMap...
Definition: Chunk.cpp:1039
virtual void BroadcastSoundParticleEffect(const EffectID a_EffectID, Vector3i a_SrcPos, int a_Data, const cClientHandle *a_Exclude=nullptr)=0
virtual bool IsUseable(void) override
Checks if the block can be placed at this point.
Definition: BlockTrapdoor.h:26
void AddFaceDirection(int &a_BlockX, int &a_BlockY, int &a_BlockZ, eBlockFace a_BlockFace, bool a_bInverse=false)
Definition: Defines.h:859
virtual void OnCancelRightClick(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace) override
Called when a right click to this block is cancelled.
Definition: BlockTrapdoor.h:51
#define ASSERT(x)
Definition: Globals.h:335
static NIBBLETYPE BlockFaceToMetaData(eBlockFace a_BlockFace)
Definition: BlockTrapdoor.h:74
#define UNUSED
Definition: Globals.h:152
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
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...
NIBBLETYPE GetBlockMeta(Vector3i a_Pos)
Byte ColourID
Definition: Globals.h:118
static eBlockFace BlockMetaDataToBlockFace(NIBBLETYPE a_Meta)
Definition: BlockTrapdoor.h:93
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 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.
Mixin to clear the block&#39;s meta value when converting to a pickup.
Definition: Mixins.h:55
virtual void SendBlockTo(int a_BlockX, int a_BlockY, int a_BlockZ, cPlayer &a_Player)=0
Sends the block on those coords to the player.
cBlockTrapdoorHandler(BLOCKTYPE a_BlockType)
Definition: BlockTrapdoor.h:18
#define UNREACHABLE(x)
Use to mark code that should be impossible to reach.
Definition: Globals.h:344
virtual cBroadcastInterface & GetBroadcastManager()=0
cClientHandle * GetClientHandle(void) const
Returns the raw client handle associated with the player.
Definition: Player.h:254