From 6eba5a283d01a47fcd3b8ac4791664e7722ee309 Mon Sep 17 00:00:00 2001 From: cron Date: Sat, 10 Oct 2020 23:35:46 +0000 Subject: [PATCH] ClientObjectRef: add some more functions --- src/client/content_cao.h | 5 +++ src/script/lua_api/l_clientobject.cpp | 44 ++++++++++++++++++++++++--- src/script/lua_api/l_clientobject.h | 9 ++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/client/content_cao.h b/src/client/content_cao.h index 4301f14f2..82867d674 100644 --- a/src/client/content_cao.h +++ b/src/client/content_cao.h @@ -288,4 +288,9 @@ public: } float m_waiting_for_reattach; + + ObjectProperties *getProperties() + { + return &m_prop; + } }; diff --git a/src/script/lua_api/l_clientobject.cpp b/src/script/lua_api/l_clientobject.cpp index 9d8f9c744..d1973321d 100644 --- a/src/script/lua_api/l_clientobject.cpp +++ b/src/script/lua_api/l_clientobject.cpp @@ -4,6 +4,7 @@ #include "l_internal.h" #include "common/c_converter.h" #include "client/client.h" +#include "object_properties.h" ClientObjectRef *ClientObjectRef::checkobject(lua_State *L, int narg) @@ -53,6 +54,36 @@ int ClientObjectRef::l_get_name(lua_State *L) return 1; } +int ClientObjectRef::l_get_parent(lua_State *L) +{ + ClientObjectRef *ref = checkobject(L, 1); + GenericCAO *gcao = get_generic_cao(ref, L); + create(L, gcao->getParent()); + return 1; +} + +int ClientObjectRef::l_get_nametag(lua_State *L) +{ + ClientObjectRef *ref = checkobject(L, 1); + GenericCAO *gcao = get_generic_cao(ref, L); + ObjectProperties *props = gcao->getProperties(); + lua_pushstring(L, props->nametag.c_str()); + return 1; +} + +int ClientObjectRef::l_get_textures(lua_State *L) +{ + ClientObjectRef *ref = checkobject(L, 1); + GenericCAO *gcao = get_generic_cao(ref, L); + ObjectProperties *props = gcao->getProperties(); + lua_newtable(L); + + for (std::string &texture : props->textures) { + lua_pushstring(L, texture.c_str()); + } + return 1; +} + ClientObjectRef::ClientObjectRef(ClientActiveObject *object): m_object(object) { @@ -60,10 +91,12 @@ ClientObjectRef::ClientObjectRef(ClientActiveObject *object): void ClientObjectRef::create(lua_State *L, ClientActiveObject *object) { - ClientObjectRef *o = new ClientObjectRef(object); - *(void **)(lua_newuserdata(L, sizeof(void *))) = o; - luaL_getmetatable(L, className); - lua_setmetatable(L, -2); + if (object) { + ClientObjectRef *o = new ClientObjectRef(object); + *(void **)(lua_newuserdata(L, sizeof(void *))) = o; + luaL_getmetatable(L, className); + lua_setmetatable(L, -2); + } } int ClientObjectRef::gc_object(lua_State *L) { @@ -103,5 +136,8 @@ luaL_Reg ClientObjectRef::methods[] = { luamethod(ClientObjectRef, get_pos), luamethod(ClientObjectRef, is_player), luamethod(ClientObjectRef, get_name), + luamethod(ClientObjectRef, get_parent), + luamethod(ClientObjectRef, get_nametag), + luamethod(ClientObjectRef, get_textures), {0, 0} }; diff --git a/src/script/lua_api/l_clientobject.h b/src/script/lua_api/l_clientobject.h index f4a24ccea..978c64a83 100644 --- a/src/script/lua_api/l_clientobject.h +++ b/src/script/lua_api/l_clientobject.h @@ -36,4 +36,13 @@ private: // get_name(self) static int l_get_name(lua_State *L); + + // get_parent(self) + static int l_get_parent(lua_State *L); + + // get_nametag(self) + static int l_get_nametag(lua_State *L); + + // get_textures(self) + static int l_get_textures(lua_State *L); };