2
0

chatcommands.lua 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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