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 
5 
6 
7 
8 class cItemMinecartHandler final:
9  public cItemHandler
10 {
12 
13 public:
14 
15  using Super::Super;
16 
17 
18 
19 
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 be used on a block
31  if (a_ClickedBlockFace < 0)
32  {
33  return false;
34  }
35 
36  // Check that there's rail in there:
37  BLOCKTYPE Block = a_World->GetBlock(a_ClickedBlockPos);
38  switch (Block)
39  {
44  {
45  // These are allowed
46  break;
47  }
48  default:
49  {
50  LOGD("Used minecart on an unsuitable block %d (%s)", Block, ItemTypeToString(Block).c_str());
51  return false;
52  }
53  }
54 
55  // Spawn the minecart:
56  auto SpawnPos = Vector3d(a_ClickedBlockPos) + Vector3d(0.5, 0.5, 0.5);
57  if (a_World->SpawnMinecart(SpawnPos, m_ItemType) == cEntity::INVALID_ID)
58  {
59  return false;
60  }
61 
62  // Remove the item from inventory:
63  if (!a_Player->IsGameModeCreative())
64  {
65  a_Player->GetInventory().RemoveOneEquippedItem();
66  }
67  return true;
68  }
69 
70 } ;
AString ItemTypeToString(short a_ItemType)
Translates itemtype into a string.
Definition: BlockType.cpp:252
@ E_BLOCK_ACTIVATOR_RAIL
Definition: BlockType.h:174
@ E_BLOCK_MINECART_TRACKS
Definition: BlockType.h:80
@ E_BLOCK_POWERED_RAIL
Definition: BlockType.h:37
@ E_BLOCK_DETECTOR_RAIL
Definition: BlockType.h:38
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
#define LOGD
Definition: LoggerSimple.h:83
Vector3< double > Vector3d
Definition: Vector3.h:485
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
const int m_ItemType
Definition: ItemHandler.h:141
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
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: ItemMinecart.h:21
Definition: World.h:53
BLOCKTYPE GetBlock(Vector3i a_BlockPos) const
Returns the block type at the specified position.
Definition: World.h:363
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:1958