1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Add function to get all HUD elements (#14042)

This commit is contained in:
cx384 2024-01-14 17:46:29 +01:00 committed by GitHub
parent ed7d4037b2
commit 92c55c27cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 85 additions and 0 deletions

View file

@ -422,6 +422,26 @@ int LuaLocalPlayer::l_hud_get(lua_State *L)
return 1;
}
// hud_get_all(self)
int LuaLocalPlayer::l_hud_get_all(lua_State *L)
{
LocalPlayer *player = getobject(L, 1);
if (player == nullptr)
return 0;
lua_newtable(L);
player->hudApply([&](const std::vector<HudElement*>& hud) {
for (std::size_t id = 0; id < hud.size(); ++id) {
HudElement *elem = hud[id];
if (elem != nullptr) {
push_hud_element(L, elem);
lua_rawseti(L, -2, id);
}
}
});
return 1;
}
LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
{
return ref->m_localplayer;
@ -483,6 +503,7 @@ const luaL_Reg LuaLocalPlayer::methods[] = {
luamethod(LuaLocalPlayer, hud_remove),
luamethod(LuaLocalPlayer, hud_change),
luamethod(LuaLocalPlayer, hud_get),
luamethod(LuaLocalPlayer, hud_get_all),
luamethod(LuaLocalPlayer, get_move_resistance),

View file

@ -93,6 +93,8 @@ private:
static int l_hud_change(lua_State *L);
// hud_get(self, id)
static int l_hud_get(lua_State *L);
// hud_get_all(self)
static int l_hud_get_all(lua_State *L);
static int l_get_move_resistance(lua_State *L);

View file

@ -1743,6 +1743,28 @@ int ObjectRef::l_hud_get(lua_State *L)
return 1;
}
// hud_get_all(self)
int ObjectRef::l_hud_get_all(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkObject<ObjectRef>(L, 1);
RemotePlayer *player = getplayer(ref);
if (player == nullptr)
return 0;
lua_newtable(L);
player->hudApply([&](const std::vector<HudElement*>& hud) {
for (std::size_t id = 0; id < hud.size(); ++id) {
HudElement *elem = hud[id];
if (elem != nullptr) {
push_hud_element(L, elem);
lua_rawseti(L, -2, id);
}
}
});
return 1;
}
// hud_set_flags(self, flags)
int ObjectRef::l_hud_set_flags(lua_State *L)
{
@ -2691,6 +2713,7 @@ luaL_Reg ObjectRef::methods[] = {
luamethod(ObjectRef, hud_remove),
luamethod(ObjectRef, hud_change),
luamethod(ObjectRef, hud_get),
luamethod(ObjectRef, hud_get_all),
luamethod(ObjectRef, hud_set_flags),
luamethod(ObjectRef, hud_get_flags),
luamethod(ObjectRef, hud_set_hotbar_itemcount),

View file

@ -300,6 +300,9 @@ private:
// hud_get(self, id)
static int l_hud_get(lua_State *L);
// hud_get_all(self)
static int l_hud_get_all(lua_State *L);
// hud_set_flags(self, flags)
static int l_hud_set_flags(lua_State *L);