3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "lua_api/l_nodemeta.h"
21 #include "lua_api/l_internal.h"
22 #include "lua_api/l_inventory.h"
23 #include "common/c_content.h"
24 #include "serverenvironment.h"
33 IMetadata* NodeMetaRef::getmeta(bool auto_create)
38 NodeMetadata *meta = m_env->getMap().getNodeMetadata(m_p);
39 if (meta == NULL && auto_create) {
40 meta = new NodeMetadata(m_env->getGameDef()->idef());
41 if (!m_env->getMap().setNodeMetadata(m_p, meta)) {
49 void NodeMetaRef::clearMeta()
51 SANITY_CHECK(!m_is_local);
52 m_env->getMap().removeNodeMetadata(m_p);
55 void NodeMetaRef::reportMetadataChange(const std::string *name)
57 SANITY_CHECK(!m_is_local);
58 // Inform other things that the metadata has changed
59 NodeMetadata *meta = dynamic_cast<NodeMetadata*>(getmeta(false));
61 // If the metadata is now empty, get rid of it
62 if (meta && meta->empty()) {
68 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
70 event.is_private_change = name && meta && meta->isPrivate(*name);
71 m_env->getMap().dispatchEvent(event);
76 // get_inventory(self)
77 int NodeMetaRef::l_get_inventory(lua_State *L)
81 NodeMetaRef *ref = checkObject<NodeMetaRef>(L, 1);
82 ref->getmeta(true); // try to ensure the metadata exists
84 InventoryLocation loc;
85 loc.setNodeMeta(ref->m_p);
86 InvRef::create(L, loc);
90 // mark_as_private(self, <string> or {<string>, <string>, ...})
91 int NodeMetaRef::l_mark_as_private(lua_State *L)
95 NodeMetaRef *ref = checkObject<NodeMetaRef>(L, 1);
96 NodeMetadata *meta = dynamic_cast<NodeMetadata*>(ref->getmeta(true));
99 if (lua_istable(L, 2)) {
101 while (lua_next(L, 2) != 0) {
102 // key at index -2 and value at index -1
103 luaL_checktype(L, -1, LUA_TSTRING);
104 meta->markPrivate(readParam<std::string>(L, -1), true);
105 // removes value, keeps key for next iteration
108 } else if (lua_isstring(L, 2)) {
109 meta->markPrivate(readParam<std::string>(L, 2), true);
111 ref->reportMetadataChange();
116 void NodeMetaRef::handleToTable(lua_State *L, IMetadata *_meta)
119 MetaDataRef::handleToTable(L, _meta);
121 NodeMetadata *meta = dynamic_cast<NodeMetadata*>(_meta);
125 Inventory *inv = meta->getInventory();
127 push_inventory_lists(L, *inv);
131 lua_setfield(L, -2, "inventory");
134 // from_table(self, table)
135 bool NodeMetaRef::handleFromTable(lua_State *L, int table, IMetadata *_meta)
138 if (!MetaDataRef::handleFromTable(L, table, _meta))
141 NodeMetadata *meta = dynamic_cast<NodeMetadata*>(_meta);
145 Inventory *inv = meta->getInventory();
146 lua_getfield(L, table, "inventory");
147 if (lua_istable(L, -1)) {
148 int inventorytable = lua_gettop(L);
150 while (lua_next(L, inventorytable) != 0) {
151 // key at index -2 and value at index -1
152 std::string name = luaL_checkstring(L, -2);
153 read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
154 lua_pop(L, 1); // Remove value, keep key for next iteration
163 NodeMetaRef::NodeMetaRef(v3s16 p, ServerEnvironment *env):
169 NodeMetaRef::NodeMetaRef(IMetadata *meta):
175 // Creates an NodeMetaRef and leaves it on top of stack
176 // Not callable from Lua; all references are created on the C side.
177 void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
179 NodeMetaRef *o = new NodeMetaRef(p, env);
180 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
181 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
182 luaL_getmetatable(L, className);
183 lua_setmetatable(L, -2);
186 // Client-sided version of the above
187 void NodeMetaRef::createClient(lua_State *L, IMetadata *meta)
189 NodeMetaRef *o = new NodeMetaRef(meta);
190 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
191 luaL_getmetatable(L, className);
192 lua_setmetatable(L, -2);
195 const char NodeMetaRef::className[] = "NodeMetaRef";
197 void NodeMetaRef::Register(lua_State *L)
199 registerMetadataClass(L, className, methodsServer);
203 const luaL_Reg NodeMetaRef::methodsServer[] = {
204 luamethod(MetaDataRef, contains),
205 luamethod(MetaDataRef, get),
206 luamethod(MetaDataRef, get_string),
207 luamethod(MetaDataRef, set_string),
208 luamethod(MetaDataRef, get_int),
209 luamethod(MetaDataRef, set_int),
210 luamethod(MetaDataRef, get_float),
211 luamethod(MetaDataRef, set_float),
212 luamethod(MetaDataRef, get_keys),
213 luamethod(MetaDataRef, to_table),
214 luamethod(MetaDataRef, from_table),
215 luamethod(NodeMetaRef, get_inventory),
216 luamethod(NodeMetaRef, mark_as_private),
217 luamethod(MetaDataRef, equals),
222 void NodeMetaRef::RegisterClient(lua_State *L)
224 registerMetadataClass(L, className, methodsClient);
228 const luaL_Reg NodeMetaRef::methodsClient[] = {
229 luamethod(MetaDataRef, contains),
230 luamethod(MetaDataRef, get),
231 luamethod(MetaDataRef, get_string),
232 luamethod(MetaDataRef, get_int),
233 luamethod(MetaDataRef, get_float),
234 luamethod(MetaDataRef, get_keys),
235 luamethod(MetaDataRef, to_table),