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_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"
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)
34 SCRIPTAPI_PRECHECKHEADER
36 lua_pushcfunction(L, script_error_handler);
37 int errorhandler = lua_gettop(L);
39 // Push callback function on stack
40 if(!getDetachedInventoryCallback(name, "allow_move"))
43 // function(inv, from_list, from_index, to_list, to_index, count, player)
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))
56 if(!lua_isnumber(L, -1))
57 throw LuaError("allow_move should return a number");
58 int ret = luaL_checkinteger(L, -1);
59 lua_pop(L, 2); // Pop integer and error handler
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)
69 SCRIPTAPI_PRECHECKHEADER
71 lua_pushcfunction(L, script_error_handler);
72 int errorhandler = lua_gettop(L);
74 // Push callback function on stack
75 if(!getDetachedInventoryCallback(name, "allow_put"))
76 return stack.count; // All will be accepted
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))
88 if(!lua_isnumber(L, -1))
89 throw LuaError("allow_put should return a number");
90 int ret = luaL_checkinteger(L, -1);
91 lua_pop(L, 2); // Pop integer and error handler
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)
101 SCRIPTAPI_PRECHECKHEADER
103 lua_pushcfunction(L, script_error_handler);
104 int errorhandler = lua_gettop(L);
106 // Push callback function on stack
107 if(!getDetachedInventoryCallback(name, "allow_take"))
108 return stack.count; // All will be accepted
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))
120 if(!lua_isnumber(L, -1))
121 throw LuaError("allow_take should return a number");
122 int ret = luaL_checkinteger(L, -1);
123 lua_pop(L, 2); // Pop integer and error handler
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)
134 SCRIPTAPI_PRECHECKHEADER
136 lua_pushcfunction(L, script_error_handler);
137 int errorhandler = lua_gettop(L);
139 // Push callback function on stack
140 if(!getDetachedInventoryCallback(name, "on_move"))
143 // function(inv, from_list, from_index, to_list, to_index, count, player)
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))
156 lua_pop(L, 1); // Pop error handler
160 void ScriptApiDetached::detached_inventory_OnPut(
161 const std::string &name,
162 const std::string &listname, int index, ItemStack &stack,
163 ServerActiveObject *player)
165 SCRIPTAPI_PRECHECKHEADER
167 lua_pushcfunction(L, script_error_handler);
168 int errorhandler = lua_gettop(L);
170 // Push callback function on stack
171 if(!getDetachedInventoryCallback(name, "on_put"))
174 // Call function(inv, listname, index, stack, player)
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))
185 lua_pop(L, 1); // Pop error handler
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)
194 SCRIPTAPI_PRECHECKHEADER
196 lua_pushcfunction(L, script_error_handler);
197 int errorhandler = lua_gettop(L);
199 // Push callback function on stack
200 if(!getDetachedInventoryCallback(name, "on_take"))
203 // Call function(inv, listname, index, stack, player)
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))
214 lua_pop(L, 1); // Pop error handler
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)
224 lua_State *L = getStack();
226 lua_getglobal(L, "minetest");
227 lua_getfield(L, -1, "detached_inventories");
229 luaL_checktype(L, -1, LUA_TTABLE);
230 lua_getfield(L, -1, name.c_str());
233 if(lua_type(L, -1) != LUA_TTABLE)
235 errorstream<<"Detached inventory \""<<name<<"\" not defined"<<std::endl;
239 lua_getfield(L, -1, callbackname);
241 // Should be a function or nil
242 if(lua_type(L, -1) == LUA_TFUNCTION)
246 else if(lua_isnil(L, -1))
253 errorstream<<"Detached inventory \""<<name<<"\" callback \""
254 <<callbackname<<"\" is not a function"<<std::endl;