3 changes: 2 additions & 1 deletion src/script/lua_api/l_metadata.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2013-8 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
Expand Down
27 changes: 20 additions & 7 deletions src/script/lua_api/l_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_internal.h"
#include "lua_api/l_inventory.h"
#include "lua_api/l_item.h"
#include "lua_api/l_playermeta.h"
#include "common/c_converter.h"
#include "common/c_content.h"
#include "log.h"
Expand Down Expand Up @@ -1218,16 +1219,15 @@ int ObjectRef::l_set_attribute(lua_State *L)
{
ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref);
if (co == NULL) {
if (co == NULL)
return 0;
}

std::string attr = luaL_checkstring(L, 2);
if (lua_isnil(L, 3)) {
co->removeExtendedAttribute(attr);
co->getMeta().removeString(attr);
} else {
std::string value = luaL_checkstring(L, 3);
co->setExtendedAttribute(attr, value);
co->getMeta().setString(attr, value);
}
return 1;
}
Expand All @@ -1237,14 +1237,13 @@ int ObjectRef::l_get_attribute(lua_State *L)
{
ObjectRef *ref = checkobject(L, 1);
PlayerSAO* co = getplayersao(ref);
if (co == NULL) {
if (co == NULL)
return 0;
}

std::string attr = luaL_checkstring(L, 2);

std::string value;
if (co->getExtendedAttribute(attr, &value)) {
if (co->getMeta().getStringToRef(attr, value)) {
lua_pushstring(L, value.c_str());
return 1;
}
Expand All @@ -1253,6 +1252,19 @@ int ObjectRef::l_get_attribute(lua_State *L)
}


// get_meta(self, attribute)
int ObjectRef::l_get_meta(lua_State *L)
{
ObjectRef *ref = checkobject(L, 1);
PlayerSAO *co = getplayersao(ref);
if (co == NULL)
return 0;

PlayerMetaRef::create(L, &co->getMeta());
return 1;
}


// set_inventory_formspec(self, formspec)
int ObjectRef::l_set_inventory_formspec(lua_State *L)
{
Expand Down Expand Up @@ -1884,6 +1896,7 @@ const luaL_Reg ObjectRef::methods[] = {
luamethod(ObjectRef, set_breath),
luamethod(ObjectRef, get_attribute),
luamethod(ObjectRef, set_attribute),
luamethod(ObjectRef, get_meta),
luamethod(ObjectRef, set_inventory_formspec),
luamethod(ObjectRef, get_inventory_formspec),
luamethod(ObjectRef, set_formspec_prepend),
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ class ObjectRef : public ModApiBase {
// get_attribute(self, attribute)
static int l_get_attribute(lua_State *L);

// get_meta(self)
static int l_get_meta(lua_State *L);

// set_inventory_formspec(self, formspec)
static int l_set_inventory_formspec(lua_State *L);

Expand Down
121 changes: 121 additions & 0 deletions src/script/lua_api/l_playermeta.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "lua_api/l_playermeta.h"
#include "lua_api/l_internal.h"
#include "common/c_content.h"

/*
PlayerMetaRef
*/
PlayerMetaRef *PlayerMetaRef::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud)
luaL_typerror(L, narg, className);

return *(PlayerMetaRef **)ud; // unbox pointer
}

Metadata *PlayerMetaRef::getmeta(bool auto_create)
{
return metadata;
}

void PlayerMetaRef::clearMeta()
{
metadata->clear();
}

void PlayerMetaRef::reportMetadataChange()
{
// TODO
}

// garbage collector
int PlayerMetaRef::gc_object(lua_State *L)
{
PlayerMetaRef *o = *(PlayerMetaRef **)(lua_touserdata(L, 1));
delete o;
return 0;
}

// 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, Metadata *metadata)
{
PlayerMetaRef *o = new PlayerMetaRef(metadata);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
}

void PlayerMetaRef::Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);

lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable); // hide metatable from Lua getmetatable()

lua_pushliteral(L, "metadata_class");
lua_pushlstring(L, className, strlen(className));
lua_settable(L, metatable);

lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);

lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);

lua_pushliteral(L, "__eq");
lua_pushcfunction(L, l_equals);
lua_settable(L, metatable);

lua_pop(L, 1); // drop metatable

luaL_openlib(L, 0, methods, 0);
lua_pop(L, 1);

// Cannot be created from Lua
// lua_register(L, className, create_object);
}

// clang-format off
const char PlayerMetaRef::className[] = "PlayerMetaRef";
const luaL_Reg PlayerMetaRef::methods[] = {
luamethod(MetaDataRef, get_string),
luamethod(MetaDataRef, set_string),
luamethod(MetaDataRef, get_int),
luamethod(MetaDataRef, set_int),
luamethod(MetaDataRef, get_float),
luamethod(MetaDataRef, set_float),
luamethod(MetaDataRef, to_table),
luamethod(MetaDataRef, from_table),
luamethod(MetaDataRef, equals),
{0,0}
};
// clang-format on
57 changes: 57 additions & 0 deletions src/script/lua_api/l_playermeta.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include "lua_api/l_base.h"
#include "lua_api/l_metadata.h"
#include "irrlichttypes_bloated.h"
#include "inventory.h"
#include "metadata.h"

class PlayerMetaRef : public MetaDataRef
{
private:
Metadata *metadata = nullptr;

static const char className[];
static const luaL_Reg methods[];

static PlayerMetaRef *checkobject(lua_State *L, int narg);

virtual Metadata *getmeta(bool auto_create);

virtual void clearMeta();

virtual void reportMetadataChange();

// garbage collector
static int gc_object(lua_State *L);

public:
PlayerMetaRef(Metadata *metadata) : metadata(metadata) {}
~PlayerMetaRef() = default;

// Creates an ItemStackMetaRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
static void create(lua_State *L, Metadata *metadata);

static void Register(lua_State *L);
};
2 changes: 2 additions & 0 deletions src/script/scripting_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_nodetimer.h"
#include "lua_api/l_noise.h"
#include "lua_api/l_object.h"
#include "lua_api/l_playermeta.h"
#include "lua_api/l_particles.h"
#include "lua_api/l_rollback.h"
#include "lua_api/l_server.h"
Expand Down Expand Up @@ -99,6 +100,7 @@ void ServerScripting::InitializeModApi(lua_State *L, int top)
NodeMetaRef::Register(L);
NodeTimerRef::Register(L);
ObjectRef::Register(L);
PlayerMetaRef::Register(L);
LuaSettings::Register(L);
StorageRef::Register(L);
ModChannelRef::Register(L);
Expand Down
2 changes: 1 addition & 1 deletion src/serverenvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ void ServerEnvironment::saveLoadedPlayers()

for (RemotePlayer *player : m_players) {
if (player->checkModified() || (player->getPlayerSAO() &&
player->getPlayerSAO()->extendedAttributesModified())) {
player->getPlayerSAO()->getMeta().isModified())) {
try {
m_player_database->savePlayer(player);
} catch (DatabaseException &e) {
Expand Down