From 9eee3c3f465c071bb9908749cf48be3c131a1bdf Mon Sep 17 00:00:00 2001 From: BlockMen Date: Fri, 20 Nov 2015 23:46:33 +0100 Subject: Add option to give every object a nametag or change the nametag text of players --- src/script/lua_api/l_object.cpp | 96 +++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 41 deletions(-) (limited to 'src/script/lua_api/l_object.cpp') diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 52190d60e..6d6614e7d 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -767,6 +767,59 @@ int ObjectRef::l_is_player(lua_State *L) return 1; } +// set_nametag_attributes(self, attributes) +int ObjectRef::l_set_nametag_attributes(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + ObjectRef *ref = checkobject(L, 1); + ServerActiveObject *co = getobject(ref); + + if (co == NULL) + return 0; + ObjectProperties *prop = co->accessObjectProperties(); + if (!prop) + return 0; + + lua_getfield(L, 2, "color"); + if (!lua_isnil(L, -1)) { + video::SColor color = prop->nametag_color; + read_color(L, -1, &color); + prop->nametag_color = color; + } + lua_pop(L, 1); + + std::string nametag = getstringfield_default(L, 2, "text", ""); + if (nametag != "") + prop->nametag = nametag; + + co->notifyObjectPropertiesModified(); + lua_pushboolean(L, true); + return 1; +} + +// get_nametag_attributes(self) +int ObjectRef::l_get_nametag_attributes(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + ObjectRef *ref = checkobject(L, 1); + ServerActiveObject *co = getobject(ref); + + if (co == NULL) + return 0; + ObjectProperties *prop = co->accessObjectProperties(); + if (!prop) + return 0; + + video::SColor color = prop->nametag_color; + + lua_newtable(L); + push_ARGB8(L, color); + lua_setfield(L, -2, "color"); + lua_pushstring(L, prop->nametag.c_str()); + lua_setfield(L, -2, "text"); + return 1; +} + /* LuaEntitySAO-only */ // setvelocity(self, {x=num, y=num, z=num}) @@ -1593,45 +1646,6 @@ int ObjectRef::l_get_day_night_ratio(lua_State *L) return 1; } -// set_nametag_attributes(self, attributes) -int ObjectRef::l_set_nametag_attributes(lua_State *L) -{ - NO_MAP_LOCK_REQUIRED; - ObjectRef *ref = checkobject(L, 1); - PlayerSAO *playersao = getplayersao(ref); - if (playersao == NULL) - return 0; - - lua_getfield(L, 2, "color"); - if (!lua_isnil(L, -1)) { - video::SColor color = playersao->getNametagColor(); - if (!read_color(L, -1, &color)) - return 0; - playersao->setNametagColor(color); - } - - lua_pushboolean(L, true); - return 1; -} - -// get_nametag_attributes(self) -int ObjectRef::l_get_nametag_attributes(lua_State *L) -{ - NO_MAP_LOCK_REQUIRED; - ObjectRef *ref = checkobject(L, 1); - PlayerSAO *playersao = getplayersao(ref); - if (playersao == NULL) - return 0; - - video::SColor color = playersao->getNametagColor(); - - lua_newtable(L); - push_ARGB8(L, color); - lua_setfield(L, -2, "color"); - - return 1; -} - ObjectRef::ObjectRef(ServerActiveObject *object): m_object(object) { @@ -1719,6 +1733,8 @@ const luaL_reg ObjectRef::methods[] = { luamethod(ObjectRef, set_detach), luamethod(ObjectRef, set_properties), luamethod(ObjectRef, get_properties), + luamethod(ObjectRef, set_nametag_attributes), + luamethod(ObjectRef, get_nametag_attributes), // LuaEntitySAO-only luamethod(ObjectRef, setvelocity), luamethod(ObjectRef, getvelocity), @@ -1768,7 +1784,5 @@ const luaL_reg ObjectRef::methods[] = { luamethod(ObjectRef, get_local_animation), luamethod(ObjectRef, set_eye_offset), luamethod(ObjectRef, get_eye_offset), - luamethod(ObjectRef, set_nametag_attributes), - luamethod(ObjectRef, get_nametag_attributes), {0,0} }; -- cgit v1.2.3