information_formspecs.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. local COLOR_BLUE = "#7AF"
  2. local COLOR_GREEN = "#7F7"
  3. local COLOR_GRAY = "#BBB"
  4. local LIST_FORMSPEC = [[
  5. size[13,6.5]
  6. label[0,-0.1;%s]
  7. tablecolumns[color;tree;text;text]
  8. table[0,0.5;12.8,5.5;list;%s;0]
  9. button_exit[5,6;3,1;quit;%s]
  10. ]]
  11. local LIST_FORMSPEC_DESCRIPTION = [[
  12. size[13,7.5]
  13. label[0,-0.1;%s]
  14. tablecolumns[color;tree;text;text]
  15. table[0,0.5;12.8,4.8;list;%s;%i]
  16. box[0,5.5;12.8,1.5;#000]
  17. textarea[0.3,5.5;13.05,1.9;;;%s]
  18. button_exit[5,7;3,1;quit;%s]
  19. ]]
  20. local F = core.formspec_escape
  21. local S = core.get_translator("__builtin")
  22. local check_player_privs = core.check_player_privs
  23. -- CHAT COMMANDS FORMSPEC
  24. local mod_cmds = {}
  25. local function load_mod_command_tree()
  26. mod_cmds = {}
  27. for name, def in pairs(core.registered_chatcommands) do
  28. mod_cmds[def.mod_origin] = mod_cmds[def.mod_origin] or {}
  29. local cmds = mod_cmds[def.mod_origin]
  30. -- Could be simplified, but avoid the priv checks whenever possible
  31. cmds[#cmds + 1] = { name, def }
  32. end
  33. local sorted_mod_cmds = {}
  34. for modname, cmds in pairs(mod_cmds) do
  35. table.sort(cmds, function(a, b) return a[1] < b[1] end)
  36. sorted_mod_cmds[#sorted_mod_cmds + 1] = { modname, cmds }
  37. end
  38. table.sort(sorted_mod_cmds, function(a, b) return a[1] < b[1] end)
  39. mod_cmds = sorted_mod_cmds
  40. end
  41. core.after(0, load_mod_command_tree)
  42. local function build_chatcommands_formspec(name, sel, copy)
  43. local rows = {}
  44. rows[1] = "#FFF,0,"..F(S("Command"))..","..F(S("Parameters"))
  45. local description = S("For more information, click on "
  46. .. "any entry in the list.").. "\n" ..
  47. S("Double-click to copy the entry to the chat history.")
  48. for i, data in ipairs(mod_cmds) do
  49. rows[#rows + 1] = COLOR_BLUE .. ",0," .. F(data[1]) .. ","
  50. for j, cmds in ipairs(data[2]) do
  51. local has_priv = check_player_privs(name, cmds[2].privs)
  52. rows[#rows + 1] = ("%s,1,%s,%s"):format(
  53. has_priv and COLOR_GREEN or COLOR_GRAY,
  54. cmds[1], F(cmds[2].params))
  55. if sel == #rows then
  56. description = cmds[2].description
  57. if copy then
  58. core.chat_send_player(name, S("Command: @1 @2",
  59. core.colorize("#0FF", "/" .. cmds[1]), cmds[2].params))
  60. end
  61. end
  62. end
  63. end
  64. return LIST_FORMSPEC_DESCRIPTION:format(
  65. F(S("Available commands: (see also: /help <cmd>)")),
  66. table.concat(rows, ","), sel or 0,
  67. F(description), F(S("Close"))
  68. )
  69. end
  70. -- PRIVILEGES FORMSPEC
  71. local function build_privs_formspec(name)
  72. local privs = {}
  73. for priv_name, def in pairs(core.registered_privileges) do
  74. privs[#privs + 1] = { priv_name, def }
  75. end
  76. table.sort(privs, function(a, b) return a[1] < b[1] end)
  77. local rows = {}
  78. rows[1] = "#FFF,0,"..F(S("Privilege"))..","..F(S("Description"))
  79. local player_privs = core.get_player_privs(name)
  80. for i, data in ipairs(privs) do
  81. rows[#rows + 1] = ("%s,0,%s,%s"):format(
  82. player_privs[data[1]] and COLOR_GREEN or COLOR_GRAY,
  83. data[1], F(data[2].description))
  84. end
  85. return LIST_FORMSPEC:format(
  86. F(S("Available privileges:")),
  87. table.concat(rows, ","),
  88. F(S("Close"))
  89. )
  90. end
  91. -- DETAILED CHAT COMMAND INFORMATION
  92. core.register_on_player_receive_fields(function(player, formname, fields)
  93. if formname ~= "__builtin:help_cmds" or fields.quit then
  94. return
  95. end
  96. local event = core.explode_table_event(fields.list)
  97. if event.type ~= "INV" then
  98. local name = player:get_player_name()
  99. core.show_formspec(name, "__builtin:help_cmds",
  100. build_chatcommands_formspec(name, event.row, event.type == "DCL"))
  101. end
  102. end)
  103. function core.show_general_help_formspec(name)
  104. core.show_formspec(name, "__builtin:help_cmds",
  105. build_chatcommands_formspec(name))
  106. end
  107. function core.show_privs_help_formspec(name)
  108. core.show_formspec(name, "__builtin:help_privs",
  109. build_privs_formspec(name))
  110. end