init.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. if not minetest.global_exists("dumpnodes") then dumpnodes = {} end
  2. dumpnodes.modpath = minetest.get_modpath("dumpnodes")
  3. local function nd_get_tiles(nd)
  4. if nd.dumpnodes_tile then
  5. if type(nd.dumpnodes_tile) == "string" then
  6. return {nd.dumpnodes_tile}
  7. else
  8. return nd.dumpnodes_tile
  9. end
  10. elseif nd.tiles then
  11. return nd.tiles
  12. elseif nd.tile_images then
  13. return nd.tile_images
  14. end
  15. return nil
  16. end
  17. local function pairs_s(dict)
  18. local keys = {}
  19. for k in pairs(dict) do
  20. table.insert(keys, k)
  21. end
  22. table.sort(keys)
  23. return ipairs(keys)
  24. end
  25. -- Name and param may be empty strings.
  26. dumpnodes.execute = function(plname, param)
  27. local n = 0
  28. local ntbl = {}
  29. for _, nn in pairs_s(minetest.registered_nodes) do
  30. local nd = minetest.registered_nodes[nn]
  31. local prefix, name = nn:match('(.*):(.*)')
  32. if prefix == nil or name == nil or prefix == '' or name == '' then
  33. print("ignored(1): " .. nn)
  34. else
  35. if ntbl[prefix] == nil then
  36. ntbl[prefix] = {}
  37. end
  38. ntbl[prefix][name] = nd
  39. end
  40. end
  41. local out = io.open(minetest.get_worldpath() .. '/dumpnodes.txt', 'wb')
  42. if not out then
  43. return false, "Could not open dumpnodes file."
  44. end
  45. for _, prefix in pairs_s(ntbl) do
  46. local nodes = ntbl[prefix]
  47. out:write('# ' .. prefix .. '\n')
  48. for _, name in pairs_s(nodes) do
  49. local nd = nodes[name]
  50. if nd.drawtype ~= 'airlike' and nd_get_tiles(nd) ~= nil then
  51. local tl = nd_get_tiles(nd)[1]
  52. if type(tl) == 'table' then
  53. tl = tl.name
  54. end
  55. tl = (tl .. '^'):match('(.-)^')
  56. out:write(prefix .. ':' .. name .. ' ' .. tl .. '\n')
  57. n = n + 1
  58. else
  59. print("ignored(2): " .. prefix .. ':' .. name)
  60. end
  61. end
  62. out:write('\n')
  63. end
  64. out:close()
  65. return true, "" .. n .. " nodes dumped."
  66. end
  67. dumpnodes.chatcommand = function(pname, param)
  68. minetest.chat_send_player(pname, "# Server: Preparing to dump nodes.")
  69. local result, msg = dumpnodes.execute(pname, param)
  70. if msg then
  71. minetest.chat_send_player(pname, "# Server: " .. msg)
  72. end
  73. if not result then
  74. easyvend.sound_error(pname)
  75. end
  76. end
  77. if not dumpnodes.run_once then
  78. minetest.register_chatcommand("dumpnodes", {
  79. params = "",
  80. description = "",
  81. privs = {server=true},
  82. func = function(...) return dumpnodes.chatcommand(...) end,
  83. })
  84. minetest.register_on_shutdown(function()
  85. dumpnodes.execute("", "")
  86. end)
  87. local name = "dumpnodes:core"
  88. local file = dumpnodes.modpath .. "/init.lua"
  89. reload.register_file(name, file, false)
  90. dumpnodes.run_once = true
  91. end