notify.lua 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. if not minetest.global_exists("notify") then notify = {} end
  2. local sub = vector.subtract
  3. local add = vector.add
  4. local find = minetest.find_nodes_in_area
  5. local getn = minetest.get_node
  6. local items = minetest.registered_items
  7. local after = minetest.after
  8. function notify.do_notify(pos)
  9. local minp = sub(pos, 1)
  10. local maxp = add(pos, 1)
  11. local nodes = find(minp, maxp, "group:want_notify")
  12. if nodes and #nodes > 0 then
  13. for i=1, #nodes do
  14. local p = nodes[i]
  15. -- Don't call `on_notify' for the node that triggered the action.
  16. if p.x ~= pos.x or p.y ~= pos.y or p.z ~= pos.z then
  17. local nn = getn(p).name
  18. local def = items[nn]
  19. if def and def.on_notify then
  20. -- Pos of node to be notified, pos of node that caused the notification.
  21. def.on_notify(p, pos)
  22. end
  23. end
  24. end
  25. end
  26. end
  27. local do_notify = notify.do_notify
  28. -- Call this function from `on_construct' or `on_destruct'.
  29. -- Adjacent nodes will not be notified until AFTER the operation completes.
  30. -- Put `notify_construct = 1' in node groups.
  31. -- Put `notify_destruct = 1' in node groups.
  32. -- If either are present, appropriate callbacks will be set up automatically.
  33. function notify.notify_adjacent(pos)
  34. after(0, do_notify, {x=pos.x, y=pos.y, z=pos.z})
  35. end