diff options
Diffstat (limited to 'callbacks.lua')
-rw-r--r-- | callbacks.lua | 95 |
1 files changed, 45 insertions, 50 deletions
diff --git a/callbacks.lua b/callbacks.lua index 32a096c..4f9913c 100644 --- a/callbacks.lua +++ b/callbacks.lua @@ -3,45 +3,46 @@ minetest.register_on_joinplayer(function(player) local player_name = player:get_player_name() unified_inventory.players[player_name] = {} unified_inventory.current_index[player_name] = 1 - unified_inventory.filtered_items_list[player_name] = unified_inventory.items_list + unified_inventory.filtered_items_list[player_name] = + unified_inventory.items_list unified_inventory.activefilter[player_name] = "" unified_inventory.apply_filter(player, "") unified_inventory.alternate[player_name] = 1 unified_inventory.current_item[player_name] = nil - unified_inventory.set_inventory_formspec(player, unified_inventory.default) + unified_inventory.set_inventory_formspec(player, + unified_inventory.default) -- Crafting guide inventories - local inv = minetest.create_detached_inventory(player:get_player_name().."craftrecipe", { + local inv = minetest.create_detached_inventory(player_name.."craftrecipe", { allow_put = function(inv, listname, index, stack, player) return 0 end, allow_take = function(inv, listname, index, stack, player) - if unified_inventory.is_creative(player:get_player_name()) then - return stack:get_count() - else - return 0 - end + return 0 end, - allow_move = function(inv, from_list, from_index, to_list, to_index, count, player) + allow_move = function(inv, from_list, from_index, to_list, + to_index, count, player) return 0 end, }) inv:set_size("output", 1) - inv:set_size("build", 3 * 3) -- Refill slot local refill = minetest.create_detached_inventory(player_name.."refill", { allow_put = function(inv, listname, index, stack, player) - if unified_inventory.is_creative(player:get_player_name()) then + local player_name = player:get_player_name() + if unified_inventory.is_creative(player_name) then return stack:get_count() else return 0 end end, on_put = function(inv, listname, index, stack, player) + local player_name = player:get_player_name() stack:set_count(stack:get_stack_max()) inv:set_stack(listname, index, stack) - minetest.sound_play("electricity", {to_player=player_name, gain = 1.0}) + minetest.sound_play("electricity", + {to_player=player_name, gain = 1.0}) end, }) refill:set_size("main", 1) @@ -60,9 +61,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end -- Inventory page controls - local start = math.floor(unified_inventory.current_index[player_name] / 80 + 1) + local start = math.floor( + unified_inventory.current_index[player_name] / 80 + 1) local start_i = start - local pagemax = math.floor((unified_inventory.filtered_items_list_size[player_name] - 1) / (80) + 1) + local pagemax = math.floor( + (#unified_inventory.filtered_items_list[player_name] - 1) + / (80) + 1) if fields.start_list then minetest.sound_play("paperflip1", @@ -106,44 +110,37 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) unified_inventory.current_page[player_name]) end - -- Item list buttons - local list_index = unified_inventory.current_index[player_name] - local page = unified_inventory.current_page[player_name] - for i = 0, 80 do - local button = "item_button"..list_index - if fields[button] then - minetest.sound_play("click", - {to_player=player_name, gain = 0.1}) - if not unified_inventory.is_creative(player_name) then - unified_inventory.set_inventory_formspec(player, "craftguide") - page = "craftguide" - end - if page == "craftguide" then - unified_inventory.current_item[player_name] = - unified_inventory.filtered_items_list - [player_name][list_index] - unified_inventory.alternate[player_name] = 1 - unified_inventory.update_recipe(player, - unified_inventory.filtered_items_list - [player_name][list_index], 1) - unified_inventory.set_inventory_formspec(player, - unified_inventory.current_page[player_name]) - else - if unified_inventory.is_creative(player_name) then - local inv = player:get_inventory() - dst_stack = {} - dst_stack.name = unified_inventory.filtered_items_list - [player_name][list_index] - dst_stack.count = 99 - if inv:room_for_item("main", dst_stack) then - inv:add_item("main", dst_stack) - end + local clicked_item = nil + for name, value in pairs(fields) do + if string.sub(name, 1, 12) == "item_button_" then + clicked_item = string.sub(name, 13) + break + end + end + if clicked_item then + minetest.sound_play("click", + {to_player=player_name, gain = 0.1}) + local page = unified_inventory.current_page[player_name] + if not unified_inventory.is_creative(player_name) then + page = "craftguide" + end + if page == "craftguide" then + unified_inventory.current_item[player_name] = clicked_item + unified_inventory.alternate[player_name] = 1 + unified_inventory.set_inventory_formspec(player, + "craftguide") + else + if unified_inventory.is_creative(player_name) then + local inv = player:get_inventory() + local stack = ItemStack(clicked_item) + stack:set_count(99) + if inv:room_for_item("main", stack) then + inv:add_item("main", stack) end end end - list_index = list_index + 1 end - + if fields.searchbutton then unified_inventory.apply_filter(player, fields.searchbox) unified_inventory.set_inventory_formspec(player, @@ -170,8 +167,6 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) alternate = 1 end unified_inventory.alternate[player_name] = alternate - unified_inventory.update_recipe(player, - unified_inventory.current_item[player_name], alternate) unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name]) end |