diff options
author | rubenwardy <rubenwardy@gmail.com> | 2017-01-31 19:49:01 +0000 |
---|---|---|
committer | rubenwardy <rubenwardy@gmail.com> | 2017-02-04 22:07:55 +0000 |
commit | f2aa2c6a986dec47856c49ae5f54fbf3c688e027 (patch) | |
tree | d12fbb3e62776efaafe059a7fdbfa9938e7298cd /src/script/common | |
parent | c2e7b1f57941cb34cb7e3d71dc040fad53a64e3e (diff) | |
download | minetest-f2aa2c6a986dec47856c49ae5f54fbf3c688e027.tar.gz minetest-f2aa2c6a986dec47856c49ae5f54fbf3c688e027.tar.bz2 minetest-f2aa2c6a986dec47856c49ae5f54fbf3c688e027.zip |
Add ItemStack key-value meta storage
Diffstat (limited to 'src/script/common')
-rw-r--r-- | src/script/common/c_content.cpp | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index ebc951295..8925b51f4 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -824,11 +824,32 @@ ItemStack read_item(lua_State* L, int index,Server* srv) std::string name = getstringfield_default(L, index, "name", ""); int count = getintfield_default(L, index, "count", 1); int wear = getintfield_default(L, index, "wear", 0); - std::string metadata = getstringfield_default(L, index, "metadata", ""); - return ItemStack(name, count, wear, metadata, idef); - } - else - { + + ItemStack istack(name, count, wear, idef); + + lua_getfield(L, index, "metadata"); + + // Support old metadata format by checking type + int 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 + } + } else { + // BACKWARDS COMPATIBLITY + std::string value = getstringfield_default(L, index, "metadata", ""); + istack.metadata.setString("", value); + } + + return istack; + } else { throw LuaError("Expecting itemstack, itemstring, table or nil"); } } |