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 INodeDefManager *ndef = getServer()->ndef();
39 // If node doesn't exist, we don't know what callback to call
40 MapNode node = getEnv()->getMap().getNodeNoEx(p);
41 if(node.getContent() == CONTENT_IGNORE)
44 // Push callback function on stack
45 if(!getItemCallback(ndef->get(node).name.c_str(),
46 "allow_metadata_inventory_move"))
49 // function(pos, from_list, from_index, to_list, to_index, count, player)
53 lua_pushstring(L, from_list.c_str());
55 lua_pushinteger(L, from_index + 1);
57 lua_pushstring(L, to_list.c_str());
59 lua_pushinteger(L, to_index + 1);
61 lua_pushinteger(L, count);
63 objectrefGetOrCreate(player);
64 if(lua_pcall(L, 7, 1, 0))
65 scriptError("error: %s", lua_tostring(L, -1));
66 if(!lua_isnumber(L, -1))
67 throw LuaError(L, "allow_metadata_inventory_move should return a number");
68 return luaL_checkinteger(L, -1);
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)
76 SCRIPTAPI_PRECHECKHEADER
78 INodeDefManager *ndef = getServer()->ndef();
80 // If node doesn't exist, we don't know what callback to call
81 MapNode node = getEnv()->getMap().getNodeNoEx(p);
82 if(node.getContent() == CONTENT_IGNORE)
85 // Push callback function on stack
86 if(!getItemCallback(ndef->get(node).name.c_str(),
87 "allow_metadata_inventory_put"))
90 // Call function(pos, listname, index, stack, player)
94 lua_pushstring(L, listname.c_str());
96 lua_pushinteger(L, index + 1);
98 LuaItemStack::create(L, stack);
100 objectrefGetOrCreate(player);
101 if(lua_pcall(L, 5, 1, 0))
102 scriptError("error: %s", lua_tostring(L, -1));
103 if(!lua_isnumber(L, -1))
104 throw LuaError(L, "allow_metadata_inventory_put should return a number");
105 return luaL_checkinteger(L, -1);
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 INodeDefManager *ndef = getServer()->ndef();
117 // If node doesn't exist, we don't know what callback to call
118 MapNode node = getEnv()->getMap().getNodeNoEx(p);
119 if(node.getContent() == CONTENT_IGNORE)
122 // Push callback function on stack
123 if(!getItemCallback(ndef->get(node).name.c_str(),
124 "allow_metadata_inventory_take"))
127 // Call function(pos, listname, index, count, player)
131 lua_pushstring(L, listname.c_str());
133 lua_pushinteger(L, index + 1);
135 LuaItemStack::create(L, stack);
137 objectrefGetOrCreate(player);
138 if(lua_pcall(L, 5, 1, 0))
139 scriptError("error: %s", lua_tostring(L, -1));
140 if(!lua_isnumber(L, -1))
141 throw LuaError(L, "allow_metadata_inventory_take should return a number");
142 return luaL_checkinteger(L, -1);
145 // Report moved items
146 void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
147 const std::string &from_list, int from_index,
148 const std::string &to_list, int to_index,
149 int count, ServerActiveObject *player)
151 SCRIPTAPI_PRECHECKHEADER
153 INodeDefManager *ndef = getServer()->ndef();
155 // If node doesn't exist, we don't know what callback to call
156 MapNode node = getEnv()->getMap().getNodeNoEx(p);
157 if(node.getContent() == CONTENT_IGNORE)
160 // Push callback function on stack
161 if(!getItemCallback(ndef->get(node).name.c_str(),
162 "on_metadata_inventory_move"))
165 // function(pos, from_list, from_index, to_list, to_index, count, player)
169 lua_pushstring(L, from_list.c_str());
171 lua_pushinteger(L, from_index + 1);
173 lua_pushstring(L, to_list.c_str());
175 lua_pushinteger(L, to_index + 1);
177 lua_pushinteger(L, count);
179 objectrefGetOrCreate(player);
180 if(lua_pcall(L, 7, 0, 0))
181 scriptError("error: %s", lua_tostring(L, -1));
185 void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
186 const std::string &listname, int index, ItemStack &stack,
187 ServerActiveObject *player)
189 SCRIPTAPI_PRECHECKHEADER
191 INodeDefManager *ndef = getServer()->ndef();
193 // If node doesn't exist, we don't know what callback to call
194 MapNode node = getEnv()->getMap().getNodeNoEx(p);
195 if(node.getContent() == CONTENT_IGNORE)
198 // Push callback function on stack
199 if(!getItemCallback(ndef->get(node).name.c_str(),
200 "on_metadata_inventory_put"))
203 // Call function(pos, listname, index, stack, player)
207 lua_pushstring(L, listname.c_str());
209 lua_pushinteger(L, index + 1);
211 LuaItemStack::create(L, stack);
213 objectrefGetOrCreate(player);
214 if(lua_pcall(L, 5, 0, 0))
215 scriptError("error: %s", lua_tostring(L, -1));
218 // Report taken items
219 void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
220 const std::string &listname, int index, ItemStack &stack,
221 ServerActiveObject *player)
223 SCRIPTAPI_PRECHECKHEADER
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)
241 lua_pushstring(L, listname.c_str());
243 lua_pushinteger(L, index + 1);
245 LuaItemStack::create(L, stack);
247 objectrefGetOrCreate(player);
248 if(lua_pcall(L, 5, 0, 0))
249 scriptError("error: %s", lua_tostring(L, -1));
252 ScriptApiNodemeta::ScriptApiNodemeta() {
255 ScriptApiNodemeta::~ScriptApiNodemeta() {