mirror of
https://github.com/minetest-mods/i3.git
synced 2024-12-27 11:20:40 +01:00
More cleaning
This commit is contained in:
parent
25e57f6a23
commit
47e68a3e92
39
etc/api.lua
39
etc/api.lua
@ -2,7 +2,7 @@ local make_fs = i3.files.gui()
|
|||||||
|
|
||||||
local gmatch, match, split = i3.get("gmatch", "match", "split")
|
local gmatch, match, split = i3.get("gmatch", "match", "split")
|
||||||
local S, err, fmt, reg_items = i3.get("S", "err", "fmt", "reg_items")
|
local S, err, fmt, reg_items = i3.get("S", "err", "fmt", "reg_items")
|
||||||
local name_sort, count_sort, sort_inventory = i3.get("name_sort", "count_sort", "sort_inventory")
|
local sorter, sort_inventory = i3.get("sorter", "sort_inventory")
|
||||||
local sort, concat, copy, insert, remove = i3.get("sort", "concat", "copy", "insert", "remove")
|
local sort, concat, copy, insert, remove = i3.get("sort", "concat", "copy", "insert", "remove")
|
||||||
local true_str, true_table, is_str, is_func, is_table, clean_name =
|
local true_str, true_table, is_str, is_func, is_table, clean_name =
|
||||||
i3.get("true_str", "true_table", "is_str", "is_func", "is_table", "clean_name")
|
i3.get("true_str", "true_table", "is_str", "is_func", "is_table", "clean_name")
|
||||||
@ -307,40 +307,12 @@ function i3.add_sorting_method(def)
|
|||||||
insert(i3.sorting_methods, def)
|
insert(i3.sorting_methods, def)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function pre_sorting(list)
|
|
||||||
local new_inv, stack_meta = {}, {}
|
|
||||||
|
|
||||||
for i = 1, #list do
|
|
||||||
local stack = list[i]
|
|
||||||
local name = stack:get_name()
|
|
||||||
local count = stack:get_count()
|
|
||||||
local empty = stack:is_empty()
|
|
||||||
local meta = stack:get_meta():to_table()
|
|
||||||
local wear = stack:get_wear() > 0
|
|
||||||
|
|
||||||
if not empty then
|
|
||||||
if next(meta.fields) or wear then
|
|
||||||
stack_meta[#stack_meta + 1] = stack
|
|
||||||
else
|
|
||||||
new_inv[#new_inv + 1] = ItemStack(fmt("%s %u", name, count))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
for i = 1, #stack_meta do
|
|
||||||
new_inv[#new_inv + 1] = stack_meta[i]
|
|
||||||
end
|
|
||||||
|
|
||||||
return new_inv
|
|
||||||
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(list, data)
|
func = function(list, data)
|
||||||
local new_inv = pre_sorting(list)
|
sorter(list, data.reverse_sorting, 1)
|
||||||
name_sort(new_inv, data.reverse_sorting)
|
return list
|
||||||
return new_inv
|
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -348,8 +320,7 @@ 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(list, data)
|
func = function(list, data)
|
||||||
local new_inv = pre_sorting(list)
|
sorter(list, data.reverse_sorting, 2)
|
||||||
count_sort(new_inv, data.reverse_sorting)
|
return list
|
||||||
return new_inv
|
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
105
etc/common.lua
105
etc/common.lua
@ -1,6 +1,4 @@
|
|||||||
local translate = core.get_translated_string
|
local translate = core.get_translated_string
|
||||||
local insert, remove, sort, vec_add, vec_mul =
|
|
||||||
table.insert, table.remove, table.sort, vector.add, vector.mul
|
|
||||||
local fmt, find, gmatch, match, sub, split, lower =
|
local fmt, find, gmatch, match, sub, split, lower =
|
||||||
string.format, string.find, string.gmatch, string.match, string.sub, string.split, string.lower
|
string.format, string.find, string.gmatch, string.match, string.sub, string.split, string.lower
|
||||||
local reg_items, reg_nodes, reg_craftitems, reg_tools =
|
local reg_items, reg_nodes, reg_craftitems, reg_tools =
|
||||||
@ -178,7 +176,7 @@ local function table_eq(T1, T2)
|
|||||||
|
|
||||||
for k in pairs(t2) do
|
for k in pairs(t2) do
|
||||||
if is_table(k) then
|
if is_table(k) then
|
||||||
insert(t2kv, k)
|
table.insert(t2kv, k)
|
||||||
end
|
end
|
||||||
|
|
||||||
t2k[k] = true
|
t2k[k] = true
|
||||||
@ -191,7 +189,7 @@ local function table_eq(T1, T2)
|
|||||||
for i = 1, #t2kv do
|
for i = 1, #t2kv do
|
||||||
local tk = t2kv[i]
|
local tk = t2kv[i]
|
||||||
if table_eq(k1, tk) and recurse(v1, t2[tk]) then
|
if table_eq(k1, tk) and recurse(v1, t2[tk]) then
|
||||||
remove(t2kv, i)
|
table.remove(t2kv, i)
|
||||||
t2k[tk] = nil
|
t2k[tk] = nil
|
||||||
ok = true
|
ok = true
|
||||||
break
|
break
|
||||||
@ -346,35 +344,11 @@ local function spawn_item(player, stack)
|
|||||||
local dir = player:get_look_dir()
|
local dir = player:get_look_dir()
|
||||||
local ppos = player:get_pos()
|
local ppos = player:get_pos()
|
||||||
ppos.y = ppos.y + 1.625
|
ppos.y = ppos.y + 1.625
|
||||||
local look_at = vec_add(ppos, vec_mul(dir, 1))
|
local look_at = vector.add(ppos, vector.multiply(dir, 1))
|
||||||
|
|
||||||
core.add_item(look_at, stack)
|
core.add_item(look_at, stack)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function name_sort(inv, reverse)
|
|
||||||
return sort(inv, function(a, b)
|
|
||||||
a, b = a:get_name(), b:get_name()
|
|
||||||
|
|
||||||
if reverse then
|
|
||||||
return a > b
|
|
||||||
end
|
|
||||||
|
|
||||||
return a < b
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function count_sort(inv, reverse)
|
|
||||||
return sort(inv, function(a, b)
|
|
||||||
a, b = a:get_count(), b:get_count()
|
|
||||||
|
|
||||||
if reverse then
|
|
||||||
return a > b
|
|
||||||
end
|
|
||||||
|
|
||||||
return a < b
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function get_sorting_idx(name)
|
local function get_sorting_idx(name)
|
||||||
local idx = 1
|
local idx = 1
|
||||||
|
|
||||||
@ -387,20 +361,46 @@ local function get_sorting_idx(name)
|
|||||||
return idx
|
return idx
|
||||||
end
|
end
|
||||||
|
|
||||||
local function apply_sort(inv, size, data, new_inv, start_i)
|
local function sorter(inv, reverse, mode)
|
||||||
if not data.ignore_hotbar then
|
table.sort(inv, function(a, b)
|
||||||
inv:set_list("main", new_inv)
|
if mode == 1 then
|
||||||
return
|
a, b = a:get_name(), b:get_name()
|
||||||
|
else
|
||||||
|
a, b = a:get_count(), b:get_count()
|
||||||
end
|
end
|
||||||
|
|
||||||
for i = start_i, size do
|
if reverse then
|
||||||
local idx = i - start_i + 1
|
return a > b
|
||||||
inv:set_stack("main", i, new_inv[idx] or "")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return a < b
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function pre_sorting(list, start_i)
|
||||||
|
local new_inv, special = {}, {}
|
||||||
|
|
||||||
|
for i = start_i, #list do
|
||||||
|
local stack = list[i]
|
||||||
|
local empty = stack:is_empty()
|
||||||
|
local meta = stack:get_meta():to_table()
|
||||||
|
local wear = stack:get_wear() > 0
|
||||||
|
|
||||||
|
if not empty then
|
||||||
|
if next(meta.fields) or wear then
|
||||||
|
special[#special + 1] = stack
|
||||||
|
else
|
||||||
|
new_inv[#new_inv + 1] = stack
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
new_inv = table_merge(new_inv, special)
|
||||||
|
return new_inv
|
||||||
end
|
end
|
||||||
|
|
||||||
local function compress_items(list, start_i)
|
local function compress_items(list, start_i)
|
||||||
local new_inv, _new_inv, special = {}, {}, {}
|
local hash, new_inv, special = {}, {}, {}
|
||||||
|
|
||||||
for i = start_i, #list do
|
for i = start_i, #list do
|
||||||
local stack = list[i]
|
local stack = list[i]
|
||||||
@ -415,28 +415,25 @@ local function compress_items(list, start_i)
|
|||||||
if next(meta.fields) or wear or count >= stackmax then
|
if next(meta.fields) or wear or count >= stackmax then
|
||||||
special[#special + 1] = stack
|
special[#special + 1] = stack
|
||||||
else
|
else
|
||||||
new_inv[name] = new_inv[name] or 0
|
hash[name] = hash[name] or 0
|
||||||
new_inv[name] = new_inv[name] + count
|
hash[name] = hash[name] + count
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for name, count in pairs(new_inv) do
|
for name, count in pairs(hash) do
|
||||||
local stackmax = ItemStack(name):get_stack_max()
|
local stackmax = ItemStack(name):get_stack_max()
|
||||||
local iter = math.ceil(count / stackmax)
|
local iter = math.ceil(count / stackmax)
|
||||||
local leftover = count
|
local leftover = count
|
||||||
|
|
||||||
for _ = 1, iter do
|
for _ = 1, iter do
|
||||||
_new_inv[#_new_inv + 1] = ItemStack(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
|
||||||
|
|
||||||
for i = 1, #special do
|
new_inv = table_merge(new_inv, special)
|
||||||
_new_inv[#_new_inv + 1] = special[i]
|
return new_inv
|
||||||
end
|
|
||||||
|
|
||||||
return _new_inv
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function sort_inventory(player, data)
|
local function sort_inventory(player, data)
|
||||||
@ -447,13 +444,22 @@ local function sort_inventory(player, data)
|
|||||||
|
|
||||||
if data.inv_compress then
|
if data.inv_compress then
|
||||||
list = compress_items(list, start_i)
|
list = compress_items(list, start_i)
|
||||||
|
else
|
||||||
|
list = pre_sorting(list, start_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
local idx = get_sorting_idx(data.sort)
|
local idx = get_sorting_idx(data.sort)
|
||||||
local new_inv = i3.sorting_methods[idx].func(list, data)
|
local new_inv = i3.sorting_methods[idx].func(list, data)
|
||||||
|
if not new_inv then return end
|
||||||
|
|
||||||
if new_inv then
|
if not data.ignore_hotbar then
|
||||||
apply_sort(inv, size, data, new_inv, start_i)
|
inv:set_list("main", new_inv)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = start_i, size do
|
||||||
|
local index = i - start_i + 1
|
||||||
|
inv:set_stack("main", i, new_inv[index] or "")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -472,8 +478,7 @@ local _ = {
|
|||||||
|
|
||||||
-- Sorting
|
-- Sorting
|
||||||
search = search,
|
search = search,
|
||||||
name_sort = name_sort,
|
sorter = sorter,
|
||||||
count_sort = count_sort,
|
|
||||||
sort_inventory = sort_inventory,
|
sort_inventory = sort_inventory,
|
||||||
get_sorting_idx = get_sorting_idx,
|
get_sorting_idx = get_sorting_idx,
|
||||||
sort_by_category = sort_by_category,
|
sort_by_category = sort_by_category,
|
||||||
|
Loading…
Reference in New Issue
Block a user