]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_nodemeta.cpp
Pass a errfunc to lua_pcall to get a traceback
[minetest.git] / src / script / cpp_api / s_nodemeta.cpp
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 #include "cpp_api/s_nodemeta.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23 #include "nodedef.h"
24 #include "mapnode.h"
25 #include "server.h"
26 #include "environment.h"
27 #include "lua_api/l_item.h"
28
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)
34 {
35         SCRIPTAPI_PRECHECKHEADER
36
37         lua_pushcfunction(L, script_error_handler);
38         int errorhandler = lua_gettop(L);
39
40         INodeDefManager *ndef = getServer()->ndef();
41
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)
45                 return 0;
46
47         // Push callback function on stack
48         if(!getItemCallback(ndef->get(node).name.c_str(),
49                         "allow_metadata_inventory_move"))
50                 return count;
51
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))
61                 scriptError();
62         lua_remove(L, errorhandler); // Remove error handler
63         if(!lua_isnumber(L, -1))
64                 throw LuaError(L, "allow_metadata_inventory_move should return a number");
65         int num = luaL_checkinteger(L, -1);
66         lua_pop(L, 1); // Pop integer
67         return num;
68 }
69
70 // Return number of accepted items to be put
71 int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
72                 const std::string &listname, int index, ItemStack &stack,
73                 ServerActiveObject *player)
74 {
75         SCRIPTAPI_PRECHECKHEADER
76
77         lua_pushcfunction(L, script_error_handler);
78         int errorhandler = lua_gettop(L);
79
80         INodeDefManager *ndef = getServer()->ndef();
81
82         // If node doesn't exist, we don't know what callback to call
83         MapNode node = getEnv()->getMap().getNodeNoEx(p);
84         if(node.getContent() == CONTENT_IGNORE)
85                 return 0;
86
87         // Push callback function on stack
88         if(!getItemCallback(ndef->get(node).name.c_str(),
89                         "allow_metadata_inventory_put"))
90                 return stack.count;
91
92         // Call function(pos, listname, index, stack, player)
93         push_v3s16(L, p);                    // pos
94         lua_pushstring(L, listname.c_str()); // listname
95         lua_pushinteger(L, index + 1);       // index
96         LuaItemStack::create(L, stack);      // stack
97         objectrefGetOrCreate(player);        // player
98         if(lua_pcall(L, 5, 1, errorhandler))
99                 scriptError();
100         lua_remove(L, errorhandler); // Remove error handler
101         if(!lua_isnumber(L, -1))
102                 throw LuaError(L, "allow_metadata_inventory_put should return a number");
103         int num = luaL_checkinteger(L, -1);
104         lua_pop(L, 1); // Pop integer
105         return num;
106 }
107
108 // Return number of accepted items to be taken
109 int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
110                 const std::string &listname, int index, ItemStack &stack,
111                 ServerActiveObject *player)
112 {
113         SCRIPTAPI_PRECHECKHEADER
114
115         lua_pushcfunction(L, script_error_handler);
116         int errorhandler = lua_gettop(L);
117
118         INodeDefManager *ndef = getServer()->ndef();
119
120         // If node doesn't exist, we don't know what callback to call
121         MapNode node = getEnv()->getMap().getNodeNoEx(p);
122         if(node.getContent() == CONTENT_IGNORE)
123                 return 0;
124
125         // Push callback function on stack
126         if(!getItemCallback(ndef->get(node).name.c_str(),
127                         "allow_metadata_inventory_take"))
128                 return stack.count;
129
130         // Call function(pos, listname, index, count, player)
131         push_v3s16(L, p);                    // pos
132         lua_pushstring(L, listname.c_str()); // listname
133         lua_pushinteger(L, index + 1);       // index
134         LuaItemStack::create(L, stack);      // stack
135         objectrefGetOrCreate(player);        // player
136         if(lua_pcall(L, 5, 1, errorhandler))
137                 scriptError();
138         lua_remove(L, errorhandler); // Remove error handler
139         if(!lua_isnumber(L, -1))
140                 throw LuaError(L, "allow_metadata_inventory_take should return a number");
141         int num = luaL_checkinteger(L, -1);
142         lua_pop(L, 1); // Pop integer
143         return num;
144 }
145
146 // Report moved items
147 void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
148                 const std::string &from_list, int from_index,
149                 const std::string &to_list, int to_index,
150                 int count, ServerActiveObject *player)
151 {
152         SCRIPTAPI_PRECHECKHEADER
153
154         lua_pushcfunction(L, script_error_handler);
155         int errorhandler = lua_gettop(L);
156
157         INodeDefManager *ndef = getServer()->ndef();
158
159         // If node doesn't exist, we don't know what callback to call
160         MapNode node = getEnv()->getMap().getNodeNoEx(p);
161         if(node.getContent() == CONTENT_IGNORE)
162                 return;
163
164         // Push callback function on stack
165         if(!getItemCallback(ndef->get(node).name.c_str(),
166                         "on_metadata_inventory_move"))
167                 return;
168
169         // function(pos, from_list, from_index, to_list, to_index, count, player)
170         push_v3s16(L, p);                     // pos
171         lua_pushstring(L, from_list.c_str()); // from_list
172         lua_pushinteger(L, from_index + 1);   // from_index
173         lua_pushstring(L, to_list.c_str());   // to_list
174         lua_pushinteger(L, to_index + 1);     // to_index
175         lua_pushinteger(L, count);            // count
176         objectrefGetOrCreate(player);         // player
177         if(lua_pcall(L, 7, 0, errorhandler))
178                 scriptError();
179         lua_pop(L, 1); // Pop error handler
180 }
181
182 // Report put items
183 void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
184                 const std::string &listname, int index, ItemStack &stack,
185                 ServerActiveObject *player)
186 {
187         SCRIPTAPI_PRECHECKHEADER
188
189         lua_pushcfunction(L, script_error_handler);
190         int errorhandler = lua_gettop(L);
191
192         INodeDefManager *ndef = getServer()->ndef();
193
194         // If node doesn't exist, we don't know what callback to call
195         MapNode node = getEnv()->getMap().getNodeNoEx(p);
196         if(node.getContent() == CONTENT_IGNORE)
197                 return;
198
199         // Push callback function on stack
200         if(!getItemCallback(ndef->get(node).name.c_str(),
201                         "on_metadata_inventory_put"))
202                 return;
203
204         // Call function(pos, listname, index, stack, player)
205         push_v3s16(L, p);                    // pos
206         lua_pushstring(L, listname.c_str()); // listname
207         lua_pushinteger(L, index + 1);       // index
208         LuaItemStack::create(L, stack);      // stack
209         objectrefGetOrCreate(player);        // player
210         if(lua_pcall(L, 5, 0, errorhandler))
211                 scriptError();
212         lua_pop(L, 1); // Pop error handler
213 }
214
215 // Report taken items
216 void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
217                 const std::string &listname, int index, ItemStack &stack,
218                 ServerActiveObject *player)
219 {
220         SCRIPTAPI_PRECHECKHEADER
221
222         lua_pushcfunction(L, script_error_handler);
223         int errorhandler = lua_gettop(L);
224
225         INodeDefManager *ndef = getServer()->ndef();
226
227         // If node doesn't exist, we don't know what callback to call
228         MapNode node = getEnv()->getMap().getNodeNoEx(p);
229         if(node.getContent() == CONTENT_IGNORE)
230                 return;
231
232         // Push callback function on stack
233         if(!getItemCallback(ndef->get(node).name.c_str(),
234                         "on_metadata_inventory_take"))
235                 return;
236
237         // Call function(pos, listname, index, stack, player)
238         push_v3s16(L, p);                    // pos
239         lua_pushstring(L, listname.c_str()); // listname
240         lua_pushinteger(L, index + 1);       // index
241         LuaItemStack::create(L, stack);      // stack
242         objectrefGetOrCreate(player);        // player
243         if(lua_pcall(L, 5, 0, errorhandler))
244                 scriptError();
245         lua_pop(L, 1); // Pop error handler
246 }
247
248 ScriptApiNodemeta::ScriptApiNodemeta() {
249 }
250
251 ScriptApiNodemeta::~ScriptApiNodemeta() {
252 }
253
254
255