Botcraft 1.21.4
Loading...
Searching...
No Matches
SlimeEntity.cpp
Go to the documentation of this file.
3
4#include <mutex>
5
6namespace Botcraft
7{
8 const std::array<std::string, SlimeEntity::metadata_count> SlimeEntity::metadata_names{ {
9 "id_size",
10 } };
11
13 {
14 // As Slime don't inherit from MonsterEntity, AttackDamage is not initialized in parent constructor
15 // It is overwritten when size is set, but it needs to be initialized before
17
18 // Initialize all metadata with default values
19 SetIdSize(1);
20 }
21
26
27
28 std::string SlimeEntity::GetName() const
29 {
30 return "slime";
31 }
32
37
38
40 {
41 return "slime";
42 }
43
48
49
51 {
53
54 output["metadata"]["id_size"] = GetIdSize();
55
56 return output;
57 }
58
59
60 void SlimeEntity::SetMetadataValue(const int index, const std::any& value)
61 {
62 if (index < hierarchy_metadata_count)
63 {
64 MobEntity::SetMetadataValue(index, value);
65 }
67 {
68 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
69 const std::string& metadata_name = metadata_names[index - hierarchy_metadata_count];
70 metadata[metadata_name] = value;
71 if (metadata_name == "id_size")
72 {
73 SizeChanged(std::any_cast<int>(value));
74#if USE_GUI
76#endif
77 }
78 }
79 }
80
82 {
83 std::shared_lock<std::shared_mutex> lock(entity_mutex);
84 return GetIdSizeImpl();
85 }
86
87
88 void SlimeEntity::SetIdSize(const int id_size)
89 {
90 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
91 metadata["id_size"] = id_size;
92 SizeChanged(id_size);
93#if USE_GUI
95#endif
96 }
97
99 {
100 return std::any_cast<int>(metadata.at("id_size"));
101 }
102
103 void SlimeEntity::SizeChanged(const int new_size)
104 {
106 if (it != attributes.end())
107 {
108 it->second.SetBaseValue(static_cast<double>(new_size) * new_size);
109 }
110 else
111 {
112 LOG_WARNING("Trying to set attribute base value for " << EntityAttribute::Type::MaxHealth << " for a " << GetName() << " but it doesn't have this attribute");
113 }
114
116 if (it != attributes.end())
117 {
118 it->second.SetBaseValue(0.2 + 0.1 * new_size);
119 }
120 else
121 {
122 LOG_WARNING("Trying to set attribute base value for " << EntityAttribute::Type::MovementSpeed << " for a " << GetName() << " but it doesn't have this attribute");
123 }
124
126 if (it != attributes.end())
127 {
128 it->second.SetBaseValue(static_cast<double>(new_size));
129 }
130 else
131 {
132 LOG_WARNING("Trying to set attribute base value for " << EntityAttribute::Type::AttackDamage << " for a slime but it doesn't have this attribute");
133 }
134 }
135
137 {
138#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
139 return 0.255 * 2.04 * GetIdSizeImpl();
140#else
141 return 0.52 * GetIdSizeImpl();
142#endif
143 }
144
146 {
147#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
148 return 0.255 * 2.04 * GetIdSizeImpl();
149#else
150 return 0.52 * GetIdSizeImpl();
151#endif
152 }
153
154}
#define LOG_WARNING(osstream)
Definition Logger.hpp:44
void OnSizeUpdated()
Definition Entity.cpp:1936
std::shared_mutex entity_mutex
Definition Entity.hpp:253
std::map< std::string, std::any > metadata
Definition Entity.hpp:268
std::map< EntityAttribute::Type, EntityAttribute > attributes
virtual ProtocolCraft::Json::Value Serialize() const override
Definition MobEntity.cpp:34
virtual void SetMetadataValue(const int index, const std::any &value) override
Definition MobEntity.cpp:49
virtual double GetHeightImpl() const override
static constexpr int hierarchy_metadata_count
virtual void SetMetadataValue(const int index, const std::any &value) override
static std::string GetClassName()
void SetIdSize(const int id_size)
static const std::array< std::string, metadata_count > metadata_names
static EntityType GetClassType()
virtual ProtocolCraft::Json::Value Serialize() const override
static constexpr int metadata_count
virtual std::string GetName() const override
int GetIdSizeImpl() const
void SizeChanged(const int new_size)
virtual EntityType GetType() const override
virtual double GetWidthImpl() const override
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45