mirror of
https://github.com/minetest-mods/unified_inventory.git
synced 2024-08-20 05:14:09 +02:00
Add sorting inventories function.
This commit is contained in:
parent
44d967bbe5
commit
3003b19322
45
internal.lua
45
internal.lua
@ -144,3 +144,48 @@ function unified_inventory.items_in_group(groups)
|
||||
end
|
||||
return items
|
||||
end
|
||||
|
||||
function unified_inventory.sort_inventory(inv)
|
||||
local inlist = inv:get_list("main")
|
||||
local typecnt = {}
|
||||
local typekeys = {}
|
||||
for _, st in ipairs(inlist) do
|
||||
if not st:is_empty() then
|
||||
local n = st:get_name()
|
||||
local w = st:get_wear()
|
||||
local m = st:get_metadata()
|
||||
local k = string.format("%s %05d %s", n, w, m)
|
||||
if not typecnt[k] then
|
||||
typecnt[k] = {
|
||||
name = n,
|
||||
wear = w,
|
||||
metadata = m,
|
||||
stack_max = st:get_stack_max(),
|
||||
count = 0,
|
||||
}
|
||||
table.insert(typekeys, k)
|
||||
end
|
||||
typecnt[k].count = typecnt[k].count + st:get_count()
|
||||
end
|
||||
end
|
||||
table.sort(typekeys)
|
||||
local outlist = {}
|
||||
for _, k in ipairs(typekeys) do
|
||||
local tc = typecnt[k]
|
||||
while tc.count > 0 do
|
||||
local c = math.min(tc.count, tc.stack_max)
|
||||
table.insert(outlist, ItemStack({
|
||||
name = tc.name,
|
||||
wear = tc.wear,
|
||||
metadata = tc.metadata,
|
||||
count = c,
|
||||
}))
|
||||
tc.count = tc.count - c
|
||||
end
|
||||
end
|
||||
if #outlist > #inlist then return end
|
||||
while #outlist < #inlist do
|
||||
table.insert(outlist, ItemStack(nil))
|
||||
end
|
||||
inv:set_list("main", outlist)
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user