Skip to content

Commit

Permalink
MetaData: restore undocumented set_string behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallJoker committed Feb 21, 2024
1 parent e3cc26c commit 52ba4d8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
36 changes: 36 additions & 0 deletions games/devtest/mods/testmetadata/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

do
-- MetaData test
local stack = ItemStack("air 20")
local meta = stack:get_meta()

-- key/value removal using set_string (undocumented, deprecated way)
meta:set_string("key", "value")
assert(meta:get_string("key") == "value")
meta:set_string("key", nil) -- ignore warning
assert(meta:to_table().fields["key"] == nil)

-- undocumented consequence of Lua's automatic string <--> number cast
meta:set_string("key", 2)
assert(meta:get_string("key") == "2")

-- Getters for inexistent keys
assert(meta:get_string("notfound") == "")
assert(meta:get_int("notfound") == 0)
assert(meta:get("notfound") == nil)

-- key/value removal using set_string (correct way)
meta:set_string("key", "value2")
assert(meta:get_string("key") == "value2")
meta:set_string("key", "")
assert(meta:to_table().fields["key"] == nil)

-- from_table
local values = meta:to_table()
values.fields["new"] = 420
meta:from_table(values)
assert(meta:get_int("new") == 420)
values.fields["new"] = nil
meta:from_table(values)
assert(meta:get("new") == nil)
end
2 changes: 2 additions & 0 deletions games/devtest/mods/testmetadata/mod.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = testmetadata
description = Tests for MetaDataRef
8 changes: 7 additions & 1 deletion src/script/lua_api/l_metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ int MetaDataRef::l_set_string(lua_State *L)

MetaDataRef *ref = checkAnyMetadata(L, 1);
std::string name = luaL_checkstring(L, 2);
auto str = readParam<std::string_view>(L, 3);
std::string_view str;
if (!lua_isnil(L, 3)) {
str = readParam<std::string_view>(L, 3);
} else {
log_deprecated(L, "Value passed to set_string is nil. This behaviour is"
" undocumented and will result in an error in the future.", 1, true);
}

IMetadata *meta = ref->getmeta(!str.empty());
if (meta != NULL && meta->setString(name, str))
Expand Down

0 comments on commit 52ba4d8

Please sign in to comment.