sort_inventory_function.lua 929 B

123456789101112131415161718192021222324252627282930313233
  1. -- This function ( sort_inventory(inv) ) was taken form the technic_chests mod
  2. -- https://github.com/minetest-mods/technic/blob/master/technic_chests/register.lua
  3. -- License:
  4. -- Copyright (C) 2012-2014 Maciej Kasatkin (RealBadAngel)
  5. -- Technic chests code is licensed under the GNU LGPLv2+.
  6. function inventorybags.sort_inventory(inv)
  7. local inlist = inv:get_list("main")
  8. local typecnt = {}
  9. local typekeys = {}
  10. for _, st in ipairs(inlist) do
  11. if not st:is_empty() then
  12. local n = st:get_name()
  13. local w = st:get_wear()
  14. local m = st:get_metadata()
  15. local k = string.format("%s %05d %s", n, w, m)
  16. if not typecnt[k] then
  17. typecnt[k] = {st}
  18. table.insert(typekeys, k)
  19. else
  20. table.insert(typecnt[k], st)
  21. end
  22. end
  23. end
  24. table.sort(typekeys)
  25. inv:set_list("main", {})
  26. for _, k in ipairs(typekeys) do
  27. for _, item in ipairs(typecnt[k]) do
  28. inv:add_item("main", item)
  29. end
  30. end
  31. end