Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemMinecart.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "../Entities/Minecart.h"
5 
6 
7 
8 
9 
11  public cItemHandler
12 {
14 
15 public:
16  cItemMinecartHandler(int a_ItemType) :
17  super(a_ItemType)
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  // Check that there's rail in there:
34  BLOCKTYPE Block = a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ);
35  switch (Block)
36  {
41  {
42  // These are allowed
43  break;
44  }
45  default:
46  {
47  LOGD("Used minecart on an unsuitable block %d (%s)", Block, ItemTypeToString(Block).c_str());
48  return false;
49  }
50  }
51 
52  double x = static_cast<double>(a_BlockX) + 0.5;
53  double y = static_cast<double>(a_BlockY) + 0.5;
54  double z = static_cast<double>(a_BlockZ) + 0.5;
55 
56  if (a_World->SpawnMinecart(x, y, z, m_ItemType) == cEntity::INVALID_ID)
57  {
58  return false;
59  }
60 
61  if (!a_Player->IsGameModeCreative())
62  {
63  a_Player->GetInventory().RemoveOneEquippedItem();
64  }
65  return true;
66  }
67 
68 } ;
BLOCKTYPE GetBlock(Vector3i a_BlockPos)
Returns the block type at the specified position.
Definition: World.h:416
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
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
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld...
cItemHandler super
Definition: ItemMinecart.h:13
cItemMinecartHandler(int a_ItemType)
Definition: ItemMinecart.h:16
Definition: World.h:65
#define LOGD(...)
Definition: LoggerSimple.h:40
UInt32 SpawnMinecart(Vector3d a_Pos, int a_MinecartType, const cItem &a_Content=cItem(), int a_BlockHeight=1)
Spawns an minecart at the given coordinates.
Definition: World.cpp:2088
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
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: ItemMinecart.h:23
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
AString ItemTypeToString(short a_ItemType)
Translates itemtype into a string.
Definition: BlockID.cpp:270