chatcommands.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 _, message = cmd_def.func(param)
  23. if message then
  24. core.display_chat_message(message)
  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 players = table.concat(core.get_player_names(), ", ")
  35. core.display_chat_message(core.gettext("Online players: ") .. players)
  36. end
  37. })
  38. core.register_chatcommand("disconnect", {
  39. description = core.gettext("Exit to main menu"),
  40. func = function(param)
  41. core.disconnect()
  42. end,
  43. })
  44. core.register_chatcommand("clear_chat_queue", {
  45. description = core.gettext("Clear the out chat queue"),
  46. func = function(param)
  47. core.clear_out_chat_queue()
  48. return true, core.gettext("The out chat queue is now empty")
  49. end,
  50. })
  51. function core.run_server_chatcommand(cmd, param)
  52. core.send_chat_message("/" .. cmd .. " " .. param)
  53. end