chatcommands.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. -- Minetest: builtin/common/chatcommands.lua
  2. core.registered_chatcommands = {}
  3. function core.register_chatcommand(cmd, def)
  4. def = def or {}
  5. def.params = def.params or ""
  6. def.description = def.description or ""
  7. def.privs = def.privs or {}
  8. def.mod_origin = core.get_current_modname() or "??"
  9. core.registered_chatcommands[cmd] = def
  10. end
  11. function core.unregister_chatcommand(name)
  12. if core.registered_chatcommands[name] then
  13. core.registered_chatcommands[name] = nil
  14. else
  15. core.log("warning", "Not unregistering chatcommand " ..name..
  16. " because it doesn't exist.")
  17. end
  18. end
  19. function core.override_chatcommand(name, redefinition)
  20. local chatcommand = core.registered_chatcommands[name]
  21. assert(chatcommand, "Attempt to override non-existent chatcommand "..name)
  22. for k, v in pairs(redefinition) do
  23. rawset(chatcommand, k, v)
  24. end
  25. core.registered_chatcommands[name] = chatcommand
  26. end
  27. local cmd_marker = "/"
  28. local function gettext(...)
  29. return ...
  30. end
  31. local function gettext_replace(text, replace)
  32. return text:gsub("$1", replace)
  33. end
  34. if INIT == "client" then
  35. cmd_marker = "."
  36. gettext = core.gettext
  37. gettext_replace = fgettext_ne
  38. end
  39. local function do_help_cmd(name, param)
  40. local function format_help_line(cmd, def)
  41. local msg = core.colorize("#00ffff", cmd_marker .. cmd)
  42. if def.params and def.params ~= "" then
  43. msg = msg .. " " .. def.params
  44. end
  45. if def.description and def.description ~= "" then
  46. msg = msg .. ": " .. def.description
  47. end
  48. return msg
  49. end
  50. if param == "" then
  51. local cmds = {}
  52. for cmd, def in pairs(core.registered_chatcommands) do
  53. if INIT == "client" or core.check_player_privs(name, def.privs) then
  54. cmds[#cmds + 1] = cmd
  55. end
  56. end
  57. table.sort(cmds)
  58. return true, gettext("Available commands: ") .. table.concat(cmds, " ") .. "\n"
  59. .. gettext_replace("Use '$1help <cmd>' to get more information,"
  60. .. " or '$1help all' to list everything.", cmd_marker)
  61. elseif param == "all" then
  62. local cmds = {}
  63. for cmd, def in pairs(core.registered_chatcommands) do
  64. if INIT == "client" or core.check_player_privs(name, def.privs) then
  65. cmds[#cmds + 1] = format_help_line(cmd, def)
  66. end
  67. end
  68. table.sort(cmds)
  69. return true, gettext("Available commands:").."\n"..table.concat(cmds, "\n")
  70. elseif INIT == "game" and param == "privs" then
  71. local privs = {}
  72. for priv, def in pairs(core.registered_privileges) do
  73. privs[#privs + 1] = priv .. ": " .. def.description
  74. end
  75. table.sort(privs)
  76. return true, "Available privileges:\n"..table.concat(privs, "\n")
  77. else
  78. local cmd = param
  79. local def = core.registered_chatcommands[cmd]
  80. if not def then
  81. return false, gettext("Command not available: ")..cmd
  82. else
  83. return true, format_help_line(cmd, def)
  84. end
  85. end
  86. end
  87. if INIT == "client" then
  88. core.register_chatcommand("help", {
  89. params = gettext("[all/<cmd>]"),
  90. description = gettext("Get help for commands"),
  91. func = function(param)
  92. return do_help_cmd(nil, param)
  93. end,
  94. })
  95. else
  96. core.register_chatcommand("help", {
  97. params = "[all/privs/<cmd>]",
  98. description = "Get help for commands or list privileges",
  99. func = do_help_cmd,
  100. })
  101. end