Botcraft 26.3
Loading...
Searching...
No Matches
PlayerEntity.cpp
Go to the documentation of this file.
2
3namespace Botcraft
4{
6 {
7 // Initialize all metadata with default values
11#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
14 SetDataShoulderLeft(ProtocolCraft::NBT::Value());
15 SetDataShoulderRight(ProtocolCraft::NBT::Value());
16#else
17 SetDataShoulderParrotLeft(std::nullopt);
18 SetDataShoulderParrotRight(std::nullopt);
19#endif
20
21 // Initialize all attributes with default values
26#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
28#if PROTOCOL_VERSION < 775 /* < 26.1 */
30#endif
32#endif
33#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
38#endif
39#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
42#endif
43 }
44
49
50
51 std::string PlayerEntity::GetName() const
52 {
53 return "player";
54 }
55
60
61#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
63 {
64 std::shared_lock<std::shared_mutex> lock(entity_mutex);
65 return GetColliderImpl(pose);
66 }
67
68 double PlayerEntity::GetHeight(const Pose pose) const
69 {
70 switch (pose)
71 {
72 case Pose::Crouching:
73 return 1.5;
74 case Pose::Sleeping:
75 case Pose::Dying:
76 return 0.2;
78 case Pose::Swimming:
80 return 0.6;
81 default:
82 return 1.8;
83 }
84 }
85
86 double PlayerEntity::GetWidth(const Pose pose) const
87 {
88 switch (pose)
89 {
90 case Pose::Sleeping:
91 case Pose::Dying:
92 return 0.2;
93 default:
94 return 0.6;
95 }
96 }
97#endif
98
100 {
101 std::shared_lock<std::shared_mutex> lock(entity_mutex);
102 return GetEyeHeightImpl();
103 }
104
105
107 {
108 return "player";
109 }
110
115
116
118 {
120
121 output["metadata"]["data_player_absorption_id"] = GetDataPlayerAbsorptionId();
122 output["metadata"]["data_score_id"] = GetDataScoreId();
123#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
124 output["metadata"]["data_player_mode_customisation"] = GetDataPlayerModeCustomisation();
125 output["metadata"]["data_player_main_hand"] = GetDataPlayerMainHand();
126 output["metadata"]["data_shoulder_left"] = GetDataShoulderLeft().Serialize();
127 output["metadata"]["data_shoulder_right"] = GetDataShoulderRight().Serialize();
128#else
129 const std::optional<int> data_shoulder_parrot_left = GetDataShoulderParrotLeft();
130 output["metadata"]["data_shoulder_parrot_left"] = data_shoulder_parrot_left.has_value() ? ProtocolCraft::Json::Value(data_shoulder_parrot_left.value()) : nullptr;
131 const std::optional<int> data_shoulder_parrot_right = GetDataShoulderParrotRight();
132 output["metadata"]["data_shoulder_parrot_right"] = data_shoulder_parrot_right.has_value() ? ProtocolCraft::Json::Value(data_shoulder_parrot_right.value()) : nullptr;
133#endif
134
135 output["attributes"]["attack_damage"] = GetAttributeAttackDamageValue();
136 output["attributes"]["attack_speed"] = GetAttributeAttackSpeedValue();
137 output["attributes"]["luck"] = GetAttributeLuckValue();
138#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
139 output["attributes"]["block_interaction_range"] = GetAttributePlayerBlockInteractionRangeValue();
140#if PROTOCOL_VERSION < 775 /* < 26.1 */
141 output["attributes"]["entity_interaction_range"] = GetAttributeEntityInteractionRangeValue();
142#endif
143 output["attributes"]["block_break_speed"] = GetAttributePlayerBlockBreakSpeedValue();
144#endif
145#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
146 output["attributes"]["submerged_mining_speed"] = GetAttributePlayerSubmergedMiningSpeedValue();
147 output["attributes"]["sneaking_speed"] = GetAttributePlayerSneakingSpeedValue();
148 output["attributes"]["mining_efficiency"] = GetAttributePlayerMiningEfficiencyValue();
149 output["attributes"]["sweeping_damage_ratio"] = GetAttributePlayerSweepingDamageRatioValue();
150#endif
151#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
152 output["attributes"]["waypoint_receive_range"] = GetAttributeWaypointReceiveRangeValue();
153#endif
154
155 return output;
156 }
157
158
160 {
161 return GetMetadata<float>(BOTCRAFT_ENTITY_METADATA_INDEX("data_player_absorption_id"));
162 }
163
165 {
166 return GetMetadata<int>(BOTCRAFT_ENTITY_METADATA_INDEX("data_score_id"));
167 }
168
169#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
171 {
172 return GetMetadata<char>(BOTCRAFT_ENTITY_METADATA_INDEX("data_player_mode_customisation"));
173 }
174
176 {
177 return GetMetadata<char>(BOTCRAFT_ENTITY_METADATA_INDEX("data_player_main_hand"));
178 }
179
180 ProtocolCraft::NBT::Value PlayerEntity::GetDataShoulderLeft() const
181 {
182 return GetMetadata<ProtocolCraft::NBT::Value>(BOTCRAFT_ENTITY_METADATA_INDEX("data_shoulder_left"));
183 }
184
185 ProtocolCraft::NBT::Value PlayerEntity::GetDataShoulderRight() const
186 {
187 return GetMetadata<ProtocolCraft::NBT::Value>(BOTCRAFT_ENTITY_METADATA_INDEX("data_shoulder_right"));
188 }
189#else
191 {
192 return GetMetadata<std::optional<int>>(BOTCRAFT_ENTITY_METADATA_INDEX("data_shoulder_parrot_left"));
193 }
194
196 {
197 return GetMetadata<std::optional<int>>(BOTCRAFT_ENTITY_METADATA_INDEX("data_shoulder_parrot_right"));
198 }
199#endif
200
201
202 void PlayerEntity::SetDataPlayerAbsorptionId(const float data_player_absorption_id)
203 {
204 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_player_absorption_id"), data_player_absorption_id);
205 }
206
207 void PlayerEntity::SetDataScoreId(const int data_score_id)
208 {
209 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_score_id"), data_score_id);
210 }
211
212#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
213 void PlayerEntity::SetDataPlayerModeCustomisation(const char data_player_mode_customisation)
214 {
215 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_player_mode_customisation"), data_player_mode_customisation);
216 }
217
218 void PlayerEntity::SetDataPlayerMainHand(const char data_player_main_hand)
219 {
220 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_player_main_hand"), data_player_main_hand);
221 }
222
223 void PlayerEntity::SetDataShoulderLeft(const ProtocolCraft::NBT::Value& data_shoulder_left)
224 {
225 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_shoulder_left"), data_shoulder_left);
226 }
227
228 void PlayerEntity::SetDataShoulderRight(const ProtocolCraft::NBT::Value& data_shoulder_right)
229 {
230 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_shoulder_right"), data_shoulder_right);
231 }
232#else
233 void PlayerEntity::SetDataShoulderParrotLeft(const std::optional<int>& data_shoulder_parrot_left)
234 {
235 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_shoulder_parrot_left"), data_shoulder_parrot_left);
236 }
237
238 void PlayerEntity::SetDataShoulderParrotRight(const std::optional<int>& data_shoulder_parrot_right)
239 {
240 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_shoulder_parrot_right"), data_shoulder_parrot_right);
241 }
242#endif
243
245 {
246 return true;
247 }
248
249
251 {
252 std::shared_lock<std::shared_mutex> lock(entity_mutex);
254 }
255
257 {
258 std::shared_lock<std::shared_mutex> lock(entity_mutex);
260 }
261
263 {
264 std::shared_lock<std::shared_mutex> lock(entity_mutex);
265 return attributes.at(EntityAttribute::Type::Luck).GetValue();
266 }
267
268#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
270 {
271 std::shared_lock<std::shared_mutex> lock(entity_mutex);
273 }
274
275#if PROTOCOL_VERSION < 775 /* < 26.1 */
277 {
278 std::shared_lock<std::shared_mutex> lock(entity_mutex);
280 }
281#endif
282
284 {
285 std::shared_lock<std::shared_mutex> lock(entity_mutex);
287 }
288#endif
289
290#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
292 {
293 std::shared_lock<std::shared_mutex> lock(entity_mutex);
295 }
296
298 {
299 std::shared_lock<std::shared_mutex> lock(entity_mutex);
301 }
302
307
309 {
310 std::shared_lock<std::shared_mutex> lock(entity_mutex);
312 }
313
315 {
316 std::shared_lock<std::shared_mutex> lock(entity_mutex);
318 }
319#endif
320
321#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
323 {
324 std::shared_lock<std::shared_mutex> lock(entity_mutex);
326 }
327#endif
328
329
331 {
332#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
333 switch (GetDataPoseImpl())
334 {
335 case Pose::Swimming:
336 case Pose::FallFlying:
337 case Pose::SpinAttack:
338 return 0.4;
339 case Pose::Crouching:
340 return 1.27;
341 default:
342 return 1.62;
343 }
344#else
345 return 1.62;
346#endif
347 }
348
350 {
351#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
352 return GetWidth(GetDataPoseImpl());
353#else
354 return 0.6;
355#endif
356 }
357
359 {
360#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
361 return GetHeight(GetDataPoseImpl());
362#else
363 return 1.8;
364#endif
365 }
366
367#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
369 {
370 const float half_width = static_cast<float>(GetWidth(pose)) / 2.0f;
371 const float half_height = static_cast<float>(GetHeight(pose)) / 2.0f;
372 return AABB(
373 Vector3<double>(position.x, position.y + half_height, position.z), Vector3<double>(half_width, half_height, half_width));
374 }
375#endif
376
377}
char GetDataPlayerModeCustomisation() const
void SetDataPlayerModeCustomisation(const char data_player_mode_customisation)
int GetDataPlayerMainHand() const
void SetDataPlayerMainHand(const int data_player_main_hand)
void SetMetadata(const int index, const std::any &value)
Definition Entity.cpp:366
std::shared_mutex entity_mutex
Definition Entity.hpp:320
std::vector< std::any > metadata
Definition Entity.hpp:335
Pose GetDataPoseImpl() const
Definition Entity.cpp:2222
Vector3< double > position
Definition Entity.hpp:324
std::map< EntityAttribute::Type, EntityAttribute > attributes
virtual ProtocolCraft::Json::Value Serialize() const override
double GetAttributeEntityInteractionRangeValue() const
double GetAttributePlayerBlockBreakSpeedValue() const
double GetAttributeAttackSpeedValue() const
std::optional< int > GetDataShoulderParrotRight() const
void SetDataPlayerAbsorptionId(const float data_player_absorption_id)
double GetHeight() const
Definition Entity.cpp:349
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:337
virtual double GetHeightImpl() const override
double GetWidth() const
Definition Entity.cpp:343
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
std::optional< int > GetDataShoulderParrotLeft() 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:2233
static EntityType GetClassType()
void SetDataScoreId(const int data_score_id)
double GetAttributePlayerSubmergedMiningSpeedValue() const
static std::string GetClassName()
double GetEyeHeight() const
virtual double GetEyeHeightImpl() const
double GetAttributePlayerMiningEfficiencyValue() const
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
#define BOTCRAFT_ENTITY_METADATA_INDEX(name)
Definition Entity.hpp:347