]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/cpp_api/s_inventory.cpp
Add LuaEntity on_death callback (#6177)
[dragonfireclient.git] / src / script / cpp_api / s_inventory.cpp
index dd7e2651093b7da134cfad388a09b02d653f87ac..c90c7d4e2e78b99cbfb632e3c6377e74fc8da590 100644 (file)
@@ -33,11 +33,10 @@ int ScriptApiDetached::detached_inventory_AllowMove(
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       lua_pushcfunction(L, script_error_handler);
-       int errorhandler = lua_gettop(L);
+       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)
@@ -50,9 +49,8 @@ int ScriptApiDetached::detached_inventory_AllowMove(
        lua_pushstring(L, to_list.c_str());   // to_list
        lua_pushinteger(L, to_index + 1);     // to_index
        lua_pushinteger(L, count);            // count
-       objectrefGetOrCreate(player);         // player
-       if(lua_pcall(L, 7, 1, errorhandler))
-               scriptError();
+       objectrefGetOrCreate(L, player);      // player
+       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);
@@ -68,11 +66,10 @@ int ScriptApiDetached::detached_inventory_AllowPut(
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       lua_pushcfunction(L, script_error_handler);
-       int errorhandler = lua_gettop(L);
+       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)
@@ -82,10 +79,9 @@ int ScriptApiDetached::detached_inventory_AllowPut(
        lua_pushstring(L, listname.c_str()); // listname
        lua_pushinteger(L, index + 1);       // index
        LuaItemStack::create(L, stack);      // stack
-       objectrefGetOrCreate(player);        // player
-       if(lua_pcall(L, 5, 1, errorhandler))
-               scriptError();
-       if(!lua_isnumber(L, -1))
+       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
@@ -100,11 +96,10 @@ int ScriptApiDetached::detached_inventory_AllowTake(
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       lua_pushcfunction(L, script_error_handler);
-       int errorhandler = lua_gettop(L);
+       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)
@@ -114,10 +109,9 @@ int ScriptApiDetached::detached_inventory_AllowTake(
        lua_pushstring(L, listname.c_str()); // listname
        lua_pushinteger(L, index + 1);       // index
        LuaItemStack::create(L, stack);      // stack
-       objectrefGetOrCreate(player);        // player
-       if(lua_pcall(L, 5, 1, errorhandler))
-               scriptError();
-       if(!lua_isnumber(L, -1))
+       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
@@ -133,11 +127,10 @@ void ScriptApiDetached::detached_inventory_OnMove(
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       lua_pushcfunction(L, script_error_handler);
-       int errorhandler = lua_gettop(L);
+       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)
@@ -150,10 +143,9 @@ void ScriptApiDetached::detached_inventory_OnMove(
        lua_pushstring(L, to_list.c_str());   // to_list
        lua_pushinteger(L, to_index + 1);     // to_index
        lua_pushinteger(L, count);            // count
-       objectrefGetOrCreate(player);         // player
-       if(lua_pcall(L, 7, 0, errorhandler))
-               scriptError();
-       lua_pop(L, 1); // Pop error handler
+       objectrefGetOrCreate(L, player);      // player
+       PCALL_RES(lua_pcall(L, 7, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
 
 // Report put items
@@ -164,11 +156,10 @@ void ScriptApiDetached::detached_inventory_OnPut(
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       lua_pushcfunction(L, script_error_handler);
-       int errorhandler = lua_gettop(L);
+       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)
@@ -179,10 +170,9 @@ void ScriptApiDetached::detached_inventory_OnPut(
        lua_pushstring(L, listname.c_str()); // listname
        lua_pushinteger(L, index + 1);       // index
        LuaItemStack::create(L, stack);      // stack
-       objectrefGetOrCreate(player);        // player
-       if(lua_pcall(L, 5, 0, errorhandler))
-               scriptError();
-       lua_pop(L, 1); // Pop error handler
+       objectrefGetOrCreate(L, player);     // player
+       PCALL_RES(lua_pcall(L, 5, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
 
 // Report taken items
@@ -193,11 +183,10 @@ void ScriptApiDetached::detached_inventory_OnTake(
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       lua_pushcfunction(L, script_error_handler);
-       int errorhandler = lua_gettop(L);
+       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)
@@ -208,13 +197,12 @@ void ScriptApiDetached::detached_inventory_OnTake(
        lua_pushstring(L, listname.c_str()); // listname
        lua_pushinteger(L, index + 1);       // index
        LuaItemStack::create(L, stack);      // stack
-       objectrefGetOrCreate(player);        // player
-       if(lua_pcall(L, 5, 0, errorhandler))
-               scriptError();
-       lua_pop(L, 1); // Pop error handler
+       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
@@ -223,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);
@@ -236,6 +224,9 @@ bool ScriptApiDetached::getDetachedInventoryCallback(
                lua_pop(L, 1);
                return false;
        }
+
+       setOriginFromTable(-1);
+
        lua_getfield(L, -1, callbackname);
        lua_remove(L, -2);
        // Should be a function or nil