Botcraft 1.21.7
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#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
45#endif
46 }
47
52
53
54 std::string PlayerEntity::GetName() const
55 {
56 return "player";
57 }
58
63
64#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
66 {
67 std::shared_lock<std::shared_mutex> lock(entity_mutex);
68 return GetColliderImpl(pose);
69 }
70
71 double PlayerEntity::GetHeight(const Pose pose) const
72 {
73 switch (pose)
74 {
75 case Pose::Crouching:
76 return 1.5;
77 case Pose::Sleeping:
78 case Pose::Dying:
79 return 0.2;
81 case Pose::Swimming:
83 return 0.6;
84 default:
85 return 1.8;
86 }
87 }
88
89 double PlayerEntity::GetWidth(const Pose pose) const
90 {
91 switch (pose)
92 {
93 case Pose::Sleeping:
94 case Pose::Dying:
95 return 0.2;
96 default:
97 return 0.6;
98 }
99 }
100#endif
101
103 {
104 std::shared_lock<std::shared_mutex> lock(entity_mutex);
105 return GetEyeHeightImpl();
106 }
107
108
110 {
111 return "player";
112 }
113
118
119
121 {
123
124 output["metadata"]["data_player_absorption_id"] = GetDataPlayerAbsorptionId();
125 output["metadata"]["data_score_id"] = GetDataScoreId();
126 output["metadata"]["data_player_mode_customisation"] = GetDataPlayerModeCustomisation();
127 output["metadata"]["data_player_main_hand"] = GetDataPlayerMainHand();
128 output["metadata"]["data_shoulder_left"] = GetDataShoulderLeft().Serialize();
129 output["metadata"]["data_shoulder_right"] = GetDataShoulderRight().Serialize();
130
131 output["attributes"]["attack_damage"] = GetAttributeAttackDamageValue();
132 output["attributes"]["attack_speed"] = GetAttributeAttackSpeedValue();
133 output["attributes"]["luck"] = GetAttributeLuckValue();
134#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
135 output["attributes"]["block_interaction_range"] = GetAttributePlayerBlockInteractionRangeValue();
136 output["attributes"]["entity_interaction_range"] = GetAttributePlayerEntityInteractionRangeValue();
137 output["attributes"]["block_break_speed"] = GetAttributePlayerBlockBreakSpeedValue();
138#endif
139#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
140 output["attributes"]["submerged_mining_speed"] = GetAttributePlayerSubmergedMiningSpeedValue();
141 output["attributes"]["sneaking_speed"] = GetAttributePlayerSneakingSpeedValue();
142 output["attributes"]["mining_efficiency"] = GetAttributePlayerMiningEfficiencyValue();
143 output["attributes"]["sweeping_damage_ratio"] = GetAttributePlayerSweepingDamageRatioValue();
144#endif
145#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
146 output["attributes"]["waypoint_receive_range"] = GetAttributeWaypointReceiveRangeValue();
147#endif
148
149 return output;
150 }
151
152
153 void PlayerEntity::SetMetadataValue(const int index, const std::any& value)
154 {
155 if (index < hierarchy_metadata_count)
156 {
157 LivingEntity::SetMetadataValue(index, value);
158 }
159 else if (index - hierarchy_metadata_count < metadata_count)
160 {
161 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
163 }
164 }
165
167 {
168 std::shared_lock<std::shared_mutex> lock(entity_mutex);
169 return std::any_cast<float>(metadata.at("data_player_absorption_id"));
170 }
171
173 {
174 std::shared_lock<std::shared_mutex> lock(entity_mutex);
175 return std::any_cast<int>(metadata.at("data_score_id"));
176 }
177
179 {
180 std::shared_lock<std::shared_mutex> lock(entity_mutex);
181 return std::any_cast<char>(metadata.at("data_player_mode_customisation"));
182 }
183
185 {
186 std::shared_lock<std::shared_mutex> lock(entity_mutex);
187 return std::any_cast<char>(metadata.at("data_player_main_hand"));
188 }
189
191 {
192 std::shared_lock<std::shared_mutex> lock(entity_mutex);
193 return std::any_cast<ProtocolCraft::NBT::Value>(metadata.at("data_shoulder_left"));
194 }
195
197 {
198 std::shared_lock<std::shared_mutex> lock(entity_mutex);
199 return std::any_cast<ProtocolCraft::NBT::Value>(metadata.at("data_shoulder_right"));
200 }
201
202
203 void PlayerEntity::SetDataPlayerAbsorptionId(const float data_player_absorption_id)
204 {
205 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
206 metadata["data_player_absorption_id"] = data_player_absorption_id;
207 }
208
209 void PlayerEntity::SetDataScoreId(const int data_score_id)
210 {
211 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
212 metadata["data_score_id"] = data_score_id;
213 }
214
215 void PlayerEntity::SetDataPlayerModeCustomisation(const char data_player_mode_customisation)
216 {
217 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
218 metadata["data_player_mode_customisation"] = data_player_mode_customisation;
219 }
220
221 void PlayerEntity::SetDataPlayerMainHand(const char data_player_main_hand)
222 {
223 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
224 metadata["data_player_main_hand"] = data_player_main_hand;
225 }
226
228 {
229 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
230 metadata["data_shoulder_left"] = data_shoulder_left;
231 }
232
234 {
235 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
236 metadata["data_shoulder_right"] = data_shoulder_right;
237 }
238
240 {
241 return true;
242 }
243
244
246 {
247 std::shared_lock<std::shared_mutex> lock(entity_mutex);
249 }
250
252 {
253 std::shared_lock<std::shared_mutex> lock(entity_mutex);
255 }
256
258 {
259 std::shared_lock<std::shared_mutex> lock(entity_mutex);
260 return attributes.at(EntityAttribute::Type::Luck).GetValue();
261 }
262
263#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
265 {
266 std::shared_lock<std::shared_mutex> lock(entity_mutex);
268 }
269
271 {
272 std::shared_lock<std::shared_mutex> lock(entity_mutex);
274 }
275
277 {
278 std::shared_lock<std::shared_mutex> lock(entity_mutex);
280 }
281#endif
282
283#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
285 {
286 std::shared_lock<std::shared_mutex> lock(entity_mutex);
288 }
289
291 {
292 std::shared_lock<std::shared_mutex> lock(entity_mutex);
294 }
295
300
302 {
303 std::shared_lock<std::shared_mutex> lock(entity_mutex);
305 }
306
308 {
309 std::shared_lock<std::shared_mutex> lock(entity_mutex);
311 }
312#endif
313
314#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
316 {
317 std::shared_lock<std::shared_mutex> lock(entity_mutex);
319 }
320#endif
321
322
324 {
325#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
326 switch (GetDataPoseImpl())
327 {
328 case Pose::Swimming:
329 case Pose::FallFlying:
330 case Pose::SpinAttack:
331 return 0.4;
332 case Pose::Crouching:
333 return 1.27;
334 default:
335 return 1.62;
336 }
337#else
338 return 1.62;
339#endif
340 }
341
343 {
344#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
345 return GetWidth(GetDataPoseImpl());
346#else
347 return 0.6;
348#endif
349 }
350
352 {
353#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
354 return GetHeight(GetDataPoseImpl());
355#else
356 return 1.8;
357#endif
358 }
359
360#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
362 {
363 const double half_width = GetWidth(pose) / 2.0;
364 const double half_height = GetHeight(pose) / 2.0;
365 return AABB(Vector3<double>(position.x, position.y + half_height, position.z), Vector3<double>(half_width, half_height, half_width));
366 }
367#endif
368
369}
std::shared_mutex entity_mutex
Definition Entity.hpp:261
Pose GetDataPoseImpl() const
Definition Entity.cpp:2064
std::map< std::string, std::any > metadata
Definition Entity.hpp:276
Vector3< double > position
Definition Entity.hpp:265
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:346
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:334
virtual double GetHeightImpl() const override
double GetWidth() const
Definition Entity.cpp:340
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:2078
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
double GetAttributeWaypointReceiveRangeValue() 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