*/
#include "cpp_api/s_inventory.h"
+#include "cpp_api/s_internal.h"
#include "inventorymanager.h"
#include "lua_api/l_inventory.h"
#include "lua_api/l_item.h"
{
SCRIPTAPI_PRECHECKHEADER
+ int error_handler = PUSH_ERROR_HANDLER(L);
+
// Push callback function on stack
- if(!getDetachedInventoryCallback(name, "allow_move"))
+ if (!getDetachedInventoryCallback(name, "allow_move"))
return count;
// function(inv, from_list, from_index, to_list, to_index, count, player)
InventoryLocation loc;
loc.setDetached(name);
InvRef::create(L, loc);
- // from_list
- lua_pushstring(L, from_list.c_str());
- // from_index
- lua_pushinteger(L, from_index + 1);
- // to_list
- lua_pushstring(L, to_list.c_str());
- // to_index
- lua_pushinteger(L, to_index + 1);
- // count
- lua_pushinteger(L, count);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 7, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ lua_pushstring(L, from_list.c_str()); // from_list
+ lua_pushinteger(L, from_index + 1); // from_index
+ lua_pushstring(L, to_list.c_str()); // to_list
+ lua_pushinteger(L, to_index + 1); // to_index
+ lua_pushinteger(L, count); // count
+ objectrefGetOrCreate(L, player); // player
+ PCALL_RES(lua_pcall(L, 7, 1, error_handler));
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_move should return a number");
- return luaL_checkinteger(L, -1);
+ throw LuaError("allow_move should return a number. name=" + name);
+ int ret = luaL_checkinteger(L, -1);
+ lua_pop(L, 2); // Pop integer and error handler
+ return ret;
}
// Return number of accepted items to be put
{
SCRIPTAPI_PRECHECKHEADER
+ int error_handler = PUSH_ERROR_HANDLER(L);
+
// Push callback function on stack
- if(!getDetachedInventoryCallback(name, "allow_put"))
+ if (!getDetachedInventoryCallback(name, "allow_put"))
return stack.count; // All will be accepted
// Call function(inv, listname, index, stack, player)
- // inv
InventoryLocation loc;
loc.setDetached(name);
- InvRef::create(L, loc);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
- if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_put should return a number");
- return luaL_checkinteger(L, -1);
+ InvRef::create(L, loc); // inv
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(L, player); // player
+ PCALL_RES(lua_pcall(L, 5, 1, error_handler));
+ if (!lua_isnumber(L, -1))
+ throw LuaError("allow_put should return a number. name=" + name);
+ int ret = luaL_checkinteger(L, -1);
+ lua_pop(L, 2); // Pop integer and error handler
+ return ret;
}
// Return number of accepted items to be taken
{
SCRIPTAPI_PRECHECKHEADER
+ int error_handler = PUSH_ERROR_HANDLER(L);
+
// Push callback function on stack
- if(!getDetachedInventoryCallback(name, "allow_take"))
+ if (!getDetachedInventoryCallback(name, "allow_take"))
return stack.count; // All will be accepted
// Call function(inv, listname, index, stack, player)
- // inv
InventoryLocation loc;
loc.setDetached(name);
- InvRef::create(L, loc);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
- if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_take should return a number");
- return luaL_checkinteger(L, -1);
+ InvRef::create(L, loc); // inv
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(L, player); // player
+ PCALL_RES(lua_pcall(L, 5, 1, error_handler));
+ if (!lua_isnumber(L, -1))
+ throw LuaError("allow_take should return a number. name=" + name);
+ int ret = luaL_checkinteger(L, -1);
+ lua_pop(L, 2); // Pop integer and error handler
+ return ret;
}
// Report moved items
{
SCRIPTAPI_PRECHECKHEADER
+ int error_handler = PUSH_ERROR_HANDLER(L);
+
// Push callback function on stack
- if(!getDetachedInventoryCallback(name, "on_move"))
+ if (!getDetachedInventoryCallback(name, "on_move"))
return;
// function(inv, from_list, from_index, to_list, to_index, count, player)
InventoryLocation loc;
loc.setDetached(name);
InvRef::create(L, loc);
- // from_list
- lua_pushstring(L, from_list.c_str());
- // from_index
- lua_pushinteger(L, from_index + 1);
- // to_list
- lua_pushstring(L, to_list.c_str());
- // to_index
- lua_pushinteger(L, to_index + 1);
- // count
- lua_pushinteger(L, count);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 7, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ lua_pushstring(L, from_list.c_str()); // from_list
+ lua_pushinteger(L, from_index + 1); // from_index
+ lua_pushstring(L, to_list.c_str()); // to_list
+ lua_pushinteger(L, to_index + 1); // to_index
+ lua_pushinteger(L, count); // count
+ objectrefGetOrCreate(L, player); // player
+ PCALL_RES(lua_pcall(L, 7, 0, error_handler));
+ lua_pop(L, 1); // Pop error handler
}
// Report put items
{
SCRIPTAPI_PRECHECKHEADER
+ int error_handler = PUSH_ERROR_HANDLER(L);
+
// Push callback function on stack
- if(!getDetachedInventoryCallback(name, "on_put"))
+ if (!getDetachedInventoryCallback(name, "on_put"))
return;
// Call function(inv, listname, index, stack, player)
InventoryLocation loc;
loc.setDetached(name);
InvRef::create(L, loc);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(L, player); // player
+ PCALL_RES(lua_pcall(L, 5, 0, error_handler));
+ lua_pop(L, 1); // Pop error handler
}
// Report taken items
{
SCRIPTAPI_PRECHECKHEADER
+ int error_handler = PUSH_ERROR_HANDLER(L);
+
// Push callback function on stack
- if(!getDetachedInventoryCallback(name, "on_take"))
+ if (!getDetachedInventoryCallback(name, "on_take"))
return;
// Call function(inv, listname, index, stack, player)
InventoryLocation loc;
loc.setDetached(name);
InvRef::create(L, loc);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(L, player); // player
+ PCALL_RES(lua_pcall(L, 5, 0, error_handler));
+ lua_pop(L, 1); // Pop error handler
}
-// Retrieves minetest.detached_inventories[name][callbackname]
+// Retrieves core.detached_inventories[name][callbackname]
// If that is nil or on error, return false and stack is unchanged
// If that is a function, returns true and pushes the
// function onto the stack
{
lua_State *L = getStack();
- lua_getglobal(L, "minetest");
+ lua_getglobal(L, "core");
lua_getfield(L, -1, "detached_inventories");
lua_remove(L, -2);
luaL_checktype(L, -1, LUA_TTABLE);
lua_getfield(L, -1, name.c_str());
lua_remove(L, -2);
// Should be a table
- if(lua_type(L, -1) != LUA_TTABLE)
- {
- errorstream<<"Item \""<<name<<"\" not defined"<<std::endl;
+ if (lua_type(L, -1) != LUA_TTABLE) {
+ errorstream<<"Detached inventory \""<<name<<"\" not defined"<<std::endl;
lua_pop(L, 1);
return false;
}
+
+ setOriginFromTable(-1);
+
lua_getfield(L, -1, callbackname);
lua_remove(L, -2);
// Should be a function or nil
- if(lua_type(L, -1) == LUA_TFUNCTION)
- {
+ if (lua_type(L, -1) == LUA_TFUNCTION) {
return true;
}
- else if(lua_isnil(L, -1))
- {
- lua_pop(L, 1);
- return false;
- }
- else
- {
- errorstream<<"Detached inventory \""<<name<<"\" callback \""
- <<callbackname<<"\" is not a function"<<std::endl;
+
+ if (lua_isnil(L, -1)) {
lua_pop(L, 1);
return false;
}
+
+ errorstream << "Detached inventory \"" << name << "\" callback \""
+ << callbackname << "\" is not a function" << std::endl;
+ lua_pop(L, 1);
+ return false;
}