chatcommands.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. -- Minetest: builtin/client/chatcommands.lua
  2. core.register_on_sending_chat_message(function(message)
  3. if message:sub(1,2) == ".." then
  4. return false
  5. end
  6. local first_char = message:sub(1,1)
  7. if first_char == "/" or first_char == "." then
  8. core.display_chat_message(core.gettext("issued command: ") .. message)
  9. end
  10. if first_char ~= "." then
  11. return false
  12. end
  13. local cmd, param = string.match(message, "^%.([^ ]+) *(.*)")
  14. param = param or ""
  15. if not cmd then
  16. core.display_chat_message(core.gettext("-!- Empty command"))
  17. return true
  18. end
  19. -- Run core.registered_on_chatcommand callbacks.
  20. if core.run_callbacks(core.registered_on_chatcommand, 5, cmd, param) then
  21. return true
  22. end
  23. local cmd_def = core.registered_chatcommands[cmd]
  24. if cmd_def then
  25. core.set_last_run_mod(cmd_def.mod_origin)
  26. local _, result = cmd_def.func(param)
  27. if result then
  28. core.display_chat_message(result)
  29. end
  30. else
  31. core.display_chat_message(core.gettext("-!- Invalid command: ") .. cmd)
  32. end
  33. return true
  34. end)
  35. core.register_chatcommand("list_players", {
  36. description = core.gettext("List online players"),
  37. func = function(param)
  38. local player_names = core.get_player_names()
  39. if not player_names then
  40. return false, core.gettext("This command is disabled by server.")
  41. end
  42. local players = table.concat(player_names, ", ")
  43. return true, core.gettext("Online players: ") .. players
  44. end
  45. })
  46. core.register_chatcommand("disconnect", {
  47. description = core.gettext("Exit to main menu"),
  48. func = function(param)
  49. core.disconnect()
  50. end,
  51. })
  52. core.register_chatcommand("clear_chat_queue", {
  53. description = core.gettext("Clear the out chat queue"),
  54. func = function(param)
  55. core.clear_out_chat_queue()
  56. return true, core.gettext("The out chat queue is now empty")
  57. end,
  58. })
  59. function core.run_server_chatcommand(cmd, param)
  60. core.send_chat_message("/" .. cmd .. " " .. param)
  61. end