From 66c1c14897fe57a5a7585849b667adf4206722c8 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Thu, 14 Apr 2016 20:27:22 +0200 Subject: [PATCH] Item Metadata: Re-add 'metadata' to ItemStack:to_table() and output a string if only the empty string field is present --- src/script/lua_api/l_item.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index a66b4d2fd..dfbec7d8d 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -211,7 +211,7 @@ int LuaItemStack::l_to_table(lua_State *L) { NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); - const ItemStack &item = o->m_stack; + ItemStack &item = o->m_stack; if(item.empty()) { lua_pushnil(L); @@ -225,6 +225,27 @@ int LuaItemStack::l_to_table(lua_State *L) lua_setfield(L, -2, "count"); lua_pushinteger(L, item.wear); lua_setfield(L, -2, "wear"); + + StringMap fields = item.metadata.getStrings(); + if(fields.size()==1 && fields.find("")!=fields.end()){ + //only "" is present, outputting a string to keep legacy behavior + lua_pushlstring(L, fields[""].c_str(), fields[""].size()); + lua_setfield(L, -2, "metadata"); + }else{ + lua_newtable(L); + { + for (StringMap::const_iterator + it = fields.begin(); it != fields.end(); ++it) { + const std::string &name = it->first; + const std::string &value = it->second; + lua_pushlstring(L, name.c_str(), name.size()); + lua_pushlstring(L, value.c_str(), value.size()); + lua_settable(L, -3); + } + lua_setfield(L, -2, "metadata"); + } + } + } return 1; }