Cuberite
A lightweight, fast and extensible game server for Minecraft
DropSpenserHandler.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "../../BlockEntities/DropSpenserEntity.h"
5 
6 
7 
8 
9 
11 {
12  static bool IsActivated(NIBBLETYPE a_Meta)
13  {
14  return (a_Meta & E_META_DROPSPENSER_ACTIVATED) != 0;
15  }
16 
18  {
19  if (IsOn)
20  {
21  return a_Meta | E_META_DROPSPENSER_ACTIVATED; // set the bit
22  }
23  else
24  {
25  return a_Meta & ~E_META_DROPSPENSER_ACTIVATED; // clear the bit
26  }
27  }
28 
29  static PowerLevel GetPowerDeliveredToPosition(const cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType, bool IsLinked)
30  {
31  UNUSED(a_Chunk);
32  UNUSED(a_Position);
33  UNUSED(a_BlockType);
34  UNUSED(a_QueryPosition);
35  UNUSED(a_QueryBlockType);
36  UNUSED(IsLinked);
37  return 0;
38  }
39 
40  static void Update(cChunk & a_Chunk, cChunk &, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, const PowerLevel Power)
41  {
42  // LOGD("Evaluating spencer the dropspenser (%d %d %d)", a_Position.x, a_Position.y, a_Position.z);
43 
44  const bool IsPoweredNow = (Power > 0);
45  const bool WasPoweredPreviously = IsActivated(a_Meta);
46 
47  if (IsPoweredNow && !WasPoweredPreviously)
48  {
49  a_Chunk.DoWithBlockEntityAt(a_Position, [](cBlockEntity & a_BlockEntity)
50  {
51  ASSERT((a_BlockEntity.GetBlockType() == E_BLOCK_DISPENSER) || (a_BlockEntity.GetBlockType() == E_BLOCK_DROPPER));
52 
53  static_cast<cDropSpenserEntity &>(a_BlockEntity).Activate();
54  return false;
55  });
56  }
57 
58  // Update the internal dropspenser state if necessary
59  if (IsPoweredNow != WasPoweredPreviously)
60  {
61  a_Chunk.SetMeta(a_Position, SetActivationState(a_Meta, IsPoweredNow));
62  }
63  }
64 
65  static void ForValidSourcePositions(const cChunk & a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, ForEachSourceCallback & Callback)
66  {
67  UNUSED(a_Chunk);
68  UNUSED(a_BlockType);
69  UNUSED(a_Meta);
70 
71  // Consider indirect power:
72  Callback.CheckIndirectPower();
73 
74  // Consider normal adjacents:
75  InvokeForAdjustedRelatives(Callback, a_Position, RelativeAdjacents);
76  }
77 };
@ E_META_DROPSPENSER_ACTIVATED
Definition: BlockType.h:652
@ E_BLOCK_DROPPER
Definition: BlockType.h:176
@ E_BLOCK_DISPENSER
Definition: BlockType.h:33
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
#define ASSERT(x)
Definition: Globals.h:276
#define UNUSED
Definition: Globals.h:72
constexpr std::array< Vector3i, 6 > RelativeAdjacents
void InvokeForAdjustedRelatives(ForEachSourceCallback &Callback, const Vector3i Position, const ArrayType &Relative)
unsigned char PowerLevel
unsigned char Power(const BlockState Block)
static void Update(cChunk &a_Chunk, cChunk &, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, const PowerLevel Power)
static NIBBLETYPE SetActivationState(NIBBLETYPE a_Meta, bool IsOn)
static void ForValidSourcePositions(const cChunk &a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta, ForEachSourceCallback &Callback)
static bool IsActivated(NIBBLETYPE a_Meta)
static PowerLevel GetPowerDeliveredToPosition(const cChunk &a_Chunk, Vector3i a_Position, BLOCKTYPE a_BlockType, Vector3i a_QueryPosition, BLOCKTYPE a_QueryBlockType, bool IsLinked)
static bool IsOn(NIBBLETYPE a_Meta)
BLOCKTYPE GetBlockType() const
Definition: BlockEntity.h:97
Definition: Chunk.h:36
bool DoWithBlockEntityAt(Vector3i a_Position, cBlockEntityCallback a_Callback)
Calls the callback for the block entity at the specified coords; returns false if there's no block en...
Definition: Chunk.cpp:1737
void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta)
Definition: Chunk.h:286
void CheckIndirectPower()
Callback invoked for blocks supporting quasiconnectivity.