Cuberite
A lightweight, fast and extensible game server for Minecraft
JukeboxEntity.cpp
Go to the documentation of this file.
1 
2 // JukeboxEntity.cpp
3 
4 // Implements the cJukeboxEntity class representing a single jukebox in the world
5 
6 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
7 #include "JukeboxEntity.h"
8 #include "../World.h"
9 #include "../EffectID.h"
10 #include "json/value.h"
11 #include "../Entities/Player.h"
12 
13 
14 
15 
16 cJukeboxEntity::cJukeboxEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World):
17  Super(a_BlockType, a_BlockMeta, a_Pos, a_World),
18  m_Record(0)
19 {
20  ASSERT(a_BlockType == E_BLOCK_JUKEBOX);
21 }
22 
23 
24 
25 
26 
28 {
29  ASSERT(m_World != nullptr);
31 }
32 
33 
34 
35 
36 
38 {
39  return IsPlayingRecord() ? cItem(static_cast<short>(m_Record)) : cItems();
40 }
41 
42 
43 
44 
45 
47 {
48  Super::CopyFrom(a_Src);
49  auto & src = static_cast<const cJukeboxEntity &>(a_Src);
50  m_Record = src.m_Record;
51 }
52 
53 
54 
55 
56 
58 {
59  if (IsPlayingRecord())
60  {
61  EjectRecord();
62  return true;
63  }
64 
65  const cItem & HeldItem = a_Player->GetEquippedItem();
66  if (PlayRecord(HeldItem.m_ItemType))
67  {
69 
70  if (!a_Player->IsGameModeCreative())
71  {
72  a_Player->GetInventory().RemoveOneEquippedItem();
73  }
74 
75  return true;
76  }
77 
78  // No state change, continue with block placement:
79  return false;
80 }
81 
82 
83 
84 
85 
86 bool cJukeboxEntity::PlayRecord(int a_Record)
87 {
88  if (!IsRecordItem(a_Record))
89  {
90  // This isn't a Record Item
91  return false;
92  }
93 
94  if (IsPlayingRecord())
95  {
96  // A Record is already in the Jukebox.
97  EjectRecord();
98  }
99 
100  m_Record = a_Record;
103  return true;
104 }
105 
106 
107 
108 
109 
111 {
112  if (!IsPlayingRecord())
113  {
114  // There's no record here
115  return false;
116  }
117 
118  m_World->SpawnItemPickups(cItem(static_cast<short>(m_Record)), Vector3d(0.5, 0.5, 0.5) + m_Pos, 10);
121 
122  m_Record = 0;
123  return true;
124 }
125 
126 
127 
128 
129 
131 {
132  return m_Record != 0;
133 }
134 
135 
136 
137 
138 
140 {
141  return m_Record;
142 }
143 
144 
145 
146 
147 
148 void cJukeboxEntity::SetRecord(int a_Record)
149 {
150  m_Record = a_Record;
151 }
@ E_META_JUKEBOX_OFF
Definition: BlockType.h:690
@ E_META_JUKEBOX_ON
Definition: BlockType.h:691
@ E_BLOCK_JUKEBOX
Definition: BlockType.h:99
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
@ SFX_RANDOM_PLAY_MUSIC_DISC
#define ASSERT(x)
Definition: Globals.h:276
Vector3< double > Vector3d
Definition: Vector3.h:485
Vector3i GetPos() const
Definition: BlockEntity.h:90
Vector3i m_Pos
Position in absolute block coordinates.
Definition: BlockEntity.h:113
virtual void CopyFrom(const cBlockEntity &a_Src)
Copies all properties of a_Src into this entity, except for its m_World and location.
Definition: BlockEntity.cpp:66
cWorld * m_World
Definition: BlockEntity.h:126
bool PlayRecord(int a_Record)
Plays the specified Record.
virtual void CopyFrom(const cBlockEntity &a_Src) override
Copies all properties of a_Src into this entity, except for its m_World and location.
bool IsPlayingRecord(void) const
Is in the Jukebox a Record?
virtual cItems ConvertToPickups() const override
Returns the contents of this block entity that it would drop if broken.
bool EjectRecord(void)
Ejects the currently held record as a pickup.
cJukeboxEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld *a_World)
int GetRecord(void)
virtual void Destroy(void) override
Called when this block entity's associated block is destroyed.
virtual bool UsedBy(cPlayer *a_Player) override
Called when a player uses this entity; should open the UI window.
static bool IsRecordItem(int a_Item)
Definition: JukeboxEntity.h:39
void SetRecord(int a_Record)
Definition: Player.h:29
StatisticsManager & GetStatistics()
Return the associated statistic and achievement manager.
Definition: Player.h:237
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
Definition: Item.h:37
short m_ItemType
Definition: Item.h:163
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
std::unordered_map< CustomStatistic, StatValue > Custom
Definition: World.h:53
virtual void BroadcastSoundParticleEffect(const EffectID a_EffectID, Vector3i a_SrcPos, int a_Data, const cClientHandle *a_Exclude=nullptr) override
void SpawnItemPickups(const cItems &a_Pickups, Vector3i a_BlockPos, double a_FlyAwaySpeed=1.0, bool a_IsPlayerCreated=false)
Spawns item pickups for each item in the list.
Definition: World.cpp:1806
void SetBlockMeta(Vector3i a_BlockPos, NIBBLETYPE a_MetaData)
Sets the meta for the specified block, while keeping the blocktype.
Definition: World.cpp:1752