nodes.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. local function print_to_everything(msg)
  2. minetest.log("action", "[callbacks] " .. msg)
  3. minetest.chat_send_all(msg)
  4. end
  5. minetest.register_node("callbacks:callback_node", {
  6. description = "Callback Test Node (construct/destruct/timer)".."\n"..
  7. "Tests callbacks: on_construct, after_place_node, on_destruct, after_destruct, after_dig_node, on_timer",
  8. tiles = {"callbacks_callback_node.png"},
  9. groups = {callback_test=1, dig_immediate=3},
  10. -- This was known to cause a bug in minetest.item_place_node() when used
  11. -- via minetest.place_node(), causing a placer with no position
  12. paramtype2 = "facedir",
  13. drop = "",
  14. on_construct = function(pos)
  15. print_to_everything("callbacks:callback_node:on_construct("..minetest.pos_to_string(pos)..")")
  16. local meta = minetest.get_meta(pos)
  17. meta:set_string("mine", "test")
  18. local timer = minetest.get_node_timer(pos)
  19. timer:start(4, 3)
  20. end,
  21. after_place_node = function(pos, placer)
  22. print_to_everything("callbacks:callback_node:after_place_node("..minetest.pos_to_string(pos)..")")
  23. local meta = minetest.get_meta(pos)
  24. if meta:get_string("mine") == "test" then
  25. print_to_everything("correct metadata found")
  26. else
  27. print_to_everything("incorrect metadata found")
  28. end
  29. end,
  30. on_destruct = function(pos)
  31. print_to_everything("callbacks:callback_node:on_destruct("..minetest.pos_to_string(pos)..")")
  32. end,
  33. after_destruct = function(pos)
  34. print_to_everything("callbacks:callback_node:after_destruct("..minetest.pos_to_string(pos)..")")
  35. end,
  36. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  37. print_to_everything("callbacks:callback_node:after_dig_node("..minetest.pos_to_string(pos)..")")
  38. end,
  39. on_timer = function(pos, elapsed)
  40. print_to_everything("callbacks:callback_node:on_timer(): elapsed="..dump(elapsed))
  41. return true
  42. end,
  43. })