#### Methods
* All methods in MetaDataRef
+* `set_tool_capabilities([tool_capabilities])`
+ * overrides the item's tool capabilities
+ * a nil value will clear the override data and restore the original behavior
### `StorageRef`
Mod metadata: per mod metadata, saved automatically.
} else if (pointed.type == POINTEDTHING_NODE) {
ToolCapabilities playeritem_toolcap =
playeritem.getToolCapabilities(itemdef_manager);
- if (playeritem.name.empty() && hand_def.tool_capabilities != NULL) {
- playeritem_toolcap = *hand_def.tool_capabilities;
+ if (playeritem.name.empty()) {
+ const ToolCapabilities *handToolcap = hlist
+ ? &hlist->getItem(0).getToolCapabilities(itemdef_manager)
+ : itemdef_manager->get("").tool_capabilities;
+
+ if (handToolcap != nullptr)
+ playeritem_toolcap = *handToolcap;
}
handlePointingAtNode(pointed, playeritem_def, playeritem,
playeritem_toolcap, dtime);
// If can't dig, try hand
if (!params.diggable) {
InventoryList *hlist = local_inventory->getList("hand");
- const ItemDefinition &hand =
- hlist ? hlist->getItem(0).getDefinition(itemdef_manager) : itemdef_manager->get("");
- const ToolCapabilities *tp = hand.tool_capabilities;
+ const ToolCapabilities *tp = hlist
+ ? &hlist->getItem(0).getToolCapabilities(itemdef_manager)
+ : itemdef_manager->get("").tool_capabilities;
if (tp)
params = getDigParams(nodedef_manager->get(n).groups, tp);
const ToolCapabilities& getToolCapabilities(
IItemDefManager *itemdef) const
{
- ToolCapabilities *cap;
- cap = itemdef->get(name).tool_capabilities;
- if(cap == NULL)
- cap = itemdef->get("").tool_capabilities;
- assert(cap != NULL);
- return *cap;
+ const ToolCapabilities *item_cap =
+ itemdef->get(name).tool_capabilities;
+
+ if (item_cap == NULL)
+ // Fall back to the hand's tool capabilities
+ item_cap = itemdef->get("").tool_capabilities;
+
+ assert(item_cap != NULL);
+ return metadata.getToolCapabilities(*item_cap); // Check for override
}
// Wear out (only tools)
#define DESERIALIZE_KV_DELIM_STR "\x02"
#define DESERIALIZE_PAIR_DELIM_STR "\x03"
+#define TOOLCAP_KEY "tool_capabilities"
+
+void ItemStackMetadata::clear()
+{
+ Metadata::clear();
+ updateToolCapabilities();
+}
+
+bool ItemStackMetadata::setString(const std::string &name, const std::string &var)
+{
+ bool result = Metadata::setString(name, var);
+ if (name == TOOLCAP_KEY)
+ updateToolCapabilities();
+ return result;
+}
+
void ItemStackMetadata::serialize(std::ostream &os) const
{
std::ostringstream os2;
m_stringvars[""] = in;
}
}
+ updateToolCapabilities();
+}
+
+void ItemStackMetadata::updateToolCapabilities()
+{
+ if (contains(TOOLCAP_KEY)) {
+ toolcaps_overridden = true;
+ toolcaps_override = ToolCapabilities();
+ std::istringstream is(getString(TOOLCAP_KEY));
+ toolcaps_override.deserializeJson(is);
+ } else {
+ toolcaps_overridden = false;
+ }
+}
+
+void ItemStackMetadata::setToolCapabilities(const ToolCapabilities &caps)
+{
+ std::ostringstream os;
+ caps.serializeJson(os);
+ setString(TOOLCAP_KEY, os.str());
+}
+
+void ItemStackMetadata::clearToolCapabilities()
+{
+ setString(TOOLCAP_KEY, "");
}
#pragma once
#include "metadata.h"
+#include "tool.h"
class Inventory;
class IItemDefManager;
class ItemStackMetadata : public Metadata
{
public:
+ ItemStackMetadata() : toolcaps_overridden(false) {}
+
+ // Overrides
+ void clear() override;
+ bool setString(const std::string &name, const std::string &var) override;
+
void serialize(std::ostream &os) const;
void deSerialize(std::istream &is);
+
+ const ToolCapabilities &getToolCapabilities(
+ const ToolCapabilities &default_caps) const
+ {
+ return toolcaps_overridden ? toolcaps_override : default_caps;
+ }
+
+ void setToolCapabilities(const ToolCapabilities &caps);
+ void clearToolCapabilities();
+
+private:
+ void updateToolCapabilities();
+
+ bool toolcaps_overridden;
+ ToolCapabilities toolcaps_override;
};
// If can't dig, try hand
if (!params.diggable) {
InventoryList *hlist = playersao->getInventory()->getList("hand");
- const ItemDefinition &hand =
- hlist ? hlist->getItem(0).getDefinition(m_itemdef) : m_itemdef->get("");
- const ToolCapabilities *tp = hand.tool_capabilities;
+ const ToolCapabilities *tp = hlist
+ ? &hlist->getItem(0).getToolCapabilities(m_itemdef)
+ : m_itemdef->get("").tool_capabilities;
+
if (tp)
params = getDigParams(m_nodedef->get(n).groups, tp);
}
}
// Exported functions
+int ItemStackMetaRef::l_set_tool_capabilities(lua_State *L)
+{
+ ItemStackMetaRef *metaref = checkobject(L, 1);
+ if (lua_isnoneornil(L, 2)) {
+ metaref->clearToolCapabilities();
+ } else if (lua_istable(L, 2)) {
+ ToolCapabilities caps = read_tool_capabilities(L, 2);
+ metaref->setToolCapabilities(caps);
+ } else {
+ luaL_typerror(L, 2, "table or nil");
+ }
+
+ return 0;
+}
// garbage collector
int ItemStackMetaRef::gc_object(lua_State *L) {
luamethod(MetaDataRef, to_table),
luamethod(MetaDataRef, from_table),
luamethod(MetaDataRef, equals),
+ luamethod(ItemStackMetaRef, set_tool_capabilities),
{0,0}
};
virtual void reportMetadataChange();
+ void setToolCapabilities(const ToolCapabilities &caps)
+ {
+ istack->metadata.setToolCapabilities(caps);
+ }
+
+ void clearToolCapabilities()
+ {
+ istack->metadata.clearToolCapabilities();
+ }
+
// Exported functions
+ static int l_set_tool_capabilities(lua_State *L);
// garbage collector
static int gc_object(lua_State *L);
#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
}
}
+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)
{
#include <string>
#include <iostream>
#include "itemgroup.h"
+#include <json/json.h>
struct ToolGroupCap
{
*time = i->second;
return true;
}
+
+ void toJson(Json::Value &object) const;
+ void fromJson(const Json::Value &json);
};
void serialize(std::ostream &os, u16 version) const;
void deSerialize(std::istream &is);
+ void serializeJson(std::ostream &os) const;
+ void deserializeJson(std::istream &is);
};
struct DigParams