diff --git a/doc/client_lua_api.md b/doc/client_lua_api.md index c9ccbeefa..68156efd3 100644 --- a/doc/client_lua_api.md +++ b/doc/client_lua_api.md @@ -690,6 +690,8 @@ Call these functions only at load time! for unloaded areas. * `minetest.get_node_or_nil(pos)` * Same as `get_node` but returns `nil` for unloaded areas. +* `minetest.get_meta(pos)` + * Get a `NodeMetaRef` at that position ### Player * `minetest.get_wielded_item()` @@ -794,7 +796,18 @@ It can be created via `Settings(filename)`. * write changes to file * `to_table()`: returns `{[key1]=value1,...}` -Definition tables +### NodeMetaRef +Node metadata: reference extra data and functionality stored in a node. +Can be obtained via `minetest.get_meta(pos)`. + +#### Methods +* `get_string(name)` +* `get_int(name)` +* `get_float(name)` +* `to_table()`: returns `nil` or a table with keys: + * `fields`: key-value storage + * `inventory`: `{list1 = {}, ...}}` + ----------------- ### Chat command definition (`register_chatcommand`) diff --git a/src/script/clientscripting.cpp b/src/script/clientscripting.cpp index df30a7253..17c53985d 100644 --- a/src/script/clientscripting.cpp +++ b/src/script/clientscripting.cpp @@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_sound.h" #include "lua_api/l_util.h" #include "lua_api/l_item.h" +#include "lua_api/l_nodemeta.h" ClientScripting::ClientScripting(Client *client): ScriptApiBase() @@ -68,4 +69,5 @@ void ClientScripting::InitializeModApi(lua_State *L, int top) LuaItemStack::Register(L); StorageRef::Register(L); LuaMinimap::Register(L); + NodeMetaRef::RegisterClient(L); } diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index 5f9474702..5a0cd5cc3 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -25,8 +25,11 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "gettext.h" #include "l_internal.h" #include "lua_api/l_item.h" +#include "lua_api/l_nodemeta.h" #include "mainmenumanager.h" #include "util/string.h" +#include "clientenvironment.h" +#include "map.h" extern MainGameCallback *g_gamecallback; @@ -182,6 +185,15 @@ int ModApiClient::l_get_wielded_item(lua_State *L) return 1; } +// get_meta(pos) +int ModApiClient::l_get_meta(lua_State *L) +{ + v3s16 p = read_v3s16(L, 1); + NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p); + NodeMetaRef::createClient(L, meta); + return 1; +} + void ModApiClient::Initialize(lua_State *L, int top) { API_FCT(get_current_modname); @@ -196,4 +208,5 @@ void ModApiClient::Initialize(lua_State *L, int top) API_FCT(get_node_or_nil); API_FCT(get_wielded_item); API_FCT(disconnect); + API_FCT(get_meta); } diff --git a/src/script/lua_api/l_client.h b/src/script/lua_api/l_client.h index d7f92ac1c..94b154bb4 100644 --- a/src/script/lua_api/l_client.h +++ b/src/script/lua_api/l_client.h @@ -62,6 +62,9 @@ private: // get_wielded_item() static int l_get_wielded_item(lua_State *L); + // get_meta(pos) + static int l_get_meta(lua_State *L); + public: static void Initialize(lua_State *L, int top); }; diff --git a/src/script/lua_api/l_nodemeta.cpp b/src/script/lua_api/l_nodemeta.cpp index 4b2b392da..4368a8c50 100644 --- a/src/script/lua_api/l_nodemeta.cpp +++ b/src/script/lua_api/l_nodemeta.cpp @@ -38,6 +38,9 @@ NodeMetaRef* NodeMetaRef::checkobject(lua_State *L, int narg) Metadata* NodeMetaRef::getmeta(bool auto_create) { + if (m_is_local) + return m_meta; + NodeMetadata *meta = m_env->getMap().getNodeMetadata(m_p); if (meta == NULL && auto_create) { meta = new NodeMetadata(m_env->getGameDef()->idef()); @@ -142,7 +145,14 @@ bool NodeMetaRef::handleFromTable(lua_State *L, int table, Metadata *_meta) NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env): m_p(p), - m_env(env) + m_env(env), + m_is_local(false) +{ +} + +NodeMetaRef::NodeMetaRef(Metadata *meta): + m_meta(meta), + m_is_local(true) { } @@ -161,7 +171,17 @@ void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env) lua_setmetatable(L, -2); } -void NodeMetaRef::Register(lua_State *L) +// Client-sided version of the above +void NodeMetaRef::createClient(lua_State *L, Metadata *meta) +{ + NodeMetaRef *o = new NodeMetaRef(meta); + *(void **)(lua_newuserdata(L, sizeof(void *))) = o; + luaL_getmetatable(L, className); + lua_setmetatable(L, -2); +} + +const char NodeMetaRef::className[] = "NodeMetaRef"; +void NodeMetaRef::RegisterCommon(lua_State *L) { lua_newtable(L); int methodtable = lua_gettop(L); @@ -185,16 +205,17 @@ void NodeMetaRef::Register(lua_State *L) lua_settable(L, metatable); lua_pop(L, 1); // drop metatable - - luaL_openlib(L, 0, methods, 0); // fill methodtable - lua_pop(L, 1); // drop methodtable - - // Cannot be created from Lua - //lua_register(L, className, create_object); } -const char NodeMetaRef::className[] = "NodeMetaRef"; -const luaL_reg NodeMetaRef::methods[] = { +void NodeMetaRef::Register(lua_State *L) +{ + RegisterCommon(L); + luaL_openlib(L, 0, methodsServer, 0); // fill methodtable + lua_pop(L, 1); // drop methodtable +} + + +const luaL_reg NodeMetaRef::methodsServer[] = { luamethod(MetaDataRef, get_string), luamethod(MetaDataRef, set_string), luamethod(MetaDataRef, get_int), @@ -206,3 +227,20 @@ const luaL_reg NodeMetaRef::methods[] = { luamethod(NodeMetaRef, get_inventory), {0,0} }; + + +void NodeMetaRef::RegisterClient(lua_State *L) +{ + RegisterCommon(L); + luaL_openlib(L, 0, methodsClient, 0); // fill methodtable + lua_pop(L, 1); // drop methodtable +} + + +const luaL_reg NodeMetaRef::methodsClient[] = { + luamethod(MetaDataRef, get_string), + luamethod(MetaDataRef, get_int), + luamethod(MetaDataRef, get_float), + luamethod(MetaDataRef, to_table), + {0,0} +}; diff --git a/src/script/lua_api/l_nodemeta.h b/src/script/lua_api/l_nodemeta.h index d03f086c9..6d146416b 100644 --- a/src/script/lua_api/l_nodemeta.h +++ b/src/script/lua_api/l_nodemeta.h @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_base.h" #include "lua_api/l_metadata.h" #include "irrlichttypes_bloated.h" +#include "nodemetadata.h" class ServerEnvironment; class NodeMetadata; @@ -34,9 +35,12 @@ class NodeMetaRef : public MetaDataRef { private: v3s16 m_p; ServerEnvironment *m_env; + Metadata *m_meta; + bool m_is_local; static const char className[]; - static const luaL_reg methods[]; + static const luaL_reg methodsServer[]; + static const luaL_reg methodsClient[]; static NodeMetaRef *checkobject(lua_State *L, int narg); @@ -71,6 +75,7 @@ private: public: NodeMetaRef(v3s16 p, ServerEnvironment *env); + NodeMetaRef(Metadata *meta); ~NodeMetaRef(); @@ -78,7 +83,12 @@ public: // Not callable from Lua; all references are created on the C side. static void create(lua_State *L, v3s16 p, ServerEnvironment *env); + // Client-sided version of the above + static void createClient(lua_State *L, Metadata *meta); + + static void RegisterCommon(lua_State *L); static void Register(lua_State *L); + static void RegisterClient(lua_State *L); }; #endif /* L_NODEMETA_H_ */