chatcommands.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. local cmd_def = core.registered_chatcommands[cmd]
  20. if cmd_def then
  21. core.set_last_run_mod(cmd_def.mod_origin)
  22. local _, result = cmd_def.func(param)
  23. if result then
  24. core.display_chat_message(result)
  25. end
  26. else
  27. core.display_chat_message(core.gettext("-!- Invalid command: ") .. cmd)
  28. end
  29. return true
  30. end)
  31. core.register_chatcommand("list_players", {
  32. description = core.gettext("List online players"),
  33. func = function(param)
  34. local player_names = core.get_player_names()
  35. if not player_names then
  36. return false, core.gettext("This command is disabled by server.")
  37. end
  38. local players = table.concat(player_names, ", ")
  39. return true, core.gettext("Online players: ") .. players
  40. end
  41. })
  42. core.register_chatcommand("disconnect", {
  43. description = core.gettext("Exit to main menu"),
  44. func = function(param)
  45. core.disconnect()
  46. end,
  47. })
  48. core.register_chatcommand("clear_chat_queue", {
  49. description = core.gettext("Clear the out chat queue"),
  50. func = function(param)
  51. core.clear_out_chat_queue()
  52. return true, core.gettext("The out chat queue is now empty")
  53. end,
  54. })
  55. function core.run_server_chatcommand(cmd, param)
  56. core.send_chat_message("/" .. cmd .. " " .. param)
  57. end