]> git.lizzy.rs Git - minetest.git/blobdiff - src/script/cpp_api/s_inventory.cpp
on_death: Fix callback number of pushed arguments (Fixes #6451)
[minetest.git] / src / script / cpp_api / s_inventory.cpp
index c8c90fd8f1f69bddd5a177eee9de25c8b2353f14..251ddb2211634c7630db30e15122006e6ee7408d 100644 (file)
@@ -33,6 +33,8 @@ int ScriptApiDetached::detached_inventory_AllowMove(
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        // Push callback function on stack
        if (!getDetachedInventoryCallback(name, "allow_move"))
                return count;
@@ -48,11 +50,11 @@ int ScriptApiDetached::detached_inventory_AllowMove(
        lua_pushinteger(L, to_index + 1);     // to_index
        lua_pushinteger(L, count);            // count
        objectrefGetOrCreate(L, player);      // player
-       PCALL_RES(lua_pcall(L, 7, 1, m_errorhandler));
+       PCALL_RES(lua_pcall(L, 7, 1, error_handler));
        if(!lua_isnumber(L, -1))
                throw LuaError("allow_move should return a number. name=" + name);
        int ret = luaL_checkinteger(L, -1);
-       lua_pop(L, 1); // Pop integer
+       lua_pop(L, 2); // Pop integer and error handler
        return ret;
 }
 
@@ -64,6 +66,8 @@ int ScriptApiDetached::detached_inventory_AllowPut(
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        // Push callback function on stack
        if (!getDetachedInventoryCallback(name, "allow_put"))
                return stack.count; // All will be accepted
@@ -76,11 +80,11 @@ int ScriptApiDetached::detached_inventory_AllowPut(
        lua_pushinteger(L, index + 1);       // index
        LuaItemStack::create(L, stack);      // stack
        objectrefGetOrCreate(L, player);     // player
-       PCALL_RES(lua_pcall(L, 5, 1, m_errorhandler));
+       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, 1); // Pop integer
+       lua_pop(L, 2); // Pop integer and error handler
        return ret;
 }
 
@@ -92,6 +96,8 @@ int ScriptApiDetached::detached_inventory_AllowTake(
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        // Push callback function on stack
        if (!getDetachedInventoryCallback(name, "allow_take"))
                return stack.count; // All will be accepted
@@ -104,11 +110,11 @@ int ScriptApiDetached::detached_inventory_AllowTake(
        lua_pushinteger(L, index + 1);       // index
        LuaItemStack::create(L, stack);      // stack
        objectrefGetOrCreate(L, player);     // player
-       PCALL_RES(lua_pcall(L, 5, 1, m_errorhandler));
+       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, 1); // Pop integer
+       lua_pop(L, 2); // Pop integer and error handler
        return ret;
 }
 
@@ -121,6 +127,8 @@ void ScriptApiDetached::detached_inventory_OnMove(
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        // Push callback function on stack
        if (!getDetachedInventoryCallback(name, "on_move"))
                return;
@@ -136,7 +144,8 @@ void ScriptApiDetached::detached_inventory_OnMove(
        lua_pushinteger(L, to_index + 1);     // to_index
        lua_pushinteger(L, count);            // count
        objectrefGetOrCreate(L, player);      // player
-       PCALL_RES(lua_pcall(L, 7, 0, m_errorhandler));
+       PCALL_RES(lua_pcall(L, 7, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
 
 // Report put items
@@ -147,6 +156,8 @@ void ScriptApiDetached::detached_inventory_OnPut(
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        // Push callback function on stack
        if (!getDetachedInventoryCallback(name, "on_put"))
                return;
@@ -160,7 +171,8 @@ void ScriptApiDetached::detached_inventory_OnPut(
        lua_pushinteger(L, index + 1);       // index
        LuaItemStack::create(L, stack);      // stack
        objectrefGetOrCreate(L, player);     // player
-       PCALL_RES(lua_pcall(L, 5, 0, m_errorhandler));
+       PCALL_RES(lua_pcall(L, 5, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
 
 // Report taken items
@@ -171,6 +183,8 @@ void ScriptApiDetached::detached_inventory_OnTake(
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        // Push callback function on stack
        if (!getDetachedInventoryCallback(name, "on_take"))
                return;
@@ -184,7 +198,8 @@ void ScriptApiDetached::detached_inventory_OnTake(
        lua_pushinteger(L, index + 1);       // index
        LuaItemStack::create(L, stack);      // stack
        objectrefGetOrCreate(L, player);     // player
-       PCALL_RES(lua_pcall(L, 5, 0, m_errorhandler));
+       PCALL_RES(lua_pcall(L, 5, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
 
 // Retrieves core.detached_inventories[name][callbackname]
@@ -203,29 +218,28 @@ bool ScriptApiDetached::getDetachedInventoryCallback(
        lua_getfield(L, -1, name.c_str());
        lua_remove(L, -2);
        // Should be a table
-       if(lua_type(L, -1) != LUA_TTABLE)
-       {
+       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;
 }