]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/cpp_api/s_inventory.cpp
Fix wrong code comment (#8061)
[dragonfireclient.git] / src / script / cpp_api / s_inventory.cpp
index 835c0818fe76eae9811e2b437e60a183e48a68a9..e9c09f72e20963ca60149c1428ced7bcbce5300a 100644 (file)
@@ -26,35 +26,29 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 // Return number of accepted items to be moved
 int ScriptApiDetached::detached_inventory_AllowMove(
-               const std::string &name,
-               const std::string &from_list, int from_index,
-               const std::string &to_list, int to_index,
-               int count, ServerActiveObject *player)
+               const MoveAction &ma, int count,
+               ServerActiveObject *player)
 {
        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(ma.from_inv.name, "allow_move"))
                return count;
 
        // function(inv, from_list, from_index, to_list, to_index, count, player)
        // inv
-       InventoryLocation loc;
-       loc.setDetached(name);
-       InvRef::create(L, loc);
-       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(player);         // player
-       if(lua_pcall(L, 7, 1, errorhandler))
-               scriptError();
+       InvRef::create(L, ma.from_inv);
+       lua_pushstring(L, ma.from_list.c_str()); // from_list
+       lua_pushinteger(L, ma.from_i + 1);       // from_index
+       lua_pushstring(L, ma.to_list.c_str());   // to_list
+       lua_pushinteger(L, ma.to_i + 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("allow_move should return a number");
+               throw LuaError("allow_move should return a number. name=" + ma.from_inv.name);
        int ret = luaL_checkinteger(L, -1);
        lua_pop(L, 2); // Pop integer and error handler
        return ret;
@@ -62,31 +56,26 @@ int ScriptApiDetached::detached_inventory_AllowMove(
 
 // Return number of accepted items to be put
 int ScriptApiDetached::detached_inventory_AllowPut(
-               const std::string &name,
-               const std::string &listname, int index, ItemStack &stack,
+               const MoveAction &ma, const ItemStack &stack,
                ServerActiveObject *player)
 {
        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(ma.to_inv.name, "allow_put"))
                return stack.count; // All will be accepted
 
        // Call function(inv, listname, index, stack, player)
-       InventoryLocation loc;
-       loc.setDetached(name);
-       InvRef::create(L, loc);              // inv
-       lua_pushstring(L, listname.c_str()); // listname
-       lua_pushinteger(L, index + 1);       // index
+       InvRef::create(L, ma.to_inv);          // inv
+       lua_pushstring(L, ma.to_list.c_str()); // listname
+       lua_pushinteger(L, ma.to_i + 1);       // index
        LuaItemStack::create(L, stack);      // stack
-       objectrefGetOrCreate(player);        // player
-       if(lua_pcall(L, 5, 1, errorhandler))
-               scriptError();
-       if(!lua_isnumber(L, -1))
-               throw LuaError("allow_put should return a number");
+       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=" + ma.to_inv.name);
        int ret = luaL_checkinteger(L, -1);
        lua_pop(L, 2); // Pop integer and error handler
        return ret;
@@ -94,31 +83,26 @@ int ScriptApiDetached::detached_inventory_AllowPut(
 
 // Return number of accepted items to be taken
 int ScriptApiDetached::detached_inventory_AllowTake(
-               const std::string &name,
-               const std::string &listname, int index, ItemStack &stack,
+               const MoveAction &ma, const ItemStack &stack,
                ServerActiveObject *player)
 {
        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(ma.from_inv.name, "allow_take"))
                return stack.count; // All will be accepted
 
        // Call function(inv, listname, index, stack, player)
-       InventoryLocation loc;
-       loc.setDetached(name);
-       InvRef::create(L, loc);              // inv
-       lua_pushstring(L, listname.c_str()); // listname
-       lua_pushinteger(L, index + 1);       // index
+       InvRef::create(L, ma.from_inv);          // inv
+       lua_pushstring(L, ma.from_list.c_str()); // listname
+       lua_pushinteger(L, ma.from_i + 1);       // index
        LuaItemStack::create(L, stack);      // stack
-       objectrefGetOrCreate(player);        // player
-       if(lua_pcall(L, 5, 1, errorhandler))
-               scriptError();
-       if(!lua_isnumber(L, -1))
-               throw LuaError("allow_take should return a number");
+       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=" + ma.from_inv.name);
        int ret = luaL_checkinteger(L, -1);
        lua_pop(L, 2); // Pop integer and error handler
        return ret;
@@ -126,95 +110,79 @@ int ScriptApiDetached::detached_inventory_AllowTake(
 
 // Report moved items
 void ScriptApiDetached::detached_inventory_OnMove(
-               const std::string &name,
-               const std::string &from_list, int from_index,
-               const std::string &to_list, int to_index,
-               int count, ServerActiveObject *player)
+               const MoveAction &ma, int count,
+               ServerActiveObject *player)
 {
        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(ma.from_inv.name, "on_move"))
                return;
 
        // function(inv, from_list, from_index, to_list, to_index, count, player)
        // inv
-       InventoryLocation loc;
-       loc.setDetached(name);
-       InvRef::create(L, loc);
-       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(player);         // player
-       if(lua_pcall(L, 7, 0, errorhandler))
-               scriptError();
-       lua_pop(L, 1); // Pop error handler
+       InvRef::create(L, ma.from_inv);
+       lua_pushstring(L, ma.from_list.c_str()); // from_list
+       lua_pushinteger(L, ma.from_i + 1);       // from_index
+       lua_pushstring(L, ma.to_list.c_str());   // to_list
+       lua_pushinteger(L, ma.to_i + 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
 void ScriptApiDetached::detached_inventory_OnPut(
-               const std::string &name,
-               const std::string &listname, int index, ItemStack &stack,
+               const MoveAction &ma, const ItemStack &stack,
                ServerActiveObject *player)
 {
        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(ma.to_inv.name, "on_put"))
                return;
 
        // Call function(inv, listname, index, stack, player)
        // inv
-       InventoryLocation loc;
-       loc.setDetached(name);
-       InvRef::create(L, loc);
-       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
+       InvRef::create(L, ma.to_inv);
+       lua_pushstring(L, ma.to_list.c_str()); // listname
+       lua_pushinteger(L, ma.to_i + 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
 void ScriptApiDetached::detached_inventory_OnTake(
-               const std::string &name,
-               const std::string &listname, int index, ItemStack &stack,
+               const MoveAction &ma, const ItemStack &stack,
                ServerActiveObject *player)
 {
        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(ma.from_inv.name, "on_take"))
                return;
 
        // Call function(inv, listname, index, stack, player)
        // inv
-       InventoryLocation loc;
-       loc.setDetached(name);
-       InvRef::create(L, loc);
-       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
+       InvRef::create(L, ma.from_inv);
+       lua_pushstring(L, ma.from_list.c_str()); // listname
+       lua_pushinteger(L, ma.from_i + 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
@@ -223,36 +191,35 @@ 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);
        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;
 }