Botcraft 1.21.4
Loading...
Searching...
No Matches
PlayerEntity.cpp
Go to the documentation of this file.
2
3#include <mutex>
4
5namespace Botcraft
6{
7 const std::array<std::string, PlayerEntity::metadata_count> PlayerEntity::metadata_names{ {
8 "data_player_absorption_id",
9 "data_score_id",
10 "data_player_mode_customisation",
11 "data_player_main_hand",
12 "data_shoulder_left",
13 "data_shoulder_right",
14 } };
15
17 {
18 // Initialize all metadata with default values
25
26 // Initialize all attributes with default values
31#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
35#endif
36#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
41#endif
42 }
43
48
49
50 std::string PlayerEntity::GetName() const
51 {
52 return "player";
53 }
54
59
60#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
62 {
63 std::shared_lock<std::shared_mutex> lock(entity_mutex);
64 return GetColliderImpl(pose);
65 }
66
67 double PlayerEntity::GetHeight(const Pose pose) const
68 {
69 switch (pose)
70 {
71 case Pose::Crouching:
72 return 1.5;
73 case Pose::Sleeping:
74 case Pose::Dying:
75 return 0.2;
77 case Pose::Swimming:
79 return 0.6;
80 default:
81 return 1.8;
82 }
83 }
84
85 double PlayerEntity::GetWidth(const Pose pose) const
86 {
87 switch (pose)
88 {
89 case Pose::Sleeping:
90 case Pose::Dying:
91 return 0.2;
92 default:
93 return 0.6;
94 }
95 }
96#endif
97
99 {
100 std::shared_lock<std::shared_mutex> lock(entity_mutex);
101 return GetEyeHeightImpl();
102 }
103
104
106 {
107 return "player";
108 }
109
114
115
117 {
119
120 output["metadata"]["data_player_absorption_id"] = GetDataPlayerAbsorptionId();
121 output["metadata"]["data_score_id"] = GetDataScoreId();
122 output["metadata"]["data_player_mode_customisation"] = GetDataPlayerModeCustomisation();
123 output["metadata"]["data_player_main_hand"] = GetDataPlayerMainHand();
124 output["metadata"]["data_shoulder_left"] = GetDataShoulderLeft().Serialize();
125 output["metadata"]["data_shoulder_right"] = GetDataShoulderRight().Serialize();
126
127 output["attributes"]["attack_damage"] = GetAttributeAttackDamageValue();
128 output["attributes"]["attack_speed"] = GetAttributeAttackSpeedValue();
129 output["attributes"]["luck"] = GetAttributeLuckValue();
130#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
131 output["attributes"]["block_interaction_range"] = GetAttributePlayerBlockInteractionRangeValue();
132 output["attributes"]["entity_interaction_range"] = GetAttributePlayerEntityInteractionRangeValue();
133 output["attributes"]["block_break_speed"] = GetAttributePlayerBlockBreakSpeedValue();
134#endif
135#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
136 output["attributes"]["submerged_mining_speed"] = GetAttributePlayerSubmergedMiningSpeedValue();
137 output["attributes"]["sneaking_speed"] = GetAttributePlayerSneakingSpeedValue();
138 output["attributes"]["mining_efficiency"] = GetAttributePlayerMiningEfficiencyValue();
139 output["attributes"]["sweeping_damage_ratio"] = GetAttributePlayerSweepingDamageRatioValue();
140#endif
141
142 return output;
143 }
144
145
146 void PlayerEntity::SetMetadataValue(const int index, const std::any& value)
147 {
148 if (index < hierarchy_metadata_count)
149 {
150 LivingEntity::SetMetadataValue(index, value);
151 }
152 else if (index - hierarchy_metadata_count < metadata_count)
153 {
154 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
156 }
157 }
158
160 {
161 std::shared_lock<std::shared_mutex> lock(entity_mutex);
162 return std::any_cast<float>(metadata.at("data_player_absorption_id"));
163 }
164
166 {
167 std::shared_lock<std::shared_mutex> lock(entity_mutex);
168 return std::any_cast<int>(metadata.at("data_score_id"));
169 }
170
172 {
173 std::shared_lock<std::shared_mutex> lock(entity_mutex);
174 return std::any_cast<char>(metadata.at("data_player_mode_customisation"));
175 }
176
178 {
179 std::shared_lock<std::shared_mutex> lock(entity_mutex);
180 return std::any_cast<char>(metadata.at("data_player_main_hand"));
181 }
182
184 {
185 std::shared_lock<std::shared_mutex> lock(entity_mutex);
186 return std::any_cast<ProtocolCraft::NBT::Value>(metadata.at("data_shoulder_left"));
187 }
188
190 {
191 std::shared_lock<std::shared_mutex> lock(entity_mutex);
192 return std::any_cast<ProtocolCraft::NBT::Value>(metadata.at("data_shoulder_right"));
193 }
194
195
196 void PlayerEntity::SetDataPlayerAbsorptionId(const float data_player_absorption_id)
197 {
198 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
199 metadata["data_player_absorption_id"] = data_player_absorption_id;
200 }
201
202 void PlayerEntity::SetDataScoreId(const int data_score_id)
203 {
204 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
205 metadata["data_score_id"] = data_score_id;
206 }
207
208 void PlayerEntity::SetDataPlayerModeCustomisation(const char data_player_mode_customisation)
209 {
210 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
211 metadata["data_player_mode_customisation"] = data_player_mode_customisation;
212 }
213
214 void PlayerEntity::SetDataPlayerMainHand(const char data_player_main_hand)
215 {
216 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
217 metadata["data_player_main_hand"] = data_player_main_hand;
218 }
219
221 {
222 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
223 metadata["data_shoulder_left"] = data_shoulder_left;
224 }
225
227 {
228 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
229 metadata["data_shoulder_right"] = data_shoulder_right;
230 }
231
233 {
234 return true;
235 }
236
237
239 {
240 std::shared_lock<std::shared_mutex> lock(entity_mutex);
242 }
243
245 {
246 std::shared_lock<std::shared_mutex> lock(entity_mutex);
248 }
249
251 {
252 std::shared_lock<std::shared_mutex> lock(entity_mutex);
253 return attributes.at(EntityAttribute::Type::Luck).GetValue();
254 }
255
256#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
258 {
259 std::shared_lock<std::shared_mutex> lock(entity_mutex);
261 }
262
264 {
265 std::shared_lock<std::shared_mutex> lock(entity_mutex);
267 }
268
270 {
271 std::shared_lock<std::shared_mutex> lock(entity_mutex);
273 }
274#endif
275
276#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
278 {
279 std::shared_lock<std::shared_mutex> lock(entity_mutex);
281 }
282
284 {
285 std::shared_lock<std::shared_mutex> lock(entity_mutex);
287 }
288
293
295 {
296 std::shared_lock<std::shared_mutex> lock(entity_mutex);
298 }
299
301 {
302 std::shared_lock<std::shared_mutex> lock(entity_mutex);
304 }
305#endif
306
307
309 {
310#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
311 switch (GetDataPoseImpl())
312 {
313 case Pose::Swimming:
314 case Pose::FallFlying:
315 case Pose::SpinAttack:
316 return 0.4;
317 case Pose::Crouching:
318 return 1.27;
319 default:
320 return 1.62;
321 }
322#else
323 return 1.62;
324#endif
325 }
326
328 {
329#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
330 return GetWidth(GetDataPoseImpl());
331#else
332 return 0.6;
333#endif
334 }
335
337 {
338#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
339 return GetHeight(GetDataPoseImpl());
340#else
341 return 1.8;
342#endif
343 }
344
345#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
347 {
348 const double half_width = GetWidth(pose) / 2.0;
349 const double half_height = GetHeight(pose) / 2.0;
350 return AABB(Vector3<double>(position.x, position.y + half_height, position.z), Vector3<double>(half_width, half_height, half_width));
351 }
352#endif
353
354}
std::shared_mutex entity_mutex
Definition Entity.hpp:253
Pose GetDataPoseImpl() const
Definition Entity.cpp:1978
std::map< std::string, std::any > metadata
Definition Entity.hpp:268
Vector3< double > position
Definition Entity.hpp:257
std::map< EntityAttribute::Type, EntityAttribute > attributes
virtual ProtocolCraft::Json::Value Serialize() const override
virtual void SetMetadataValue(const int index, const std::any &value) override
double GetAttributePlayerBlockBreakSpeedValue() const
virtual void SetMetadataValue(const int index, const std::any &value) override
void SetDataPlayerModeCustomisation(const char data_player_mode_customisation)
double GetAttributeAttackSpeedValue() const
void SetDataPlayerAbsorptionId(const float data_player_absorption_id)
double GetHeight() const
Definition Entity.cpp:331
void SetDataShoulderRight(const ProtocolCraft::NBT::Value &data_shoulder_right)
virtual double GetWidthImpl() const override
void SetDataShoulderLeft(const ProtocolCraft::NBT::Value &data_shoulder_left)
double GetAttributePlayerSweepingDamageRatioValue() const
static constexpr int hierarchy_metadata_count
AABB GetCollider() const
Definition Entity.cpp:319
virtual double GetHeightImpl() const override
double GetWidth() const
Definition Entity.cpp:325
float GetDataPlayerAbsorptionId() const
virtual std::string GetName() const override
Get the name of this entity TYPE as a string. For player name see NetworkManager::GetMyName()
virtual EntityType GetType() const override
double GetAttributeLuckValue() const
double GetAttributePlayerSneakingSpeedValueImpl() const
void SetDataPlayerMainHand(const char data_player_main_hand)
char GetDataPlayerModeCustomisation() const
double GetAttributePlayerBlockInteractionRangeValue() const
virtual ProtocolCraft::Json::Value Serialize() const override
virtual bool IsRemotePlayer() const override
double GetAttributePlayerEntityInteractionRangeValue() const
static constexpr int metadata_count
AABB GetColliderImpl() const
Definition Entity.cpp:1992
static EntityType GetClassType()
void SetDataScoreId(const int data_score_id)
double GetAttributePlayerSubmergedMiningSpeedValue() const
static std::string GetClassName()
char GetDataPlayerMainHand() const
ProtocolCraft::NBT::Value GetDataShoulderLeft() const
ProtocolCraft::NBT::Value GetDataShoulderRight() const
double GetEyeHeight() const
virtual double GetEyeHeightImpl() const
double GetAttributePlayerMiningEfficiencyValue() const
static const std::array< std::string, metadata_count > metadata_names
double GetAttributePlayerSneakingSpeedValue() const
double GetAttributeAttackDamageValue() const
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45
virtual Json::Value Serialize() const