2
0

callbacks.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. local callback_test = 0
  2. local out = function(player, formname, fields, number)
  3. local snum = ""
  4. if number then
  5. snum = " "..number
  6. end
  7. local msg = "Formspec callback"..snum..": player="..player:get_player_name()..", formname=\""..tostring(formname).."\", fields="..dump(fields)
  8. core.chat_send_player(player:get_player_name(), msg)
  9. core.log("action", msg)
  10. end
  11. core.register_on_player_receive_fields(function(player, formname, fields)
  12. if callback_test == 1 then
  13. out(player, formname, fields)
  14. elseif callback_test == 2 then
  15. out(player, formname, fields, 1)
  16. end
  17. end)
  18. core.register_on_player_receive_fields(function(player, formname, fields)
  19. if callback_test == 2 then
  20. out(player, formname, fields, 2)
  21. return true -- Disable the first callback
  22. end
  23. end)
  24. core.register_on_player_receive_fields(function(player, formname, fields)
  25. if callback_test == 2 then
  26. out(player, formname, fields, 3)
  27. end
  28. end)
  29. core.register_chatcommand("test_formspec_callbacks", {
  30. params = "[ 0 | 1 | 2 ]",
  31. description = "Test: Change formspec callbacks testing mode",
  32. func = function(name, param)
  33. local mode = tonumber(param)
  34. if not mode then
  35. callback_test = (callback_test + 1 % 3)
  36. else
  37. callback_test = mode
  38. end
  39. if callback_test == 1 then
  40. core.chat_send_player(name, "Formspec callback test mode 1 enabled: Logging only")
  41. elseif callback_test == 2 then
  42. core.chat_send_player(name, "Formspec callback test mode 2 enabled: Three callbacks, disable pre-registered callbacks")
  43. else
  44. callback_test = 0
  45. core.chat_send_player(name, "Formspec callback test disabled!")
  46. end
  47. end
  48. })