Improve inventory callbacks a bit

This commit is contained in:
Perttu Ahola 2012-07-25 14:35:59 +03:00
parent 0a18dda158
commit 983e45ae92
6 changed files with 65 additions and 46 deletions

View File

@ -1371,14 +1371,14 @@ Node definition (register_node)
^ Called when a player wants to put something into the inventory ^ Called when a player wants to put something into the inventory
^ Return value: number of items allowed to put ^ Return value: number of items allowed to put
allow_metadata_inventory_take = func(pos, listname, index, count, player), allow_metadata_inventory_take = func(pos, listname, index, stack, player),
^ Called when a player wants to take something out of the inventory ^ Called when a player wants to take something out of the inventory
^ Return value: number of items allowed to take ^ Return value: number of items allowed to take
on_metadata_inventory_move = func(pos, from_list, from_index, on_metadata_inventory_move = func(pos, from_list, from_index,
to_list, to_index, count, player), to_list, to_index, count, player),
on_metadata_inventory_put = func(pos, listname, index, stack, player), on_metadata_inventory_put = func(pos, listname, index, stack, player),
on_metadata_inventory_take = func(pos, listname, index, count, player), on_metadata_inventory_take = func(pos, listname, index, stack, player),
^ Called after the actual action has happened, according to what was allowed. ^ Called after the actual action has happened, according to what was allowed.
^ No return value ^ No return value
} }
@ -1447,13 +1447,13 @@ Detached inventory callbacks
^ Called when a player wants to put something into the inventory ^ Called when a player wants to put something into the inventory
^ Return value: number of items allowed to put ^ Return value: number of items allowed to put
allow_take = func(inv, listname, index, count, player), allow_take = func(inv, listname, index, stack, player),
^ Called when a player wants to take something out of the inventory ^ Called when a player wants to take something out of the inventory
^ Return value: number of items allowed to take ^ Return value: number of items allowed to take
on_move = func(inv, from_list, from_index, to_list, to_index, count, player), on_move = func(inv, from_list, from_index, to_list, to_index, count, player),
on_put = func(inv, listname, index, stack, player), on_put = func(inv, listname, index, stack, player),
on_take = func(inv, listname, index, count, player), on_take = func(inv, listname, index, stack, player),
^ Called after the actual action has happened, according to what was allowed. ^ Called after the actual action has happened, according to what was allowed.
^ No return value ^ No return value
} }

View File

@ -1223,7 +1223,7 @@ minetest.register_node("default:chest_locked", {
end end
return stack:get_count() return stack:get_count()
end, end,
allow_metadata_inventory_take = function(pos, listname, index, count, player) allow_metadata_inventory_take = function(pos, listname, index, stack, player)
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
if not has_locked_chest_privilege(meta, player) then if not has_locked_chest_privilege(meta, player) then
minetest.log("action", player:get_player_name().. minetest.log("action", player:get_player_name()..
@ -1232,7 +1232,7 @@ minetest.register_node("default:chest_locked", {
minetest.pos_to_string(pos)) minetest.pos_to_string(pos))
return 0 return 0
end end
return count return stack:get_count()
end, end,
on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
minetest.log("action", player:get_player_name().. minetest.log("action", player:get_player_name()..
@ -1242,7 +1242,7 @@ minetest.register_node("default:chest_locked", {
minetest.log("action", player:get_player_name().. minetest.log("action", player:get_player_name()..
" moves stuff to locked chest at "..minetest.pos_to_string(pos)) " moves stuff to locked chest at "..minetest.pos_to_string(pos))
end, end,
on_metadata_inventory_take = function(pos, listname, index, count, player) on_metadata_inventory_take = function(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name().. minetest.log("action", player:get_player_name()..
" takes stuff from locked chest at "..minetest.pos_to_string(pos)) " takes stuff from locked chest at "..minetest.pos_to_string(pos))
end, end,

View File

@ -534,7 +534,7 @@ local inv = minetest.create_detached_inventory("test_inventory", {
experimental.print_to_everything("allow put asked") experimental.print_to_everything("allow put asked")
return 1 -- Allow only 1 return 1 -- Allow only 1
end, end,
allow_take = function(inv, listname, index, count, player) allow_take = function(inv, listname, index, stack, player)
experimental.print_to_everything("allow take asked") experimental.print_to_everything("allow take asked")
return 4 -- Allow 4 at max return 4 -- Allow 4 at max
end, end,
@ -544,7 +544,7 @@ local inv = minetest.create_detached_inventory("test_inventory", {
on_put = function(inv, listname, index, stack, player) on_put = function(inv, listname, index, stack, player)
experimental.print_to_everything(player:get_player_name().." put items") experimental.print_to_everything(player:get_player_name().." put items")
end, end,
on_take = function(inv, listname, index, count, player) on_take = function(inv, listname, index, stack, player)
experimental.print_to_everything(player:get_player_name().." took items") experimental.print_to_everything(player:get_player_name().." took items")
end, end,
}) })

View File

@ -238,8 +238,10 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
if(from_inv.type == InventoryLocation::DETACHED) if(from_inv.type == InventoryLocation::DETACHED)
{ {
lua_State *L = player->getEnv()->getLua(); lua_State *L = player->getEnv()->getLua();
ItemStack src_item = list_from->getItem(from_i);
src_item.count = try_take_count;
src_can_take_count = scriptapi_detached_inventory_allow_take( src_can_take_count = scriptapi_detached_inventory_allow_take(
L, from_inv.name, from_list, from_i, try_take_count, player); L, from_inv.name, from_list, from_i, src_item, player);
} }
} }
@ -272,8 +274,10 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
if(from_inv.type == InventoryLocation::NODEMETA) if(from_inv.type == InventoryLocation::NODEMETA)
{ {
lua_State *L = player->getEnv()->getLua(); lua_State *L = player->getEnv()->getLua();
ItemStack src_item = list_from->getItem(from_i);
src_item.count = try_take_count;
src_can_take_count = scriptapi_nodemeta_inventory_allow_take( src_can_take_count = scriptapi_nodemeta_inventory_allow_take(
L, from_inv.p, from_list, from_i, try_take_count, player); L, from_inv.p, from_list, from_i, src_item, player);
} }
} }
@ -300,6 +304,9 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
} }
count = new_count; count = new_count;
ItemStack src_item = list_from->getItem(from_i);
src_item.count = count;
/* /*
Perform actual move Perform actual move
@ -341,8 +348,6 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
if(to_inv.type == InventoryLocation::DETACHED) if(to_inv.type == InventoryLocation::DETACHED)
{ {
lua_State *L = player->getEnv()->getLua(); lua_State *L = player->getEnv()->getLua();
ItemStack src_item = list_from->getItem(from_i);
src_item.count = count;
scriptapi_detached_inventory_on_put( scriptapi_detached_inventory_on_put(
L, to_inv.name, to_list, to_i, src_item, player); L, to_inv.name, to_list, to_i, src_item, player);
} }
@ -350,10 +355,8 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
if(from_inv.type == InventoryLocation::DETACHED) if(from_inv.type == InventoryLocation::DETACHED)
{ {
lua_State *L = player->getEnv()->getLua(); lua_State *L = player->getEnv()->getLua();
ItemStack src_item = list_from->getItem(from_i);
src_item.count = count;
scriptapi_detached_inventory_on_take( scriptapi_detached_inventory_on_take(
L, from_inv.name, from_list, from_i, src_item.count, player); L, from_inv.name, from_list, from_i, src_item, player);
} }
} }
@ -374,8 +377,6 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
if(to_inv.type == InventoryLocation::NODEMETA) if(to_inv.type == InventoryLocation::NODEMETA)
{ {
lua_State *L = player->getEnv()->getLua(); lua_State *L = player->getEnv()->getLua();
ItemStack src_item = list_from->getItem(from_i);
src_item.count = count;
scriptapi_nodemeta_inventory_on_put( scriptapi_nodemeta_inventory_on_put(
L, to_inv.p, to_list, to_i, src_item, player); L, to_inv.p, to_list, to_i, src_item, player);
} }
@ -383,10 +384,8 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
else if(from_inv.type == InventoryLocation::NODEMETA) else if(from_inv.type == InventoryLocation::NODEMETA)
{ {
lua_State *L = player->getEnv()->getLua(); lua_State *L = player->getEnv()->getLua();
ItemStack src_item = list_from->getItem(from_i);
src_item.count = count;
scriptapi_nodemeta_inventory_on_take( scriptapi_nodemeta_inventory_on_take(
L, from_inv.p, from_list, from_i, src_item.count, player); L, from_inv.p, from_list, from_i, src_item, player);
} }
} }
@ -485,16 +484,20 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
if(from_inv.type == InventoryLocation::DETACHED) if(from_inv.type == InventoryLocation::DETACHED)
{ {
lua_State *L = player->getEnv()->getLua(); lua_State *L = player->getEnv()->getLua();
ItemStack src_item = list_from->getItem(from_i);
src_item.count = take_count;
src_can_take_count = scriptapi_detached_inventory_allow_take( src_can_take_count = scriptapi_detached_inventory_allow_take(
L, from_inv.name, from_list, from_i, take_count, player); L, from_inv.name, from_list, from_i, src_item, player);
} }
// Source is nodemeta // Source is nodemeta
if(from_inv.type == InventoryLocation::NODEMETA) if(from_inv.type == InventoryLocation::NODEMETA)
{ {
lua_State *L = player->getEnv()->getLua(); lua_State *L = player->getEnv()->getLua();
ItemStack src_item = list_from->getItem(from_i);
src_item.count = take_count;
src_can_take_count = scriptapi_nodemeta_inventory_allow_take( src_can_take_count = scriptapi_nodemeta_inventory_allow_take(
L, from_inv.p, from_list, from_i, take_count, player); L, from_inv.p, from_list, from_i, src_item, player);
} }
if(src_can_take_count < take_count) if(src_can_take_count < take_count)
@ -502,6 +505,8 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
int actually_dropped_count = 0; int actually_dropped_count = 0;
ItemStack src_item = list_from->getItem(from_i);
// Drop the item // Drop the item
ItemStack item1 = list_from->getItem(from_i); ItemStack item1 = list_from->getItem(from_i);
if(scriptapi_item_on_drop(player->getEnv()->getLua(), item1, player, if(scriptapi_item_on_drop(player->getEnv()->getLua(), item1, player,
@ -528,6 +533,8 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
<<" list=\""<<from_list<<"\"" <<" list=\""<<from_list<<"\""
<<" i="<<from_i <<" i="<<from_i
<<std::endl; <<std::endl;
src_item.count = actually_dropped_count;
/* /*
Report drop to endpoints Report drop to endpoints
@ -538,7 +545,7 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
{ {
lua_State *L = player->getEnv()->getLua(); lua_State *L = player->getEnv()->getLua();
scriptapi_detached_inventory_on_take( scriptapi_detached_inventory_on_take(
L, from_inv.name, from_list, from_i, actually_dropped_count, player); L, from_inv.name, from_list, from_i, src_item, player);
} }
// Source is nodemeta // Source is nodemeta
@ -546,7 +553,7 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
{ {
lua_State *L = player->getEnv()->getLua(); lua_State *L = player->getEnv()->getLua();
scriptapi_nodemeta_inventory_on_take( scriptapi_nodemeta_inventory_on_take(
L, from_inv.p, from_list, from_i, actually_dropped_count, player); L, from_inv.p, from_list, from_i, src_item, player);
} }
} }

View File

@ -5762,6 +5762,8 @@ int scriptapi_nodemeta_inventory_allow_move(lua_State *L, v3s16 p,
objectref_get_or_create(L, player); objectref_get_or_create(L, player);
if(lua_pcall(L, 7, 1, 0)) if(lua_pcall(L, 7, 1, 0))
script_error(L, "error: %s", lua_tostring(L, -1)); script_error(L, "error: %s", lua_tostring(L, -1));
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_metadata_inventory_move should return a number");
return luaL_checkinteger(L, -1); return luaL_checkinteger(L, -1);
} }
@ -5799,12 +5801,14 @@ int scriptapi_nodemeta_inventory_allow_put(lua_State *L, v3s16 p,
objectref_get_or_create(L, player); objectref_get_or_create(L, player);
if(lua_pcall(L, 5, 1, 0)) if(lua_pcall(L, 5, 1, 0))
script_error(L, "error: %s", lua_tostring(L, -1)); script_error(L, "error: %s", lua_tostring(L, -1));
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_metadata_inventory_put should return a number");
return luaL_checkinteger(L, -1); return luaL_checkinteger(L, -1);
} }
// Return number of accepted items to be taken // Return number of accepted items to be taken
int scriptapi_nodemeta_inventory_allow_take(lua_State *L, v3s16 p, int scriptapi_nodemeta_inventory_allow_take(lua_State *L, v3s16 p,
const std::string &listname, int index, int count, const std::string &listname, int index, ItemStack &stack,
ServerActiveObject *player) ServerActiveObject *player)
{ {
realitycheck(L); realitycheck(L);
@ -5821,7 +5825,7 @@ int scriptapi_nodemeta_inventory_allow_take(lua_State *L, v3s16 p,
// Push callback function on stack // Push callback function on stack
if(!get_item_callback(L, ndef->get(node).name.c_str(), if(!get_item_callback(L, ndef->get(node).name.c_str(),
"allow_metadata_inventory_take")) "allow_metadata_inventory_take"))
return count; return stack.count;
// Call function(pos, listname, index, count, player) // Call function(pos, listname, index, count, player)
// pos // pos
@ -5830,12 +5834,14 @@ int scriptapi_nodemeta_inventory_allow_take(lua_State *L, v3s16 p,
lua_pushstring(L, listname.c_str()); lua_pushstring(L, listname.c_str());
// index // index
lua_pushinteger(L, index + 1); lua_pushinteger(L, index + 1);
// count // stack
lua_pushinteger(L, count); LuaItemStack::create(L, stack);
// player // player
objectref_get_or_create(L, player); objectref_get_or_create(L, player);
if(lua_pcall(L, 5, 1, 0)) if(lua_pcall(L, 5, 1, 0))
script_error(L, "error: %s", lua_tostring(L, -1)); script_error(L, "error: %s", lua_tostring(L, -1));
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_metadata_inventory_take should return a number");
return luaL_checkinteger(L, -1); return luaL_checkinteger(L, -1);
} }
@ -5918,7 +5924,7 @@ void scriptapi_nodemeta_inventory_on_put(lua_State *L, v3s16 p,
// Report taken items // Report taken items
void scriptapi_nodemeta_inventory_on_take(lua_State *L, v3s16 p, void scriptapi_nodemeta_inventory_on_take(lua_State *L, v3s16 p,
const std::string &listname, int index, int count, const std::string &listname, int index, ItemStack &stack,
ServerActiveObject *player) ServerActiveObject *player)
{ {
realitycheck(L); realitycheck(L);
@ -5937,15 +5943,15 @@ void scriptapi_nodemeta_inventory_on_take(lua_State *L, v3s16 p,
"on_metadata_inventory_take")) "on_metadata_inventory_take"))
return; return;
// Call function(pos, listname, index, count, player) // Call function(pos, listname, index, stack, player)
// pos // pos
push_v3s16(L, p); push_v3s16(L, p);
// listname // listname
lua_pushstring(L, listname.c_str()); lua_pushstring(L, listname.c_str());
// index // index
lua_pushinteger(L, index + 1); lua_pushinteger(L, index + 1);
// count // stack
lua_pushinteger(L, count); LuaItemStack::create(L, stack);
// player // player
objectref_get_or_create(L, player); objectref_get_or_create(L, player);
if(lua_pcall(L, 5, 0, 0)) if(lua_pcall(L, 5, 0, 0))
@ -6031,6 +6037,8 @@ int scriptapi_detached_inventory_allow_move(lua_State *L,
objectref_get_or_create(L, player); objectref_get_or_create(L, player);
if(lua_pcall(L, 7, 1, 0)) if(lua_pcall(L, 7, 1, 0))
script_error(L, "error: %s", lua_tostring(L, -1)); script_error(L, "error: %s", lua_tostring(L, -1));
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_move should return a number");
return luaL_checkinteger(L, -1); return luaL_checkinteger(L, -1);
} }
@ -6063,13 +6071,15 @@ int scriptapi_detached_inventory_allow_put(lua_State *L,
objectref_get_or_create(L, player); objectref_get_or_create(L, player);
if(lua_pcall(L, 5, 1, 0)) if(lua_pcall(L, 5, 1, 0))
script_error(L, "error: %s", lua_tostring(L, -1)); script_error(L, "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); return luaL_checkinteger(L, -1);
} }
// Return number of accepted items to be taken // Return number of accepted items to be taken
int scriptapi_detached_inventory_allow_take(lua_State *L, int scriptapi_detached_inventory_allow_take(lua_State *L,
const std::string &name, const std::string &name,
const std::string &listname, int index, int count, const std::string &listname, int index, ItemStack &stack,
ServerActiveObject *player) ServerActiveObject *player)
{ {
realitycheck(L); realitycheck(L);
@ -6078,9 +6088,9 @@ int scriptapi_detached_inventory_allow_take(lua_State *L,
// Push callback function on stack // Push callback function on stack
if(!get_detached_inventory_callback(L, name, "allow_take")) if(!get_detached_inventory_callback(L, name, "allow_take"))
return count; // All will be accepted return stack.count; // All will be accepted
// Call function(inv, listname, index, count, player) // Call function(inv, listname, index, stack, player)
// inv // inv
InventoryLocation loc; InventoryLocation loc;
loc.setDetached(name); loc.setDetached(name);
@ -6089,12 +6099,14 @@ int scriptapi_detached_inventory_allow_take(lua_State *L,
lua_pushstring(L, listname.c_str()); lua_pushstring(L, listname.c_str());
// index // index
lua_pushinteger(L, index + 1); lua_pushinteger(L, index + 1);
// count // stack
lua_pushinteger(L, count); LuaItemStack::create(L, stack);
// player // player
objectref_get_or_create(L, player); objectref_get_or_create(L, player);
if(lua_pcall(L, 5, 1, 0)) if(lua_pcall(L, 5, 1, 0))
script_error(L, "error: %s", lua_tostring(L, -1)); script_error(L, "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); return luaL_checkinteger(L, -1);
} }
@ -6168,7 +6180,7 @@ void scriptapi_detached_inventory_on_put(lua_State *L,
// Report taken items // Report taken items
void scriptapi_detached_inventory_on_take(lua_State *L, void scriptapi_detached_inventory_on_take(lua_State *L,
const std::string &name, const std::string &name,
const std::string &listname, int index, int count, const std::string &listname, int index, ItemStack &stack,
ServerActiveObject *player) ServerActiveObject *player)
{ {
realitycheck(L); realitycheck(L);
@ -6179,7 +6191,7 @@ void scriptapi_detached_inventory_on_take(lua_State *L,
if(!get_detached_inventory_callback(L, name, "on_take")) if(!get_detached_inventory_callback(L, name, "on_take"))
return; return;
// Call function(inv, listname, index, count, player) // Call function(inv, listname, index, stack, player)
// inv // inv
InventoryLocation loc; InventoryLocation loc;
loc.setDetached(name); loc.setDetached(name);
@ -6188,8 +6200,8 @@ void scriptapi_detached_inventory_on_take(lua_State *L,
lua_pushstring(L, listname.c_str()); lua_pushstring(L, listname.c_str());
// index // index
lua_pushinteger(L, index + 1); lua_pushinteger(L, index + 1);
// count // stack
lua_pushinteger(L, count); LuaItemStack::create(L, stack);
// player // player
objectref_get_or_create(L, player); objectref_get_or_create(L, player);
if(lua_pcall(L, 5, 0, 0)) if(lua_pcall(L, 5, 0, 0))

View File

@ -113,7 +113,7 @@ int scriptapi_nodemeta_inventory_allow_put(lua_State *L, v3s16 p,
ServerActiveObject *player); ServerActiveObject *player);
// Return number of accepted items to be taken // Return number of accepted items to be taken
int scriptapi_nodemeta_inventory_allow_take(lua_State *L, v3s16 p, int scriptapi_nodemeta_inventory_allow_take(lua_State *L, v3s16 p,
const std::string &listname, int index, int count, const std::string &listname, int index, ItemStack &stack,
ServerActiveObject *player); ServerActiveObject *player);
// Report moved items // Report moved items
void scriptapi_nodemeta_inventory_on_move(lua_State *L, v3s16 p, void scriptapi_nodemeta_inventory_on_move(lua_State *L, v3s16 p,
@ -126,7 +126,7 @@ void scriptapi_nodemeta_inventory_on_put(lua_State *L, v3s16 p,
ServerActiveObject *player); ServerActiveObject *player);
// Report taken items // Report taken items
void scriptapi_nodemeta_inventory_on_take(lua_State *L, v3s16 p, void scriptapi_nodemeta_inventory_on_take(lua_State *L, v3s16 p,
const std::string &listname, int index, int count, const std::string &listname, int index, ItemStack &stack,
ServerActiveObject *player); ServerActiveObject *player);
/* Detached inventory callbacks */ /* Detached inventory callbacks */
@ -144,7 +144,7 @@ int scriptapi_detached_inventory_allow_put(lua_State *L,
// Return number of accepted items to be taken // Return number of accepted items to be taken
int scriptapi_detached_inventory_allow_take(lua_State *L, int scriptapi_detached_inventory_allow_take(lua_State *L,
const std::string &name, const std::string &name,
const std::string &listname, int index, int count, const std::string &listname, int index, ItemStack &stack,
ServerActiveObject *player); ServerActiveObject *player);
// Report moved items // Report moved items
void scriptapi_detached_inventory_on_move(lua_State *L, void scriptapi_detached_inventory_on_move(lua_State *L,
@ -160,7 +160,7 @@ void scriptapi_detached_inventory_on_put(lua_State *L,
// Report taken items // Report taken items
void scriptapi_detached_inventory_on_take(lua_State *L, void scriptapi_detached_inventory_on_take(lua_State *L,
const std::string &name, const std::string &name,
const std::string &listname, int index, int count, const std::string &listname, int index, ItemStack &stack,
ServerActiveObject *player); ServerActiveObject *player);
/* luaentity */ /* luaentity */