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 std::string nodename = ndef->get(node).name;
46 if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_move"))
49 // function(pos, from_list, from_index, to_list, to_index, count, player)
50 push_v3s16(L, p); // pos
51 lua_pushstring(L, from_list.c_str()); // from_list
52 lua_pushinteger(L, from_index + 1); // from_index
53 lua_pushstring(L, to_list.c_str()); // to_list
54 lua_pushinteger(L, to_index + 1); // to_index
55 lua_pushinteger(L, count); // count
56 objectrefGetOrCreate(L, player); // player
57 PCALL_RES(lua_pcall(L, 7, 1, m_errorhandler));
58 if (!lua_isnumber(L, -1))
59 throw LuaError("allow_metadata_inventory_move should"
60 " return a number, guilty node: " + nodename);
61 int num = luaL_checkinteger(L, -1);
62 lua_pop(L, 1); // Pop integer
66 // Return number of accepted items to be put
67 int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
68 const std::string &listname, int index, ItemStack &stack,
69 ServerActiveObject *player)
71 SCRIPTAPI_PRECHECKHEADER
73 INodeDefManager *ndef = getServer()->ndef();
75 // If node doesn't exist, we don't know what callback to call
76 MapNode node = getEnv()->getMap().getNodeNoEx(p);
77 if (node.getContent() == CONTENT_IGNORE)
80 // Push callback function on stack
81 std::string nodename = ndef->get(node).name;
82 if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_put"))
85 // Call function(pos, listname, index, stack, player)
86 push_v3s16(L, p); // pos
87 lua_pushstring(L, listname.c_str()); // listname
88 lua_pushinteger(L, index + 1); // index
89 LuaItemStack::create(L, stack); // stack
90 objectrefGetOrCreate(L, player); // player
91 PCALL_RES(lua_pcall(L, 5, 1, m_errorhandler));
92 if(!lua_isnumber(L, -1))
93 throw LuaError("allow_metadata_inventory_put should"
94 " return a number, guilty node: " + nodename);
95 int num = luaL_checkinteger(L, -1);
96 lua_pop(L, 1); // Pop integer
100 // Return number of accepted items to be taken
101 int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
102 const std::string &listname, int index, ItemStack &stack,
103 ServerActiveObject *player)
105 SCRIPTAPI_PRECHECKHEADER
107 INodeDefManager *ndef = getServer()->ndef();
109 // If node doesn't exist, we don't know what callback to call
110 MapNode node = getEnv()->getMap().getNodeNoEx(p);
111 if (node.getContent() == CONTENT_IGNORE)
114 // Push callback function on stack
115 std::string nodename = ndef->get(node).name;
116 if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_take"))
119 // Call function(pos, listname, index, count, player)
120 push_v3s16(L, p); // pos
121 lua_pushstring(L, listname.c_str()); // listname
122 lua_pushinteger(L, index + 1); // index
123 LuaItemStack::create(L, stack); // stack
124 objectrefGetOrCreate(L, player); // player
125 PCALL_RES(lua_pcall(L, 5, 1, m_errorhandler));
126 if (!lua_isnumber(L, -1))
127 throw LuaError("allow_metadata_inventory_take should"
128 " return a number, guilty node: " + nodename);
129 int num = luaL_checkinteger(L, -1);
130 lua_pop(L, 1); // Pop integer
134 // Report moved items
135 void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
136 const std::string &from_list, int from_index,
137 const std::string &to_list, int to_index,
138 int count, ServerActiveObject *player)
140 SCRIPTAPI_PRECHECKHEADER
142 INodeDefManager *ndef = getServer()->ndef();
144 // If node doesn't exist, we don't know what callback to call
145 MapNode node = getEnv()->getMap().getNodeNoEx(p);
146 if (node.getContent() == CONTENT_IGNORE)
149 // Push callback function on stack
150 std::string nodename = ndef->get(node).name;
151 if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_move"))
154 // function(pos, from_list, from_index, to_list, to_index, count, player)
155 push_v3s16(L, p); // pos
156 lua_pushstring(L, from_list.c_str()); // from_list
157 lua_pushinteger(L, from_index + 1); // from_index
158 lua_pushstring(L, to_list.c_str()); // to_list
159 lua_pushinteger(L, to_index + 1); // to_index
160 lua_pushinteger(L, count); // count
161 objectrefGetOrCreate(L, player); // player
162 PCALL_RES(lua_pcall(L, 7, 0, m_errorhandler));
166 void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
167 const std::string &listname, int index, ItemStack &stack,
168 ServerActiveObject *player)
170 SCRIPTAPI_PRECHECKHEADER
172 INodeDefManager *ndef = getServer()->ndef();
174 // If node doesn't exist, we don't know what callback to call
175 MapNode node = getEnv()->getMap().getNodeNoEx(p);
176 if (node.getContent() == CONTENT_IGNORE)
179 // Push callback function on stack
180 std::string nodename = ndef->get(node).name;
181 if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_put"))
184 // Call function(pos, listname, index, stack, player)
185 push_v3s16(L, p); // pos
186 lua_pushstring(L, listname.c_str()); // listname
187 lua_pushinteger(L, index + 1); // index
188 LuaItemStack::create(L, stack); // stack
189 objectrefGetOrCreate(L, player); // player
190 PCALL_RES(lua_pcall(L, 5, 0, m_errorhandler));
193 // Report taken items
194 void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
195 const std::string &listname, int index, ItemStack &stack,
196 ServerActiveObject *player)
198 SCRIPTAPI_PRECHECKHEADER
200 INodeDefManager *ndef = getServer()->ndef();
202 // If node doesn't exist, we don't know what callback to call
203 MapNode node = getEnv()->getMap().getNodeNoEx(p);
204 if (node.getContent() == CONTENT_IGNORE)
207 // Push callback function on stack
208 std::string nodename = ndef->get(node).name;
209 if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_take"))
212 // Call function(pos, listname, index, stack, player)
213 push_v3s16(L, p); // pos
214 lua_pushstring(L, listname.c_str()); // listname
215 lua_pushinteger(L, index + 1); // index
216 LuaItemStack::create(L, stack); // stack
217 objectrefGetOrCreate(L, player); // player
218 PCALL_RES(lua_pcall(L, 5, 0, m_errorhandler));
221 ScriptApiNodemeta::ScriptApiNodemeta()
225 ScriptApiNodemeta::~ScriptApiNodemeta()