]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_nodemeta.cpp
Make sure we get a stacktrace for as many lua errors as possible
[dragonfireclient.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         std::string nodename = ndef->get(node).name;
49         if(!getItemCallback(nodename.c_str(), "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"
65                                 " return a number, guilty node: " + nodename);
66         int num = luaL_checkinteger(L, -1);
67         lua_pop(L, 1); // Pop integer
68         return num;
69 }
70
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)
75 {
76         SCRIPTAPI_PRECHECKHEADER
77
78         lua_pushcfunction(L, script_error_handler);
79         int errorhandler = lua_gettop(L);
80
81         INodeDefManager *ndef = getServer()->ndef();
82
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)
86                 return 0;
87
88         // Push callback function on stack
89         std::string nodename = ndef->get(node).name;
90         if(!getItemCallback(nodename.c_str(), "allow_metadata_inventory_put"))
91                 return stack.count;
92
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))
100                 scriptError();
101         lua_remove(L, errorhandler); // Remove error handler
102         if(!lua_isnumber(L, -1))
103                 throw LuaError(L, "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
107         return num;
108 }
109
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)
114 {
115         SCRIPTAPI_PRECHECKHEADER
116
117         lua_pushcfunction(L, script_error_handler);
118         int errorhandler = lua_gettop(L);
119
120         INodeDefManager *ndef = getServer()->ndef();
121
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)
125                 return 0;
126
127         // Push callback function on stack
128         std::string nodename = ndef->get(node).name;
129         if(!getItemCallback(nodename.c_str(), "allow_metadata_inventory_take"))
130                 return stack.count;
131
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))
139                 scriptError();
140         lua_remove(L, errorhandler); // Remove error handler
141         if(!lua_isnumber(L, -1))
142                 throw LuaError(L, "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
146         return num;
147 }
148
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)
154 {
155         SCRIPTAPI_PRECHECKHEADER
156
157         lua_pushcfunction(L, script_error_handler);
158         int errorhandler = lua_gettop(L);
159
160         INodeDefManager *ndef = getServer()->ndef();
161
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)
165                 return;
166
167         // Push callback function on stack
168         std::string nodename = ndef->get(node).name;
169         if(!getItemCallback(nodename.c_str(), "on_metadata_inventory_move"))
170                 return;
171
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))
181                 scriptError();
182         lua_pop(L, 1); // Pop error handler
183 }
184
185 // Report put items
186 void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
187                 const std::string &listname, int index, ItemStack &stack,
188                 ServerActiveObject *player)
189 {
190         SCRIPTAPI_PRECHECKHEADER
191
192         lua_pushcfunction(L, script_error_handler);
193         int errorhandler = lua_gettop(L);
194
195         INodeDefManager *ndef = getServer()->ndef();
196
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)
200                 return;
201
202         // Push callback function on stack
203         std::string nodename = ndef->get(node).name;
204         if(!getItemCallback(nodename.c_str(), "on_metadata_inventory_put"))
205                 return;
206
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))
214                 scriptError();
215         lua_pop(L, 1); // Pop error handler
216 }
217
218 // Report taken items
219 void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
220                 const std::string &listname, int index, ItemStack &stack,
221                 ServerActiveObject *player)
222 {
223         SCRIPTAPI_PRECHECKHEADER
224
225         lua_pushcfunction(L, script_error_handler);
226         int errorhandler = lua_gettop(L);
227
228         INodeDefManager *ndef = getServer()->ndef();
229
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)
233                 return;
234
235         // Push callback function on stack
236         std::string nodename = ndef->get(node).name;
237         if(!getItemCallback(nodename.c_str(), "on_metadata_inventory_take"))
238                 return;
239
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))
247                 scriptError();
248         lua_pop(L, 1); // Pop error handler
249 }
250
251 ScriptApiNodemeta::ScriptApiNodemeta() {
252 }
253
254 ScriptApiNodemeta::~ScriptApiNodemeta() {
255 }
256
257
258