1
0
Fork 0

MetaDataRef: Add contains() and get() (#7214)

mutilcraft-mt53
rubenwardy 2018-04-30 17:42:51 +01:00 committed by SmallJoker
parent 54606e103d
commit 0b5b32b026
8 changed files with 72 additions and 6 deletions

View File

@ -3909,12 +3909,15 @@ An interface to use mod channels on client and server
See `StorageRef`, `NodeMetaRef`, `ItemStackMetaRef`, and `PlayerMetaRef`.
#### Methods
* `set_string(name, value)`
* `get_string(name)`
* `set_int(name, value)`
* `get_int(name)`
* `set_float(name, value)`
* `get_float(name)`
* `contains(key)`: Returns true if key present, otherwise false.
* Returns `nil` when the MetaData is inexistent.
* `get(key)`: Returns `nil` if key not present, else the stored string.
* `set_string(key, value)`: Value of `""` will delete the key.
* `get_string(key)`: Returns `""` if key not present.
* `set_int(key, value)`
* `get_int(key)`: Returns `0` if key not present.
* `set_float(key, value)`
* `get_float(key)`: Returns `0` if key not present.
* `to_table()`: returns `nil` or a table with keys:
* `fields`: key-value storage
* `inventory`: `{list1 = {}, ...}}` (NodeMetaRef only)

View File

@ -43,21 +43,32 @@ end)
local function run_player_meta_tests(player)
local meta = player:get_meta()
meta:set_string("foo", "bar")
assert(meta:contains("foo"))
assert(meta:get_string("foo") == "bar")
assert(meta:get("foo") == "bar")
local meta2 = player:get_meta()
assert(meta2:get_string("foo") == "bar")
assert(meta2:get("foo") == "bar")
assert(meta:equals(meta2))
assert(player:get_attribute("foo") == "bar")
meta:set_string("bob", "dillan")
assert(meta:get_string("foo") == "bar")
assert(meta:get_string("bob") == "dillan")
assert(meta:get("bob") == "dillan")
assert(meta2:get_string("foo") == "bar")
assert(meta2:get_string("bob") == "dillan")
assert(meta2:get("bob") == "dillan")
assert(meta:equals(meta2))
assert(player:get_attribute("foo") == "bar")
assert(player:get_attribute("bob") == "dillan")
meta:set_string("foo", "")
assert(not meta:contains("foo"))
assert(meta:get("foo") == nil)
assert(meta:get_string("foo") == "")
assert(meta:equals(meta2))
end
local function run_player_tests(player)

View File

@ -123,6 +123,8 @@ void ItemStackMetaRef::Register(lua_State *L)
const char ItemStackMetaRef::className[] = "ItemStackMetaRef";
const luaL_Reg ItemStackMetaRef::methods[] = {
luamethod(MetaDataRef, contains),
luamethod(MetaDataRef, get),
luamethod(MetaDataRef, get_string),
luamethod(MetaDataRef, set_string),
luamethod(MetaDataRef, get_int),

View File

@ -51,6 +51,42 @@ MetaDataRef* MetaDataRef::checkobject(lua_State *L, int narg)
// Exported functions
// contains(self, name)
int MetaDataRef::l_contains(lua_State *L)
{
MAP_LOCK_REQUIRED;
MetaDataRef *ref = checkobject(L, 1);
std::string name = luaL_checkstring(L, 2);
Metadata *meta = ref->getmeta(false);
if (meta == NULL)
return 0;
lua_pushboolean(L, meta->contains(name));
return 1;
}
// get(self, name)
int MetaDataRef::l_get(lua_State *L)
{
MAP_LOCK_REQUIRED;
MetaDataRef *ref = checkobject(L, 1);
std::string name = luaL_checkstring(L, 2);
Metadata *meta = ref->getmeta(false);
if (meta == NULL)
return 0;
std::string str;
if (meta->getStringToRef(name, str)) {
lua_pushlstring(L, str.c_str(), str.size());
return 1;
}
return 0;
}
// get_string(self, name)
int MetaDataRef::l_get_string(lua_State *L)
{

View File

@ -46,6 +46,12 @@ protected:
// Exported functions
// contains(self, name)
static int l_contains(lua_State *L);
// get(self, name)
static int l_get(lua_State *L);
// get_string(self, name)
static int l_get_string(lua_State *L);

View File

@ -242,6 +242,8 @@ void NodeMetaRef::Register(lua_State *L)
const luaL_Reg NodeMetaRef::methodsServer[] = {
luamethod(MetaDataRef, contains),
luamethod(MetaDataRef, get),
luamethod(MetaDataRef, get_string),
luamethod(MetaDataRef, set_string),
luamethod(MetaDataRef, get_int),
@ -266,6 +268,8 @@ void NodeMetaRef::RegisterClient(lua_State *L)
const luaL_Reg NodeMetaRef::methodsClient[] = {
luamethod(MetaDataRef, contains),
luamethod(MetaDataRef, get),
luamethod(MetaDataRef, get_string),
luamethod(MetaDataRef, get_int),
luamethod(MetaDataRef, get_float),

View File

@ -107,6 +107,8 @@ void PlayerMetaRef::Register(lua_State *L)
// clang-format off
const char PlayerMetaRef::className[] = "PlayerMetaRef";
const luaL_Reg PlayerMetaRef::methods[] = {
luamethod(MetaDataRef, contains),
luamethod(MetaDataRef, get),
luamethod(MetaDataRef, get_string),
luamethod(MetaDataRef, set_string),
luamethod(MetaDataRef, get_int),

View File

@ -134,6 +134,8 @@ void StorageRef::clearMeta()
const char StorageRef::className[] = "StorageRef";
const luaL_Reg StorageRef::methods[] = {
luamethod(MetaDataRef, contains),
luamethod(MetaDataRef, get),
luamethod(MetaDataRef, get_string),
luamethod(MetaDataRef, set_string),
luamethod(MetaDataRef, get_int),