Make ToolItem and MaterialItem to convert names by aliases at creation time too ...
authorPerttu Ahola <celeron55@gmail.com>
Sun, 4 Dec 2011 00:12:45 +0000 (02:12 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Sun, 4 Dec 2011 00:12:45 +0000 (02:12 +0200)
src/inventory.cpp
src/inventory.h
src/nodedef.cpp
src/nodedef.h
src/tooldef.cpp
src/tooldef.h

index e9600ece5c66733c66a9e0535ebf1e0ec89b3e77..dd2713ca9f6b854f05667922a2e02ff48e452825 100644 (file)
@@ -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);
 }
 
index 57af376501b4d5fec503518bf815cf0afaa7cc19..c16b9a310aa1df57300d9d8f4394198d3b9dee79 100644 (file)
@@ -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
        */
index d76f9b8acdbe379553f7f7abd5c796485f63fee6..d7769700b90762139ba4de75f4b3e729e5893e36 100644 (file)
@@ -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<std::string, std::string>::const_iterator i;
-               i = m_aliases.find(name);
-               if(i != m_aliases.end()){
-                       /*infostream<<"ndef: alias active: "<<name<<" -> "<<i->second
-                                       <<std::endl;*/
-                       name = i->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<std::string, std::string>::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)
        {
index f69b66c9f46d4eab4485f917aad3108120c9b1db..fdf2f8c45b6e1d1525b4f8f81e88e2aa250d0e13 100644 (file)
@@ -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;
index 3b7863ba14345910e55fa0a3d639d6e352a318aa..7d7eceab0a6e9d364478d68afcaf1fe882b2dbc5 100644 (file)
@@ -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<std::string, std::string>::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<std::string, ToolDefinition*>::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<std::string, std::string>::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 \""<<toolname<<"\""<<std::endl;
index e28935e4322b53d3a77a12b383c5e28e7d38c122..c30579cb2358709daa4e69813de5f5a4e3d32fc3 100644 (file)
@@ -69,6 +69,7 @@ public:
        virtual std::string getImagename(const std::string &toolname) const =0;
        virtual ToolDiggingProperties getDiggingProperties(
                        const std::string &toolname) const =0;
+       virtual std::string getAlias(const std::string &name) const =0;
        
        virtual void serialize(std::ostream &os)=0;
 };
@@ -82,6 +83,7 @@ public:
        virtual std::string getImagename(const std::string &toolname) const =0;
        virtual ToolDiggingProperties getDiggingProperties(
                        const std::string &toolname) const =0;
+       virtual std::string getAlias(const std::string &name) const =0;
                        
        virtual bool registerTool(std::string toolname, const ToolDefinition &def)=0;
        virtual void clear()=0;