Botcraft 1.21.10
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 */
45#endif
46#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
51#endif
52#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
55#endif
56 }
57
62
63
64 std::string PlayerEntity::GetName() const
65 {
66 return "player";
67 }
68
73
74#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
76 {
77 std::shared_lock<std::shared_mutex> lock(entity_mutex);
78 return GetColliderImpl(pose);
79 }
80
81 double PlayerEntity::GetHeight(const Pose pose) const
82 {
83 switch (pose)
84 {
85 case Pose::Crouching:
86 return 1.5;
87 case Pose::Sleeping:
88 case Pose::Dying:
89 return 0.2;
91 case Pose::Swimming:
93 return 0.6;
94 default:
95 return 1.8;
96 }
97 }
98
99 double PlayerEntity::GetWidth(const Pose pose) const
100 {
101 switch (pose)
102 {
103 case Pose::Sleeping:
104 case Pose::Dying:
105 return 0.2;
106 default:
107 return 0.6;
108 }
109 }
110#endif
111
113 {
114 std::shared_lock<std::shared_mutex> lock(entity_mutex);
115 return GetEyeHeightImpl();
116 }
117
118
120 {
121 return "player";
122 }
123
128
129
131 {
133
134 output["metadata"]["data_player_absorption_id"] = GetDataPlayerAbsorptionId();
135 output["metadata"]["data_score_id"] = GetDataScoreId();
136#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
137 output["metadata"]["data_player_mode_customisation"] = GetDataPlayerModeCustomisation();
138 output["metadata"]["data_player_main_hand"] = GetDataPlayerMainHand();
139 output["metadata"]["data_shoulder_left"] = GetDataShoulderLeft().Serialize();
140 output["metadata"]["data_shoulder_right"] = GetDataShoulderRight().Serialize();
141#else
142 output["metadata"]["data_shoulder_parrot_left"] = GetDataShoulderParrotLeft().has_value() ? ProtocolCraft::Json::Value(GetDataShoulderParrotLeft().value()) : nullptr;
143 output["metadata"]["data_shoulder_parrot_right"] = GetDataShoulderParrotRight().has_value() ? ProtocolCraft::Json::Value(GetDataShoulderParrotRight().value()) : nullptr;
144#endif
145
146 output["attributes"]["attack_damage"] = GetAttributeAttackDamageValue();
147 output["attributes"]["attack_speed"] = GetAttributeAttackSpeedValue();
148 output["attributes"]["luck"] = GetAttributeLuckValue();
149#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
150 output["attributes"]["block_interaction_range"] = GetAttributePlayerBlockInteractionRangeValue();
151 output["attributes"]["entity_interaction_range"] = GetAttributePlayerEntityInteractionRangeValue();
152 output["attributes"]["block_break_speed"] = GetAttributePlayerBlockBreakSpeedValue();
153#endif
154#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
155 output["attributes"]["submerged_mining_speed"] = GetAttributePlayerSubmergedMiningSpeedValue();
156 output["attributes"]["sneaking_speed"] = GetAttributePlayerSneakingSpeedValue();
157 output["attributes"]["mining_efficiency"] = GetAttributePlayerMiningEfficiencyValue();
158 output["attributes"]["sweeping_damage_ratio"] = GetAttributePlayerSweepingDamageRatioValue();
159#endif
160#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
161 output["attributes"]["waypoint_receive_range"] = GetAttributeWaypointReceiveRangeValue();
162#endif
163
164 return output;
165 }
166
167
168 void PlayerEntity::SetMetadataValue(const int index, const std::any& value)
169 {
170 if (index < hierarchy_metadata_count)
171 {
172#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
173 LivingEntity::SetMetadataValue(index, value);
174#else
175 AvatarEntity::SetMetadataValue(index, value);
176#endif
177 }
178 else if (index - hierarchy_metadata_count < metadata_count)
179 {
180 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
182 }
183 }
184
186 {
187 std::shared_lock<std::shared_mutex> lock(entity_mutex);
188 return std::any_cast<float>(metadata.at("data_player_absorption_id"));
189 }
190
192 {
193 std::shared_lock<std::shared_mutex> lock(entity_mutex);
194 return std::any_cast<int>(metadata.at("data_score_id"));
195 }
196
197#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
199 {
200 std::shared_lock<std::shared_mutex> lock(entity_mutex);
201 return std::any_cast<char>(metadata.at("data_player_mode_customisation"));
202 }
203
205 {
206 std::shared_lock<std::shared_mutex> lock(entity_mutex);
207 return std::any_cast<char>(metadata.at("data_player_main_hand"));
208 }
209
210 ProtocolCraft::NBT::Value PlayerEntity::GetDataShoulderLeft() const
211 {
212 std::shared_lock<std::shared_mutex> lock(entity_mutex);
213 return std::any_cast<ProtocolCraft::NBT::Value>(metadata.at("data_shoulder_left"));
214 }
215
216 ProtocolCraft::NBT::Value PlayerEntity::GetDataShoulderRight() const
217 {
218 std::shared_lock<std::shared_mutex> lock(entity_mutex);
219 return std::any_cast<ProtocolCraft::NBT::Value>(metadata.at("data_shoulder_right"));
220 }
221#else
222 const std::optional<int>& PlayerEntity::GetDataShoulderParrotLeft() const
223 {
224 std::shared_lock<std::shared_mutex> lock(entity_mutex);
225 return std::any_cast<const std::optional<int>&>(metadata.at("data_shoulder_parrot_left"));
226 }
227
228 const std::optional<int>& PlayerEntity::GetDataShoulderParrotRight() const
229 {
230 std::shared_lock<std::shared_mutex> lock(entity_mutex);
231 return std::any_cast<const std::optional<int>&>(metadata.at("data_shoulder_parrot_right"));
232 }
233#endif
234
235
236 void PlayerEntity::SetDataPlayerAbsorptionId(const float data_player_absorption_id)
237 {
238 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
239 metadata["data_player_absorption_id"] = data_player_absorption_id;
240 }
241
242 void PlayerEntity::SetDataScoreId(const int data_score_id)
243 {
244 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
245 metadata["data_score_id"] = data_score_id;
246 }
247
248#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
249 void PlayerEntity::SetDataPlayerModeCustomisation(const char data_player_mode_customisation)
250 {
251 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
252 metadata["data_player_mode_customisation"] = data_player_mode_customisation;
253 }
254
255 void PlayerEntity::SetDataPlayerMainHand(const char data_player_main_hand)
256 {
257 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
258 metadata["data_player_main_hand"] = data_player_main_hand;
259 }
260
261 void PlayerEntity::SetDataShoulderLeft(const ProtocolCraft::NBT::Value& data_shoulder_left)
262 {
263 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
264 metadata["data_shoulder_left"] = data_shoulder_left;
265 }
266
267 void PlayerEntity::SetDataShoulderRight(const ProtocolCraft::NBT::Value& data_shoulder_right)
268 {
269 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
270 metadata["data_shoulder_right"] = data_shoulder_right;
271 }
272#else
273 void PlayerEntity::SetDataShoulderParrotLeft(const std::optional<int>& data_shoulder_parrot_left)
274 {
275 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
276 metadata["data_shoulder_parrot_left"] = data_shoulder_parrot_left;
277 }
278
279 void PlayerEntity::SetDataShoulderParrotRight(const std::optional<int>& data_shoulder_parrot_right)
280 {
281 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
282 metadata["data_shoulder_parrot_right"] = data_shoulder_parrot_right;
283 }
284#endif
285
287 {
288 return true;
289 }
290
291
293 {
294 std::shared_lock<std::shared_mutex> lock(entity_mutex);
296 }
297
299 {
300 std::shared_lock<std::shared_mutex> lock(entity_mutex);
302 }
303
305 {
306 std::shared_lock<std::shared_mutex> lock(entity_mutex);
307 return attributes.at(EntityAttribute::Type::Luck).GetValue();
308 }
309
310#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
312 {
313 std::shared_lock<std::shared_mutex> lock(entity_mutex);
315 }
316
318 {
319 std::shared_lock<std::shared_mutex> lock(entity_mutex);
321 }
322
324 {
325 std::shared_lock<std::shared_mutex> lock(entity_mutex);
327 }
328#endif
329
330#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
332 {
333 std::shared_lock<std::shared_mutex> lock(entity_mutex);
335 }
336
338 {
339 std::shared_lock<std::shared_mutex> lock(entity_mutex);
341 }
342
347
349 {
350 std::shared_lock<std::shared_mutex> lock(entity_mutex);
352 }
353
355 {
356 std::shared_lock<std::shared_mutex> lock(entity_mutex);
358 }
359#endif
360
361#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
363 {
364 std::shared_lock<std::shared_mutex> lock(entity_mutex);
366 }
367#endif
368
369
371 {
372#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
373 switch (GetDataPoseImpl())
374 {
375 case Pose::Swimming:
376 case Pose::FallFlying:
377 case Pose::SpinAttack:
378 return 0.4;
379 case Pose::Crouching:
380 return 1.27;
381 default:
382 return 1.62;
383 }
384#else
385 return 1.62;
386#endif
387 }
388
390 {
391#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
392 return GetWidth(GetDataPoseImpl());
393#else
394 return 0.6;
395#endif
396 }
397
399 {
400#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
401 return GetHeight(GetDataPoseImpl());
402#else
403 return 1.8;
404#endif
405 }
406
407#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
409 {
410 const double half_width = GetWidth(pose) / 2.0;
411 const double half_height = GetHeight(pose) / 2.0;
412 return AABB(Vector3<double>(position.x, position.y + half_height, position.z), Vector3<double>(half_width, half_height, half_width));
413 }
414#endif
415
416}
char GetDataPlayerModeCustomisation() const
void SetDataPlayerMainHand(const char data_player_main_hand)
void SetDataPlayerModeCustomisation(const char data_player_mode_customisation)
char GetDataPlayerMainHand() const
virtual void SetMetadataValue(const int index, const std::any &value) override
std::shared_mutex entity_mutex
Definition Entity.hpp:264
Pose GetDataPoseImpl() const
Definition Entity.cpp:2113
std::map< std::string, std::any > metadata
Definition Entity.hpp:279
Vector3< double > position
Definition Entity.hpp:268
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
double GetAttributeAttackSpeedValue() const
void SetDataPlayerAbsorptionId(const float data_player_absorption_id)
double GetHeight() const
Definition Entity.cpp:357
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:345
virtual double GetHeightImpl() const override
double GetWidth() const
Definition Entity.cpp:351
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
double GetAttributePlayerEntityInteractionRangeValue() const
static constexpr int metadata_count
AABB GetColliderImpl() const
Definition Entity.cpp:2127
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