Refactor tool.cpp (#14873)

Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
wrrrzr 2024-08-21 21:24:59 +03:00 committed by GitHub
parent f2c66b9ceb
commit 1bccb4e48c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 68 additions and 43 deletions

@ -44,18 +44,25 @@ void ToolGroupCap::toJson(Json::Value &object) const
void ToolGroupCap::fromJson(const Json::Value &json)
{
if (json.isObject()) {
if (json["maxlevel"].isInt())
maxlevel = json["maxlevel"].asInt();
if (json["uses"].isInt())
uses = json["uses"].asInt();
const Json::Value &times_object = json["times"];
if (times_object.isArray()) {
Json::ArrayIndex size = times_object.size();
for (Json::ArrayIndex i = 0; i < size; ++i)
if (times_object[i].isDouble())
times[i] = times_object[i].asFloat();
}
if (!json.isObject())
return;
if (json["maxlevel"].isInt())
maxlevel = json["maxlevel"].asInt();
if (json["uses"].isInt())
uses = json["uses"].asInt();
const Json::Value &times_object = json["times"];
if (!times_object.isArray())
return;
Json::ArrayIndex size = times_object.size();
for (Json::ArrayIndex i = 0; i < size; ++i) {
if (times_object[i].isDouble())
times[i] = times_object[i].asFloat();
}
}
@ -65,9 +72,11 @@ void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
writeU8(os, 5);
else
writeU8(os, 4); // proto == 37
writeF32(os, full_punch_interval);
writeS16(os, max_drop_level);
writeU32(os, groupcaps.size());
for (const auto &groupcap : groupcaps) {
const std::string *name = &groupcap.first;
const ToolGroupCap *cap = &groupcap.second;
@ -102,6 +111,7 @@ void ToolCapabilities::deSerialize(std::istream &is)
max_drop_level = readS16(is);
groupcaps.clear();
u32 groupcaps_size = readU32(is);
for (u32 i = 0; i < groupcaps_size; i++) {
std::string name = deSerializeString16(is);
ToolGroupCap cap;
@ -135,15 +145,19 @@ void ToolCapabilities::serializeJson(std::ostream &os) const
root["punch_attack_uses"] = punch_attack_uses;
Json::Value groupcaps_object;
for (const auto &groupcap : groupcaps) {
groupcap.second.toJson(groupcaps_object[groupcap.first]);
}
root["groupcaps"] = std::move(groupcaps_object);
Json::Value damage_groups_object;
for (const auto &damagegroup : damageGroups) {
damage_groups_object[damagegroup.first] = damagegroup.second;
}
root["damage_groups"] = std::move(damage_groups_object);
fastWriteJson(root, os);
@ -153,36 +167,44 @@ void ToolCapabilities::deserializeJson(std::istream &is)
{
Json::Value root;
is >> root;
if (root.isObject()) {
if (root["full_punch_interval"].isDouble())
full_punch_interval = root["full_punch_interval"].asFloat();
if (root["max_drop_level"].isInt())
max_drop_level = root["max_drop_level"].asInt();
if (root["punch_attack_uses"].isInt())
punch_attack_uses = root["punch_attack_uses"].asInt();
Json::Value &groupcaps_object = root["groupcaps"];
if (groupcaps_object.isObject()) {
Json::ValueIterator gciter;
for (gciter = groupcaps_object.begin();
gciter != groupcaps_object.end(); ++gciter) {
ToolGroupCap groupcap;
groupcap.fromJson(*gciter);
groupcaps[gciter.key().asString()] = groupcap;
}
}
if (!root.isObject())
return;
Json::Value &damage_groups_object = root["damage_groups"];
if (damage_groups_object.isObject()) {
Json::ValueIterator dgiter;
for (dgiter = damage_groups_object.begin();
dgiter != damage_groups_object.end(); ++dgiter) {
Json::Value &value = *dgiter;
if (value.isInt())
damageGroups[dgiter.key().asString()] =
value.asInt();
}
}
if (root["full_punch_interval"].isDouble())
full_punch_interval = root["full_punch_interval"].asFloat();
if (root["max_drop_level"].isInt())
max_drop_level = root["max_drop_level"].asInt();
if (root["punch_attack_uses"].isInt())
punch_attack_uses = root["punch_attack_uses"].asInt();
deserializeJsonGroupcaps(root["groupcaps"]);
deserializeJsonDamageGroups(root["damage_groups"]);
}
void ToolCapabilities::deserializeJsonGroupcaps(Json::Value &json)
{
if (!json.isObject())
return;
for (Json::ValueIterator iter = json.begin(); iter != json.end(); ++iter) {
ToolGroupCap value;
value.fromJson(*iter);
groupcaps[iter.key().asString()] = value;
}
}
void ToolCapabilities::deserializeJsonDamageGroups(Json::Value &json)
{
if (!json.isObject())
return;
for (Json::ValueIterator iter = json.begin(); iter != json.end(); ++iter) {
Json::Value &value = *iter;
if (value.isInt())
damageGroups[iter.key().asString()] = value.asInt();
}
}
@ -362,9 +384,8 @@ u32 calculateResultWear(const u32 uses, const u16 initial_wear)
player.
*/
u16 wear_extra_at = blocks_normal * wear_normal;
if (initial_wear >= wear_extra_at) {
if (initial_wear >= wear_extra_at)
wear_extra = 1;
}
}
result_wear = wear_normal + wear_extra;
return result_wear;
@ -410,6 +431,7 @@ DigParams getDigParams(const ItemGroupList &groups,
if (leveldiff > 1)
time /= leveldiff;
if (!result_diggable || time < result_time) {
result_time = time;
result_diggable = true;
@ -509,4 +531,3 @@ f32 getToolRange(const ItemStack &wielded_item, const ItemStack &hand_item,
return max_d;
}

@ -83,6 +83,10 @@ struct ToolCapabilities
void deSerialize(std::istream &is);
void serializeJson(std::ostream &os) const;
void deserializeJson(std::istream &is);
private:
void deserializeJsonGroupcaps(Json::Value &json);
void deserializeJsonDamageGroups(Json::Value &json);
};
struct WearBarParams