chatcommands.lua 3.5 KB

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