Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockIce.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "BlockHandler.h"
5 
6 
7 
8 
9 
10 class cBlockIceHandler final :
11  public cBlockHandler
12 {
14 
15 public:
16 
17  using Super::Super;
18 
19 private:
20 
21  virtual cItems ConvertToPickups(const NIBBLETYPE a_BlockMeta, const cItem * const a_Tool) const override
22  {
23  // Only drop self when using silk-touch:
24  if (ToolHasSilkTouch(a_Tool))
25  {
26  return cItem(m_BlockType);
27  }
28 
29  return {};
30  }
31 
32  virtual void OnUpdate(
33  cChunkInterface & a_ChunkInterface,
34  cWorldInterface & a_WorldInterface,
35  cBlockPluginInterface & a_PluginInterface,
36  cChunk & a_Chunk,
37  const Vector3i a_RelPos
38  ) const override
39  {
40  // Disappears instantly in nether:
41  if (a_WorldInterface.GetDimension() == dimNether)
42  {
43  a_Chunk.SetBlock(a_RelPos, E_BLOCK_AIR, 0);
44  return;
45  }
46 
47  // Artificial light on any of the surrounding block > 11 leads to melting the ice.
48  static const std::array<Vector3i, 7> Adjacents
49  {
50  {
51  { 1, 0, 0 }, { -1, 0, 0 },
52  { 0, 1, 0 }, { 0, -1, 0 },
53  { 0, 0, 1 }, { 0, 0, -1 }
54  }
55  };
56 
57  for (const auto & Offset : Adjacents)
58  {
59  auto Position = a_RelPos + Offset;
60  const auto Chunk = a_Chunk.GetRelNeighborChunkAdjustCoords(Position);
61 
62  if ((Chunk == nullptr) || !Chunk->IsValid())
63  {
64  continue;
65  }
66 
67  if (!Chunk->IsLightValid())
68  {
69  Chunk->GetWorld()->QueueLightChunk(Chunk->GetPosX(), Chunk->GetPosZ());
70  continue;
71  }
72 
73  if (Chunk->GetBlockLight(Position) > 11)
74  {
75  a_Chunk.SetBlock(a_RelPos, E_BLOCK_STATIONARY_WATER, 0);
76  return;
77  }
78  }
79  }
80 
81  virtual void OnBroken(
82  cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface,
83  Vector3i a_BlockPos,
84  BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta,
85  const cEntity * a_Digger
86  ) const override
87  {
88  UNUSED(a_Digger);
89  // If there's a solid block or a liquid underneath, convert to water, rather than air
90  if (a_BlockPos.y <= 0)
91  {
92  return;
93  }
94 
95  const auto Below = a_ChunkInterface.GetBlock(a_BlockPos.addedY(-1));
96  if (cBlockInfo::FullyOccupiesVoxel(Below) || IsBlockLiquid(Below))
97  {
98  a_ChunkInterface.SetBlock(a_BlockPos, E_BLOCK_WATER, 0);
99  }
100  }
101 
102 
103 
104 
105 
106  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
107  {
108  UNUSED(a_Meta);
109  return 5;
110  }
111 } ;
bool IsBlockLiquid(BLOCKTYPE a_BlockType)
Definition: BlockInfo.cpp:58
@ E_BLOCK_WATER
Definition: BlockType.h:18
@ E_BLOCK_AIR
Definition: BlockType.h:10
@ E_BLOCK_STATIONARY_WATER
Definition: BlockType.h:19
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
@ dimNether
Definition: Defines.h:232
Byte ColourID
Definition: Globals.h:162
#define UNUSED
Definition: Globals.h:72
static bool FullyOccupiesVoxel(BLOCKTYPE Block)
Does this block fully occupy its voxel - is it a 'full' block?
Definition: BlockInfo.cpp:606
static bool ToolHasSilkTouch(const cItem *a_Tool)
Returns true if the specified tool is valid and has a non-zero silk-touch enchantment.
constexpr cBlockHandler(BLOCKTYPE a_BlockType)
Definition: BlockHandler.h:29
const BLOCKTYPE m_BlockType
Definition: BlockHandler.h:205
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.
Definition: BlockIce.h:81
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
Returns the base colour ID of the block, as will be represented on a map, as per documentation: https...
Definition: BlockIce.h:106
virtual void OnUpdate(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, cBlockPluginInterface &a_PluginInterface, cChunk &a_Chunk, const Vector3i a_RelPos) const override
Called when the block gets ticked either by a random tick or by a queued tick.
Definition: BlockIce.h:32
virtual cItems ConvertToPickups(const NIBBLETYPE a_BlockMeta, const cItem *const a_Tool) const override
Returns the pickups that would result if the block was mined by a_Digger using a_Tool.
Definition: BlockIce.h:21
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
void SetBlock(Vector3i a_BlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Sets the block at the specified coords to the specified value.
BLOCKTYPE GetBlock(Vector3i a_Pos)
virtual eDimension GetDimension(void) const =0
Definition: Chunk.h:36
cChunk * GetRelNeighborChunkAdjustCoords(Vector3i &a_RelPos) const
Returns the chunk into which the relatively-specified block belongs.
Definition: Chunk.cpp:1885
void SetBlock(Vector3i a_RelBlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Definition: Chunk.cpp:1263
cWorld * GetWorld(void) const
Definition: Chunk.h:135
Definition: Entity.h:76
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 y
Definition: Vector3.h:17
void QueueLightChunk(int a_ChunkX, int a_ChunkZ, std::unique_ptr< cChunkCoordCallback > a_Callback={})
Queues a chunk for lighting; a_Callback is called after the chunk is lighted.
Definition: World.cpp:2676