notify.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. notify = notify or {}
  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. --minetest.chat_send_player("MustTest", "# Server: Executing notify @ " .. minetest.pos_to_string(pos) .. "!")
  10. local minp = sub(pos, 1)
  11. local maxp = add(pos, 1)
  12. local nodes = find(minp, maxp, "group:want_notify")
  13. if nodes and #nodes > 0 then
  14. --minetest.chat_send_player("MustTest", "# Server: Notifying " .. #nodes .. " node(s)!")
  15. for i=1, #nodes do
  16. local p = nodes[i]
  17. -- Don't call `on_notify' for the node that triggered the action.
  18. if p.x ~= pos.x or p.y ~= pos.y or p.z ~= pos.z then
  19. local nn = getn(p).name
  20. local def = items[nn]
  21. if def and def.on_notify then
  22. --minetest.chat_send_player("MustTest", "# Server: Notifying node @ " .. minetest.pos_to_string(p) .. "!")
  23. -- Pos of node to be notified, pos of node that caused the notification.
  24. def.on_notify(p, pos)
  25. end
  26. end
  27. end
  28. end
  29. end
  30. local do_notify = notify.do_notify
  31. -- Call this function from `on_construct' or `on_destruct'.
  32. -- Adjacent nodes will not be notified until AFTER the operation completes.
  33. -- Put `notify_construct = 1' in node groups.
  34. -- Put `notify_destruct = 1' in node groups.
  35. -- If either are present, appropriate callbacks will be set up automatically.
  36. function notify.notify_adjacent(pos)
  37. after(0, do_notify, {x=pos.x, y=pos.y, z=pos.z})
  38. end