mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Allow overriding tool capabilities through itemstack metadata
This makes it possible to modify the tool capabilities of individual itemstacks by calling a method on itemstack metadata references.
This commit is contained in:
parent
610ea6f216
commit
a637107a4e
10 changed files with 206 additions and 14 deletions
85
src/tool.cpp
85
src/tool.cpp
|
@ -25,6 +25,34 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "util/serialize.h"
|
||||
#include "util/numeric.h"
|
||||
|
||||
void ToolGroupCap::toJson(Json::Value &object) const
|
||||
{
|
||||
object["maxlevel"] = maxlevel;
|
||||
object["uses"] = uses;
|
||||
|
||||
Json::Value times_object;
|
||||
for (auto time : times)
|
||||
times_object[time.first] = time.second;
|
||||
object["times"] = times_object;
|
||||
}
|
||||
|
||||
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 ×_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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
|
||||
{
|
||||
writeU8(os, 3); // protocol_version >= 36
|
||||
|
@ -84,6 +112,63 @@ void ToolCapabilities::deSerialize(std::istream &is)
|
|||
}
|
||||
}
|
||||
|
||||
void ToolCapabilities::serializeJson(std::ostream &os) const
|
||||
{
|
||||
Json::Value root;
|
||||
root["full_punch_interval"] = full_punch_interval;
|
||||
root["max_drop_level"] = max_drop_level;
|
||||
|
||||
Json::Value groupcaps_object;
|
||||
for (auto groupcap : groupcaps) {
|
||||
groupcap.second.toJson(groupcaps_object[groupcap.first]);
|
||||
}
|
||||
root["groupcaps"] = groupcaps_object;
|
||||
|
||||
Json::Value damage_groups_object;
|
||||
DamageGroup::const_iterator dgiter;
|
||||
for (dgiter = damageGroups.begin(); dgiter != damageGroups.end(); ++dgiter) {
|
||||
damage_groups_object[dgiter->first] = dgiter->second;
|
||||
}
|
||||
root["damage_groups"] = damage_groups_object;
|
||||
|
||||
os << root;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DigParams getDigParams(const ItemGroupList &groups,
|
||||
const ToolCapabilities *tp, float time_from_last_punch)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue