2
0

chatcommands.lua 1.8 KB

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