Skip to content

Commit 026b25f

Browse files
committed
rewrite pattern & fix a bug in craft
FIX: the crafting process could continue on another recipe.
1 parent c9a07ba commit 026b25f

File tree

8 files changed

+73
-57
lines changed

8 files changed

+73
-57
lines changed

craft_all.lua

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,6 @@ local function infer_width(list, expected)
4848
end
4949

5050

51-
-- InvRef :room_for_item() does not check for multiple stacks need. That's the purpose of this function
52-
local function room_left_for_item(list, item)
53-
local item_name = item:get_name()
54-
local room_left = 0
55-
for k,v in pairs(list) do
56-
minetest.chat_send_all("")
57-
if(v:get_name() == item_name) then room_left = room_left + v:get_free_space()
58-
elseif v:is_empty() then room_left = room_left + item:get_stack_max() end
59-
end
60-
return room_left
61-
end
62-
63-
6451
-- Craft max possible items and put the result in the main inventory
6552
local function craft_craftall(player, formname, fields)
6653
local player_inv = player:get_inventory()
@@ -75,25 +62,17 @@ local function craft_craftall(player, formname, fields)
7562
if room_left == 0 then return end
7663

7764
-- While there are ingredients & room, craft !
65+
local expected_type_name = tmp_result.item:get_name()
7866
local no_stack_limit = minetest.get_player_privs(player:get_player_name()).creative and not tmp_result.item:get_stack_max() == 1
79-
local nb_res, result, decremented_input = 0, tmp_result, craft_list
80-
while not tmp_result.item:is_empty() and (no_stack_limit or nb_res + tmp_result.item:get_count() <= room_left) do
67+
local nb_res, result, decremented_input = 0, tmp_result, tmp_inv
68+
while not tmp_result.item:is_empty() and tmp_result.item:get_name() == expected_type_name and (no_stack_limit or nb_res + tmp_result.item:get_count() <= room_left) do
8169
nb_res = nb_res + tmp_result.item:get_count()
8270
decremented_input = tmp_inv
83-
tmp_result, tmp_inv = minetest.get_craft_result({ method = "normal", width = craft_width, items = decremented_input.items})
71+
tmp_result, tmp_inv = minetest.get_craft_result(decremented_input)
8472
end
8573

8674
-- Put a single stack for creative players and split the result for non creatives
87-
if no_stack_limit then
88-
player_inv:add_item("main", result.item:get_name().." "..nb_res)
89-
else
90-
local nb_stacks = math.floor(nb_res / result.item:get_stack_max())
91-
local remaining = nb_res % result.item:get_stack_max()
92-
for i=1,nb_stacks do
93-
player_inv:add_item("main", result.item:get_name().." "..result.item:get_stack_max())
94-
end
95-
if remaining ~= 0 then player_inv:add_item("main", result.item:get_name().." "..remaining) end
96-
end
75+
place_item_in_stacks(player, "main", result.item:get_name(), nb_res)
9776
player_inv:set_list("craft", decremented_input.items)
9877
end
9978

craft_organize.lua

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
-- Organize items in the craft inventory following a pattern:
2-
-- 1 Compact stacks
3-
-- 3 Fill the first line
4-
-- 4 Fill a square
5-
-- 6 Fill a 'stair pattern'
6-
-- 8 Fill a circle
7-
-- 9 Equal repartition in the grid
2+
3+
4+
5+
-- Pattern struct: (Comment unwanted ones & organize on your wish. Buttons placed left to right then up)
6+
-- Inventory indexes to fill (then others for non creatives)
7+
local craft_patterns = {
8+
{ico="1.png" , pattern={1}},
9+
{ico="3.png" , pattern={1,2,3}},
10+
{ico="3b.png", pattern={1,4,7}},
11+
{ico="4.png" , pattern={1,2,4,5}},
12+
{ico="5.png" , pattern={1,3,5,7,9}},
13+
{ico="6.png" , pattern={1,4,5,7,8,9}},
14+
{ico="6b.png", pattern={1,2,3,4,5,6}},
15+
{ico="7.png" , pattern={1,2,3,4,5,6,8}},
16+
{ico="8.png" , pattern={1,2,3,4,6,7,8,9}},
17+
{ico="9.png" , pattern={1,2,3,4,5,6,7,8,9}}
18+
}
819

920

1021
local S = unified_inventory.gettext
@@ -18,13 +29,9 @@ local function onload()
1829
get_formspec = function(player, perplayer_formspec)
1930
local formspecy = perplayer_formspec.formspec_y
2031
local formspec = unified_inventory_plus.craft_organize(player, perplayer_formspec).formspec
21-
--formspec = formspec.."label[3.0,"..(formspecy - 2.0)..";Organize:]"
22-
formspec = formspec.."image_button[2.0,"..(formspecy - 0.5)..";0.5,0.5;1.png;craft_organize_1;]"
23-
formspec = formspec.."image_button[2.5,"..(formspecy - 0.5)..";0.5,0.5;3.png;craft_organize_3;]"
24-
formspec = formspec.."image_button[3.0,"..(formspecy - 0.5)..";0.5,0.5;4.png;craft_organize_4;]"
25-
formspec = formspec.."image_button[3.5,"..(formspecy - 0.5)..";0.5,0.5;6.png;craft_organize_6;]"
26-
formspec = formspec.."image_button[4.0,"..(formspecy - 0.5)..";0.5,0.5;8.png;craft_organize_8;]"
27-
formspec = formspec.."image_button[4.5,"..(formspecy - 0.5)..";0.5,0.5;9.png;craft_organize_9;]"
32+
for i,v in ipairs(craft_patterns) do
33+
formspec = formspec.."image_button["..(2.0 + 0.5 * ((i-1)%6))..","..(formspecy - 0.5 * math.ceil(i/6))..";0.5,0.5;"..v.ico..";craft_organize_"..i..";]"
34+
end
2835
return {formspec=formspec}
2936
end,
3037
}
@@ -33,32 +40,23 @@ end
3340
onload()
3441

3542

36-
-- inventory indexes to fill (then others for non creatives)
37-
local craft_patterns = {
38-
["1"]={1},
39-
["3"]={1,2,3},
40-
["4"]={1,2,4,5},
41-
["6"]={1,4,5,7,8,9},
42-
["8"]={1,2,3,4,6,7,8,9},
43-
["9"]={1,2,3,4,5,6,7,8,9}
44-
}
45-
4643

47-
-- Return if there is only one type and the item type name in the StackItems list.
44+
-- Return if there is only one type and the item type name in the StackItems list (nil if none)
4845
local function get_type_infos(craft_list)
4946
local item = nil
5047
for j=0,2 do
5148
for i=1,3 do
5249
if not craft_list[3*j+i]:is_empty() then
5350
if item == nil then item = craft_list[3*j+i]
54-
elseif item:get_name() ~= craft_list[3*j+i]:get_name() then return false, item:get_name()
51+
elseif item:get_name() ~= craft_list[3*j+i]:get_name() then return false, ""
5552
end
5653
end
5754
end
5855
end
5956
return true, (item ~= nil) and item:get_name() or nil
6057
end
6158

59+
-- Sum the craft_list items
6260
local function get_total_amount(craft_list)
6361
local nb = 0
6462
for j=0,2 do
@@ -90,24 +88,26 @@ local function craft_organize(player, formname, fields)
9088
if not type_name then return end -- craft is empty
9189
if not only_one_type then minetest.chat_send_player(player_name, "You can only organize one type of item.") return end
9290

93-
-- Don't exceed 9*99 for non creative players (it shouldn't but who knows ...)
91+
-- Don't exceed 9*99 for non creative players. It shouldn't happen but avoids potential losses then
9492
local total_amount = get_total_amount(craft_list)
9593
if not is_creative and total_amount > 891 then minetest.chat_send_player(player_name, "There are too many items to organize ! Have less than 9 x 99 items.") return end
9694

9795
local res = {ItemStack(type_name),ItemStack(type_name),ItemStack(type_name),ItemStack(type_name),ItemStack(type_name),ItemStack(type_name),ItemStack(type_name),ItemStack(type_name),ItemStack(type_name)}
98-
for i=1,9 do res[i]:set_count(0) end -- doing this cause empty ItemStack constructor in list crashes the game :S
96+
for i=1,9 do res[i]:set_count(0) end -- Doing this because using empty ItemStack in list constructor crashes the game :S
9997

100-
local nb_stacks = tonumber(pattern_id)
98+
local pattern = craft_patterns[tonumber(pattern_id)].pattern
99+
local nb_stacks = 0
100+
for i in pairs(pattern) do nb_stacks = nb_stacks + 1 end
101101
local stack_size = math.floor(total_amount / nb_stacks)
102102
if not is_creative then stack_size = math.min(stack_size, ItemStack(type_name):get_stack_max()) end -- limit stacks to get_stack_max() for non creatives
103-
local remaining = total_amount - nb_stacks * stack_size -- no % nb_stacks if limit
103+
local remaining = total_amount - nb_stacks * stack_size -- no % nb_stacks if limit: remaining could be greater than a stack
104104

105105
for i=1,nb_stacks do
106-
res[craft_patterns[pattern_id][i]]:add_item(type_name.." "..stack_size)
106+
res[pattern[i]]:add_item(type_name.." "..stack_size)
107107
end
108108

109109
player_inv:set_list("craft", res)
110-
player_inv:add_item("craft", type_name.." "..remaining)
110+
place_item_in_stacks(player, "craft", type_name, remaining)
111111
end
112112

113113

functions.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
3+
-- InvRef :room_for_item() does not check for multiple stacks need. That's the purpose of this function
4+
function room_left_for_item(list, item)
5+
local item_name = item:get_name()
6+
local room_left = 0
7+
for k,v in pairs(list) do
8+
minetest.chat_send_all("")
9+
if(v:get_name() == item_name) then room_left = room_left + v:get_free_space()
10+
elseif v:is_empty() then room_left = room_left + item:get_stack_max() end
11+
end
12+
return room_left
13+
end
14+
15+
16+
17+
-- Add items to the inventory, splitting in stacks if necessary
18+
-- Have to separate item_name & nb_items instead of using an Itemstack in the case you want to add many not stackable ItemStack (keys, filled buckets...)
19+
function place_item_in_stacks(player, inv_name, item_name, nb_items)
20+
local player_inv = player:get_inventory()
21+
assert(player_inv)
22+
local stack_max = ItemStack(item_name):get_stack_max()
23+
24+
-- Put a single stack for creative players and split the result for non creatives
25+
if minetest.get_player_privs(player:get_player_name()).creative and not stack_max == 1 then
26+
player_inv:add_item(inv_name, ItemStack(item_name.." "..nb_items))
27+
else
28+
local nb_stacks = math.floor(nb_items / stack_max)
29+
local remaining = nb_items % stack_max
30+
for i=1,nb_stacks do
31+
player_inv:add_item(inv_name, item_name.." "..stack_max)
32+
end
33+
if remaining ~= 0 then player_inv:add_item(inv_name, item_name.." "..remaining) end
34+
end
35+
end

init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ local worldpath = minetest.get_worldpath()
66
unified_inventory_plus = {}
77

88

9+
dofile(modpath.."/functions.lua")
10+
911
-- Functionalities are independants.
1012
-- Comment the following lines to disable those you don't want
1113
dofile(modpath.."/craft_all.lua")

textures/3b.png

249 Bytes
Loading

textures/5.png

248 Bytes
Loading

textures/6b.png

245 Bytes
Loading

textures/7.png

246 Bytes
Loading

0 commit comments

Comments
 (0)