Skip to content

Commit

Permalink
add clear button
Browse files Browse the repository at this point in the history
add clear button
  • Loading branch information
bousket committed Dec 27, 2019
1 parent f861288 commit 2b09f0f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions craft_all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
54 changes: 54 additions & 0 deletions craft_clear.lua
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 0 additions & 4 deletions functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file added textures/pattern_clear.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2b09f0f

Please sign in to comment.