summaryrefslogtreecommitdiff
path: root/src/inventory.cpp
diff options
context:
space:
mode:
authorPaul Ouellette <oue.paul18@gmail.com>2020-06-09 13:37:25 -0400
committerGitHub <noreply@github.com>2020-06-09 19:37:25 +0200
commitb16f841756ef86e83710ad2fddf2cd5bafdf4bcc (patch)
tree777e5dee13e1e2d20a7f8e4a5b8df19a7fad6b85 /src/inventory.cpp
parent09e285f38cd96b4278b921ab82c5266082bb1a0d (diff)
downloadminetest-b16f841756ef86e83710ad2fddf2cd5bafdf4bcc.tar.gz
minetest-b16f841756ef86e83710ad2fddf2cd5bafdf4bcc.tar.bz2
minetest-b16f841756ef86e83710ad2fddf2cd5bafdf4bcc.zip
LuaItemStack: Add __tostring metamethod (#8785)
* LuaItemStack: Add __tostring metamethod * Clean up LuaItemStack::checkobject
Diffstat (limited to 'src/inventory.cpp')
-rw-r--r--src/inventory.cpp29
1 files changed, 16 insertions, 13 deletions
diff --git a/src/inventory.cpp b/src/inventory.cpp
index 77ecf5876..349ee503d 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -56,28 +56,31 @@ ItemStack::ItemStack(const std::string &name_, u16 count_,
count = 1;
}
-void ItemStack::serialize(std::ostream &os) const
+void ItemStack::serialize(std::ostream &os, bool serialize_meta) const
{
if (empty())
return;
// Check how many parts of the itemstring are needed
int parts = 1;
- if(count != 1)
- parts = 2;
- if(wear != 0)
- parts = 3;
if (!metadata.empty())
parts = 4;
+ else if (wear != 0)
+ parts = 3;
+ else if (count != 1)
+ parts = 2;
- os<<serializeJsonStringIfNeeded(name);
- if(parts >= 2)
- os<<" "<<count;
- if(parts >= 3)
- os<<" "<<wear;
+ os << serializeJsonStringIfNeeded(name);
+ if (parts >= 2)
+ os << " " << count;
+ if (parts >= 3)
+ os << " " << wear;
if (parts >= 4) {
os << " ";
- metadata.serialize(os);
+ if (serialize_meta)
+ metadata.serialize(os);
+ else
+ os << "<metadata size=" << metadata.size() << ">";
}
}
@@ -240,10 +243,10 @@ void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef)
deSerialize(is, itemdef);
}
-std::string ItemStack::getItemString() const
+std::string ItemStack::getItemString(bool include_meta) const
{
std::ostringstream os(std::ios::binary);
- serialize(os);
+ serialize(os, include_meta);
return os.str();
}