]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_nodemeta.h
Get rid of node metadata when it becomes empty
[dragonfireclient.git] / src / script / lua_api / l_nodemeta.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "lua_api/l_base.h"
23 #include "lua_api/l_metadata.h"
24 #include "irrlichttypes_bloated.h"
25 #include "nodemetadata.h"
26
27 class ServerEnvironment;
28 class NodeMetadata;
29
30 /*
31         NodeMetaRef
32 */
33
34 class NodeMetaRef : public MetaDataRef {
35 private:
36         bool m_is_local = false;
37         // Set for server metadata
38         v3s16 m_p;
39         ServerEnvironment *m_env = nullptr;
40         // Set for client metadata
41         Metadata *m_local_meta = nullptr;
42
43         static const char className[];
44         static const luaL_Reg methodsServer[];
45         static const luaL_Reg methodsClient[];
46
47         static NodeMetaRef *checkobject(lua_State *L, int narg);
48
49         /**
50          * Retrieve metadata for a node.
51          * If @p auto_create is set and the specified node has no metadata information
52          * associated with it yet, the method attempts to attach a new metadata object
53          * to the node and returns a pointer to the metadata when successful.
54          *
55          * However, it is NOT guaranteed that the method will return a pointer,
56          * and @c NULL may be returned in case of an error regardless of @p auto_create.
57          *
58          * @param ref specifies the node for which the associated metadata is retrieved.
59          * @param auto_create when true, try to create metadata information for the node if it has none.
60          * @return pointer to a @c NodeMetadata object or @c NULL in case of error.
61          */
62         virtual Metadata* getmeta(bool auto_create);
63         virtual void clearMeta();
64
65         virtual void reportMetadataChange(const std::string *name = nullptr);
66
67         virtual void handleToTable(lua_State *L, Metadata *_meta);
68         virtual bool handleFromTable(lua_State *L, int table, Metadata *_meta);
69
70         // Exported functions
71
72         // garbage collector
73         static int gc_object(lua_State *L);
74
75         // get_inventory(self)
76         static int l_get_inventory(lua_State *L);
77
78         // mark_as_private(self, <string> or {<string>, <string>, ...})
79         static int l_mark_as_private(lua_State *L);
80
81 public:
82         NodeMetaRef(v3s16 p, ServerEnvironment *env);
83         NodeMetaRef(Metadata *meta);
84
85         ~NodeMetaRef() = default;
86
87         // Creates an NodeMetaRef and leaves it on top of stack
88         // Not callable from Lua; all references are created on the C side.
89         static void create(lua_State *L, v3s16 p, ServerEnvironment *env);
90
91         // Client-sided version of the above
92         static void createClient(lua_State *L, Metadata *meta);
93
94         static void RegisterCommon(lua_State *L);
95         static void Register(lua_State *L);
96         static void RegisterClient(lua_State *L);
97 };