Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemEndCrystal.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemHandler.h"
5 #include "../World.h"
6 #include "../Entities/Player.h"
7 
8 
9 
10 
11 
13  public cItemHandler
14 {
16 
17 public:
18 
19  constexpr cItemEndCrystalHandler(int a_ItemType) :
20  Super(a_ItemType)
21  {
22  }
23 
24 
25  virtual bool OnItemUse(
26  cWorld * a_World, cPlayer * a_Player,
27  cBlockPluginInterface & a_PluginInterface, const cItem & a_HeldItem,
28  const Vector3i a_BlockPos,
29  eBlockFace a_ClickedBlockFace) const override
30  {
31  // Must click a valid block:
32  if (a_ClickedBlockFace < 0)
33  {
34  return false;
35  }
36 
37  if (
38  const auto BlockType = a_World->GetBlock(a_BlockPos);
39 
40  // Don't place if placement block is not obsidian or bedrock:
42  )
43  {
44  return false;
45  }
46 
47  // The position of the block above the placement block.
48  const auto Above = a_BlockPos.addedY(1);
49 
50  if (
51  // Don't place if two blocks above placement block aren't air:
52  ((Above.y != cChunkDef::Height) && (a_World->GetBlock(Above) != E_BLOCK_AIR)) ||
53  ((Above.y < (cChunkDef::Height - 1)) && (a_World->GetBlock(Above.addedY(1)) != E_BLOCK_AIR)) ||
54 
55  // Refuse placement if there are any entities in a (1 by 2 by 1) bounding box with base at the block above:
56  !a_World->ForEachEntityInBox(
57  { Above, Above + Vector3i(1, 2, 1) },
58  [](cEntity & a_Entity)
59  {
60  return true;
61  }
62  )
63  )
64  {
65  return false;
66  }
67 
68  // Spawns ender crystal entity, aborts if plugin cancelled:
69  if (a_World->SpawnEnderCrystal(Vector3d(0.5, 0, 0.5) + Above, false) == cEntity::INVALID_ID)
70  {
71  return false;
72  }
73 
74  if (!a_Player->IsGameModeCreative())
75  {
76  a_Player->GetInventory().RemoveOneEquippedItem();
77  }
78 
79  return true;
80  }
81 };
@ E_BLOCK_AIR
Definition: BlockType.h:10
@ E_BLOCK_BEDROCK
Definition: BlockType.h:17
@ E_BLOCK_OBSIDIAN
Definition: BlockType.h:59
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
BlockType
Definition: BlockTypes.h:4
This interface is used to decouple block handlers from the cPluginManager dependency through cWorld.
static const int Height
Definition: ChunkDef.h:125
Definition: Entity.h:76
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
constexpr cItemEndCrystalHandler(int a_ItemType)
virtual bool OnItemUse(cWorld *a_World, cPlayer *a_Player, cBlockPluginInterface &a_PluginInterface, const cItem &a_HeldItem, const Vector3i a_BlockPos, eBlockFace a_ClickedBlockFace) const override
Called when the player tries to use the item (right mouse button).
constexpr cItemHandler(int a_ItemType)
Definition: ItemHandler.h:37
Vector3< T > addedY(T a_AddY) const
Returns a copy of this vector moved by the specified amount on the y axis.
Definition: Vector3.h:314
Definition: World.h:53
BLOCKTYPE GetBlock(Vector3i a_BlockPos) const
Returns the block type at the specified position.
Definition: World.h:363
UInt32 SpawnEnderCrystal(Vector3d a_Pos, bool a_ShowBottom)
Spawns a new ender crystal at the specified block coords.
Definition: World.cpp:2028
virtual bool ForEachEntityInBox(const cBoundingBox &a_Box, cEntityCallback a_Callback) override
Calls the callback for each entity that has a nonempty intersection with the specified boundingbox.
Definition: World.cpp:2445