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  public cItemHandler
14 {
16 
17 public:
18 
19  using Super::Super;
20 
21  virtual bool OnItemUse(
22  cWorld * a_World,
23  cPlayer * a_Player,
24  cBlockPluginInterface & a_PluginInterface,
25  const cItem & a_HeldItem,
26  const Vector3i a_ClickedBlockPos,
27  eBlockFace a_ClickedBlockFace
28  ) const override
29  {
30  // Must click a valid block:
31  if (a_ClickedBlockFace < 0)
32  {
33  return false;
34  }
35 
36  auto PlacementPos = AddFaceDirection(a_ClickedBlockPos, a_ClickedBlockFace);
37  if (a_ClickedBlockFace == BLOCK_FACE_YM)
38  {
39  PlacementPos.y--;
40  }
41 
42  auto MonsterType = ItemDamageToMonsterType(a_HeldItem.m_ItemDamage);
43  if (
44  (MonsterType != mtInvalidType) && // Valid monster type
45  (a_World->SpawnMob(PlacementPos.x + 0.5, PlacementPos.y, PlacementPos.z + 0.5, MonsterType, false) != cEntity::INVALID_ID)) // Spawning succeeded
46  {
47  if (!a_Player->IsGameModeCreative())
48  {
49  // The mob was spawned, "use" the item:
50  a_Player->GetInventory().RemoveOneEquippedItem();
51  }
52  return true;
53  }
54 
55  return false;
56  }
57 
58 
59 
60 
61 
64  static eMonsterType ItemDamageToMonsterType(short a_ItemDamage)
65  {
66  switch (a_ItemDamage)
67  {
68  case E_META_SPAWN_EGG_BAT: return mtBat;
69  case E_META_SPAWN_EGG_BLAZE: return mtBlaze;
72  case E_META_SPAWN_EGG_COW: return mtCow;
76  case E_META_SPAWN_EGG_GHAST: return mtGhast;
78  case E_META_SPAWN_EGG_HORSE: return mtHorse;
81  case E_META_SPAWN_EGG_OCELOT: return mtOcelot;
82  case E_META_SPAWN_EGG_PIG: return mtPig;
83  case E_META_SPAWN_EGG_RABBIT: return mtRabbit;
84  case E_META_SPAWN_EGG_SHEEP: return mtSheep;
87  case E_META_SPAWN_EGG_SLIME: return mtSlime;
88  case E_META_SPAWN_EGG_SPIDER: return mtSpider;
89  case E_META_SPAWN_EGG_SQUID: return mtSquid;
91  case E_META_SPAWN_EGG_WITCH: return mtWitch;
93  case E_META_SPAWN_EGG_WOLF: return mtWolf;
94  case E_META_SPAWN_EGG_ZOMBIE: return mtZombie;
97  default: return mtInvalidType;
98  }
99  }
100 } ;
@ E_META_SPAWN_EGG_CHICKEN
Definition: BlockType.h:1138
@ E_META_SPAWN_EGG_WOLF
Definition: BlockType.h:1140
@ E_META_SPAWN_EGG_BLAZE
Definition: BlockType.h:1127
@ E_META_SPAWN_EGG_SILVERFISH
Definition: BlockType.h:1126
@ E_META_SPAWN_EGG_OCELOT
Definition: BlockType.h:1143
@ E_META_SPAWN_EGG_SHEEP
Definition: BlockType.h:1136
@ E_META_SPAWN_EGG_ZOMBIE
Definition: BlockType.h:1120
@ E_META_SPAWN_EGG_PIG
Definition: BlockType.h:1135
@ E_META_SPAWN_EGG_VILLAGER
Definition: BlockType.h:1147
@ E_META_SPAWN_EGG_CAVE_SPIDER
Definition: BlockType.h:1125
@ E_META_SPAWN_EGG_CREEPER
Definition: BlockType.h:1116
@ E_META_SPAWN_EGG_MOOSHROOM
Definition: BlockType.h:1141
@ E_META_SPAWN_EGG_ENDERMAN
Definition: BlockType.h:1124
@ E_META_SPAWN_EGG_ZOMBIE_VILLAGER
Definition: BlockType.h:1108
@ E_META_SPAWN_EGG_SQUID
Definition: BlockType.h:1139
@ E_META_SPAWN_EGG_BAT
Definition: BlockType.h:1131
@ E_META_SPAWN_EGG_HORSE
Definition: BlockType.h:1145
@ E_META_SPAWN_EGG_GUARDIAN
Definition: BlockType.h:1134
@ E_META_SPAWN_EGG_GHAST
Definition: BlockType.h:1122
@ E_META_SPAWN_EGG_SPIDER
Definition: BlockType.h:1118
@ E_META_SPAWN_EGG_ENDERMITE
Definition: BlockType.h:1133
@ E_META_SPAWN_EGG_MAGMA_CUBE
Definition: BlockType.h:1128
@ E_META_SPAWN_EGG_COW
Definition: BlockType.h:1137
@ E_META_SPAWN_EGG_WITHER_SKELETON
Definition: BlockType.h:1092
@ E_META_SPAWN_EGG_SKELETON
Definition: BlockType.h:1117
@ E_META_SPAWN_EGG_WITCH
Definition: BlockType.h:1132
@ E_META_SPAWN_EGG_RABBIT
Definition: BlockType.h:1146
@ E_META_SPAWN_EGG_SLIME
Definition: BlockType.h:1121
@ E_META_SPAWN_EGG_ZOMBIE_PIGMAN
Definition: BlockType.h:1123
void AddFaceDirection(int &a_BlockX, int &a_BlockY, int &a_BlockZ, eBlockFace a_BlockFace, bool a_bInverse)
Modifies the specified coords so that they point to the block adjacent to the one specified through i...
Definition: Defines.cpp:378
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
@ BLOCK_FACE_YM
Definition: Defines.h:42
eMonsterType
Identifies individual monster type.
Definition: MonsterTypes.h:11
@ mtZombieVillager
Definition: MonsterTypes.h:82
@ mtSkeleton
Definition: MonsterTypes.h:59
@ mtSheep
Definition: MonsterTypes.h:56
@ mtEndermite
Definition: MonsterTypes.h:28
@ mtMagmaCube
Definition: MonsterTypes.h:40
@ mtWolf
Definition: MonsterTypes.h:77
@ mtRabbit
Definition: MonsterTypes.h:53
@ mtZombie
Definition: MonsterTypes.h:79
@ mtOcelot
Definition: MonsterTypes.h:43
@ mtEnderman
Definition: MonsterTypes.h:27
@ mtCaveSpider
Definition: MonsterTypes.h:17
@ mtPig
Definition: MonsterTypes.h:47
@ mtVillager
Definition: MonsterTypes.h:71
@ mtHorse
Definition: MonsterTypes.h:34
@ mtWitch
Definition: MonsterTypes.h:74
@ mtCow
Definition: MonsterTypes.h:20
@ mtMooshroom
Definition: MonsterTypes.h:41
@ mtSquid
Definition: MonsterTypes.h:64
@ mtInvalidType
Definition: MonsterTypes.h:12
@ mtBat
Definition: MonsterTypes.h:14
@ mtChicken
Definition: MonsterTypes.h:18
@ mtBlaze
Definition: MonsterTypes.h:15
@ mtCreeper
Definition: MonsterTypes.h:21
@ mtSpider
Definition: MonsterTypes.h:63
@ mtGhast
Definition: MonsterTypes.h:31
@ mtSilverfish
Definition: MonsterTypes.h:58
@ mtZombiePigman
Definition: MonsterTypes.h:85
@ mtSlime
Definition: MonsterTypes.h:61
@ mtWitherSkeleton
Definition: MonsterTypes.h:76
@ mtGuardian
Definition: MonsterTypes.h:33
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
static const UInt32 INVALID_ID
Special ID that is considered an "invalid value", signifying no entity.
Definition: Entity.h:128
Definition: Player.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:1025
cInventory & GetInventory(void)
Definition: Player.h:156
bool RemoveOneEquippedItem(void)
Removes one item out of the currently equipped item stack, returns true if successful,...
Definition: Inventory.cpp:232
Definition: Item.h:37
short m_ItemDamage
Definition: Item.h:165
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
static eMonsterType ItemDamageToMonsterType(short a_ItemDamage)
Converts the Spawn egg item damage to the monster type to spawn.
Definition: ItemSpawnEgg.h:64
virtual bool OnItemUse(cWorld *a_World, cPlayer *a_Player, cBlockPluginInterface &a_PluginInterface, const cItem &a_HeldItem, const Vector3i a_ClickedBlockPos, eBlockFace a_ClickedBlockFace) const override
Called when the player tries to use the item (right mouse button).
Definition: ItemSpawnEgg.h:21
Definition: World.h:53
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:2897