From 9eaf93d41d6745b877f8f52cf54b21050abefda1 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Wed, 25 Jul 2012 02:36:54 +0300 Subject: Detached inventory callbacks and reworked node metadata callbacks --- games/minimal/mods/default/init.lua | 63 ++++++++++++-------------------- games/minimal/mods/experimental/init.lua | 26 ++++++++++++- 2 files changed, 47 insertions(+), 42 deletions(-) (limited to 'games') diff --git a/games/minimal/mods/default/init.lua b/games/minimal/mods/default/init.lua index 428dfd9f4..fe7ab955e 100644 --- a/games/minimal/mods/default/init.lua +++ b/games/minimal/mods/default/init.lua @@ -1150,7 +1150,7 @@ minetest.register_node("default:chest", { on_construct = function(pos) local meta = minetest.env:get_meta(pos) meta:set_string("formspec", - "invsize[8,9;]".. + "size[8,9]".. "list[current_name;main;0,0;8,4;]".. "list[current_player;main;0,5;8,4;]") meta:set_string("infotext", "Chest") @@ -1162,25 +1162,6 @@ minetest.register_node("default:chest", { local inv = meta:get_inventory() return inv:is_empty("main") end, - on_metadata_inventory_move = function(pos, from_list, from_index, - to_list, to_index, count, player) - minetest.log("action", player:get_player_name().. - " moves stuff in chest at "..minetest.pos_to_string(pos)) - return minetest.node_metadata_inventory_move_allow_all( - pos, from_list, from_index, to_list, to_index, count, player) - end, - on_metadata_inventory_offer = function(pos, listname, index, stack, player) - minetest.log("action", player:get_player_name().. - " moves stuff to chest at "..minetest.pos_to_string(pos)) - return minetest.node_metadata_inventory_offer_allow_all( - pos, listname, index, stack, player) - end, - on_metadata_inventory_take = function(pos, listname, index, count, player) - minetest.log("action", player:get_player_name().. - " takes stuff from chest at "..minetest.pos_to_string(pos)) - return minetest.node_metadata_inventory_take_allow_all( - pos, listname, index, count, player) - end, }) local function has_locked_chest_privilege(meta, player) @@ -1207,7 +1188,7 @@ minetest.register_node("default:chest_locked", { on_construct = function(pos) local meta = minetest.env:get_meta(pos) meta:set_string("formspec", - "invsize[8,9;]".. + "size[8,9]".. "list[current_name;main;0,0;8,4;]".. "list[current_player;main;0,5;8,4;]") meta:set_string("infotext", "Locked Chest") @@ -1220,53 +1201,55 @@ minetest.register_node("default:chest_locked", { local inv = meta:get_inventory() return inv:is_empty("main") end, - on_metadata_inventory_move = function(pos, from_list, from_index, - to_list, to_index, count, player) + allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) local meta = minetest.env:get_meta(pos) if not has_locked_chest_privilege(meta, player) then minetest.log("action", player:get_player_name().. " tried to access a locked chest belonging to ".. meta:get_string("owner").." at ".. minetest.pos_to_string(pos)) - return + return 0 end - minetest.log("action", player:get_player_name().. - " moves stuff in locked chest at "..minetest.pos_to_string(pos)) - return minetest.node_metadata_inventory_move_allow_all( - pos, from_list, from_index, to_list, to_index, count, player) + return count end, - on_metadata_inventory_offer = function(pos, listname, index, stack, player) + allow_metadata_inventory_put = function(pos, listname, index, stack, player) local meta = minetest.env:get_meta(pos) if not has_locked_chest_privilege(meta, player) then minetest.log("action", player:get_player_name().. " tried to access a locked chest belonging to ".. meta:get_string("owner").." at ".. minetest.pos_to_string(pos)) - return stack + return 0 end - minetest.log("action", player:get_player_name().. - " moves stuff to locked chest at "..minetest.pos_to_string(pos)) - return minetest.node_metadata_inventory_offer_allow_all( - pos, listname, index, stack, player) + return stack:get_count() end, - on_metadata_inventory_take = function(pos, listname, index, count, player) + allow_metadata_inventory_take = function(pos, listname, index, count, player) local meta = minetest.env:get_meta(pos) if not has_locked_chest_privilege(meta, player) then minetest.log("action", player:get_player_name().. " tried to access a locked chest belonging to ".. meta:get_string("owner").." at ".. minetest.pos_to_string(pos)) - return + return 0 end + return count + end, + on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) + minetest.log("action", player:get_player_name().. + " moves stuff in locked chest at "..minetest.pos_to_string(pos)) + end, + on_metadata_inventory_put = function(pos, listname, index, stack, player) + minetest.log("action", player:get_player_name().. + " moves stuff to locked chest at "..minetest.pos_to_string(pos)) + end, + on_metadata_inventory_take = function(pos, listname, index, count, player) minetest.log("action", player:get_player_name().. " takes stuff from locked chest at "..minetest.pos_to_string(pos)) - return minetest.node_metadata_inventory_take_allow_all( - pos, listname, index, count, player) end, }) default.furnace_inactive_formspec = - "invsize[8,9;]".. + "size[8,9]".. "image[2,2;1,1;default_furnace_fire_bg.png]".. "list[current_name;fuel;2,3;1,1;]".. "list[current_name;src;2,1;1,1;]".. @@ -1405,7 +1388,7 @@ minetest.register_abm({ meta:set_string("infotext","Furnace active: "..percent.."%") hacky_swap_node(pos,"default:furnace_active") meta:set_string("formspec", - "invsize[8,9;]".. + "size[8,9]".. "image[2,2;1,1;default_furnace_fire_bg.png^[lowpart:".. (100-percent)..":default_furnace_fire_fg.png]".. "list[current_name;fuel;2,3;1,1;]".. diff --git a/games/minimal/mods/experimental/init.lua b/games/minimal/mods/experimental/init.lua index 498c64623..0b45aeb62 100644 --- a/games/minimal/mods/experimental/init.lua +++ b/games/minimal/mods/experimental/init.lua @@ -516,7 +516,7 @@ minetest.register_craft({ --[[minetest.register_on_joinplayer(function(player) minetest.after(3, function() - player:set_inventory_formspec("invsize[8,7.5;]".. + player:set_inventory_formspec("size[8,7.5]".. "image[1,0.6;1,2;player.png]".. "list[current_player;main;0,3.5;8,4;]".. "list[current_player;craft;3,0;3,3;]".. @@ -525,7 +525,29 @@ minetest.register_craft({ end)]] -- Create a detached inventory -local inv = minetest.create_detached_inventory("test_inventory") +local inv = minetest.create_detached_inventory("test_inventory", { + allow_move = function(inv, from_list, from_index, to_list, to_index, count, player) + experimental.print_to_everything("allow move asked") + return count -- Allow all + end, + allow_put = function(inv, listname, index, stack, player) + experimental.print_to_everything("allow put asked") + return 1 -- Allow only 1 + end, + allow_take = function(inv, listname, index, count, player) + experimental.print_to_everything("allow take asked") + return 4 -- Allow 4 at max + end, + on_move = function(inv, from_list, from_index, to_list, to_index, count, player) + experimental.print_to_everything(player:get_player_name().." moved items") + end, + on_put = function(inv, listname, index, stack, player) + experimental.print_to_everything(player:get_player_name().." put items") + end, + on_take = function(inv, listname, index, count, player) + experimental.print_to_everything(player:get_player_name().." took items") + end, +}) inv:set_size("main", 4*6) inv:add_item("main", "experimental:tester_tool_1") inv:add_item("main", "experimental:tnt 5") -- cgit v1.2.3