Allows admins to run code snippets without crashing the server as often.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

forms.lua 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. --
  2. -- Minetest snippets mod: A formspec API
  3. --
  4. -- This should probably be put in formspeclib.
  5. --
  6. local open_formspecs = {}
  7. minetest.register_on_leaveplayer(function(player)
  8. local name = player:get_player_name()
  9. open_formspecs[name] = nil
  10. end)
  11. local get_player_by_name = minetest.global_exists('cloaking') and
  12. cloaking.get_player_by_name or minetest.get_player_by_name
  13. -- Formspec objects
  14. -- You can create one of these per player and handle input
  15. local Form = {}
  16. local forms = {}
  17. setmetatable(forms, {__mode = 'k'})
  18. local function get(form)
  19. if not forms[form] then
  20. error('snippets.Form method called on a non-Form!', 3)
  21. end
  22. return forms[form]
  23. end
  24. -- Get unique formnames
  25. local used_ids = {}
  26. setmetatable(used_ids, {__mode = 'v'})
  27. local function get_next_formname(form)
  28. -- Iterate over it because of inconsistencies when getting the length of a
  29. -- list containing nil.
  30. local id = 1
  31. for _ in ipairs(used_ids) do id = id + 1 end
  32. -- ID should be equal to #used_ids + 1.
  33. used_ids[id] = form
  34. return 'snippets:form_' .. id
  35. end
  36. -- Override minetest.show_formspec
  37. local show_formspec = minetest.show_formspec
  38. function minetest.show_formspec(pname, formname, formspec)
  39. if pname and (formspec ~= '' or formname == '') then
  40. open_formspecs[pname] = nil
  41. end
  42. return show_formspec(pname, formname, formspec)
  43. end
  44. -- Show formspecs
  45. function Form:show()
  46. local data = get(self)
  47. if not get_player_by_name(data.victim) then return false end
  48. open_formspecs[data.victim] = self
  49. show_formspec(data.victim, data.formname,
  50. data.prepend .. data.formspec .. data.append)
  51. return true
  52. end
  53. Form.open = Form.show
  54. -- Close formspecs
  55. function Form:close()
  56. local data = get(self)
  57. if open_formspecs[data.victim] == self then
  58. minetest.close_formspec(data.victim, data.formname)
  59. open_formspecs[data.victim] = nil
  60. end
  61. end
  62. Form.hide = Form.close
  63. -- Check if the form is open
  64. function Form:is_open()
  65. return open_formspecs[get(self).victim] == self
  66. end
  67. -- Prepends etc
  68. function Form:get_prepend() return get(self).prepend end
  69. function Form:get_formspec() return get(self).formspec end
  70. function Form:get_append() return get(self).append end
  71. function Form:set_prepend(text)
  72. local data = get(self)
  73. data.prepend = tostring(text or '')
  74. if open_formspecs[data.victim] == self then self:show() end
  75. end
  76. function Form:set_formspec(text)
  77. local data = get(self)
  78. data.formspec = tostring(text or '')
  79. if open_formspecs[data.victim] == self then self:show() end
  80. end
  81. function Form:set_append(text)
  82. local data = get(self)
  83. data.append = tostring(text or '')
  84. if open_formspecs[data.victim] == self then self:show() end
  85. end
  86. -- Callbacks
  87. function Form:add_callback(...)
  88. local data, argc = get(self), select('#', ...)
  89. local event, func
  90. if argc == 1 then
  91. event, func = '', ...
  92. elseif argc == 2 then
  93. event, func = ...
  94. if type(event) ~= 'string' then
  95. error('Invalid usage for snippets.Form:add_callback().', 2)
  96. end
  97. else
  98. error('snippets.Form:add_callback() takes one or two arguments.', 2)
  99. end
  100. if not data.callbacks[event] then data.callbacks[event] = {} end
  101. table.insert(data.callbacks[event], snippets.wrap_callback(func))
  102. end
  103. -- Create a Formspec object
  104. function snippets.Form(player)
  105. if minetest.is_player(player) then player = player:get_player_name() end
  106. if type(player) ~= 'string' or not get_player_by_name(player) then
  107. error('Attempted to create a Form for a non-existent player!', 2)
  108. end
  109. local form = {context = {}, pname = player}
  110. setmetatable(form, {__index = Form})
  111. forms[form] = {
  112. victim = player, prepend = '', formspec = '', append = '',
  113. callbacks = {}, formname = get_next_formname(form), pname = player,
  114. }
  115. return form
  116. end
  117. -- Callbacks
  118. local function run_callbacks(callbacks, ...)
  119. if not callbacks then return end
  120. for _, func in ipairs(callbacks) do func(...) end
  121. end
  122. minetest.register_on_player_receive_fields(function(player, formname, fields)
  123. if formname:sub(1, 14) ~= 'snippets:form_' then return end
  124. local pname = player:get_player_name()
  125. local form = open_formspecs[pname]
  126. local data = forms[form]
  127. if not data or data.formname ~= formname then return end
  128. -- Nuke the formspec if required
  129. if fields.quit then open_formspecs[pname] = nil end
  130. -- Run generic callbacks
  131. run_callbacks(data.callbacks[''], form, fields)
  132. -- Run field-specific callbacks
  133. for k, v in pairs(fields) do
  134. run_callbacks(data.callbacks[k], form, fields)
  135. end
  136. end)