Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemEmptyMap.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "../Item.h"
5 
6 
7 
8 
9 
11  public cItemHandler
12 {
14 
15  static const unsigned int DEFAULT_SCALE = 0;
16 
17 public:
19  super(E_ITEM_EMPTY_MAP)
20  {
21  }
22 
23 
24 
25  virtual bool OnItemUse(
26  cWorld * a_World, cPlayer * a_Player, cBlockPluginInterface & a_PluginInterface, const cItem & a_Item,
27  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace
28  ) override
29  {
30  UNUSED(a_Item);
31  UNUSED(a_BlockX);
32  UNUSED(a_BlockZ);
33  UNUSED(a_BlockFace);
34 
35  // The map center is fixed at the central point of the 8x8 block of chunks you are standing in when you right-click it.
36 
37  const int RegionWidth = cChunkDef::Width * 8;
38 
39  int CenterX = FloorC(a_Player->GetPosX() / RegionWidth) * RegionWidth + (RegionWidth / 2);
40  int CenterZ = FloorC(a_Player->GetPosZ() / RegionWidth) * RegionWidth + (RegionWidth / 2);
41 
42  cMap * NewMap = a_World->GetMapManager().CreateMap(CenterX, CenterZ, DEFAULT_SCALE);
43 
44  // Remove empty map from inventory
45  if (!a_Player->GetInventory().RemoveOneEquippedItem())
46  {
47  ASSERT(!"Inventory mismatch");
48  return true;
49  }
50 
51  if (NewMap == nullptr)
52  {
53  return true;
54  }
55 
56  a_Player->GetInventory().AddItem(cItem(E_ITEM_MAP, 1, static_cast<short>(NewMap->GetID() & 0x7fff)));
57 
58  return true;
59  }
60 } ;
cMap * CreateMap(int a_CenterX, int a_CenterY, unsigned int a_Scale=3)
Creates a new map.
Definition: MapManager.cpp:74
double GetPosX(void) const
Definition: Entity.h:206
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
Encapsulates an in-game world map.
Definition: Map.h:80
static const unsigned int DEFAULT_SCALE
Definition: ItemEmptyMap.h:15
static const int Width
Definition: ChunkDef.h:134
Definition: Player.h:27
unsigned int GetID(void) const
Definition: Map.h:150
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld...
Definition: World.h:65
cItemHandler super
Definition: ItemEmptyMap.h:13
cMapManager & GetMapManager(void)
Returns the associated map manager instance.
Definition: World.h:886
#define ASSERT(x)
Definition: Globals.h:335
#define UNUSED
Definition: Globals.h:152
int AddItem(const cItem &a_ItemStack, bool a_AllowNewStacks=true)
Adds as many items out of a_ItemStack as can fit.
Definition: Inventory.cpp:106
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
double GetPosZ(void) const
Definition: Entity.h:208
std::enable_if< std::is_arithmetic< T >::value, C >::type FloorC(T a_Value)
Floors a value, then casts it to C (an int by default)
Definition: Globals.h:362
cInventory & GetInventory(void)
Definition: Player.h:136
Definition: Item.h:36
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: ItemEmptyMap.h:25