information_formspecs.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 = INIT == "client" or 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. local msg = S("Command: @1 @2",
  59. core.colorize("#0FF", (INIT == "client" and "." or "/") .. cmds[1]), cmds[2].params)
  60. if INIT == "client" then
  61. core.display_chat_message(msg)
  62. else
  63. core.chat_send_player(name, msg)
  64. end
  65. end
  66. end
  67. end
  68. end
  69. return LIST_FORMSPEC_DESCRIPTION:format(
  70. F(S("Available commands: (see also: /help <cmd>)")),
  71. table.concat(rows, ","), sel or 0,
  72. F(description), F(S("Close"))
  73. )
  74. end
  75. -- PRIVILEGES FORMSPEC
  76. local function build_privs_formspec(name)
  77. local privs = {}
  78. for priv_name, def in pairs(core.registered_privileges) do
  79. privs[#privs + 1] = { priv_name, def }
  80. end
  81. table.sort(privs, function(a, b) return a[1] < b[1] end)
  82. local rows = {}
  83. rows[1] = "#FFF,0,"..F(S("Privilege"))..","..F(S("Description"))
  84. local player_privs = core.get_player_privs(name)
  85. for i, data in ipairs(privs) do
  86. rows[#rows + 1] = ("%s,0,%s,%s"):format(
  87. player_privs[data[1]] and COLOR_GREEN or COLOR_GRAY,
  88. data[1], F(data[2].description))
  89. end
  90. return LIST_FORMSPEC:format(
  91. F(S("Available privileges:")),
  92. table.concat(rows, ","),
  93. F(S("Close"))
  94. )
  95. end
  96. -- DETAILED CHAT COMMAND INFORMATION
  97. if INIT == "client" then
  98. core.register_on_formspec_input(function(formname, fields)
  99. if formname ~= "__builtin:help_cmds" or fields.quit then
  100. return
  101. end
  102. local event = core.explode_table_event(fields.list)
  103. if event.type ~= "INV" then
  104. core.show_formspec("__builtin:help_cmds",
  105. build_chatcommands_formspec(nil, event.row, event.type == "DCL"))
  106. end
  107. end)
  108. else
  109. core.register_on_player_receive_fields(function(player, formname, fields)
  110. if formname ~= "__builtin:help_cmds" or fields.quit then
  111. return
  112. end
  113. local event = core.explode_table_event(fields.list)
  114. if event.type ~= "INV" then
  115. local name = player:get_player_name()
  116. core.show_formspec(name, "__builtin:help_cmds",
  117. build_chatcommands_formspec(name, event.row, event.type == "DCL"))
  118. end
  119. end)
  120. end
  121. function core.show_general_help_formspec(name)
  122. if INIT == "client" then
  123. core.show_formspec("__builtin:help_cmds",
  124. build_chatcommands_formspec(name))
  125. else
  126. core.show_formspec(name, "__builtin:help_cmds",
  127. build_chatcommands_formspec(name))
  128. end
  129. end
  130. if INIT ~= "client" then
  131. function core.show_privs_help_formspec(name)
  132. core.show_formspec(name, "__builtin:help_privs",
  133. build_privs_formspec(name))
  134. end
  135. end