From 6145a135bd71425c69d31e6c65112238e08349a5 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sun, 4 Dec 2011 02:12:45 +0200 Subject: [PATCH] Make ToolItem and MaterialItem to convert names by aliases at creation time too (necessary for eg. crafting) --- src/inventory.cpp | 17 +++++++++++++---- src/inventory.h | 7 +------ src/nodedef.cpp | 17 +++++++++-------- src/nodedef.h | 2 ++ src/tooldef.cpp | 16 ++++++++++------ src/tooldef.h | 2 ++ 6 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/inventory.cpp b/src/inventory.cpp index e9600ece..dd2713ca 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -246,7 +246,9 @@ MaterialItem::MaterialItem(IGameDef *gamedef, std::string nodename, u16 count): { if(nodename == "") nodename = "unknown_block"; - m_nodename = nodename; + + // Convert directly to the correct name through aliases + m_nodename = gamedef->ndef()->getAlias(nodename); } // Legacy constructor MaterialItem::MaterialItem(IGameDef *gamedef, content_t content, u16 count): @@ -309,6 +311,15 @@ content_t MaterialItem::getMaterial() const ToolItem */ +ToolItem::ToolItem(IGameDef *gamedef, std::string toolname, u16 wear): + InventoryItem(gamedef, 1) +{ + // Convert directly to the correct name through aliases + m_toolname = gamedef->tdef()->getAlias(toolname); + + m_wear = wear; +} + std::string ToolItem::getImageBasename() const { return m_gamedef->getToolDefManager()->getImagename(m_toolname); @@ -357,9 +368,7 @@ bool ToolItem::isKnown() const CraftItem::CraftItem(IGameDef *gamedef, std::string subname, u16 count): InventoryItem(gamedef, count) { - // Convert directly to the correct name through aliases. - // This is necessary because CraftItem callbacks are stored in - // Lua refenced by their correct name + // Convert directly to the correct name through aliases m_subname = gamedef->cidef()->getAlias(subname); } diff --git a/src/inventory.h b/src/inventory.h index 57af3765..c16b9a31 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -331,12 +331,7 @@ private: class ToolItem : public InventoryItem { public: - ToolItem(IGameDef *gamedef, std::string toolname, u16 wear): - InventoryItem(gamedef, 1) - { - m_toolname = toolname; - m_wear = wear; - } + ToolItem(IGameDef *gamedef, std::string toolname, u16 wear); /* Implementation interface */ diff --git a/src/nodedef.cpp b/src/nodedef.cpp index d76f9b8a..d7769700 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -403,15 +403,8 @@ public: } virtual bool getId(const std::string &name_, content_t &result) const { - std::string name = name_; // Convert name according to possible alias - std::map::const_iterator i; - i = m_aliases.find(name); - if(i != m_aliases.end()){ - /*infostream<<"ndef: alias active: "< "<second - <second; - } + std::string name = getAlias(name_); // Get id return m_name_id_mapping.getId(name, result); } @@ -427,6 +420,14 @@ public: getId(name, id); return get(id); } + virtual std::string getAlias(const std::string &name) const + { + std::map::const_iterator i; + i = m_aliases.find(name); + if(i != m_aliases.end()) + return i->second; + return name; + } // IWritableNodeDefManager virtual void set(content_t c, const ContentFeatures &def) { diff --git a/src/nodedef.h b/src/nodedef.h index f69b66c9..fdf2f8c4 100644 --- a/src/nodedef.h +++ b/src/nodedef.h @@ -253,6 +253,7 @@ public: virtual bool getId(const std::string &name, content_t &result) const=0; virtual content_t getId(const std::string &name) const=0; virtual const ContentFeatures& get(const std::string &name) const=0; + virtual std::string getAlias(const std::string &name) const =0; virtual void serialize(std::ostream &os)=0; }; @@ -270,6 +271,7 @@ public: virtual content_t getId(const std::string &name) const=0; // If not found, returns the features of CONTENT_IGNORE virtual const ContentFeatures& get(const std::string &name) const=0; + virtual std::string getAlias(const std::string &name) const =0; // Register node definition virtual void set(content_t c, const ContentFeatures &def)=0; diff --git a/src/tooldef.cpp b/src/tooldef.cpp index 3b7863ba..7d7eceab 100644 --- a/src/tooldef.cpp +++ b/src/tooldef.cpp @@ -96,12 +96,7 @@ public: virtual const ToolDefinition* getToolDefinition(const std::string &toolname_) const { // Convert name according to possible alias - std::string toolname = toolname_; - std::map::const_iterator i; - i = m_aliases.find(toolname); - if(i != m_aliases.end()){ - toolname = i->second; - } + std::string toolname = getAlias(toolname_); // Get the definition core::map::Node *n; n = m_tool_definitions.find(toolname); @@ -130,6 +125,15 @@ public: } return def->properties; } + virtual std::string getAlias(const std::string &name) const + { + std::map::const_iterator i; + i = m_aliases.find(name); + if(i != m_aliases.end()) + return i->second; + return name; + } + // IWritableToolDefManager virtual bool registerTool(std::string toolname, const ToolDefinition &def) { infostream<<"registerTool: registering tool \""<