diff options
author | rubenwardy <rw@rubenwardy.com> | 2018-04-30 17:42:51 +0100 |
---|---|---|
committer | SmallJoker <SmallJoker@users.noreply.github.com> | 2018-04-30 18:42:51 +0200 |
commit | 0b5b32b026291d5bd8e47acebca10259a379fc1c (patch) | |
tree | 6e308bb5ccf3fb3050a40cd784456387c67b106e /src/script | |
parent | 54606e103d5b2d2bace6d57c45e48804c5dec487 (diff) | |
download | minetest-0b5b32b026291d5bd8e47acebca10259a379fc1c.tar.gz minetest-0b5b32b026291d5bd8e47acebca10259a379fc1c.tar.bz2 minetest-0b5b32b026291d5bd8e47acebca10259a379fc1c.zip |
MetaDataRef: Add contains() and get() (#7214)
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/lua_api/l_itemstackmeta.cpp | 2 | ||||
-rw-r--r-- | src/script/lua_api/l_metadata.cpp | 36 | ||||
-rw-r--r-- | src/script/lua_api/l_metadata.h | 6 | ||||
-rw-r--r-- | src/script/lua_api/l_nodemeta.cpp | 4 | ||||
-rw-r--r-- | src/script/lua_api/l_playermeta.cpp | 2 | ||||
-rw-r--r-- | src/script/lua_api/l_storage.cpp | 2 |
6 files changed, 52 insertions, 0 deletions
diff --git a/src/script/lua_api/l_itemstackmeta.cpp b/src/script/lua_api/l_itemstackmeta.cpp index 0661ec25d..07ab86499 100644 --- a/src/script/lua_api/l_itemstackmeta.cpp +++ b/src/script/lua_api/l_itemstackmeta.cpp @@ -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), diff --git a/src/script/lua_api/l_metadata.cpp b/src/script/lua_api/l_metadata.cpp index 69f495c4a..3901ac2cf 100644 --- a/src/script/lua_api/l_metadata.cpp +++ b/src/script/lua_api/l_metadata.cpp @@ -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) { diff --git a/src/script/lua_api/l_metadata.h b/src/script/lua_api/l_metadata.h index 3aaf62d4a..cee38ed45 100644 --- a/src/script/lua_api/l_metadata.h +++ b/src/script/lua_api/l_metadata.h @@ -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); diff --git a/src/script/lua_api/l_nodemeta.cpp b/src/script/lua_api/l_nodemeta.cpp index 348d73ad5..f390664a4 100644 --- a/src/script/lua_api/l_nodemeta.cpp +++ b/src/script/lua_api/l_nodemeta.cpp @@ -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), diff --git a/src/script/lua_api/l_playermeta.cpp b/src/script/lua_api/l_playermeta.cpp index b8f6aabb7..e690f6bd0 100644 --- a/src/script/lua_api/l_playermeta.cpp +++ b/src/script/lua_api/l_playermeta.cpp @@ -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), diff --git a/src/script/lua_api/l_storage.cpp b/src/script/lua_api/l_storage.cpp index 01810ca8b..280ec7266 100644 --- a/src/script/lua_api/l_storage.cpp +++ b/src/script/lua_api/l_storage.cpp @@ -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), |