X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fscript%2Fcpp_api%2Fs_inventory.cpp;h=c90c7d4e2e78b99cbfb632e3c6377e74fc8da590;hb=91a9382c25328075d1a27593b22b0a75863951e1;hp=09f26d80caceb92c6a47eadd7f3a1db64d025d87;hpb=4e1f50035e860a00636ca5d804c267119df99601;p=dragonfireclient.git diff --git a/src/script/cpp_api/s_inventory.cpp b/src/script/cpp_api/s_inventory.cpp index 09f26d80c..c90c7d4e2 100644 --- a/src/script/cpp_api/s_inventory.cpp +++ b/src/script/cpp_api/s_inventory.cpp @@ -33,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) @@ -42,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 @@ -69,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 @@ -101,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 @@ -134,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) @@ -143,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 @@ -167,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) @@ -176,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 @@ -196,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) @@ -205,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 @@ -226,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); @@ -235,10 +220,13 @@ bool ScriptApiDetached::getDetachedInventoryCallback( // Should be a table if(lua_type(L, -1) != LUA_TTABLE) { - errorstream<<"Item \""<