From 7a29b14a20b16b97fd0d97d9f305fe0d53c160bf Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Wed, 16 Nov 2011 14:36:33 +0200 Subject: [PATCH] Improved MaterialItem (stores nodename) --- src/inventory.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++----- src/inventory.h | 39 +++++++++++++++++++++++---------------- src/nodedef.cpp | 6 ++++++ src/nodedef.h | 3 +++ 4 files changed, 74 insertions(+), 21 deletions(-) diff --git a/src/inventory.cpp b/src/inventory.cpp index 98bf57738..b0b128418 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "nodedef.h" #include "tooldef.h" #include "gamedef.h" +#include "strfnd.h" /* InventoryItem @@ -95,6 +96,16 @@ InventoryItem* InventoryItem::deSerialize(std::istream &is, IGameDef *gamedef) throw SerializationError("Too large material number"); return new MaterialItem(gamedef, material, count); } + else if(name == "MaterialItem3") + { + std::string all; + std::getline(is, all, '\n'); + Strfnd fnd(all); + fnd.next("\""); + std::string nodename = fnd.next("\""); + u16 count = stoi(trim(fnd.next(""))); + return new MaterialItem(gamedef, nodename, count); + } else if(name == "MBOItem") { std::string inventorystring; @@ -149,24 +160,42 @@ ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f MaterialItem */ +MaterialItem::MaterialItem(IGameDef *gamedef, std::string nodename, u16 count): + InventoryItem(gamedef, count) +{ + if(nodename == "") + nodename = "unknown_block"; + m_nodename = nodename; +} +// Legacy constructor +MaterialItem::MaterialItem(IGameDef *gamedef, content_t content, u16 count): + InventoryItem(gamedef, count) +{ + INodeDefManager *ndef = m_gamedef->ndef(); + std::string nodename = ndef->get(content).name; + if(nodename == "") + nodename = "unknown_block"; + m_nodename = nodename; +} + #ifndef SERVER video::ITexture * MaterialItem::getImage(ITextureSource *tsrc) const { - return m_gamedef->getNodeDefManager()->get(m_content).inventory_texture; + return m_gamedef->getNodeDefManager()->get(m_nodename).inventory_texture; } #endif bool MaterialItem::isCookable() const { INodeDefManager *ndef = m_gamedef->ndef(); - const ContentFeatures &f = ndef->get(m_content); + const ContentFeatures &f = ndef->get(m_nodename); return (f.cookresult_item != ""); } InventoryItem *MaterialItem::createCookResult() const { INodeDefManager *ndef = m_gamedef->ndef(); - const ContentFeatures &f = ndef->get(m_content); + const ContentFeatures &f = ndef->get(m_nodename); std::istringstream is(f.cookresult_item, std::ios::binary); return InventoryItem::deSerialize(is, m_gamedef); } @@ -174,17 +203,25 @@ InventoryItem *MaterialItem::createCookResult() const float MaterialItem::getCookTime() const { INodeDefManager *ndef = m_gamedef->ndef(); - const ContentFeatures &f = ndef->get(m_content); + const ContentFeatures &f = ndef->get(m_nodename); return f.furnace_cooktime; } float MaterialItem::getBurnTime() const { INodeDefManager *ndef = m_gamedef->ndef(); - const ContentFeatures &f = ndef->get(m_content); + const ContentFeatures &f = ndef->get(m_nodename); return f.furnace_burntime; } +content_t MaterialItem::getMaterial() const +{ + INodeDefManager *ndef = m_gamedef->ndef(); + content_t id = CONTENT_IGNORE; + ndef->getId(m_nodename, id); + return id; +} + /* ToolItem */ diff --git a/src/inventory.h b/src/inventory.h index b9de48f9a..32e1cd93e 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -127,11 +127,9 @@ protected: class MaterialItem : public InventoryItem { public: - MaterialItem(IGameDef *gamedef, content_t content, u16 count): - InventoryItem(gamedef, count) - { - m_content = content; - } + MaterialItem(IGameDef *gamedef, std::string nodename, u16 count); + // Legacy constructor + MaterialItem(IGameDef *gamedef, content_t content, u16 count); /* Implementation interface */ @@ -141,16 +139,26 @@ public: } virtual void serialize(std::ostream &os) const { - //os.imbue(std::locale("C")); - os<<"MaterialItem2"; + std::string nodename = m_nodename; + if(nodename == "") + nodename = "unknown_block"; + + os<<"MaterialItem3"; + os<<" \""; + os<getName()) != "MaterialItem") return false; MaterialItem *m = (MaterialItem*)other; - if(m->getMaterial() != m_content) + if(m->m_nodename != m_nodename) return false; return true; } @@ -185,14 +193,13 @@ public: float getCookTime() const; float getBurnTime() const; /* - Special methods + Special properties (not part of virtual interface) */ - content_t getMaterial() - { - return m_content; - } + std::string getNodeName() const + { return m_nodename; } + content_t getMaterial() const; private: - content_t m_content; + std::string m_nodename; }; /* diff --git a/src/nodedef.cpp b/src/nodedef.cpp index baaaa1048..565e3cb34 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -406,6 +406,12 @@ public: { return m_name_id_mapping.getId(name, result); } + virtual const ContentFeatures& get(const std::string &name) const + { + content_t id = CONTENT_IGNORE; + getId(name, id); + return get(id); + } // IWritableNodeDefManager virtual void set(content_t c, const ContentFeatures &def) { diff --git a/src/nodedef.h b/src/nodedef.h index 055ed7a4b..54722f5af 100644 --- a/src/nodedef.h +++ b/src/nodedef.h @@ -254,6 +254,7 @@ public: virtual const ContentFeatures& get(content_t c) const=0; virtual const ContentFeatures& get(const MapNode &n) const=0; virtual bool getId(const std::string &name, content_t &result) const=0; + virtual const ContentFeatures& get(const std::string &name) const=0; virtual void serialize(std::ostream &os)=0; }; @@ -268,6 +269,8 @@ public: virtual const ContentFeatures& get(content_t c) const=0; virtual const ContentFeatures& get(const MapNode &n) const=0; virtual bool getId(const std::string &name, content_t &result) const=0; + // If not found, returns the features of CONTENT_IGNORE + virtual const ContentFeatures& get(const std::string &name) const=0; // Register node definition virtual void set(content_t c, const ContentFeatures &def)=0;