sort.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. scaffolding = scaffolding or {}
  2. function scaffolding.wrench_on_use(itemstack, user, pt)
  3. if pt.type ~= "node" then
  4. --minetest.chat_send_player("MustTest", "Bad!")
  5. return
  6. end
  7. local pname = user:get_player_name()
  8. local pos = pt.under
  9. local node = minetest.get_node(pos)
  10. if string.find(node.name, "^scaffolding:") then
  11. local def = minetest.registered_items[node.name]
  12. if def and def.on_punch then
  13. return def.on_punch(pos, node, user)
  14. end
  15. return
  16. end
  17. if minetest.get_item_group(node.name, "chest") ~= 0 then
  18. local meta = minetest.get_meta(pos)
  19. local inv = meta:get_inventory()
  20. if inv:get_size("main") > 0 then
  21. scaffolding.sort_inventory(pname, pos, inv)
  22. end
  23. end
  24. end
  25. function scaffolding.sort_inventory(pname, pos, inv)
  26. if minetest.test_protection(pos, pname) then
  27. minetest.chat_send_player(pname, "# Server: Cannot sort protected chest!")
  28. return
  29. end
  30. local inlist = inv:get_list("main")
  31. local typecnt = {}
  32. local typekeys = {}
  33. for _, st in ipairs(inlist) do
  34. if not st:is_empty() then
  35. local n = st:get_name()
  36. local k = string.format("%s", n)
  37. if not typecnt[k] then
  38. typecnt[k] = {st}
  39. table.insert(typekeys, k)
  40. else
  41. table.insert(typecnt[k], st)
  42. end
  43. end
  44. end
  45. table.sort(typekeys)
  46. inv:set_list("main", {})
  47. for _, k in ipairs(typekeys) do
  48. for _, item in ipairs(typecnt[k]) do
  49. inv:add_item("main", item)
  50. end
  51. end
  52. minetest.chat_send_player(pname, "# Server: Successfully sorted chest at " .. rc.pos_to_namestr(pos) .. "!")
  53. end