sort.lua 1.5 KB

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