Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemItemFrame.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemHandler.h"
5 #include "../Entities/ItemFrame.h"
6 #include "../Entities/Player.h"
7 
8 
9 
10 
11 
13  public cItemHandler
14 {
15 public:
16  cItemItemFrameHandler(int a_ItemType)
17  : cItemHandler(a_ItemType)
18  {
19 
20  }
21 
22 
23 
24  virtual bool OnItemUse(
25  cWorld * a_World, cPlayer * a_Player, cBlockPluginInterface & a_PluginInterface, const cItem & a_Item,
26  int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace
27  ) override
28  {
29  if ((a_BlockFace == BLOCK_FACE_NONE) || (a_BlockFace == BLOCK_FACE_YP) || (a_BlockFace == BLOCK_FACE_YM))
30  {
31  // Client sends this if clicked on top or bottom face
32  return false;
33  }
34 
35  AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace); // Make sure block that will be occupied is free
36  BLOCKTYPE Block = a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ);
37  AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, true); // We want the clicked block, so go back again
38 
39  if (Block == E_BLOCK_AIR)
40  {
41  auto ItemFrame = cpp14::make_unique<cItemFrame>(a_BlockFace, Vector3i{a_BlockX, a_BlockY, a_BlockZ});
42  auto ItemFramePtr = ItemFrame.get();
43  if (!ItemFramePtr->Initialize(std::move(ItemFrame), *a_World))
44  {
45  return false;
46  }
47 
48  if (!a_Player->IsGameModeCreative())
49  {
50  a_Player->GetInventory().RemoveOneEquippedItem();
51  }
52 
53  return true;
54 
55  }
56  return false;
57  }
58 };
59 
60 
61 
62 
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...
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
cItemItemFrameHandler(int a_ItemType)
Definition: ItemItemFrame.h:16
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
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: ItemItemFrame.h:24
Definition: Item.h:36