Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockOre.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "BlockHandler.h"
5 
6 
7 
8 
9 
11  public cBlockHandler
12 {
14 public:
16  : cBlockHandler(a_BlockType)
17  {
18  }
19 
20 
21 
22 
23 
24  virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) override
25  {
26  // If using silk-touch, drop self rather than the resource:
27  if (ToolHasSilkTouch(a_Tool))
28  {
29  return cItem(m_BlockType);
30  }
31 
32  // TODO: Handle the Fortune enchantment here
33  auto & random = GetRandomProvider();
34  switch (m_BlockType)
35  {
36  case E_BLOCK_LAPIS_ORE: return cItem(E_ITEM_DYE, random.RandInt<char>(4, 8), 4);
37  case E_BLOCK_REDSTONE_ORE: return cItem(E_ITEM_REDSTONE_DUST, random.RandInt<char>(4, 5), 0);
38  case E_BLOCK_REDSTONE_ORE_GLOWING: return cItem(E_ITEM_REDSTONE_DUST, random.RandInt<char>(4, 5), 0);
41  case E_BLOCK_COAL_ORE: return cItem(E_ITEM_COAL);
43  case E_BLOCK_CLAY: return cItem(E_ITEM_CLAY, 4);
44  default:
45  {
46  return cItem(m_BlockType);
47  }
48  }
49  }
50 
51 
52 
53 
54 
55  virtual void OnPlayerBrokeBlock(
56  cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface,
57  cPlayer & a_Player, Vector3i a_BlockPos,
58  BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta
59  ) override
60  {
61  if (!a_Player.IsGameModeSurvival())
62  {
63  // Don't drop XP unless the player is in survival mode.
64  return;
65  }
66 
68  {
69  // Don't drop XP when the ore is mined with the Silk Touch enchantment
70  return;
71  }
72 
73  auto & random = GetRandomProvider();
74  int reward = 0;
75 
76  switch (a_OldBlockType)
77  {
79  case E_BLOCK_LAPIS_ORE:
80  {
81  // Lapis and nether quartz get 2 - 5 experience
82  reward = random.RandInt(2, 5);
83  break;
84  }
87  {
88  // Redstone gets 1 - 5 experience
89  reward = random.RandInt(1, 5);
90  break;
91  }
94  {
95  // Diamond and emerald get 3 - 7 experience
96  reward = random.RandInt(3, 7);
97  break;
98  }
99  case E_BLOCK_COAL_ORE:
100  {
101  // Coal gets 0 - 2 experience
102  reward = random.RandInt(2);
103  break;
104  }
105 
106  default: break;
107  }
108 
109  if (reward > 0)
110  {
111  a_WorldInterface.SpawnSplitExperienceOrbs(Vector3d(0.5, 0.5, 0.5) + a_BlockPos, reward);
112  }
113  }
114 } ;
115 
116 
117 
118 
cBlockHandler super
Definition: BlockOre.h:13
virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity *a_BlockEntity, const cEntity *a_Digger, const cItem *a_Tool) override
Returns the pickups that would result if the block was mined by a_Digger using a_Tool.
Definition: BlockOre.h:24
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
MTRand & GetRandomProvider()
Returns the current thread&#39;s random number source.
Definition: FastRandom.cpp:20
const cItem & GetEquippedItem(void) const
Definition: Player.h:142
Definition: Player.h:27
BLOCKTYPE m_BlockType
Definition: BlockHandler.h:213
unsigned int GetLevel(int a_EnchantmentID) const
Returns the level for the specified enchantment; 0 if not stored.
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
static bool ToolHasSilkTouch(const cItem *a_Tool)
Returns true if the specified tool is valid and has a non-zero silk-touch enchantment.
cEnchantments m_Enchantments
Definition: Item.h:212
virtual void OnPlayerBrokeBlock(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, cPlayer &a_Player, Vector3i a_BlockPos, BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta) override
Called just after the player breaks the block.
Definition: BlockOre.h:55
Vector3< double > Vector3d
Definition: Vector3.h:445
virtual std::vector< UInt32 > SpawnSplitExperienceOrbs(Vector3d a_Pos, int a_Reward)=0
Spawns experience orbs of the specified total value at the given location.
bool IsGameModeSurvival(void) const
Returns true if the player is in Survival mode, either explicitly, or by inheriting from current worl...
Definition: Player.cpp:1269
Definition: Entity.h:73
cBlockOreHandler(BLOCKTYPE a_BlockType)
Definition: BlockOre.h:15
Definition: Item.h:36
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234