Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemSpawnEgg.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemHandler.h"
5 #include "../World.h"
6 #include "../Entities/Player.h"
7 
8 
9 
10 
11 
13 {
14 public:
15  cItemSpawnEggHandler(int a_ItemType) :
16  cItemHandler(a_ItemType)
17  {
18 
19  }
20 
21 
22 
23  virtual bool OnItemUse(
24  cWorld * a_World, cPlayer * a_Player, cBlockPluginInterface & a_PluginInterface, const cItem & a_Item,
25  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace
26  ) override
27  {
28  if (a_BlockFace < 0)
29  {
30  return false;
31  }
32 
33  AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
34 
35  if (a_BlockFace == BLOCK_FACE_YM)
36  {
37  a_BlockY--;
38  }
39 
41  if (
42  (MonsterType != mtInvalidType) && // Valid monster type
43  (a_World->SpawnMob(a_BlockX + 0.5, a_BlockY, a_BlockZ + 0.5, MonsterType, false) != cEntity::INVALID_ID)) // Spawning succeeded
44  {
45  if (!a_Player->IsGameModeCreative())
46  {
47  // The mob was spawned, "use" the item:
48  a_Player->GetInventory().RemoveOneEquippedItem();
49  }
50  return true;
51  }
52 
53  return false;
54  }
55 
56 
59  static eMonsterType ItemDamageToMonsterType(short a_ItemDamage)
60  {
61  switch (a_ItemDamage)
62  {
63  case E_META_SPAWN_EGG_BAT: return mtBat;
64  case E_META_SPAWN_EGG_BLAZE: return mtBlaze;
67  case E_META_SPAWN_EGG_COW: return mtCow;
70  case E_META_SPAWN_EGG_GHAST: return mtGhast;
72  case E_META_SPAWN_EGG_HORSE: return mtHorse;
75  case E_META_SPAWN_EGG_OCELOT: return mtOcelot;
76  case E_META_SPAWN_EGG_PIG: return mtPig;
77  case E_META_SPAWN_EGG_RABBIT: return mtRabbit;
78  case E_META_SPAWN_EGG_SHEEP: return mtSheep;
81  case E_META_SPAWN_EGG_SLIME: return mtSlime;
82  case E_META_SPAWN_EGG_SPIDER: return mtSpider;
83  case E_META_SPAWN_EGG_SQUID: return mtSquid;
85  case E_META_SPAWN_EGG_WITCH: return mtWitch;
86  case E_META_SPAWN_EGG_WOLF: return mtWolf;
87  case E_META_SPAWN_EGG_ZOMBIE: return mtZombie;
89  }
90  return mtInvalidType;
91  }
92 } ;
93 
94 
95 
96 
virtual UInt32 SpawnMob(double a_PosX, double a_PosY, double a_PosZ, eMonsterType a_MonsterType, bool a_Baby=false) override
Spawns a mob of the specified type.
Definition: World.cpp:3190
short m_ItemDamage
Definition: Item.h:211
virtual bool OnItemUse(cWorld *a_World, cPlayer *a_Player, cBlockPluginInterface &a_PluginInterface, const cItem &a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace) override
Called when the player tries to use the item (right mouse button).
Definition: ItemSpawnEgg.h:23
bool RemoveOneEquippedItem(void)
Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed.
Definition: Inventory.cpp:207
Definition: Player.h:27
cItemSpawnEggHandler(int a_ItemType)
Definition: ItemSpawnEgg.h:15
eMonsterType
Identifies individual monster type, as well as their network type-ID.
Definition: MonsterTypes.h:10
static eMonsterType ItemDamageToMonsterType(short a_ItemDamage)
Converts the Spawn egg item damage to the monster type to spawn.
Definition: ItemSpawnEgg.h:59
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld...
void AddFaceDirection(int &a_BlockX, int &a_BlockY, int &a_BlockZ, eBlockFace a_BlockFace, bool a_bInverse=false)
Definition: Defines.h:859
Definition: World.h:65
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
bool IsGameModeCreative(void) const
Returns true if the player is in Creative mode, either explicitly, or by inheriting from current worl...
Definition: Player.cpp:1260
cInventory & GetInventory(void)
Definition: Player.h:136
static const UInt32 INVALID_ID
Special ID that is considered an "invalid value", signifying no entity.
Definition: Entity.h:156
Definition: Item.h:36