example.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. --[[
  2. These three nodes could probably be registered with a single function, maybe in the future I'll code that.
  3. ]]
  4. minetest.register_node('tasks:example_setup', { --This is the node that can be placed.
  5. description = 'Example node setup',
  6. tiles = {name='task_1.png'},
  7. groups = {breakable=1},
  8. light_source = 2,
  9. on_construct = function(pos)
  10. local meta = minetest.get_meta(pos)
  11. meta:set_string('infotext', 'Unconfigured task')
  12. meta:set_string('formspec', tasks.formspec_configuration)
  13. meta:set_string('xp', '')
  14. meta:set_string('timer', '')
  15. end,
  16. on_receive_fields = function(pos, forname, fields, sender)
  17. local meta = minetest.get_meta(pos)
  18. if fields ['save'] then
  19. if not fields.input or fields.input == "" then
  20. return
  21. end
  22. local input_text = fields.input:split(', ')
  23. meta:set_string('xp', input_text[1])
  24. meta:set_string('timer', input_text[2])
  25. meta:set_string('infotext', input_text[3])
  26. meta:set_string('formspec', '')
  27. local node = minetest.get_node(pos)
  28. minetest.swap_node(pos, {name = 'tasks:example_1', param2 = node.param2}) --Swap to the active node.
  29. end
  30. end,
  31. })
  32. minetest.register_node('tasks:example_0', { --This node is inactive, so somebody completed the task.
  33. description = 'example',
  34. tiles = {name='task_1.png'},
  35. groups = {breakable=1, not_in_creative_inventory=1},
  36. light_source = 14,
  37. drop = 'tasks:example_setup',
  38. on_timer = function(pos)
  39. local this_node = minetest.get_node(pos)
  40. minetest.swap_node(pos, {name = 'tasks:example_1', param2 = this_node.param2})
  41. end,
  42. on_punch = function(pos)
  43. local meta = minetest.get_meta(pos)
  44. local timer_duration = tonumber(meta:get_string('timer')) or 120
  45. local timer = minetest.get_node_timer(pos)
  46. timer:start(timer_duration*2) --Make the player wait twice as long if they try to do a task that isn't available.
  47. end,
  48. })
  49. minetest.register_node('tasks:example_1', { --This node is waiting for somebody to come along and complete the task.
  50. description = 'example',
  51. tiles = {name='task_1.png'},
  52. groups = {breakable=1, not_in_creative_inventory=1},
  53. light_source = 2,
  54. drop = 'tasks:example_setup',
  55. on_punch = function(pos, node, puncher, pointed_thing)
  56. tasks.add_xp(pos, node, puncher, 'tasks:example_0') --Everything is pulled from the node meta expect for what node to swap to.
  57. end
  58. })