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 diff --git a/internal.lua b/internal.lua index 6d22530..cbd134f 100644 --- a/internal.lua +++ b/internal.lua @@ -47,8 +47,9 @@ function unified_inventory.get_formspec(player, page) -- Items list local list_index = unified_inventory.current_index[player_name] local page = math.floor(list_index / (80) + 1) - local pagemax = math.floor((unified_inventory.filtered_items_list_size[player_name] - 1) / (80) + 1) - local image = nil + local pagemax = math.floor( + (#unified_inventory.filtered_items_list[player_name] - 1) + / (80) + 1) local item = {} for y = 0, 9 do for x = 0, 7 do @@ -57,8 +58,8 @@ function unified_inventory.get_formspec(player, page) formspec = formspec.."item_image_button[" ..(8.2 + x * 0.7).."," ..(1 + y * 0.7)..";.81,.81;" - ..name..";item_button" - ..list_index..";]" + ..name..";item_button_" + ..name..";]" list_index = list_index + 1 end end @@ -112,102 +113,6 @@ function unified_inventory.apply_filter(player, filter) unified_inventory.current_page[player_name]) end - --- update_recipe -function unified_inventory.update_recipe(player, stack_name, alternate) - local inv = minetest.get_inventory({ - type = "detached", - name = player:get_player_name().."craftrecipe" - }) - for i = 0, inv:get_size("build") do - inv:set_stack("build", i, nil) - end - inv:set_stack("output", 1, nil) - alternate = tonumber(alternate) or 1 - local crafts = unified_inventory.crafts_table[stack_name] - --print(dump(crafts)) - if next(crafts) == nil then -- No craft recipes - return - end - if alternate < 1 or alternate > #crafts then - alternate = 1 - end - local craft = crafts[alternate] - inv:set_stack("output", 1, craft.output) - local items = craft.items - - if craft.type == "cooking" or - craft.type == "fuel" or - craft.type == "grinding" or - craft.type == "extracting" or - craft.type == "compressing" then - def = unified_inventory.find_item_def(craft["items"][1]) - if def then - inv:set_stack("build", 1, def) - end - return - end - if craft.width == 0 then - for i = 1, 3 do - if craft.items[i] then - def = unified_inventory.find_item_def(craft.items[i]) - if def then - inv:set_stack("build", i, def) - end - end - end - end - if craft.width == 1 then - local build_table={1, 4, 7} - for i = 1, 3 do - if craft.items[i] then - def = unified_inventory.find_item_def(craft.items[i]) - if def then - inv:set_stack("build", build_table[i], def) - end - end - end - end - if craft.width == 2 then - local build_table = {1, 2, 4, 5, 7, 8} - for i=1, 6 do - if craft.items[i] then - def = unified_inventory.find_item_def(craft.items[i]) - if def then - inv:set_stack("build", build_table[i], def) - end - end - end - end - if craft.width == 3 then - for i=1, 9 do - if craft.items[i] then - def = unified_inventory.find_item_def(craft.items[i]) - if def then - inv:set_stack("build", i, def) - end - end - end - end -end - -function unified_inventory.find_item_def(def) - if type(def) ~= "string" then - return nil - end - if string.find(def, "group:") then - def = string.gsub(def, "group:", "") - def = string.gsub(def, "\"", "") - if minetest.registered_nodes["default:"..def] then - return "default:"..def - end - local items = unified_inventory.items_in_group(def) - return items[1] - else - return def - end -end - function unified_inventory.items_in_group(groups) local items = {} for name, item in pairs(minetest.registered_items) do diff --git a/register.lua b/register.lua index ad85537..9ca1f77 100644 --- a/register.lua +++ b/register.lua @@ -1,6 +1,6 @@ minetest.register_privilege("creative", { - description="Can use the creative inventory", + description = "Can use the creative inventory", give_to_singleplayer = false, }) @@ -137,27 +137,27 @@ unified_inventory.register_page("craftguide", { local player_name = player:get_player_name() formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_craftguide_form.png]" formspec = formspec.."label[0,0;Crafting Guide]" - formspec = formspec.."list[detached:"..player_name.."craftrecipe;build;2,1;3,3;]" formspec = formspec.."list[detached:"..player_name.."craftrecipe;output;6,1;1,1;]" formspec = formspec.."label[2,0.5;Input:]" formspec = formspec.."label[6,0.5;Output:]" formspec = formspec.."label[6,2.6;Method:]" local item_name = unified_inventory.current_item[player_name] + local craft = nil if item_name then formspec = formspec.."label[2,0;"..item_name.."]" local alternates = 0 local alternate = unified_inventory.alternate[player_name] local crafts = unified_inventory.crafts_table[item_name] - if crafts ~= nil and #crafts > 0 then alternates = #crafts - local craft = crafts[alternate] + craft = crafts[alternate] local method = craft.type - if craft.type == "shapeless" then - method="shapeless crafting" - end - if craft.type == "alloy" then - method="alloy cooking" + if craft.type == "normal" then + method = "crafting" + elseif craft.type == "shapeless" then + method = "shapeless crafting" + elseif craft.type == "alloy" then + method = "alloy cooking" end formspec = formspec.."label[6,3;"..method.."]" end @@ -168,6 +168,47 @@ unified_inventory.register_page("craftguide", { formspec = formspec.."button[0,3.15;2,1;alternate;Alternate]" end end + + local craftinv = minetest.get_inventory({ + type = "detached", + name = player_name.."craftrecipe" + }) + + if not craft then + craftinv:set_stack("output", 1, nil) + return formspec + end + + craftinv:set_stack("output", 1, craft.output) + + local width = craft.width + if width == 0 then + -- Shapeless recipe + width = 3 + end + + local i = 1 + for y = 1, 3 do + for x = 1, width do + local item = craft.items[i] + if item then + if string.sub(item, 1, 6) == "group:" then + local group = string.sub(item, 7) + formspec = formspec.."image_button[" + ..(1.05 + x)..","..(0.05 + y)..";0.9,0.9;" + .."ui_group.png;;" + ..minetest.formspec_escape(group).."]" + else + formspec = formspec.."item_image_button[" + ..(1.05 + x)..","..(0.05 + y)..";0.9,0.9;" + ..minetest.formspec_escape(item)..";" + .."item_button_" + ..minetest.formspec_escape(item)..";]" + end + end + i = i + 1 + end + end return formspec end, }) diff --git a/textures/ui_group.png b/textures/ui_group.png new file mode 100644 index 0000000..8de5a77 Binary files /dev/null and b/textures/ui_group.png differ