1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-05 18:41:05 +00:00

Minor improvements to metadata handling

This commit is contained in:
sfan5 2024-09-25 23:24:30 +02:00
parent 610ddaba7c
commit 700fbc803d
6 changed files with 31 additions and 29 deletions

View file

@ -58,6 +58,8 @@ void NodeMetaRef::reportMetadataChange(const std::string *name)
// Inform other things that the metadata has changed
NodeMetadata *meta = dynamic_cast<NodeMetadata*>(getmeta(false));
bool is_private_change = meta && name && meta->isPrivate(*name);
// If the metadata is now empty, get rid of it
if (meta && meta->empty()) {
clearMeta();
@ -67,7 +69,7 @@ void NodeMetaRef::reportMetadataChange(const std::string *name)
MapEditEvent event;
event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
event.setPositionModified(m_p);
event.is_private_change = name && meta && meta->isPrivate(*name);
event.is_private_change = is_private_change;
m_env->getMap().dispatchEvent(event);
}
@ -94,21 +96,24 @@ int NodeMetaRef::l_mark_as_private(lua_State *L)
NodeMetaRef *ref = checkObject<NodeMetaRef>(L, 1);
NodeMetadata *meta = dynamic_cast<NodeMetadata*>(ref->getmeta(true));
assert(meta);
if (!meta)
return 0;
bool modified = false;
if (lua_istable(L, 2)) {
lua_pushnil(L);
while (lua_next(L, 2) != 0) {
// key at index -2 and value at index -1
luaL_checktype(L, -1, LUA_TSTRING);
meta->markPrivate(readParam<std::string>(L, -1), true);
modified |= meta->markPrivate(readParam<std::string>(L, -1), true);
// removes value, keeps key for next iteration
lua_pop(L, 1);
}
} else if (lua_isstring(L, 2)) {
meta->markPrivate(readParam<std::string>(L, 2), true);
modified |= meta->markPrivate(readParam<std::string>(L, 2), true);
}
ref->reportMetadataChange();
if (modified)
ref->reportMetadataChange();
return 0;
}
@ -145,12 +150,13 @@ bool NodeMetaRef::handleFromTable(lua_State *L, int table, IMetadata *_meta)
Inventory *inv = meta->getInventory();
lua_getfield(L, table, "inventory");
if (lua_istable(L, -1)) {
auto *gamedef = getGameDef(L);
int inventorytable = lua_gettop(L);
lua_pushnil(L);
while (lua_next(L, inventorytable) != 0) {
// key at index -2 and value at index -1
std::string name = luaL_checkstring(L, -2);
read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
const char *name = luaL_checkstring(L, -2);
read_inventory_list(L, -1, inv, name, gamedef);
lua_pop(L, 1); // Remove value, keep key for next iteration
}
lua_pop(L, 1);
@ -177,7 +183,6 @@ NodeMetaRef::NodeMetaRef(IMetadata *meta):
void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
{
NodeMetaRef *o = new NodeMetaRef(p, env);
//infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);