Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockFenceGate.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, 0x02, 0x03, 0x00, 0x01, true>>
13 {
15 
16 public:
17 
19  super(a_BlockType)
20  {
21  }
22 
24  cChunkInterface & a_ChunkInterface, cPlayer & a_Player,
25  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace,
26  int a_CursorX, int a_CursorY, int a_CursorZ,
27  BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta
28  ) override
29  {
30  a_BlockType = m_BlockType;
31  a_BlockMeta = PlayerYawToMetaData(a_Player.GetYaw());
32  return true;
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  {
37  NIBBLETYPE OldMetaData = a_ChunkInterface.GetBlockMeta({a_BlockX, a_BlockY, a_BlockZ});
38  NIBBLETYPE NewMetaData = PlayerYawToMetaData(a_Player.GetYaw());
39  OldMetaData ^= 4; // Toggle the gate
40 
41  if ((OldMetaData & 1) == (NewMetaData & 1))
42  {
43  // Standing in front of the gate - apply new direction
44  a_ChunkInterface.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, (OldMetaData & 4) | (NewMetaData & 3));
45  }
46  else
47  {
48  // Standing aside - use last direction
49  a_ChunkInterface.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, OldMetaData);
50  }
51  a_Player.GetWorld()->BroadcastSoundParticleEffect(EffectID::SFX_RANDOM_FENCE_GATE_OPEN, {a_BlockX, a_BlockY, a_BlockZ}, 0, a_Player.GetClientHandle());
52  return true;
53  }
54 
55  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
56  {
57  a_WorldInterface.SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, a_Player);
58  }
59 
60  virtual bool IsUseable(void) override
61  {
62  return true;
63  }
64 
66  inline static NIBBLETYPE PlayerYawToMetaData(double a_Yaw)
67  {
68  ASSERT((a_Yaw >= -180) && (a_Yaw < 180));
69 
70  a_Yaw += 360 + 45;
71  if (a_Yaw > 360)
72  {
73  a_Yaw -= 360;
74  }
75  if ((a_Yaw >= 0) && (a_Yaw < 90))
76  {
77  return 0x0;
78  }
79  else if ((a_Yaw >= 180) && (a_Yaw < 270))
80  {
81  return 0x2;
82  }
83  else if ((a_Yaw >= 90) && (a_Yaw < 180))
84  {
85  return 0x1;
86  }
87  else
88  {
89  return 0x3;
90  }
91  }
92 
93  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
94  {
95  UNUSED(a_Meta);
96  switch (m_BlockType)
97  {
98  case E_BLOCK_OAK_FENCE_GATE: return 13;
99  case E_BLOCK_SPRUCE_FENCE_GATE: return 34;
100  case E_BLOCK_BIRCH_FENCE_GATE: return 2;
101  case E_BLOCK_JUNGLE_FENCE_GATE: return 10;
102  case E_BLOCK_DARK_OAK_FENCE_GATE: return 26;
103  case E_BLOCK_ACACIA_FENCE_GATE: return 15;
104  default:
105  {
106  ASSERT(!"Unhandled blocktype in fence gate handler!");
107  return 0;
108  }
109  }
110  }
111 } ;
112 
113 
114 
115 
cBlockFenceGateHandler(BLOCKTYPE a_BlockType)
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
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...
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
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.
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.
virtual void BroadcastSoundParticleEffect(const EffectID a_EffectID, Vector3i a_SrcPos, int a_Data, const cClientHandle *a_Exclude=nullptr) override
#define ASSERT(x)
Definition: Globals.h:335
#define UNUSED
Definition: Globals.h:152
double GetYaw(void) const
Definition: Entity.h:209
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)
static NIBBLETYPE PlayerYawToMetaData(double a_Yaw)
Converts the player&#39;s yaw to placed gate&#39;s blockmeta.
Byte ColourID
Definition: Globals.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 bool IsUseable(void) override
Checks if the block can be placed at this point.
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.
cWorld * GetWorld(void) const
Definition: Entity.h:201
cClientHandle * GetClientHandle(void) const
Returns the raw client handle associated with the player.
Definition: Player.h:254