diff --git a/README.md b/README.md index 2632f9c..5ea707e 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ It adds 3 button types to the craft page: - Craft All: crafting a stack and putting the result in the inventory - Patterns buttons: reorganizing items in the craft inventory following usual patterns - Rotation button: rotating the craft inventory content +- Clear button: clear the craft inventory content In `init.lua`, you can easily: Disable the functionalities you don't want @@ -16,6 +17,7 @@ Features can be toggled with the following settings (true per default): * `unified_inventory_plus.enable_craft_all` * `unified_inventory_plus.enable_craft_organize` * `unified_inventory_plus.enable_craft_rotate` +* `unified_inventory_plus.enable_craft_clear` ## Dependencies - [unified_inventory](https://github.com/minetest-mods/unified_inventory) diff --git a/craft_all.lua b/craft_all.lua index 768bad9..6406a3e 100644 --- a/craft_all.lua +++ b/craft_all.lua @@ -63,6 +63,9 @@ local function craft_craftall(player, formname, fields) -- Put a single stack for creative players and split the result for non creatives place_item_in_stacks(player, "main", result.item:get_name(), nb_res) player_inv:set_list("craft", decremented_input.items) + + -- log event! + minetest.log("action", player:get_player_name().." crafts "..result.item:get_name().." "..nb_res) end diff --git a/craft_clear.lua b/craft_clear.lua new file mode 100644 index 0000000..6f48d4d --- /dev/null +++ b/craft_clear.lua @@ -0,0 +1,54 @@ +-- Clear items in the craft inventory + + +-- Backup to inject code +unified_inventory_plus.craft_clear = unified_inventory.pages["craft"].get_formspec + +local function onload() + unified_inventory.pages["craft"] = { + get_formspec = function(player, perplayer_formspec) + local formspecy = perplayer_formspec.formspec_y + 1 + local formspec = unified_inventory_plus.craft_clear(player, perplayer_formspec).formspec + formspec = formspec.."image_button[1.25,"..(formspecy)..";0.75,0.75;pattern_clear.png;craft_clear;]" + return {formspec=formspec} + end, +} +end + +onload() + + +-- Rotate items in the craft inventory +local function craft_clear(player, formname, fields) + local player_inv = player:get_inventory() + local craft_list = player_inv:get_list("craft") + local remaining_craft_list = craft_list + + for k,v in pairs(craft_list) do + --minetest.chat_send_all(v:get_name().." "..v:get_count()) + if(v:get_count() > 0) then + local nb_left = room_left_for_item(player_inv:get_list("main"), v) + if(nb_left >= v:get_count()) then + place_item_in_stacks(player, "main", v:get_name(), v:get_count()) + remaining_craft_list[k]:clear() + else + place_item_in_stacks(player, "main", v:get_name(), nb_left) + remaining_craft_list[k]:set_count(v:get_count() - nb_left) + end + end + end + + player_inv:set_list("craft", remaining_craft_list) +end + + + + +minetest.register_on_player_receive_fields(function(player, formname, fields) + --if not formname:match("craft") then return end + for k, v in pairs(fields) do + if k:match("craft_clear") then + craft_clear(player, formname, fields) + end + end +end) diff --git a/functions.lua b/functions.lua index 6ddcfc5..d07e4f1 100644 --- a/functions.lua +++ b/functions.lua @@ -5,7 +5,6 @@ function room_left_for_item(list, item) local item_name = item:get_name() local room_left = 0 for k,v in pairs(list) do - minetest.chat_send_all("") if(v:get_name() == item_name) then room_left = room_left + v:get_free_space() elseif v:is_empty() then room_left = room_left + item:get_stack_max() end end @@ -40,7 +39,4 @@ function place_item_in_stacks(player, inv_name, item_name, nb_items) skyblock.feats.on_craft(ItemStack(item_name), player) end end - - -- log event! - minetest.log("action", player:get_player_name().." crafts "..item_name.." "..nb_items) end diff --git a/init.lua b/init.lua index 45320f7..52515cb 100644 --- a/init.lua +++ b/init.lua @@ -48,3 +48,7 @@ end if minetest.settings:get_bool("unified_inventory_plus.enable_craft_rotate", true) then dofile(modpath.."/craft_rotate.lua") end + +if minetest.settings:get_bool("unified_inventory_plus.enable_craft_clear", true) then + dofile(modpath.."/craft_clear.lua") +end diff --git a/textures/pattern_clear.png b/textures/pattern_clear.png new file mode 100644 index 0000000..fd558fd Binary files /dev/null and b/textures/pattern_clear.png differ