Botcraft 26.2
Loading...
Searching...
No Matches
SlimeEntity.cpp
Go to the documentation of this file.
3
4#include <mutex>
5
6namespace Botcraft
7{
8#if PROTOCOL_VERSION < 776 /* < 26.2 */
9 const std::array<std::string, SlimeEntity::metadata_count> SlimeEntity::metadata_names{ {
10 "id_size",
11 } };
12#endif
13
15 {
16 // As Slime don't inherit from MonsterEntity, AttackDamage is not initialized in parent constructor
17 // It is overwritten when size is set, but it needs to be initialized before
19
20#if PROTOCOL_VERSION < 776 /* < 26.2 */
21
22 // Initialize all metadata with default values
23 SetIdSize(1);
24#endif
25 }
26
31
32
33 std::string SlimeEntity::GetName() const
34 {
35 return "slime";
36 }
37
42
43
45 {
46 return "slime";
47 }
48
53
54#if PROTOCOL_VERSION < 776 /* < 26.2 */
56 {
58
59 output["metadata"]["id_size"] = GetIdSize();
60
61 return output;
62 }
63
64
65 void SlimeEntity::SetMetadataValue(const int index, const std::any& value)
66 {
67 if (index < hierarchy_metadata_count)
68 {
69 MobEntity::SetMetadataValue(index, value);
70 }
72 {
73 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
74 const std::string& metadata_name = metadata_names[index - hierarchy_metadata_count];
75 metadata[metadata_name] = value;
76 if (metadata_name == "id_size")
77 {
78 SizeChanged(std::any_cast<int>(value));
79#if USE_GUI
81#endif
82 }
83 }
84 }
85
86 int SlimeEntity::GetIdSize() const
87 {
88 std::shared_lock<std::shared_mutex> lock(entity_mutex);
89 return GetIdSizeImpl();
90 }
91
92
93 void SlimeEntity::SetIdSize(const int id_size)
94 {
95 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
96 metadata["id_size"] = id_size;
97 SizeChanged(id_size);
98#if USE_GUI
100#endif
101 }
102
104 {
105 return std::any_cast<int>(metadata.at("id_size"));
106 }
107
108 void SlimeEntity::SizeChanged(const int new_size)
109 {
111 if (it != attributes.end())
112 {
113 it->second.SetBaseValue(static_cast<double>(new_size) * new_size);
114 }
115 else
116 {
117 LOG_WARNING("Trying to set attribute base value for " << EntityAttribute::Type::MaxHealth << " for a " << GetName() << " but it doesn't have this attribute");
118 }
119
121 if (it != attributes.end())
122 {
123 it->second.SetBaseValue(0.2 + 0.1 * new_size);
124 }
125 else
126 {
127 LOG_WARNING("Trying to set attribute base value for " << EntityAttribute::Type::MovementSpeed << " for a " << GetName() << " but it doesn't have this attribute");
128 }
129
131 if (it != attributes.end())
132 {
133 it->second.SetBaseValue(static_cast<double>(new_size));
134 }
135 else
136 {
137 LOG_WARNING("Trying to set attribute base value for " << EntityAttribute::Type::AttackDamage << " for a slime but it doesn't have this attribute");
138 }
139 }
140#else
141 void SlimeEntity::SizeChanged(const int new_size)
142 {
145 if (it != attributes.end())
146 {
147 it->second.SetBaseValue(static_cast<double>(new_size));
148 }
149 else
150 {
151 LOG_WARNING("Trying to set attribute base value for " << EntityAttribute::Type::AttackDamage << " for a slime but it doesn't have this attribute");
152 }
153 }
154#endif
155
157 {
158#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
159 return 0.255 * 2.04 * GetIdSizeImpl();
160#else
161 return 0.52 * GetIdSizeImpl();
162#endif
163 }
164
166 {
167#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
168 return 0.255 * 2.04 * GetIdSizeImpl();
169#else
170 return 0.52 * GetIdSizeImpl();
171#endif
172 }
173
174}
#define LOG_WARNING(osstream)
Definition Logger.hpp:44
static const std::array< std::string, metadata_count > metadata_names
virtual void SizeChanged(const int new_size)
virtual ProtocolCraft::Json::Value Serialize() const override
static const std::array< std::string, metadata_count > metadata_names
virtual void SetMetadataValue(const int index, const std::any &value) override
void OnSizeUpdated()
Definition Entity.cpp:2162
std::shared_mutex entity_mutex
Definition Entity.hpp:270
std::map< std::string, std::any > metadata
Definition Entity.hpp:285
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
static std::string GetClassName()
virtual void SizeChanged(const int new_size) override
static EntityType GetClassType()
static constexpr int metadata_count
virtual std::string GetName() const override
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