Cuberite
A lightweight, fast and extensible game server for Minecraft
ItemFrame.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
3 
4 #include "ItemFrame.h"
5 #include "Player.h"
6 #include "../ClientHandle.h"
7 #include "Chunk.h"
8 
9 
10 
11 
12 
14  Super(etItemFrame, a_BlockFace, a_Pos),
15  m_Item(E_BLOCK_AIR),
16  m_ItemRotation(0)
17 {
18 }
19 
20 
21 
22 
23 
25 {
26  // Take environmental or non-player damage normally:
27  if (m_Item.IsEmpty() || (a_TDI.Attacker == nullptr) || !a_TDI.Attacker->IsPlayer())
28  {
29  return Super::DoTakeDamage(a_TDI);
30  }
31 
32  // Only pop out a pickup if attacked by a non-creative player:
33  if (!static_cast<cPlayer *>(a_TDI.Attacker)->IsGameModeCreative())
34  {
35  // Where the pickup spawns, offset by half cPickup height to centre in the block.
36  const auto SpawnPosition = GetPosition().addedY(-0.125);
37 
38  // The direction the pickup travels to simulate a pop-out effect.
39  const auto FlyOutSpeed = AddFaceDirection(Vector3i(), ProtocolFaceToBlockFace(m_Facing)) * 2;
40 
41  // Spawn the frame's held item:
42  GetWorld()->SpawnItemPickup(SpawnPosition, m_Item, FlyOutSpeed);
43  }
44 
45  // In any case we have a held item and were hit by a player, so clear it:
46  m_Item.Empty();
47  m_ItemRotation = 0;
48  a_TDI.FinalDamage = 0;
51  return false;
52 }
53 
54 
55 
56 
57 
58 void cItemFrame::GetDrops(cItems & a_Items, cEntity * a_Killer)
59 {
60  if (!m_Item.IsEmpty())
61  {
62  a_Items.push_back(m_Item);
63  }
64 
65  a_Items.emplace_back(E_ITEM_ITEM_FRAME);
66 }
67 
68 
69 
70 
71 
73 {
74  Super::OnRightClicked(a_Player);
75 
76  if (!m_Item.IsEmpty())
77  {
78  // Item not empty, rotate, clipping values to zero to seven inclusive
80  if (m_ItemRotation >= 8)
81  {
82  m_ItemRotation = 0;
83  }
84  }
85  else if (!a_Player.GetEquippedItem().IsEmpty())
86  {
87  // Item empty, and player held item not empty - add this item to self
88  m_Item = a_Player.GetEquippedItem();
89  m_Item.m_ItemCount = 1;
90 
91  if (!a_Player.IsGameModeCreative())
92  {
94  }
95  }
96 
97  GetWorld()->BroadcastEntityMetadata(*this); // Update clients
98  GetParentChunk()->MarkDirty(); // Mark chunk dirty to save rotation or item
99 }
100 
101 
102 
103 
104 
105 void cItemFrame::SpawnOn(cClientHandle & a_ClientHandle)
106 {
107  Super::SpawnOn(a_ClientHandle);
108  a_ClientHandle.SendSpawnEntity(*this);
109  a_ClientHandle.SendEntityMetadata(*this);
110 
112  {
113  cMap * Map = GetWorld()->GetMapManager().GetMapData(static_cast<unsigned>(m_Item.m_ItemDamage));
114  if (Map != nullptr)
115  {
116  a_ClientHandle.SendMapData(*Map, 0, 0);
117  }
118  }
119 }
@ E_BLOCK_AIR
Definition: BlockType.h:10
@ E_ITEM_MAP
Definition: BlockType.h:403
@ E_ITEM_ITEM_FRAME
Definition: BlockType.h:435
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
Vector3< int > Vector3i
Definition: Vector3.h:487
void MarkDirty(void)
Definition: Chunk.h:265
void SendSpawnEntity(const cEntity &a_Entity)
void SendEntityMetadata(const cEntity &a_Entity)
void SendMapData(const cMap &a_Map, int a_DataStartX, int a_DataStartY)
cEntity * Attacker
Definition: Entity.h:62
float FinalDamage
Definition: Entity.h:64
Definition: Entity.h:76
bool IsPlayer(void) const
Definition: Entity.h:160
cChunk * GetParentChunk()
Returns the chunk responsible for ticking this entity.
Definition: Entity.h:541
void SetInvulnerableTicks(int a_InvulnerableTicks)
Set the invulnerable ticks from the entity.
Definition: Entity.h:516
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI)
Makes this entity take damage specified in the a_TDI.
Definition: Entity.cpp:404
virtual void OnRightClicked(cPlayer &a_Player)
Called when the specified player right-clicks this entity.
Definition: Entity.h:524
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
cWorld * GetWorld(void) const
Definition: Entity.h:190
static eBlockFace ProtocolFaceToBlockFace(Byte a_ProtocolFace)
Converts protocol hanging item facing to eBlockFace values.
Definition: HangingEntity.h:52
virtual void SpawnOn(cClientHandle &a_ClientHandle) override
Descendants override this function to send a command to the specified client to spawn the entity on t...
cItemFrame(eBlockFace a_BlockFace, Vector3d a_Pos)
Definition: ItemFrame.cpp:13
virtual void SpawnOn(cClientHandle &a_ClientHandle) override
Descendants override this function to send a command to the specified client to spawn the entity on t...
Definition: ItemFrame.cpp:105
virtual void GetDrops(cItems &a_Items, cEntity *a_Killer) override
Returns the list of drops for this pawn when it is killed.
Definition: ItemFrame.cpp:58
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
Makes this entity take damage specified in the a_TDI.
Definition: ItemFrame.cpp:24
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: ItemFrame.cpp:72
Byte m_ItemRotation
Definition: ItemFrame.h:48
cItem m_Item
Definition: ItemFrame.h:47
Definition: Player.h:29
const cItem & GetEquippedItem(void) const
Definition: Player.h:162
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
char m_ItemCount
Definition: Item.h:164
bool IsEmpty(void) const
Returns true if the item represents an empty stack - either the type is invalid, or count is zero.
Definition: Item.h:69
short m_ItemType
Definition: Item.h:163
void Empty(void)
Empties the item and frees up any dynamic storage used by the internals.
Definition: Item.cpp:63
short m_ItemDamage
Definition: Item.h:165
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
Encapsulates an in-game world map.
Definition: Map.h:83
cMap * GetMapData(unsigned int a_ID)
Returns the map with the specified ID, nullptr if out of range.
Definition: MapManager.cpp:58
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
UInt32 SpawnItemPickup(Vector3d a_Pos, const cItem &a_Item, Vector3f a_Speed, int a_LifetimeTicks=6000, bool a_CanCombine=true)
Spawns a single pickup containing the specified item.
Definition: World.cpp:1863
virtual void BroadcastEntityMetadata(const cEntity &a_Entity, const cClientHandle *a_Exclude=nullptr) override
cMapManager & GetMapManager(void)
Returns the associated map manager instance.
Definition: World.h:703