]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/cpp_api/s_inventory.cpp
Pass clang-format on various cpp/header files (#5559)
[dragonfireclient.git] / src / script / cpp_api / s_inventory.cpp
index 2402d198c1ca6a5d6c2c73c42d16bec98a83c511..c90c7d4e2e78b99cbfb632e3c6377e74fc8da590 100644 (file)
@@ -18,6 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #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"
@@ -32,8 +33,10 @@ int ScriptApiDetached::detached_inventory_AllowMove(
 {
        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)
@@ -41,23 +44,18 @@ int ScriptApiDetached::detached_inventory_AllowMove(
        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
@@ -68,28 +66,26 @@ int ScriptApiDetached::detached_inventory_AllowPut(
 {
        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
@@ -100,28 +96,26 @@ int ScriptApiDetached::detached_inventory_AllowTake(
 {
        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
@@ -133,8 +127,10 @@ void ScriptApiDetached::detached_inventory_OnMove(
 {
        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)
@@ -142,20 +138,14 @@ void ScriptApiDetached::detached_inventory_OnMove(
        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
@@ -166,8 +156,10 @@ void ScriptApiDetached::detached_inventory_OnPut(
 {
        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)
@@ -175,16 +167,12 @@ void ScriptApiDetached::detached_inventory_OnPut(
        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
@@ -195,8 +183,10 @@ void ScriptApiDetached::detached_inventory_OnTake(
 {
        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)
@@ -204,19 +194,15 @@ void ScriptApiDetached::detached_inventory_OnTake(
        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
@@ -225,7 +211,7 @@ bool ScriptApiDetached::getDetachedInventoryCallback(
 {
        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);
@@ -234,10 +220,13 @@ bool ScriptApiDetached::getDetachedInventoryCallback(
        // Should be a table
        if(lua_type(L, -1) != LUA_TTABLE)
        {
-               errorstream<<"Item \""<<name<<"\" not defined"<<std::endl;
+               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