summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRealBadAngel <maciej.kasatkin@o2.pl>2014-05-22 21:24:10 +0200
committerRealBadAngel <maciej.kasatkin@o2.pl>2014-05-22 21:24:24 +0200
commit3003b193226ccbe75321a7d8e099711f77b3430c (patch)
tree58922d0a9a170139338cf21389f34cc4dce5bb30
parent44d967bbe5630816b99c8d131d620924cd105491 (diff)
Add sorting inventories function.
-rw-r--r--internal.lua45
1 files changed, 45 insertions, 0 deletions
diff --git a/internal.lua b/internal.lua
index 4238eca..335af7a 100644
--- a/internal.lua
+++ b/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