]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_inventory.cpp
Revert "Make sure we get a stacktrace for as many lua errors as possible"
[dragonfireclient.git] / src / script / cpp_api / s_inventory.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_inventory.h"
21 #include "cpp_api/s_internal.h"
22 #include "inventorymanager.h"
23 #include "lua_api/l_inventory.h"
24 #include "lua_api/l_item.h"
25 #include "log.h"
26
27 // Return number of accepted items to be moved
28 int ScriptApiDetached::detached_inventory_AllowMove(
29                 const std::string &name,
30                 const std::string &from_list, int from_index,
31                 const std::string &to_list, int to_index,
32                 int count, ServerActiveObject *player)
33 {
34         SCRIPTAPI_PRECHECKHEADER
35
36         lua_pushcfunction(L, script_error_handler);
37         int errorhandler = lua_gettop(L);
38
39         // Push callback function on stack
40         if(!getDetachedInventoryCallback(name, "allow_move"))
41                 return count;
42
43         // function(inv, from_list, from_index, to_list, to_index, count, player)
44         // inv
45         InventoryLocation loc;
46         loc.setDetached(name);
47         InvRef::create(L, loc);
48         lua_pushstring(L, from_list.c_str()); // from_list
49         lua_pushinteger(L, from_index + 1);   // from_index
50         lua_pushstring(L, to_list.c_str());   // to_list
51         lua_pushinteger(L, to_index + 1);     // to_index
52         lua_pushinteger(L, count);            // count
53         objectrefGetOrCreate(player);         // player
54         if(lua_pcall(L, 7, 1, errorhandler))
55                 scriptError();
56         if(!lua_isnumber(L, -1))
57                 throw LuaError(NULL, "allow_move should return a number");
58         int ret = luaL_checkinteger(L, -1);
59         lua_pop(L, 2); // Pop integer and error handler
60         return ret;
61 }
62
63 // Return number of accepted items to be put
64 int ScriptApiDetached::detached_inventory_AllowPut(
65                 const std::string &name,
66                 const std::string &listname, int index, ItemStack &stack,
67                 ServerActiveObject *player)
68 {
69         SCRIPTAPI_PRECHECKHEADER
70
71         lua_pushcfunction(L, script_error_handler);
72         int errorhandler = lua_gettop(L);
73
74         // Push callback function on stack
75         if(!getDetachedInventoryCallback(name, "allow_put"))
76                 return stack.count; // All will be accepted
77
78         // Call function(inv, listname, index, stack, player)
79         InventoryLocation loc;
80         loc.setDetached(name);
81         InvRef::create(L, loc);              // inv
82         lua_pushstring(L, listname.c_str()); // listname
83         lua_pushinteger(L, index + 1);       // index
84         LuaItemStack::create(L, stack);      // stack
85         objectrefGetOrCreate(player);        // player
86         if(lua_pcall(L, 5, 1, errorhandler))
87                 scriptError();
88         if(!lua_isnumber(L, -1))
89                 throw LuaError(NULL, "allow_put should return a number");
90         int ret = luaL_checkinteger(L, -1);
91         lua_pop(L, 2); // Pop integer and error handler
92         return ret;
93 }
94
95 // Return number of accepted items to be taken
96 int ScriptApiDetached::detached_inventory_AllowTake(
97                 const std::string &name,
98                 const std::string &listname, int index, ItemStack &stack,
99                 ServerActiveObject *player)
100 {
101         SCRIPTAPI_PRECHECKHEADER
102
103         lua_pushcfunction(L, script_error_handler);
104         int errorhandler = lua_gettop(L);
105
106         // Push callback function on stack
107         if(!getDetachedInventoryCallback(name, "allow_take"))
108                 return stack.count; // All will be accepted
109
110         // Call function(inv, listname, index, stack, player)
111         InventoryLocation loc;
112         loc.setDetached(name);
113         InvRef::create(L, loc);              // inv
114         lua_pushstring(L, listname.c_str()); // listname
115         lua_pushinteger(L, index + 1);       // index
116         LuaItemStack::create(L, stack);      // stack
117         objectrefGetOrCreate(player);        // player
118         if(lua_pcall(L, 5, 1, errorhandler))
119                 scriptError();
120         if(!lua_isnumber(L, -1))
121                 throw LuaError(NULL, "allow_take should return a number");
122         int ret = luaL_checkinteger(L, -1);
123         lua_pop(L, 2); // Pop integer and error handler
124         return ret;
125 }
126
127 // Report moved items
128 void ScriptApiDetached::detached_inventory_OnMove(
129                 const std::string &name,
130                 const std::string &from_list, int from_index,
131                 const std::string &to_list, int to_index,
132                 int count, ServerActiveObject *player)
133 {
134         SCRIPTAPI_PRECHECKHEADER
135
136         lua_pushcfunction(L, script_error_handler);
137         int errorhandler = lua_gettop(L);
138
139         // Push callback function on stack
140         if(!getDetachedInventoryCallback(name, "on_move"))
141                 return;
142
143         // function(inv, from_list, from_index, to_list, to_index, count, player)
144         // inv
145         InventoryLocation loc;
146         loc.setDetached(name);
147         InvRef::create(L, loc);
148         lua_pushstring(L, from_list.c_str()); // from_list
149         lua_pushinteger(L, from_index + 1);   // from_index
150         lua_pushstring(L, to_list.c_str());   // to_list
151         lua_pushinteger(L, to_index + 1);     // to_index
152         lua_pushinteger(L, count);            // count
153         objectrefGetOrCreate(player);         // player
154         if(lua_pcall(L, 7, 0, errorhandler))
155                 scriptError();
156         lua_pop(L, 1); // Pop error handler
157 }
158
159 // Report put items
160 void ScriptApiDetached::detached_inventory_OnPut(
161                 const std::string &name,
162                 const std::string &listname, int index, ItemStack &stack,
163                 ServerActiveObject *player)
164 {
165         SCRIPTAPI_PRECHECKHEADER
166
167         lua_pushcfunction(L, script_error_handler);
168         int errorhandler = lua_gettop(L);
169
170         // Push callback function on stack
171         if(!getDetachedInventoryCallback(name, "on_put"))
172                 return;
173
174         // Call function(inv, listname, index, stack, player)
175         // inv
176         InventoryLocation loc;
177         loc.setDetached(name);
178         InvRef::create(L, loc);
179         lua_pushstring(L, listname.c_str()); // listname
180         lua_pushinteger(L, index + 1);       // index
181         LuaItemStack::create(L, stack);      // stack
182         objectrefGetOrCreate(player);        // player
183         if(lua_pcall(L, 5, 0, errorhandler))
184                 scriptError();
185         lua_pop(L, 1); // Pop error handler
186 }
187
188 // Report taken items
189 void ScriptApiDetached::detached_inventory_OnTake(
190                 const std::string &name,
191                 const std::string &listname, int index, ItemStack &stack,
192                 ServerActiveObject *player)
193 {
194         SCRIPTAPI_PRECHECKHEADER
195
196         lua_pushcfunction(L, script_error_handler);
197         int errorhandler = lua_gettop(L);
198
199         // Push callback function on stack
200         if(!getDetachedInventoryCallback(name, "on_take"))
201                 return;
202
203         // Call function(inv, listname, index, stack, player)
204         // inv
205         InventoryLocation loc;
206         loc.setDetached(name);
207         InvRef::create(L, loc);
208         lua_pushstring(L, listname.c_str()); // listname
209         lua_pushinteger(L, index + 1);       // index
210         LuaItemStack::create(L, stack);      // stack
211         objectrefGetOrCreate(player);        // player
212         if(lua_pcall(L, 5, 0, errorhandler))
213                 scriptError();
214         lua_pop(L, 1); // Pop error handler
215 }
216
217 // Retrieves minetest.detached_inventories[name][callbackname]
218 // If that is nil or on error, return false and stack is unchanged
219 // If that is a function, returns true and pushes the
220 // function onto the stack
221 bool ScriptApiDetached::getDetachedInventoryCallback(
222                 const std::string &name, const char *callbackname)
223 {
224         lua_State *L = getStack();
225
226         lua_getglobal(L, "minetest");
227         lua_getfield(L, -1, "detached_inventories");
228         lua_remove(L, -2);
229         luaL_checktype(L, -1, LUA_TTABLE);
230         lua_getfield(L, -1, name.c_str());
231         lua_remove(L, -2);
232         // Should be a table
233         if(lua_type(L, -1) != LUA_TTABLE)
234         {
235                 errorstream<<"Detached inventory \""<<name<<"\" not defined"<<std::endl;
236                 lua_pop(L, 1);
237                 return false;
238         }
239         lua_getfield(L, -1, callbackname);
240         lua_remove(L, -2);
241         // Should be a function or nil
242         if(lua_type(L, -1) == LUA_TFUNCTION)
243         {
244                 return true;
245         }
246         else if(lua_isnil(L, -1))
247         {
248                 lua_pop(L, 1);
249                 return false;
250         }
251         else
252         {
253                 errorstream<<"Detached inventory \""<<name<<"\" callback \""
254                         <<callbackname<<"\" is not a function"<<std::endl;
255                 lua_pop(L, 1);
256                 return false;
257         }
258 }