Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemPainting.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 #include "../Entities/Painting.h"
8 
9 
10 
11 
12 
14  public cItemHandler
15 {
17 
18 public:
19 
20  using Super::Super;
21 
22 
23 
24 
25 
26  virtual bool OnItemUse(
27  cWorld * a_World,
28  cPlayer * a_Player,
29  cBlockPluginInterface & a_PluginInterface,
30  const cItem & a_HeldItem,
31  const Vector3i a_ClickedBlockPos,
32  eBlockFace a_ClickedBlockFace
33  ) const override
34  {
35  // Paintings can't be flatly placed:
36  if (
37  (a_ClickedBlockFace == BLOCK_FACE_NONE) ||
38  (a_ClickedBlockFace == BLOCK_FACE_YM) ||
39  (a_ClickedBlockFace == BLOCK_FACE_YP)
40  )
41  {
42  return false;
43  }
44 
45  // Make sure the support block is a valid block to place a painting on:
46  if (!cHangingEntity::IsValidSupportBlock(a_World->GetBlock(a_ClickedBlockPos)))
47  {
48  return false;
49  }
50 
51  // Make sure block that will be occupied is free:
52  auto PlacePos = AddFaceDirection(a_ClickedBlockPos, a_ClickedBlockFace);
53  BLOCKTYPE PlaceBlockType = a_World->GetBlock(PlacePos);
54  if (PlaceBlockType != E_BLOCK_AIR)
55  {
56  return false;
57  }
58 
59  // Define all the possible painting titles
60  static const AString gPaintingTitlesList[] =
61  {
62  { "Kebab" },
63  { "Aztec" },
64  { "Alban" },
65  { "Aztec2" },
66  { "Bomb" },
67  { "Plant" },
68  { "Wasteland" },
69  { "Wanderer" },
70  { "Graham" },
71  { "Pool" },
72  { "Courbet" },
73  { "Sunset" },
74  { "Sea" },
75  { "Creebet" },
76  { "Match" },
77  { "Bust" },
78  { "Stage" },
79  { "Void" },
80  { "SkullAndRoses" },
81  { "Wither" },
82  { "Fighters" },
83  { "Skeleton" },
84  { "DonkeyKong" },
85  { "Pointer" },
86  { "Pigscene" },
87  { "BurningSkull" }
88  };
89 
90  auto PaintingTitle = gPaintingTitlesList[a_World->GetTickRandomNumber(ARRAYCOUNT(gPaintingTitlesList) - 1)];
91 
92  // A painting, centred so pickups spawn nicely.
93  auto Painting = std::make_unique<cPainting>(PaintingTitle, a_ClickedBlockFace, Vector3d(0.5, 0.5, 0.5) + PlacePos);
94  auto PaintingPtr = Painting.get();
95  if (!PaintingPtr->Initialize(std::move(Painting), *a_World))
96  {
97  return false;
98  }
99 
100  if (!a_Player->IsGameModeCreative())
101  {
102  a_Player->GetInventory().RemoveOneEquippedItem();
103  }
104  return true;
105  }
106 };
107 
108 
109 
110 
@ 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
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:231
@ Painting
std::string AString
Definition: StringUtils.h:11
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: ItemPainting.h:26
Definition: World.h:53
BLOCKTYPE GetBlock(Vector3i a_BlockPos) const
Returns the block type at the specified position.
Definition: World.h:363
int GetTickRandomNumber(int a_Range)
Returns a random number in range [0 .
Definition: World.cpp:2978