Botcraft 26.1.2
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#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
11 "data_player_mode_customisation",
12 "data_player_main_hand",
13 "data_shoulder_left",
14 "data_shoulder_right",
15#else
16 "data_shoulder_parrot_left",
17 "data_shoulder_parrot_right",
18#endif
19 } };
20
22 {
23 // Initialize all metadata with default values
26#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
29 SetDataShoulderLeft(ProtocolCraft::NBT::Value());
30 SetDataShoulderRight(ProtocolCraft::NBT::Value());
31#else
32 SetDataShoulderParrotLeft(std::nullopt);
33 SetDataShoulderParrotRight(std::nullopt);
34#endif
35
36 // Initialize all attributes with default values
41#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
43#if PROTOCOL_VERSION < 775 /* < 26.1 */
45#endif
47#endif
48#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
53#endif
54#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
57#endif
58 }
59
64
65
66 std::string PlayerEntity::GetName() const
67 {
68 return "player";
69 }
70
75
76#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
78 {
79 std::shared_lock<std::shared_mutex> lock(entity_mutex);
80 return GetColliderImpl(pose);
81 }
82
83 double PlayerEntity::GetHeight(const Pose pose) const
84 {
85 switch (pose)
86 {
87 case Pose::Crouching:
88 return 1.5;
89 case Pose::Sleeping:
90 case Pose::Dying:
91 return 0.2;
93 case Pose::Swimming:
95 return 0.6;
96 default:
97 return 1.8;
98 }
99 }
100
101 double PlayerEntity::GetWidth(const Pose pose) const
102 {
103 switch (pose)
104 {
105 case Pose::Sleeping:
106 case Pose::Dying:
107 return 0.2;
108 default:
109 return 0.6;
110 }
111 }
112#endif
113
115 {
116 std::shared_lock<std::shared_mutex> lock(entity_mutex);
117 return GetEyeHeightImpl();
118 }
119
120
122 {
123 return "player";
124 }
125
130
131
133 {
135
136 output["metadata"]["data_player_absorption_id"] = GetDataPlayerAbsorptionId();
137 output["metadata"]["data_score_id"] = GetDataScoreId();
138#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
139 output["metadata"]["data_player_mode_customisation"] = GetDataPlayerModeCustomisation();
140 output["metadata"]["data_player_main_hand"] = GetDataPlayerMainHand();
141 output["metadata"]["data_shoulder_left"] = GetDataShoulderLeft().Serialize();
142 output["metadata"]["data_shoulder_right"] = GetDataShoulderRight().Serialize();
143#else
144 output["metadata"]["data_shoulder_parrot_left"] = GetDataShoulderParrotLeft().has_value() ? ProtocolCraft::Json::Value(GetDataShoulderParrotLeft().value()) : nullptr;
145 output["metadata"]["data_shoulder_parrot_right"] = GetDataShoulderParrotRight().has_value() ? ProtocolCraft::Json::Value(GetDataShoulderParrotRight().value()) : nullptr;
146#endif
147
148 output["attributes"]["attack_damage"] = GetAttributeAttackDamageValue();
149 output["attributes"]["attack_speed"] = GetAttributeAttackSpeedValue();
150 output["attributes"]["luck"] = GetAttributeLuckValue();
151#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
152 output["attributes"]["block_interaction_range"] = GetAttributePlayerBlockInteractionRangeValue();
153#if PROTOCOL_VERSION < 775 /* < 26.1 */
154 output["attributes"]["entity_interaction_range"] = GetAttributeEntityInteractionRangeValue();
155#endif
156 output["attributes"]["block_break_speed"] = GetAttributePlayerBlockBreakSpeedValue();
157#endif
158#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
159 output["attributes"]["submerged_mining_speed"] = GetAttributePlayerSubmergedMiningSpeedValue();
160 output["attributes"]["sneaking_speed"] = GetAttributePlayerSneakingSpeedValue();
161 output["attributes"]["mining_efficiency"] = GetAttributePlayerMiningEfficiencyValue();
162 output["attributes"]["sweeping_damage_ratio"] = GetAttributePlayerSweepingDamageRatioValue();
163#endif
164#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
165 output["attributes"]["waypoint_receive_range"] = GetAttributeWaypointReceiveRangeValue();
166#endif
167
168 return output;
169 }
170
171
172 void PlayerEntity::SetMetadataValue(const int index, const std::any& value)
173 {
174 if (index < hierarchy_metadata_count)
175 {
176#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
177 LivingEntity::SetMetadataValue(index, value);
178#else
179 AvatarEntity::SetMetadataValue(index, value);
180#endif
181 }
182 else if (index - hierarchy_metadata_count < metadata_count)
183 {
184 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
186 }
187 }
188
190 {
191 std::shared_lock<std::shared_mutex> lock(entity_mutex);
192 return std::any_cast<float>(metadata.at("data_player_absorption_id"));
193 }
194
196 {
197 std::shared_lock<std::shared_mutex> lock(entity_mutex);
198 return std::any_cast<int>(metadata.at("data_score_id"));
199 }
200
201#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
203 {
204 std::shared_lock<std::shared_mutex> lock(entity_mutex);
205 return std::any_cast<char>(metadata.at("data_player_mode_customisation"));
206 }
207
209 {
210 std::shared_lock<std::shared_mutex> lock(entity_mutex);
211 return std::any_cast<char>(metadata.at("data_player_main_hand"));
212 }
213
214 ProtocolCraft::NBT::Value PlayerEntity::GetDataShoulderLeft() const
215 {
216 std::shared_lock<std::shared_mutex> lock(entity_mutex);
217 return std::any_cast<ProtocolCraft::NBT::Value>(metadata.at("data_shoulder_left"));
218 }
219
220 ProtocolCraft::NBT::Value PlayerEntity::GetDataShoulderRight() const
221 {
222 std::shared_lock<std::shared_mutex> lock(entity_mutex);
223 return std::any_cast<ProtocolCraft::NBT::Value>(metadata.at("data_shoulder_right"));
224 }
225#else
226 const std::optional<int>& PlayerEntity::GetDataShoulderParrotLeft() const
227 {
228 std::shared_lock<std::shared_mutex> lock(entity_mutex);
229 return std::any_cast<const std::optional<int>&>(metadata.at("data_shoulder_parrot_left"));
230 }
231
232 const std::optional<int>& PlayerEntity::GetDataShoulderParrotRight() const
233 {
234 std::shared_lock<std::shared_mutex> lock(entity_mutex);
235 return std::any_cast<const std::optional<int>&>(metadata.at("data_shoulder_parrot_right"));
236 }
237#endif
238
239
240 void PlayerEntity::SetDataPlayerAbsorptionId(const float data_player_absorption_id)
241 {
242 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
243 metadata["data_player_absorption_id"] = data_player_absorption_id;
244 }
245
246 void PlayerEntity::SetDataScoreId(const int data_score_id)
247 {
248 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
249 metadata["data_score_id"] = data_score_id;
250 }
251
252#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
253 void PlayerEntity::SetDataPlayerModeCustomisation(const char data_player_mode_customisation)
254 {
255 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
256 metadata["data_player_mode_customisation"] = data_player_mode_customisation;
257 }
258
259 void PlayerEntity::SetDataPlayerMainHand(const char data_player_main_hand)
260 {
261 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
262 metadata["data_player_main_hand"] = data_player_main_hand;
263 }
264
265 void PlayerEntity::SetDataShoulderLeft(const ProtocolCraft::NBT::Value& data_shoulder_left)
266 {
267 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
268 metadata["data_shoulder_left"] = data_shoulder_left;
269 }
270
271 void PlayerEntity::SetDataShoulderRight(const ProtocolCraft::NBT::Value& data_shoulder_right)
272 {
273 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
274 metadata["data_shoulder_right"] = data_shoulder_right;
275 }
276#else
277 void PlayerEntity::SetDataShoulderParrotLeft(const std::optional<int>& data_shoulder_parrot_left)
278 {
279 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
280 metadata["data_shoulder_parrot_left"] = data_shoulder_parrot_left;
281 }
282
283 void PlayerEntity::SetDataShoulderParrotRight(const std::optional<int>& data_shoulder_parrot_right)
284 {
285 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
286 metadata["data_shoulder_parrot_right"] = data_shoulder_parrot_right;
287 }
288#endif
289
291 {
292 return true;
293 }
294
295
297 {
298 std::shared_lock<std::shared_mutex> lock(entity_mutex);
300 }
301
303 {
304 std::shared_lock<std::shared_mutex> lock(entity_mutex);
306 }
307
309 {
310 std::shared_lock<std::shared_mutex> lock(entity_mutex);
311 return attributes.at(EntityAttribute::Type::Luck).GetValue();
312 }
313
314#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
316 {
317 std::shared_lock<std::shared_mutex> lock(entity_mutex);
319 }
320
321#if PROTOCOL_VERSION < 775 /* < 26.1 */
323 {
324 std::shared_lock<std::shared_mutex> lock(entity_mutex);
326 }
327#endif
328
330 {
331 std::shared_lock<std::shared_mutex> lock(entity_mutex);
333 }
334#endif
335
336#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
338 {
339 std::shared_lock<std::shared_mutex> lock(entity_mutex);
341 }
342
344 {
345 std::shared_lock<std::shared_mutex> lock(entity_mutex);
347 }
348
353
355 {
356 std::shared_lock<std::shared_mutex> lock(entity_mutex);
358 }
359
361 {
362 std::shared_lock<std::shared_mutex> lock(entity_mutex);
364 }
365#endif
366
367#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
369 {
370 std::shared_lock<std::shared_mutex> lock(entity_mutex);
372 }
373#endif
374
375
377 {
378#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
379 switch (GetDataPoseImpl())
380 {
381 case Pose::Swimming:
382 case Pose::FallFlying:
383 case Pose::SpinAttack:
384 return 0.4;
385 case Pose::Crouching:
386 return 1.27;
387 default:
388 return 1.62;
389 }
390#else
391 return 1.62;
392#endif
393 }
394
396 {
397#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
398 return GetWidth(GetDataPoseImpl());
399#else
400 return 0.6;
401#endif
402 }
403
405 {
406#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
407 return GetHeight(GetDataPoseImpl());
408#else
409 return 1.8;
410#endif
411 }
412
413#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
415 {
416 const double half_width = GetWidth(pose) / 2.0;
417 const double half_height = GetHeight(pose) / 2.0;
418 return AABB(Vector3<double>(position.x, position.y + half_height, position.z), Vector3<double>(half_width, half_height, half_width));
419 }
420#endif
421
422}
char GetDataPlayerModeCustomisation() const
void SetDataPlayerModeCustomisation(const char data_player_mode_customisation)
virtual void SetMetadataValue(const int index, const std::any &value) override
int GetDataPlayerMainHand() const
void SetDataPlayerMainHand(const int data_player_main_hand)
std::shared_mutex entity_mutex
Definition Entity.hpp:267
Pose GetDataPoseImpl() const
Definition Entity.cpp:2190
std::map< std::string, std::any > metadata
Definition Entity.hpp:282
Vector3< double > position
Definition Entity.hpp:271
std::map< EntityAttribute::Type, EntityAttribute > attributes
virtual ProtocolCraft::Json::Value Serialize() const override
double GetAttributeEntityInteractionRangeValue() const
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
double GetAttributeAttackSpeedValue() const
void SetDataPlayerAbsorptionId(const float data_player_absorption_id)
double GetHeight() const
Definition Entity.cpp:358
virtual double GetWidthImpl() const override
double GetAttributePlayerSweepingDamageRatioValue() const
static constexpr int hierarchy_metadata_count
void SetDataShoulderParrotLeft(const std::optional< int > &data_shoulder_parrot_left)
AABB GetCollider() const
Definition Entity.cpp:346
virtual double GetHeightImpl() const override
double GetWidth() const
Definition Entity.cpp:352
float GetDataPlayerAbsorptionId() const
void SetDataShoulderParrotRight(const std::optional< int > &data_shoulder_parrot_right)
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
const std::optional< int > & GetDataShoulderParrotRight() const
double GetAttributePlayerBlockInteractionRangeValue() const
virtual ProtocolCraft::Json::Value Serialize() const override
virtual bool IsRemotePlayer() const override
static constexpr int metadata_count
AABB GetColliderImpl() const
Definition Entity.cpp:2204
static EntityType GetClassType()
void SetDataScoreId(const int data_score_id)
double GetAttributePlayerSubmergedMiningSpeedValue() const
static std::string GetClassName()
const std::optional< int > & GetDataShoulderParrotLeft() 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
double GetAttributeWaypointReceiveRangeValue() const
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45