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 "cpp_api/s_nodemeta.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
26 #include "environment.h"
27 #include "lua_api/l_item.h"
29 // Return number of accepted items to be moved
30 int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,
31 const std::string &from_list, int from_index,
32 const std::string &to_list, int to_index,
33 int count, ServerActiveObject *player)
35 SCRIPTAPI_PRECHECKHEADER
37 lua_pushcfunction(L, script_error_handler);
38 int errorhandler = lua_gettop(L);
40 INodeDefManager *ndef = getServer()->ndef();
42 // If node doesn't exist, we don't know what callback to call
43 MapNode node = getEnv()->getMap().getNodeNoEx(p);
44 if(node.getContent() == CONTENT_IGNORE)
47 // Push callback function on stack
48 std::string nodename = ndef->get(node).name;
49 if(!getItemCallback(nodename.c_str(), "allow_metadata_inventory_move"))
52 // function(pos, from_list, from_index, to_list, to_index, count, player)
53 push_v3s16(L, p); // pos
54 lua_pushstring(L, from_list.c_str()); // from_list
55 lua_pushinteger(L, from_index + 1); // from_index
56 lua_pushstring(L, to_list.c_str()); // to_list
57 lua_pushinteger(L, to_index + 1); // to_index
58 lua_pushinteger(L, count); // count
59 objectrefGetOrCreate(player); // player
60 if(lua_pcall(L, 7, 1, errorhandler))
62 lua_remove(L, errorhandler); // Remove error handler
63 if(!lua_isnumber(L, -1))
64 throw LuaError(NULL, "allow_metadata_inventory_move should"
65 " return a number, guilty node: " + nodename);
66 int num = luaL_checkinteger(L, -1);
67 lua_pop(L, 1); // Pop integer
71 // Return number of accepted items to be put
72 int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
73 const std::string &listname, int index, ItemStack &stack,
74 ServerActiveObject *player)
76 SCRIPTAPI_PRECHECKHEADER
78 lua_pushcfunction(L, script_error_handler);
79 int errorhandler = lua_gettop(L);
81 INodeDefManager *ndef = getServer()->ndef();
83 // If node doesn't exist, we don't know what callback to call
84 MapNode node = getEnv()->getMap().getNodeNoEx(p);
85 if(node.getContent() == CONTENT_IGNORE)
88 // Push callback function on stack
89 std::string nodename = ndef->get(node).name;
90 if(!getItemCallback(nodename.c_str(), "allow_metadata_inventory_put"))
93 // Call function(pos, listname, index, stack, player)
94 push_v3s16(L, p); // pos
95 lua_pushstring(L, listname.c_str()); // listname
96 lua_pushinteger(L, index + 1); // index
97 LuaItemStack::create(L, stack); // stack
98 objectrefGetOrCreate(player); // player
99 if(lua_pcall(L, 5, 1, errorhandler))
101 lua_remove(L, errorhandler); // Remove error handler
102 if(!lua_isnumber(L, -1))
103 throw LuaError(NULL, "allow_metadata_inventory_put should"
104 " return a number, guilty node: " + nodename);
105 int num = luaL_checkinteger(L, -1);
106 lua_pop(L, 1); // Pop integer
110 // Return number of accepted items to be taken
111 int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
112 const std::string &listname, int index, ItemStack &stack,
113 ServerActiveObject *player)
115 SCRIPTAPI_PRECHECKHEADER
117 lua_pushcfunction(L, script_error_handler);
118 int errorhandler = lua_gettop(L);
120 INodeDefManager *ndef = getServer()->ndef();
122 // If node doesn't exist, we don't know what callback to call
123 MapNode node = getEnv()->getMap().getNodeNoEx(p);
124 if(node.getContent() == CONTENT_IGNORE)
127 // Push callback function on stack
128 std::string nodename = ndef->get(node).name;
129 if(!getItemCallback(nodename.c_str(), "allow_metadata_inventory_take"))
132 // Call function(pos, listname, index, count, player)
133 push_v3s16(L, p); // pos
134 lua_pushstring(L, listname.c_str()); // listname
135 lua_pushinteger(L, index + 1); // index
136 LuaItemStack::create(L, stack); // stack
137 objectrefGetOrCreate(player); // player
138 if(lua_pcall(L, 5, 1, errorhandler))
140 lua_remove(L, errorhandler); // Remove error handler
141 if(!lua_isnumber(L, -1))
142 throw LuaError(NULL, "allow_metadata_inventory_take should"
143 " return a number, guilty node: " + nodename);
144 int num = luaL_checkinteger(L, -1);
145 lua_pop(L, 1); // Pop integer
149 // Report moved items
150 void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
151 const std::string &from_list, int from_index,
152 const std::string &to_list, int to_index,
153 int count, ServerActiveObject *player)
155 SCRIPTAPI_PRECHECKHEADER
157 lua_pushcfunction(L, script_error_handler);
158 int errorhandler = lua_gettop(L);
160 INodeDefManager *ndef = getServer()->ndef();
162 // If node doesn't exist, we don't know what callback to call
163 MapNode node = getEnv()->getMap().getNodeNoEx(p);
164 if(node.getContent() == CONTENT_IGNORE)
167 // Push callback function on stack
168 std::string nodename = ndef->get(node).name;
169 if(!getItemCallback(nodename.c_str(), "on_metadata_inventory_move"))
172 // function(pos, from_list, from_index, to_list, to_index, count, player)
173 push_v3s16(L, p); // pos
174 lua_pushstring(L, from_list.c_str()); // from_list
175 lua_pushinteger(L, from_index + 1); // from_index
176 lua_pushstring(L, to_list.c_str()); // to_list
177 lua_pushinteger(L, to_index + 1); // to_index
178 lua_pushinteger(L, count); // count
179 objectrefGetOrCreate(player); // player
180 if(lua_pcall(L, 7, 0, errorhandler))
182 lua_pop(L, 1); // Pop error handler
186 void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
187 const std::string &listname, int index, ItemStack &stack,
188 ServerActiveObject *player)
190 SCRIPTAPI_PRECHECKHEADER
192 lua_pushcfunction(L, script_error_handler);
193 int errorhandler = lua_gettop(L);
195 INodeDefManager *ndef = getServer()->ndef();
197 // If node doesn't exist, we don't know what callback to call
198 MapNode node = getEnv()->getMap().getNodeNoEx(p);
199 if(node.getContent() == CONTENT_IGNORE)
202 // Push callback function on stack
203 std::string nodename = ndef->get(node).name;
204 if(!getItemCallback(nodename.c_str(), "on_metadata_inventory_put"))
207 // Call function(pos, listname, index, stack, player)
208 push_v3s16(L, p); // pos
209 lua_pushstring(L, listname.c_str()); // listname
210 lua_pushinteger(L, index + 1); // index
211 LuaItemStack::create(L, stack); // stack
212 objectrefGetOrCreate(player); // player
213 if(lua_pcall(L, 5, 0, errorhandler))
215 lua_pop(L, 1); // Pop error handler
218 // Report taken items
219 void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
220 const std::string &listname, int index, ItemStack &stack,
221 ServerActiveObject *player)
223 SCRIPTAPI_PRECHECKHEADER
225 lua_pushcfunction(L, script_error_handler);
226 int errorhandler = lua_gettop(L);
228 INodeDefManager *ndef = getServer()->ndef();
230 // If node doesn't exist, we don't know what callback to call
231 MapNode node = getEnv()->getMap().getNodeNoEx(p);
232 if(node.getContent() == CONTENT_IGNORE)
235 // Push callback function on stack
236 std::string nodename = ndef->get(node).name;
237 if(!getItemCallback(nodename.c_str(), "on_metadata_inventory_take"))
240 // Call function(pos, listname, index, stack, player)
241 push_v3s16(L, p); // pos
242 lua_pushstring(L, listname.c_str()); // listname
243 lua_pushinteger(L, index + 1); // index
244 LuaItemStack::create(L, stack); // stack
245 objectrefGetOrCreate(player); // player
246 if(lua_pcall(L, 5, 0, errorhandler))
248 lua_pop(L, 1); // Pop error handler
251 ScriptApiNodemeta::ScriptApiNodemeta() {
254 ScriptApiNodemeta::~ScriptApiNodemeta() {