Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockPiston.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "BlockHandler.h"
5 #include "Mixins.h"
6 #include "../Item.h"
7 
8 
9 
10 
11 // fwd:
12 class cWorld;
13 
14 
15 
16 
17 
18 class cBlockPistonHandler final :
19  public cClearMetaOnDrop<cDisplacementYawRotator<cBlockHandler, 0x07, 0x03, 0x04, 0x02, 0x05, 0x01, 0x00>>
20 {
22 
23 public:
24 
25  using Super::Super;
26 
28  {
29  switch (a_MetaData & 0x7) // We only want the bottom three bits (4th controls extended-ness))
30  {
31  case 0x0: return BLOCK_FACE_YM;
32  case 0x1: return BLOCK_FACE_YP;
33  case 0x2: return BLOCK_FACE_ZM;
34  case 0x3: return BLOCK_FACE_ZP;
35  case 0x4: return BLOCK_FACE_XM;
36  case 0x5: return BLOCK_FACE_XP;
37  default:
38  {
39  ASSERT(!"Invalid Metadata");
40  return BLOCK_FACE_NONE;
41  }
42  }
43  }
44 
46  static Vector3i MetadataToOffset(NIBBLETYPE a_PistonMeta);
47 
48  static void ExtendPiston(Vector3i a_BlockPos, cWorld & a_World);
49  static void RetractPiston(Vector3i a_BlockPos, cWorld & a_World);
50 
52  static inline bool IsExtended(NIBBLETYPE a_PistonMeta) { return ((a_PistonMeta & 0x8) != 0x0); }
53 
54 private:
55 
56  typedef std::unordered_set<Vector3i, VectorHasher<int>> Vector3iSet;
57 
59  static const Byte PistonExtendAction = 0U;
60 
62  static const Byte PistonRetractAction = 1U;
63 
65  static inline bool IsSticky(BLOCKTYPE a_BlockType) { return (a_BlockType == E_BLOCK_STICKY_PISTON); }
66 
68  static inline bool CanPush(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
69  {
70  switch (a_BlockType)
71  {
72  case E_BLOCK_ANVIL:
73  case E_BLOCK_BARRIER:
74  case E_BLOCK_BEACON:
75  case E_BLOCK_BEDROCK:
78  case E_BLOCK_CHEST:
81  case E_BLOCK_DISPENSER:
82  case E_BLOCK_DROPPER:
85  case E_BLOCK_END_PORTAL:
88  case E_BLOCK_FURNACE:
91  case E_BLOCK_HOPPER:
92  case E_BLOCK_JUKEBOX:
95  case E_BLOCK_NOTE_BLOCK:
96  case E_BLOCK_OBSIDIAN:
102  case E_BLOCK_WALL_BANNER:
103  {
104  return false;
105  }
107  case E_BLOCK_PISTON:
108  {
109  // A piston can only be pushed if retracted:
110  return !IsExtended(a_BlockMeta);
111  }
112  }
113  return true;
114  }
115 
117  static bool CanPushBlock(
118  const Vector3i & a_BlockPos, cWorld & a_World, bool a_RequirePushable,
119  Vector3iSet & a_BlocksPushed, const Vector3i & a_PushDir
120  );
121 
122  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
123  {
124  UNUSED(a_Meta);
125  return 11;
126  }
127 
128  virtual void OnBroken(
129  cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface,
130  Vector3i a_BlockPos,
131  BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta,
132  const cEntity * a_Digger
133  ) const override;
134 
136  static void PushBlocks(const Vector3iSet & a_BlocksToPush,
137  cWorld & a_World, const Vector3i & a_PushDir
138  );
139 } ;
140 
141 
142 
143 
144 
146  public cBlockHandler
147 {
149 
150 public:
151 
152  constexpr cBlockPistonHeadHandler(void) :
154  {
155  }
156 
157  virtual void OnBroken(
158  cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface,
159  Vector3i a_BlockPos,
160  BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta,
161  const cEntity * a_Digger
162  ) const override;
163 
164  virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, const cItem * a_Tool) const override;
165 } ;
@ E_BLOCK_STANDING_BANNER
Definition: BlockType.h:195
@ E_BLOCK_DAYLIGHT_SENSOR
Definition: BlockType.h:167
@ E_BLOCK_BREWING_STAND
Definition: BlockType.h:132
@ E_BLOCK_STICKY_PISTON
Definition: BlockType.h:39
@ E_BLOCK_NETHER_PORTAL
Definition: BlockType.h:105
@ E_BLOCK_JUKEBOX
Definition: BlockType.h:99
@ E_BLOCK_REPEATING_COMMAND_BLOCK
Definition: BlockType.h:229
@ E_BLOCK_ENCHANTMENT_TABLE
Definition: BlockType.h:131
@ E_BLOCK_FURNACE
Definition: BlockType.h:73
@ E_BLOCK_END_PORTAL_FRAME
Definition: BlockType.h:135
@ E_BLOCK_BEACON
Definition: BlockType.h:153
@ E_BLOCK_CHAIN_COMMAND_BLOCK
Definition: BlockType.h:230
@ E_BLOCK_PISTON_EXTENSION
Definition: BlockType.h:44
@ E_BLOCK_CHEST
Definition: BlockType.h:64
@ E_BLOCK_LIT_FURNACE
Definition: BlockType.h:74
@ E_BLOCK_BEDROCK
Definition: BlockType.h:17
@ E_BLOCK_OBSIDIAN
Definition: BlockType.h:59
@ E_BLOCK_STRUCTURE_BLOCK
Definition: BlockType.h:273
@ E_BLOCK_TRAPPED_CHEST
Definition: BlockType.h:161
@ E_BLOCK_ANVIL
Definition: BlockType.h:160
@ E_BLOCK_COMMAND_BLOCK
Definition: BlockType.h:152
@ E_BLOCK_END_GATEWAY
Definition: BlockType.h:228
@ E_BLOCK_NOTE_BLOCK
Definition: BlockType.h:35
@ E_BLOCK_HOPPER
Definition: BlockType.h:171
@ E_BLOCK_DROPPER
Definition: BlockType.h:176
@ E_BLOCK_ENDER_CHEST
Definition: BlockType.h:145
@ E_BLOCK_END_PORTAL
Definition: BlockType.h:134
@ E_BLOCK_DISPENSER
Definition: BlockType.h:33
@ E_BLOCK_INVERTED_DAYLIGHT_SENSOR
Definition: BlockType.h:197
@ E_BLOCK_MOB_SPAWNER
Definition: BlockType.h:62
@ E_BLOCK_BARRIER
Definition: BlockType.h:185
@ E_BLOCK_PISTON
Definition: BlockType.h:43
@ E_BLOCK_WALL_BANNER
Definition: BlockType.h:196
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
@ BLOCK_FACE_XP
Definition: Defines.h:41
@ BLOCK_FACE_YP
Definition: Defines.h:43
@ BLOCK_FACE_YM
Definition: Defines.h:42
@ BLOCK_FACE_ZM
Definition: Defines.h:44
@ BLOCK_FACE_ZP
Definition: Defines.h:45
@ BLOCK_FACE_XM
Definition: Defines.h:40
@ BLOCK_FACE_NONE
Definition: Defines.h:39
Byte ColourID
Definition: Globals.h:162
#define ASSERT(x)
Definition: Globals.h:276
#define UNUSED
Definition: Globals.h:72
unsigned char Byte
Definition: Globals.h:161
constexpr cBlockHandler(BLOCKTYPE a_BlockType)
Definition: BlockHandler.h:29
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
Definition: BlockPiston.h:122
virtual void OnBroken(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, Vector3i a_BlockPos, BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta, const cEntity *a_Digger) const override
static const Byte PistonExtendAction
Piston extension block action.
Definition: BlockPiston.h:59
static Vector3i MetadataToOffset(NIBBLETYPE a_PistonMeta)
Converts piston block's metadata into a unit vector representing the direction in which the piston wi...
Definition: BlockPiston.cpp:21
static bool IsSticky(BLOCKTYPE a_BlockType)
Returns true if the piston (specified by blocktype) is a sticky piston.
Definition: BlockPiston.h:65
static void RetractPiston(Vector3i a_BlockPos, cWorld &a_World)
std::unordered_set< Vector3i, VectorHasher< int > > Vector3iSet
Definition: BlockPiston.h:56
static void ExtendPiston(Vector3i a_BlockPos, cWorld &a_World)
Definition: BlockPiston.cpp:44
static void PushBlocks(const Vector3iSet &a_BlocksToPush, cWorld &a_World, const Vector3i &a_PushDir)
Moves a list of blocks in a specific direction.
static bool CanPushBlock(const Vector3i &a_BlockPos, cWorld &a_World, bool a_RequirePushable, Vector3iSet &a_BlocksPushed, const Vector3i &a_PushDir)
Tries to push a block and increases the pushed blocks variable.
static const Byte PistonRetractAction
Piston retraction block action.
Definition: BlockPiston.h:62
static eBlockFace MetaDataToDirection(NIBBLETYPE a_MetaData)
Definition: BlockPiston.h:27
static bool CanPush(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Returns true if the specified block can be pushed by a piston (and left intact)
Definition: BlockPiston.h:68
static bool IsExtended(NIBBLETYPE a_PistonMeta)
Returns true if the piston (with the specified meta) is extended.
Definition: BlockPiston.h:52
virtual void OnBroken(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, Vector3i a_BlockPos, BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta, const cEntity *a_Digger) const override
Called after a block gets broken (replaced with air), by natural means.
virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, const cItem *a_Tool) const override
Returns the pickups that would result if the block was mined by a_Digger using a_Tool.
constexpr cBlockPistonHeadHandler(void)
Definition: BlockPiston.h:152
Mixin to clear the block's meta value when converting to a pickup.
Definition: Mixins.h:31
cYawRotator< Base, BitMask, North, East, South, West > Super
Definition: Mixins.h:216
Definition: Entity.h:76
Definition: Item.h:37
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
Definition: World.h:53