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 if(!getItemCallback(ndef->get(node).name.c_str(),
49 "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(L, "allow_metadata_inventory_move should return a number");
65 int num = luaL_checkinteger(L, -1);
66 lua_pop(L, 1); // Pop integer
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)
75 SCRIPTAPI_PRECHECKHEADER
77 lua_pushcfunction(L, script_error_handler);
78 int errorhandler = lua_gettop(L);
80 INodeDefManager *ndef = getServer()->ndef();
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)
87 // Push callback function on stack
88 if(!getItemCallback(ndef->get(node).name.c_str(),
89 "allow_metadata_inventory_put"))
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))
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
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)
113 SCRIPTAPI_PRECHECKHEADER
115 lua_pushcfunction(L, script_error_handler);
116 int errorhandler = lua_gettop(L);
118 INodeDefManager *ndef = getServer()->ndef();
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)
125 // Push callback function on stack
126 if(!getItemCallback(ndef->get(node).name.c_str(),
127 "allow_metadata_inventory_take"))
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))
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
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)
152 SCRIPTAPI_PRECHECKHEADER
154 lua_pushcfunction(L, script_error_handler);
155 int errorhandler = lua_gettop(L);
157 INodeDefManager *ndef = getServer()->ndef();
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)
164 // Push callback function on stack
165 if(!getItemCallback(ndef->get(node).name.c_str(),
166 "on_metadata_inventory_move"))
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))
179 lua_pop(L, 1); // Pop error handler
183 void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
184 const std::string &listname, int index, ItemStack &stack,
185 ServerActiveObject *player)
187 SCRIPTAPI_PRECHECKHEADER
189 lua_pushcfunction(L, script_error_handler);
190 int errorhandler = lua_gettop(L);
192 INodeDefManager *ndef = getServer()->ndef();
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)
199 // Push callback function on stack
200 if(!getItemCallback(ndef->get(node).name.c_str(),
201 "on_metadata_inventory_put"))
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))
212 lua_pop(L, 1); // Pop error handler
215 // Report taken items
216 void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
217 const std::string &listname, int index, ItemStack &stack,
218 ServerActiveObject *player)
220 SCRIPTAPI_PRECHECKHEADER
222 lua_pushcfunction(L, script_error_handler);
223 int errorhandler = lua_gettop(L);
225 INodeDefManager *ndef = getServer()->ndef();
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)
232 // Push callback function on stack
233 if(!getItemCallback(ndef->get(node).name.c_str(),
234 "on_metadata_inventory_take"))
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))
245 lua_pop(L, 1); // Pop error handler
248 ScriptApiNodemeta::ScriptApiNodemeta() {
251 ScriptApiNodemeta::~ScriptApiNodemeta() {