Botcraft 26.3
Loading...
Searching...
No Matches
LivingEntity.cpp
Go to the documentation of this file.
3
4namespace Botcraft
5{
7 {
8 // Initialize all metadata with default values
11 SetDataHealthId(1.0f);
12#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
13 SetDataEffectColorId(0);
14#else
16#endif
19#if PROTOCOL_VERSION > 498 /* > 1.14.4 */
21#endif
22#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
23 SetSleepingPosId(std::optional<Position>());
24#endif
25
26 // Initialize all attributes with default values
32#if PROTOCOL_VERSION > 763 /* > 1.20.1 */
34#endif
35#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
42#endif
43#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
50#endif
51#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
54#endif
55#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
57#endif
58#if PROTOCOL_VERSION > 775 /* > 26.1.2 */
64#endif
65 }
66
71
73 {
74 return true;
75 }
76
77
79 {
81
82 output["metadata"]["data_living_entity_flags"] = GetDataLivingEntityFlags();
83 output["metadata"]["data_health_id"] = GetDataHealthId();
84#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
85 output["metadata"]["data_effect_color_id"] = GetDataEffectColorId();
86#else
87 output["metadata"]["data_effect_particles"] = GetDataEffectParticles();
88#endif
89 output["metadata"]["data_effect_ambience_id"] = GetDataEffectAmbienceId();
90 output["metadata"]["data_arrow_count_id"] = GetDataArrowCountId();
91#if PROTOCOL_VERSION > 498 /* > 1.14.4 */
92 output["metadata"]["data_stinger_count_id"] = GetDataStingerCountId();
93#endif
94#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
95 output["metadata"]["sleeping_pos_id"] = GetSleepingPosId() ? GetSleepingPosId().value().Serialize() : ProtocolCraft::Json::Value();
96#endif
97
98 output["attributes"] = ProtocolCraft::Json::Value();
99
100 output["attributes"]["max_health"] = GetAttributeMaxHealthValue();
101 output["attributes"]["knockback_resistance"] = GetAttributeKnockbackResistanceValue();
102 output["attributes"]["movement_speed"] = GetAttributeMovementSpeedValue();
103 output["attributes"]["armor"] = GetAttributeArmorValue();
104 output["attributes"]["armor_toughness"] = GetAttributeArmorToughnessValue();
105#if PROTOCOL_VERSION > 763 /* > 1.20.1 */
106 output["attributes"]["max_absorption"] = GetAttributeMaxAbsorptionValue();
107#endif
108#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
109 output["attributes"]["step_height"] = GetAttributeStepHeightValue();
110 output["attributes"]["scale"] = GetAttributeScaleValue();
111 output["attributes"]["gravity"] = GetAttributeGravityValue();
112 output["attributes"]["safe_fall_distance"] = GetAttributeSafeFallDistanceValue();
113 output["attributes"]["fall_damage_multiplier"] = GetAttributeFallDamageMultiplierValue();
114 output["attributes"]["jump_strength"] = GetAttributeJumpStrengthValue();
115#endif
116#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
117 output["attributes"]["oxygen_bonus"] = GetAttributeOxygenBonusValue();
118 output["attributes"]["burning_time"] = GetAttributeBurningTimeValue();
119 output["attributes"]["explosion_knockback_resistance"] = GetAttributeExplosionKnockbackResistanceValue();
120 output["attributes"]["water_movement_efficiency"] = GetAttributeWaterMovementEfficiencyValue();
121 output["attributes"]["movement_efficiency"] = GetAttributeMovementEfficiencyValue();
122 output["attributes"]["attack_knockback"] = GetAttributeAttackKnockbackValue();
123#endif
124#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
125 output["attributes"]["camera_distance"] = GetAttributeCameraDistanceValue();
126 output["attributes"]["waypoint_transmit_range"] = GetAttributeWaypointTransmitRangeValue();
127#endif
128#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
129 output["attributes"]["entity_interaction_range"] = GetAttributeEntityInteractionRangeValue();
130 output["attributes"]["entity_interaction_range"] = GetAttributeEntityInteractionRangeValue();
131#endif
132#if PROTOCOL_VERSION > 775 /* > 26.1.2 */
133 output["attributes"]["bounciness"] = GetAttributeBouncinessValue();
134 output["attributes"]["air_drag_modifier"] = GetAttributeAirDragModifierValue();
135 output["attributes"]["friction_modifier"] = GetAttributeFrictionModifierValue();
136 output["attributes"]["name_tag_distance"] = GetAttributeNameTagDistanceValue();
137 output["attributes"]["below_name_distance"] = GetAttributeBelowNameDistanceValue();
138
139#endif
140
141 return output;
142 }
143
144
146 {
147 std::shared_lock<std::shared_mutex> lock(entity_mutex);
149 }
150
152 {
153 return GetMetadata<float>(BOTCRAFT_ENTITY_METADATA_INDEX("data_health_id"));
154 }
155
156#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
157 int LivingEntity::GetDataEffectColorId() const
158 {
159 return GetMetadata<int>(BOTCRAFT_ENTITY_METADATA_INDEX("data_effect_color_id"));
160 }
161#else
162 std::vector<ProtocolCraft::Particle> LivingEntity::GetDataEffectParticles() const
163 {
164 return GetMetadata<std::vector<ProtocolCraft::Particle>>(BOTCRAFT_ENTITY_METADATA_INDEX("data_effect_particles"));
165 }
166#endif
167
169 {
170 return GetMetadata<bool>(BOTCRAFT_ENTITY_METADATA_INDEX("data_effect_ambience_id"));
171 }
172
174 {
175 return GetMetadata<int>(BOTCRAFT_ENTITY_METADATA_INDEX("data_arrow_count_id"));
176 }
177
178#if PROTOCOL_VERSION > 498 /* > 1.14.4 */
180 {
181 return GetMetadata<int>(BOTCRAFT_ENTITY_METADATA_INDEX("data_stinger_count_id"));
182 }
183#endif
184
185#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
186 std::optional<Position> LivingEntity::GetSleepingPosId() const
187 {
188 std::shared_lock<std::shared_mutex> lock(entity_mutex);
189 return GetSleepingPosIdImpl();
190 }
191#endif
192
193
194 void LivingEntity::SetDataLivingEntityFlags(const char data_living_entity_flags)
195 {
196 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_living_entity_flags"), data_living_entity_flags);
197 }
198
199 void LivingEntity::SetDataHealthId(const float data_health_id)
200 {
201 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_health_id"), data_health_id);
202 }
203
204#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
205 void LivingEntity::SetDataEffectColorId(const int data_effect_color_id)
206 {
207 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_effect_color_id"), data_effect_color_id);
208 }
209#else
210 void LivingEntity::SetDataEffectParticles(const std::vector<ProtocolCraft::Particle>& data_effect_particles)
211 {
212 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_effect_particles"), data_effect_particles);
213 }
214#endif
215
216 void LivingEntity::SetDataEffectAmbienceId(const bool data_effect_ambience_id)
217 {
218 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_effect_ambience_id"), data_effect_ambience_id);
219 }
220
221 void LivingEntity::SetDataArrowCountId(const int data_arrow_count_id)
222 {
223 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_arrow_count_id"), data_arrow_count_id);
224 }
225
226#if PROTOCOL_VERSION > 498 /* > 1.14.4 */
227 void LivingEntity::SetDataStingerCountId(const int data_stinger_count_id)
228 {
229 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("data_stinger_count_id"), data_stinger_count_id);
230 }
231#endif
232
233#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
234 void LivingEntity::SetSleepingPosId(const std::optional<Position>& sleeping_pos_id)
235 {
236 SetMetadata(BOTCRAFT_ENTITY_METADATA_INDEX("sleeping_pos_id"), sleeping_pos_id);
237 }
238#endif
239
240 std::optional<EntityAttribute> LivingEntity::GetAttribute(const EntityAttribute::Type type) const
241 {
242 std::shared_lock<std::shared_mutex> lock(entity_mutex);
243
244 auto it = attributes.find(type);
245 if (it == attributes.end())
246 {
247 return std::optional<EntityAttribute>();
248 }
249 return it->second;
250 }
251
253 {
254 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
255
256 auto it = attributes.find(type);
257 if (it == attributes.end())
258 {
259 LOG_WARNING("Trying to set attribute base value for " << type << " for a " << GetName() << " but it doesn't have this attribute");
260 return;
261 }
262 it->second.SetBaseValue(value);
263 }
264
266 {
267 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
269 }
270
272 {
273 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
274 SetAttributeModifierImpl(type, key, modifier);
275 }
276
278 {
279 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
280
281 auto it = attributes.find(type);
282 if (it == attributes.end())
283 {
284 return;
285 }
286 it->second.ClearModifiers();
287 }
288
290 {
291 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
292
293 auto it = attributes.find(attribute.GetType());
294 if (it == attributes.end())
295 {
296 LOG_WARNING("Trying to add an attribute " << attribute.GetType() << " to a " << GetName() << " but it was not present");
297 }
298 else
299 {
300 attributes.erase(it);
301 }
302 attributes.insert({ attribute.GetType(), attribute });
303 }
304
306 {
307 std::shared_lock<std::shared_mutex> lock(entity_mutex);
308 return attributes.at(EntityAttribute::Type::MaxHealth).GetValue();
309 }
310
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);
326 return attributes.at(EntityAttribute::Type::Armor).GetValue();
327 }
328
330 {
331 std::shared_lock<std::shared_mutex> lock(entity_mutex);
333 }
334
335#if PROTOCOL_VERSION > 763 /* > 1.20.1 */
337 {
338 std::shared_lock<std::shared_mutex> lock(entity_mutex);
340 }
341#endif
342
347
348#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
350 {
351 std::shared_lock<std::shared_mutex> lock(entity_mutex);
353 }
354
356 {
357 std::shared_lock<std::shared_mutex> lock(entity_mutex);
358 return attributes.at(EntityAttribute::Type::Scale).GetValue();
359 }
360
362 {
363 std::shared_lock<std::shared_mutex> lock(entity_mutex);
365 }
366
368 {
369 std::shared_lock<std::shared_mutex> lock(entity_mutex);
371 }
372
374 {
375 std::shared_lock<std::shared_mutex> lock(entity_mutex);
377 }
378
380 {
381 std::shared_lock<std::shared_mutex> lock(entity_mutex);
383 }
384#endif
385
386#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
388 {
389 std::shared_lock<std::shared_mutex> lock(entity_mutex);
391 }
392
394 {
395 std::shared_lock<std::shared_mutex> lock(entity_mutex);
397 }
398
400 {
401 std::shared_lock<std::shared_mutex> lock(entity_mutex);
403 }
404
406 {
407 std::shared_lock<std::shared_mutex> lock(entity_mutex);
409 }
410
412 {
413 std::shared_lock<std::shared_mutex> lock(entity_mutex);
415 }
416
418 {
419 std::shared_lock<std::shared_mutex> lock(entity_mutex);
421 }
422#endif
423
424#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
426 {
427 std::shared_lock<std::shared_mutex> lock(entity_mutex);
429 }
430
432 {
433 std::shared_lock<std::shared_mutex> lock(entity_mutex);
435 }
436#endif
437
438#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
440 {
441 std::shared_lock<std::shared_mutex> lock(entity_mutex);
443 }
444#endif
445
446#if PROTOCOL_VERSION > 775 /* > 26.1.2 */
448 {
449 std::shared_lock<std::shared_mutex> lock(entity_mutex);
451 }
452
454 {
455 std::shared_lock<std::shared_mutex> lock(entity_mutex);
457 }
458
460 {
461 std::shared_lock<std::shared_mutex> lock(entity_mutex);
463 }
464
466 {
467 std::shared_lock<std::shared_mutex> lock(entity_mutex);
469 }
470
472 {
473 std::shared_lock<std::shared_mutex> lock(entity_mutex);
475 }
476#endif
477
479 {
480 auto it = attributes.find(type);
481 if (it == attributes.end())
482 {
483 return;
484 }
485 it->second.RemoveModifier(key);
486 }
487
489 {
490 auto it = attributes.find(type);
491 if (it == attributes.end())
492 {
493 LOG_WARNING("Trying to set attribute modifier for " << type << " for a " << GetName() << " but it doesn't have this attribute");
494 return;
495 }
496 it->second.SetModifier(key, modifier);
497 }
498
500 {
501 return GetMetadataImpl<char>(BOTCRAFT_ENTITY_METADATA_INDEX("data_living_entity_flags"));
502 }
503
504#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
505 std::optional<Position> LivingEntity::GetSleepingPosIdImpl() const
506 {
507 return GetMetadataImpl<std::optional<Position>>(BOTCRAFT_ENTITY_METADATA_INDEX("sleeping_pos_id"));
508 }
509#endif
510
511#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
516
518 {
519 return attributes.at(EntityAttribute::Type::Gravity).GetValue();
520 }
521
526#endif
527
528#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
533
538#endif
539
540#if PROTOCOL_VERSION > 775 /* > 26.1.2 */
545
550#endif
551
552}
#define LOG_WARNING(osstream)
Definition Logger.hpp:44
void SetMetadata(const int index, const std::any &value)
Definition Entity.cpp:366
virtual ProtocolCraft::Json::Value Serialize() const
Definition Entity.cpp:1246
std::shared_mutex entity_mutex
Definition Entity.hpp:320
std::vector< std::any > metadata
Definition Entity.hpp:335
virtual std::string GetName() const =0
double GetAttributeGravityValueImpl() const
double GetAttributeMovementEfficiencyValueImpl() const
double GetAttributeBouncinessValueImpl() const
double GetAttributeFrictionModifierValue() const
char GetDataLivingEntityFlags() const
double GetAttributeStepHeightValueImpl() const
double GetAttributeExplosionKnockbackResistanceValue() const
double GetAttributeNameTagDistanceValue() const
double GetAttributeScaleValue() const
bool GetDataEffectAmbienceId() const
void RemoveAttributeModifier(const EntityAttribute::Type type, const EntityAttribute::ModifierKey &key)
double GetAttributeJumpStrengthValueImpl() const
void SetDataStingerCountId(const int data_stinger_count_id)
double GetAttributeMaxHealthValue() const
std::optional< Position > GetSleepingPosId() const
void SetAttributeModifierImpl(const EntityAttribute::Type type, const EntityAttribute::ModifierKey &key, const EntityAttribute::Modifier &modifier)
virtual bool IsLivingEntity() const override
double GetAttributeSafeFallDistanceValue() const
double GetAttributeOxygenBonusValue() const
void SetDataArrowCountId(const int data_arrow_count_id)
void ClearModifiers(const EntityAttribute::Type type)
double GetAttributeMaxAbsorptionValue() const
std::optional< Position > GetSleepingPosIdImpl() const
static constexpr int hierarchy_metadata_count
void SetAttributeModifier(const EntityAttribute::Type type, const EntityAttribute::ModifierKey &key, const EntityAttribute::Modifier &modifier)
void SetDataEffectAmbienceId(const bool data_effect_ambience_id)
int GetDataStingerCountId() const
std::map< EntityAttribute::Type, EntityAttribute > attributes
void SetDataLivingEntityFlags(const char data_living_entity_flags)
void RemoveAttributeModifierImpl(const EntityAttribute::Type type, const EntityAttribute::ModifierKey &key)
double GetAttributeJumpStrengthValue() const
void SetDataEffectParticles(const std::vector< ProtocolCraft::Particle > &data_effect_particles)
double GetAttributeMovementSpeedValueImpl() const
double GetAttributeMovementSpeedValue() const
void SetDataHealthId(const float data_health_id)
double GetAttributeWaterMovementEfficiencyValueImpl() const
double GetAttributeArmorToughnessValue() const
virtual ProtocolCraft::Json::Value Serialize() const override
static constexpr int metadata_count
double GetAttributeBurningTimeValue() const
char GetDataLivingEntityFlagsImpl() const
double GetAttributeGravityValue() const
int GetDataArrowCountId() const
double GetAttributeEntityInteractionRangeValue() const
double GetAttributeCameraDistanceValue() const
double GetAttributeArmorValue() const
double GetAttributeAirDragModifierValue() const
double GetAttributeStepHeightValue() const
double GetAttributeWaypointTransmitRangeValue() const
double GetAttributeBouncinessValue() const
void SetSleepingPosId(const std::optional< Position > &sleeping_pos_id)
double GetAttributeAttackKnockbackValue() const
double GetAttributeFallDamageMultiplierValue() const
double GetAttributeKnockbackResistanceValue() const
void AddAttribute(const EntityAttribute &attribute)
double GetAttributeMovementEfficiencyValue() const
float GetDataHealthId() const
double GetAttributeBelowNameDistanceValue() const
void SetAttributeBaseValue(const EntityAttribute::Type type, const double value)
double GetAttributeAirDragModifierValueImpl() const
std::vector< ProtocolCraft::Particle > GetDataEffectParticles() const
double GetAttributeWaterMovementEfficiencyValue() const
std::optional< EntityAttribute > GetAttribute(const EntityAttribute::Type type) 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