/*MinetestCopyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>This program is free software; you can redistribute it and/or modifyit under the terms of the GNU Lesser General Public License as published bythe Free Software Foundation; either version 2.1 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU Lesser General Public License for more details.You should have received a copy of the GNU Lesser General Public License alongwith this program; if not, write to the Free Software Foundation, Inc.,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.*/#include"cpp_api/s_nodemeta.h"#include"cpp_api/s_internal.h"#include"common/c_converter.h"#include"nodedef.h"#include"mapnode.h"#include"server.h"#include"environment.h"#include"lua_api/l_item.h"// Return number of accepted items to be movedintScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,conststd::string &from_list,int from_index,conststd::string &to_list,int to_index,int count, ServerActiveObject *player){
SCRIPTAPI_PRECHECKHEADER
int error_handler =PUSH_ERROR_HANDLER(L);
INodeDefManager *ndef =getServer()->ndef();// If node doesn't exist, we don't know what callback to call
MapNode node =getEnv()->getMap().getNodeNoEx(p);if(node.getContent() == CONTENT_IGNORE)return0;// Push callback function on stackstd::string nodename = ndef->get(node).name;if(!getItemCallback(nodename.c_str(),"allow_metadata_inventory_move"))return count;// function(pos, from_list, from_index, to_list, to_index, count, player)push_v3s16(L, p);// poslua_pushstring(L, from_list.c_str());// from_listlua_pushinteger(L, from_index +1);// from_indexlua_pushstring(L, to_list.c_str());// to_listlua_pushinteger(L, to_index +1);// to_indexlua_pushinteger(L, count);// countobjectrefGetOrCreate(L, player);// playerPCALL_RES(lua_pcall(L,7,1, error_handler));if(!lua_isnumber(L, -1))throwLuaError("allow_metadata_inventory_move should"" return a number, guilty node: "+ nodename);int num =luaL_checkinteger(L, -1);lua_pop(L,2);// Pop integer and error handlerreturn num;}// Return number of accepted items to be putintScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,conststd::string &listname,int index, ItemStack &stack,
ServerActiveObject *player){
SCRIPTAPI_PRECHECKHEADER
int error_handler =PUSH_ERROR_HANDLER(L);
INodeDefManager *ndef =getServer()->ndef();// If node doesn't exist, we don't know what callback to call
MapNode node =getEnv()->getMap().getNodeNoEx(p);if(node.getContent() == CONTENT_IGNORE)return0;// Push callback function on stackstd::string nodename = ndef->get(node).name;if(!getItemCallback(nodename.c_str(),"allow_metadata_inventory_put"))return stack.count;// Call function(pos, listname, index, stack, player)push_v3s16(L, p);// poslua_pushstring(L, listname.c_str());// listnamelua_pushinteger(L, index +1);// indexLuaItemStack::create(L, stack);// stackobjectrefGetOrCreate(L, player);// playerPCALL_RES(lua_pcall(L,5,1, error_handler));if(!lua_isnumber(L, -1))throwLuaError("allow_metadata_inventory_put should"" return a number, guilty node: "+ nodename);int num =luaL_checkinteger(L, -1);lua_pop(L,2);// Pop integer and error handlerreturn num;}// Return number of accepted items to be takenintScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,conststd::string &listname,int index, ItemStack &stack,
ServerActiveObject *player){
SCRIPTAPI_PRECHECKHEADER
int error_handler =PUSH_ERROR_HANDLER(L);
INodeDefManager *ndef =getServer()->ndef();// If node doesn't exist, we don't know what callback to call
MapNode node =getEnv()->getMap().getNodeNoEx(p);if(node.getContent() == CONTENT_IGNORE)return0;// Push callback function on stackstd::string nodename = ndef->get(node).name;if(!getItemCallback(nodename.c_str(),"allow_metadata_inventory_take"))return stack.count;// Call function(pos, listname, index, count, player)push_v3s16(L, p);// poslua_pushstring(L, listname.c_str());// listnamelua_pushinteger(L, index +1);// indexLuaItemStack::create(L, stack);// stackobjectrefGetOrCreate(L, player);// playerPCALL_RES(lua_pcall(L,5,1, error_handler));if(!lua_isnumber(L, -1))throwLuaError("allow_metadata_inventory_take should"" return a number, guilty node: "+ nodename);int num =luaL_checkinteger(L, -1);lua_pop(L,2);// Pop integer and error handlerreturn num;}// Report moved itemsvoidScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,conststd::string &from_list,int from_index,conststd::string &to_list,int to_index,int count, ServerActiveObject *player){
SCRIPTAPI_PRECHECKHEADER
int error_handler =PUSH_ERROR_HANDLER(L);
INodeDefManager *ndef =getServer()->ndef();// If node doesn't exist, we don't know what callback to call
MapNode node =getEnv()->getMap().getNodeNoEx(p);if(node.getContent() == CONTENT_IGNORE)return;// Push callback function on stackstd::string nodename = ndef->get(node).name;if(!getItemCallback(nodename.c_str(),"on_metadata_inventory_move"))return;// function(pos, from_list, from_index, to_list, to_index, count, player)push_v3s16(L, p);// poslua_pushstring(L, from_list.c_str());// from_listlua_pushinteger(L, from_index +1);// from_indexlua_pushstring(L, to_list.c_str());// to_listlua_pushinteger(L, to_index +1);// to_indexlua_pushinteger(L, count);// countobjectrefGetOrCreate(L, player);// playerPCALL_RES(lua_pcall(L,7,0, error_handler));lua_pop(L,1);// Pop error handler}// Report put itemsvoidScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,conststd::string &listname,int index, ItemStack &stack,
ServerActiveObject *player){
SCRIPTAPI_PRECHECKHEADER
int error_handler =PUSH_ERROR_HANDLER(L);
INodeDefManager *ndef =getServer()->ndef();// If node doesn't exist, we don't know what callback to call
MapNode node =getEnv()->getMap().getNodeNoEx(p);if(node.getContent() == CONTENT_IGNORE)return;// Push callback function on stackstd::string nodename = ndef->get(node).name;if(!getItemCallback(nodename.c_str(),"on_metadata_inventory_put"))return;// Call function(pos, listname, index, stack, player)push_v3s16(L, p);// poslua_pushstring(L, listname.c_str());// listnamelua_pushinteger(L, index +1);// indexLuaItemStack::create(L, stack);// stackobjectrefGetOrCreate(L, player);// playerPCALL_RES(lua_pcall(L,5,0, error_handler));lua_pop(L,1);// Pop error handler}// Report taken itemsvoidScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,conststd::string &listname,int index, ItemStack &stack,
ServerActiveObject *player){
SCRIPTAPI_PRECHECKHEADER
int error_handler =PUSH_ERROR_HANDLER(L);
INodeDefManager *ndef =getServer()->ndef();// If node doesn't exist, we don't know what callback to call
MapNode node =getEnv()->getMap().getNodeNoEx(p);if(node.getContent() == CONTENT_IGNORE)return;// Push callback function on stackstd::string nodename = ndef->get(node).name;if(!getItemCallback(nodename.c_str(),"on_metadata_inventory_take"))return;// Call function(pos, listname, index, stack, player)push_v3s16(L, p);// poslua_pushstring(L, listname.c_str());// listnamelua_pushinteger(L, index +1);// indexLuaItemStack::create(L, stack);// stackobjectrefGetOrCreate(L, player);// playerPCALL_RES(lua_pcall(L,5,0, error_handler));lua_pop(L,1);// Pop error handler}ScriptApiNodemeta::ScriptApiNodemeta(){}ScriptApiNodemeta::~ScriptApiNodemeta(){}