mirror of
https://github.com/minetest-mods/i3.git
synced 2024-12-26 10:50:42 +01:00
Add 'ignore hotbar' option for inventory sorting
This commit is contained in:
parent
9276598e3e
commit
1728f4beac
9
API.md
9
API.md
@ -251,12 +251,13 @@ Example:
|
||||
i3.add_sorting_method {
|
||||
name = "test",
|
||||
description = "Cool sorting method",
|
||||
func = function(player, data)
|
||||
local inv = player:get_inventory()
|
||||
local list = inv:get_list("main")
|
||||
func = function(list, data)
|
||||
-- `list`: inventory list
|
||||
-- `data`: player data
|
||||
|
||||
table.sort(list)
|
||||
|
||||
-- An array of items must be returned
|
||||
-- A list must be returned
|
||||
return list
|
||||
end,
|
||||
}
|
||||
|
15
etc/api.lua
15
etc/api.lua
@ -307,13 +307,10 @@ function i3.add_sorting_method(def)
|
||||
insert(i3.sorting_methods, def)
|
||||
end
|
||||
|
||||
local function pre_sorting(player)
|
||||
local inv = player:get_inventory()
|
||||
local list = inv:get_list("main")
|
||||
local size = inv:get_size("main")
|
||||
local function pre_sorting(list)
|
||||
local new_inv, stack_meta = {}, {}
|
||||
|
||||
for i = 1, size do
|
||||
for i = 1, #list do
|
||||
local stack = list[i]
|
||||
local name = stack:get_name()
|
||||
local count = stack:get_count()
|
||||
@ -340,8 +337,8 @@ end
|
||||
i3.add_sorting_method {
|
||||
name = "alphabetical",
|
||||
description = S"Sort items by name (A-Z)",
|
||||
func = function(player, data)
|
||||
local new_inv = pre_sorting(player)
|
||||
func = function(list, data)
|
||||
local new_inv = pre_sorting(list)
|
||||
name_sort(new_inv, data.reverse_sorting)
|
||||
return new_inv
|
||||
end
|
||||
@ -350,8 +347,8 @@ i3.add_sorting_method {
|
||||
i3.add_sorting_method {
|
||||
name = "numerical",
|
||||
description = S"Sort items by number of items per stack",
|
||||
func = function(player, data)
|
||||
local new_inv = pre_sorting(player)
|
||||
func = function(list, data)
|
||||
local new_inv = pre_sorting(list)
|
||||
count_sort(new_inv, data.reverse_sorting)
|
||||
return new_inv
|
||||
end,
|
||||
|
@ -387,13 +387,22 @@ local function get_sorting_idx(name)
|
||||
return idx
|
||||
end
|
||||
|
||||
local function compress_items(player)
|
||||
local inv = player:get_inventory()
|
||||
local list = inv:get_list("main")
|
||||
local size = inv:get_size("main")
|
||||
local function apply_sort(inv, size, data, new_inv, start_i)
|
||||
if not data.ignore_hotbar then
|
||||
inv:set_list("main", new_inv)
|
||||
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 = {}, {}, {}
|
||||
|
||||
for i = 1, size do
|
||||
for i = start_i, #list do
|
||||
local stack = list[i]
|
||||
local name = stack:get_name()
|
||||
local count = stack:get_count()
|
||||
@ -418,7 +427,7 @@ local function compress_items(player)
|
||||
local leftover = count
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
@ -427,12 +436,17 @@ local function compress_items(player)
|
||||
_new_inv[#_new_inv + 1] = special[i]
|
||||
end
|
||||
|
||||
inv:set_list("main", _new_inv)
|
||||
return _new_inv
|
||||
end
|
||||
|
||||
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
|
||||
compress_items(player)
|
||||
list = compress_items(list, start_i)
|
||||
end
|
||||
|
||||
local sorts = {}
|
||||
@ -441,11 +455,10 @@ local function sort_inventory(player, data)
|
||||
sorts[def.name] = def.func
|
||||
end
|
||||
|
||||
local new_inv = sorts[data.sort](player, data)
|
||||
local new_inv = sorts[data.sort](list, data)
|
||||
|
||||
if new_inv then
|
||||
local inv = player:get_inventory()
|
||||
inv:set_list("main", new_inv)
|
||||
apply_sort(inv, size, data, new_inv, start_i)
|
||||
end
|
||||
end
|
||||
|
||||
|
14
etc/gui.lua
14
etc/gui.lua
@ -461,7 +461,7 @@ local function show_popup(fs, data)
|
||||
fs("image_button", 7.65, 10.6, 0.35, 0.35, "", "next_sort", "")
|
||||
|
||||
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 desc = i3.sorting_methods[idx].description
|
||||
@ -471,14 +471,10 @@ local function show_popup(fs, data)
|
||||
end
|
||||
|
||||
elseif show_misc then
|
||||
fs("checkbox", 2.4, 10.05,
|
||||
"inv_compress", ES"Inventory compression", tostring(data.inv_compress))
|
||||
|
||||
fs("checkbox", 2.4, 10.5,
|
||||
"auto_sorting", ES"Automatic sorting", tostring(data.auto_sorting))
|
||||
|
||||
fs("checkbox", 2.4, 10.95,
|
||||
"reverse_sorting", ES"Reverse sorting", tostring(data.reverse_sorting))
|
||||
fs("checkbox", 2.4, 10.05, "cb_inv_compress", "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", 5.4, 10.05, "cb_ignore_hotbar", "Ignore hotbar", tostring(data.ignore_hotbar))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -106,6 +106,14 @@ i3.new_tab {
|
||||
data.subcat = indexof(i3.SUBCAT, sub(field, 5))
|
||||
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
|
||||
data.show_setting = match(field, "_(%w+)$")
|
||||
|
||||
@ -192,27 +200,6 @@ i3.new_tab {
|
||||
|
||||
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
|
||||
if not data.home then
|
||||
return msg(name, "No home set")
|
||||
|
2
init.lua
2
init.lua
@ -151,6 +151,7 @@ local function init_data(player, info)
|
||||
data.favs = {}
|
||||
data.sort = "alphabetical"
|
||||
data.show_setting = "home"
|
||||
data.ignore_hotbar = false
|
||||
data.auto_sorting = false
|
||||
data.reverse_sorting = false
|
||||
data.inv_compress = true
|
||||
@ -159,7 +160,6 @@ local function init_data(player, info)
|
||||
data.current_itab = 1
|
||||
data.subcat = 1
|
||||
data.scrbar_inv = 0
|
||||
data.compress = true
|
||||
data.lang_code = get_lang_code(info)
|
||||
data.fs_version = info.formspec_version
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user