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

Keep PlayerMetaRef via name not pointer

This commit is contained in:
sfan5 2024-10-07 21:08:47 +02:00
parent c8dc9c2b8d
commit 3778ed7466
3 changed files with 23 additions and 10 deletions

View file

@ -21,6 +21,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_playermeta.h"
#include "lua_api/l_internal.h"
#include "common/c_content.h"
#include "serverenvironment.h"
#include "remoteplayer.h"
#include "server/player_sao.h"
/*
PlayerMetaRef
@ -28,12 +31,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
IMetadata *PlayerMetaRef::getmeta(bool auto_create)
{
return metadata;
auto *player = m_env->getPlayer(m_name);
auto *sao = player ? player->getPlayerSAO() : nullptr;
return sao ? &sao->getMeta() : nullptr;
}
void PlayerMetaRef::clearMeta()
{
metadata->clear();
if (auto *meta = getmeta(true))
meta->clear();
}
void PlayerMetaRef::reportMetadataChange(const std::string *name)
@ -43,9 +49,9 @@ void PlayerMetaRef::reportMetadataChange(const std::string *name)
// Creates an PlayerMetaRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
void PlayerMetaRef::create(lua_State *L, IMetadata *metadata)
void PlayerMetaRef::create(lua_State *L, ServerEnvironment *env, std::string_view name)
{
PlayerMetaRef *o = new PlayerMetaRef(metadata);
PlayerMetaRef *o = new PlayerMetaRef(env, name);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);