Fix incompatibility of ItemStack.to_table() introduced by stack meta 5182/head
authorrubenwardy <rubenwardy@gmail.com>
Sun, 5 Feb 2017 18:15:46 +0000 (18:15 +0000)
committerrubenwardy <rubenwardy@gmail.com>
Tue, 7 Feb 2017 21:18:17 +0000 (21:18 +0000)
src/script/common/c_content.cpp
src/script/lua_api/l_item.cpp

index 8925b51f428e1268a4de6d7e3a53d2346537dfa2..399aa88d0e806974bf0ee6b7ee3bcf2d31c35ea8 100644 (file)
@@ -848,6 +848,21 @@ ItemStack read_item(lua_State* L, int index,Server* srv)
                        istack.metadata.setString("", value);
                }
 
+               lua_getfield(L, index, "meta");
+               fieldstable = lua_gettop(L);
+               if (lua_istable(L, fieldstable)) {
+                       lua_pushnil(L);
+                       while (lua_next(L, fieldstable) != 0) {
+                               // key at index -2 and value at index -1
+                               std::string key = lua_tostring(L, -2);
+                               size_t value_len;
+                               const char *value_cs = lua_tolstring(L, -1, &value_len);
+                               std::string value(value_cs, value_len);
+                               istack.metadata.setString(name, value);
+                               lua_pop(L, 1); // removes value, keeps key for next iteration
+                       }
+               }
+
                return istack;
        } else {
                throw LuaError("Expecting itemstack, itemstring, table or nil");
index f0293bed89e79c43b051006ad99b6af83ecb12bb..9638740e8068e66dfb5681c82688eb722ca862ca 100644 (file)
@@ -225,23 +225,21 @@ int LuaItemStack::l_to_table(lua_State *L)
                lua_pushinteger(L, item.wear);
                lua_setfield(L, -2, "wear");
 
-               if (item.metadata.size() == 1 && item.metadata.contains("")) {
-                       const std::string &value = item.metadata.getString("");
+               const std::string &metadata_str = item.metadata.getString("");
+               lua_pushlstring(L, metadata_str.c_str(), metadata_str.size());
+               lua_setfield(L, -2, "metadata");
+
+               lua_newtable(L);
+               const StringMap &fields = item.metadata.getStrings();
+               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_setfield(L, -2, "metadata");
-               } else {
-                       lua_newtable(L);
-                       const StringMap &fields = item.metadata.getStrings();
-                       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");
+                       lua_settable(L, -3);
                }
+               lua_setfield(L, -2, "meta");
        }
        return 1;
 }