Add 'ignore hotbar' option for inventory sorting

This commit is contained in:
Jean-Patrick Guerrero 2021-11-01 15:12:32 +01:00
parent 9276598e3e
commit 1728f4beac
6 changed files with 49 additions and 55 deletions

9
API.md
View File

@ -251,12 +251,13 @@ Example:
i3.add_sorting_method { i3.add_sorting_method {
name = "test", name = "test",
description = "Cool sorting method", description = "Cool sorting method",
func = function(player, data) func = function(list, data)
local inv = player:get_inventory() -- `list`: inventory list
local list = inv:get_list("main") -- `data`: player data
table.sort(list) table.sort(list)
-- An array of items must be returned -- A list must be returned
return list return list
end, end,
} }

View File

@ -307,13 +307,10 @@ function i3.add_sorting_method(def)
insert(i3.sorting_methods, def) insert(i3.sorting_methods, def)
end end
local function pre_sorting(player) local function pre_sorting(list)
local inv = player:get_inventory()
local list = inv:get_list("main")
local size = inv:get_size("main")
local new_inv, stack_meta = {}, {} local new_inv, stack_meta = {}, {}
for i = 1, size do for i = 1, #list do
local stack = list[i] local stack = list[i]
local name = stack:get_name() local name = stack:get_name()
local count = stack:get_count() local count = stack:get_count()
@ -340,8 +337,8 @@ end
i3.add_sorting_method { i3.add_sorting_method {
name = "alphabetical", name = "alphabetical",
description = S"Sort items by name (A-Z)", description = S"Sort items by name (A-Z)",
func = function(player, data) func = function(list, data)
local new_inv = pre_sorting(player) local new_inv = pre_sorting(list)
name_sort(new_inv, data.reverse_sorting) name_sort(new_inv, data.reverse_sorting)
return new_inv return new_inv
end end
@ -350,8 +347,8 @@ i3.add_sorting_method {
i3.add_sorting_method { i3.add_sorting_method {
name = "numerical", name = "numerical",
description = S"Sort items by number of items per stack", description = S"Sort items by number of items per stack",
func = function(player, data) func = function(list, data)
local new_inv = pre_sorting(player) local new_inv = pre_sorting(list)
count_sort(new_inv, data.reverse_sorting) count_sort(new_inv, data.reverse_sorting)
return new_inv return new_inv
end, end,

View File

@ -387,13 +387,22 @@ local function get_sorting_idx(name)
return idx return idx
end end
local function compress_items(player) local function apply_sort(inv, size, data, new_inv, start_i)
local inv = player:get_inventory() if not data.ignore_hotbar then
local list = inv:get_list("main") inv:set_list("main", new_inv)
local size = inv:get_size("main") return
end
for i = start_i, size do
local idx = i - start_i + 1
inv:set_stack("main", i, new_inv[idx] or "")
end
end
local function compress_items(list, start_i)
local new_inv, _new_inv, special = {}, {}, {} local new_inv, _new_inv, special = {}, {}, {}
for i = 1, size do for i = start_i, #list do
local stack = list[i] local stack = list[i]
local name = stack:get_name() local name = stack:get_name()
local count = stack:get_count() local count = stack:get_count()
@ -418,7 +427,7 @@ local function compress_items(player)
local leftover = count local leftover = count
for _ = 1, iter do for _ = 1, iter do
_new_inv[#_new_inv + 1] = fmt("%s %u", name, math.min(stackmax, leftover)) _new_inv[#_new_inv + 1] = ItemStack(fmt("%s %u", name, math.min(stackmax, leftover)))
leftover = leftover - stackmax leftover = leftover - stackmax
end end
end end
@ -427,12 +436,17 @@ local function compress_items(player)
_new_inv[#_new_inv + 1] = special[i] _new_inv[#_new_inv + 1] = special[i]
end end
inv:set_list("main", _new_inv) return _new_inv
end end
local function sort_inventory(player, data) local function sort_inventory(player, data)
local inv = player:get_inventory()
local list = inv:get_list("main")
local size = inv:get_size("main")
local start_i = data.ignore_hotbar and 10 or 1
if data.inv_compress then if data.inv_compress then
compress_items(player) list = compress_items(list, start_i)
end end
local sorts = {} local sorts = {}
@ -441,11 +455,10 @@ local function sort_inventory(player, data)
sorts[def.name] = def.func sorts[def.name] = def.func
end end
local new_inv = sorts[data.sort](player, data) local new_inv = sorts[data.sort](list, data)
if new_inv then if new_inv then
local inv = player:get_inventory() apply_sort(inv, size, data, new_inv, start_i)
inv:set_list("main", new_inv)
end end
end end

View File

@ -461,7 +461,7 @@ local function show_popup(fs, data)
fs("image_button", 7.65, 10.6, 0.35, 0.35, "", "next_sort", "") fs("image_button", 7.65, 10.6, 0.35, 0.35, "", "next_sort", "")
fs("style[sort_method;font=bold;font_size=20]") fs("style[sort_method;font=bold;font_size=20]")
fs("button", 2.55, 10.35, 5.1, 0.8, "sort_method", data.sort:gsub("^%l", upper)) fs("button", 2.55, 10.36, 5.1, 0.8, "sort_method", data.sort:gsub("^%l", upper))
local idx = get_sorting_idx(data.sort) local idx = get_sorting_idx(data.sort)
local desc = i3.sorting_methods[idx].description local desc = i3.sorting_methods[idx].description
@ -471,14 +471,10 @@ local function show_popup(fs, data)
end end
elseif show_misc then elseif show_misc then
fs("checkbox", 2.4, 10.05, fs("checkbox", 2.4, 10.05, "cb_inv_compress", "Compression", tostring(data.inv_compress))
"inv_compress", ES"Inventory compression", tostring(data.inv_compress)) fs("checkbox", 2.4, 10.5, "cb_reverse_sorting", "Reverse sorting", tostring(data.reverse_sorting))
fs("checkbox", 2.4, 10.95, "cb_auto_sorting", "Automatic sorting", tostring(data.auto_sorting))
fs("checkbox", 2.4, 10.5, fs("checkbox", 5.4, 10.05, "cb_ignore_hotbar", "Ignore hotbar", tostring(data.ignore_hotbar))
"auto_sorting", ES"Automatic sorting", tostring(data.auto_sorting))
fs("checkbox", 2.4, 10.95,
"reverse_sorting", ES"Reverse sorting", tostring(data.reverse_sorting))
end end
end end
end end

View File

@ -106,6 +106,14 @@ i3.new_tab {
data.subcat = indexof(i3.SUBCAT, sub(field, 5)) data.subcat = indexof(i3.SUBCAT, sub(field, 5))
break break
elseif sub(field, 1, 3) == "cb_" then
local str = sub(field, 4)
data[str] = false
if fields[field] == "true" then
data[str] = true
end
elseif sub(field, 1, 8) == "setting_" then elseif sub(field, 1, 8) == "setting_" then
data.show_setting = match(field, "_(%w+)$") data.show_setting = match(field, "_(%w+)$")
@ -192,27 +200,6 @@ i3.new_tab {
data.sort = i3.sorting_methods[idx].name data.sort = i3.sorting_methods[idx].name
elseif fields.inv_compress then
data.inv_compress = false
if fields.inv_compress == "true" then
data.inv_compress = true
end
elseif fields.auto_sorting then
data.auto_sorting = false
if fields.auto_sorting == "true" then
data.auto_sorting = true
end
elseif fields.reverse_sorting then
data.reverse_sorting = false
if fields.reverse_sorting == "true" then
data.reverse_sorting = true
end
elseif fields.home then elseif fields.home then
if not data.home then if not data.home then
return msg(name, "No home set") return msg(name, "No home set")

View File

@ -151,6 +151,7 @@ local function init_data(player, info)
data.favs = {} data.favs = {}
data.sort = "alphabetical" data.sort = "alphabetical"
data.show_setting = "home" data.show_setting = "home"
data.ignore_hotbar = false
data.auto_sorting = false data.auto_sorting = false
data.reverse_sorting = false data.reverse_sorting = false
data.inv_compress = true data.inv_compress = true
@ -159,7 +160,6 @@ local function init_data(player, info)
data.current_itab = 1 data.current_itab = 1
data.subcat = 1 data.subcat = 1
data.scrbar_inv = 0 data.scrbar_inv = 0
data.compress = true
data.lang_code = get_lang_code(info) data.lang_code = get_lang_code(info)
data.fs_version = info.formspec_version data.fs_version = info.formspec_version