Skip to content
Permalink
Browse files

Store legacy metadata separate from new item meta data

Removes need for empty key in metadata storage. Also fixes a bug
where "" in meta overrides the value of metadata
  • Loading branch information
rubenwardy committed Apr 6, 2017
1 parent be06636 commit f012e6f4396af68f40ce0333223728c947b4d0f4
Showing with 12 additions and 5 deletions.
  1. +6 −1 src/itemstackmetadata.cpp
  2. +2 −0 src/itemstackmetadata.h
  3. +1 −1 src/script/common/c_content.cpp
  4. +3 −3 src/script/lua_api/l_item.cpp
@@ -19,6 +19,7 @@ void ItemStackMetadata::serialize(std::ostream &os) const
os2 << it->first << DESERIALIZE_KV_DELIM
<< it->second << DESERIALIZE_PAIR_DELIM;
}
os2 << DESERIALIZE_KV_DELIM << legacy_metadata << DESERIALIZE_PAIR_DELIM;
os << serializeJsonStringIfNeeded(os2.str());
}

@@ -34,7 +35,11 @@ void ItemStackMetadata::deSerialize(std::istream &is)
while (!fnd.at_end()) {
std::string name = fnd.next(DESERIALIZE_KV_DELIM_STR);
std::string var = fnd.next(DESERIALIZE_PAIR_DELIM_STR);
m_stringvars[name] = var;
if (name == "") {
legacy_metadata = var;
} else {
m_stringvars[name] = var;
}
}
} else {
// BACKWARDS COMPATIBILITY
@@ -30,6 +30,8 @@ class ItemStackMetadata : public Metadata
public:
void serialize(std::ostream &os) const;
void deSerialize(std::istream &is);

std::string legacy_metadata;
};

#endif
@@ -826,7 +826,7 @@ ItemStack read_item(lua_State* L, int index, IItemDefManager *idef)

// BACKWARDS COMPATIBLITY
std::string value = getstringfield_default(L, index, "metadata", "");
istack.metadata.setString("", value);
istack.metadata.legacy_metadata = value;

// Get meta
lua_getfield(L, index, "meta");
@@ -154,7 +154,7 @@ int LuaItemStack::l_get_metadata(lua_State *L)
NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
const std::string &value = item.metadata.getString("");
const std::string &value = item.metadata.legacy_metadata;
lua_pushlstring(L, value.c_str(), value.size());
return 1;
}
@@ -169,7 +169,7 @@ int LuaItemStack::l_set_metadata(lua_State *L)

size_t len = 0;
const char *ptr = luaL_checklstring(L, 2, &len);
item.metadata.setString("", std::string(ptr, len));
item.metadata.legacy_metadata = std::string(ptr, len);

lua_pushboolean(L, true);
return 1;
@@ -225,7 +225,7 @@ int LuaItemStack::l_to_table(lua_State *L)
lua_pushinteger(L, item.wear);
lua_setfield(L, -2, "wear");

const std::string &metadata_str = item.metadata.getString("");
const std::string &metadata_str = item.metadata.legacy_metadata;
lua_pushlstring(L, metadata_str.c_str(), metadata_str.size());
lua_setfield(L, -2, "metadata");

0 comments on commit f012e6f

Please sign in to comment.
You can’t perform that action at this time.