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 {
16 
17 public:
18 
19  using Super::Super;
20 
21 
22 
23 
24 
25  virtual bool OnItemUse(
26  cWorld * a_World,
27  cPlayer * a_Player,
28  cBlockPluginInterface & a_PluginInterface,
29  const cItem & a_HeldItem,
30  const Vector3i a_ClickedBlockPos,
31  eBlockFace a_ClickedBlockFace
32  ) const override
33  {
34  // Can only place on a side face:
35  if ((a_ClickedBlockFace == BLOCK_FACE_NONE) || (a_ClickedBlockFace == BLOCK_FACE_YP) || (a_ClickedBlockFace == BLOCK_FACE_YM))
36  {
37  return false;
38  }
39 
40  // Make sure the support block is a valid block to place an item frame on:
41  if (!cHangingEntity::IsValidSupportBlock(a_World->GetBlock(a_ClickedBlockPos)))
42  {
43  return false;
44  }
45 
46  // Make sure block that will be occupied by the item frame is free now:
47  const auto PlacePos = AddFaceDirection(a_ClickedBlockPos, a_ClickedBlockFace);
48  BLOCKTYPE Block = a_World->GetBlock(PlacePos);
49  if (Block != E_BLOCK_AIR)
50  {
51  return false;
52  }
53 
54  // An item frame, centred so pickups spawn nicely.
55  auto ItemFrame = std::make_unique<cItemFrame>(a_ClickedBlockFace, Vector3d(0.5, 0.5, 0.5) + PlacePos);
56  auto ItemFramePtr = ItemFrame.get();
57  if (!ItemFramePtr->Initialize(std::move(ItemFrame), *a_World))
58  {
59  return false;
60  }
61 
62  if (!a_Player->IsGameModeCreative())
63  {
64  a_Player->GetInventory().RemoveOneEquippedItem();
65  }
66 
67  return true;
68  }
69 };
70 
71 
72 
73 
@ E_BLOCK_AIR
Definition: BlockType.h:10
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
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_YP
Definition: Defines.h:43
@ BLOCK_FACE_YM
Definition: Defines.h:42
@ BLOCK_FACE_NONE
Definition: Defines.h:39
@ ItemFrame
Vector3< double > Vector3d
Definition: Vector3.h:485
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
static bool IsValidSupportBlock(BLOCKTYPE a_BlockType)
Returns if the given block can support hanging entity placements.
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
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: ItemItemFrame.h:25
Definition: World.h:53
BLOCKTYPE GetBlock(Vector3i a_BlockPos) const
Returns the block type at the specified position.
Definition: World.h:363