teleport_callback.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. minetestd.register_on_teleportplayer = function(func)--function(player, oldpos, newpos)
  2. table.insert(minetestd.teleport_callbacks, func)
  3. end
  4. if not minetestd.services.teleport_callback.initialized then
  5. minetestd.teleport_callbacks = {}
  6. minetestd.playerctl.register_metatable_change(
  7. function(metatable)
  8. local mto = metatable.move_to
  9. local sps = metatable.set_pos
  10. metatable.set_pos_nc = sps
  11. metatable.move_to_nc = mto
  12. --[[
  13. _nc = No-callbacks.
  14. Should only be used if you REALLY need default functions. If you do, but don't want to use these,
  15. toggle off minetestd.services.teleport_callback.enabled just before your call,
  16. then set it to its original value right afterward.
  17. If you're just having issues with a callback behavior defined in your mod,
  18. you may want to create a local variable to toogle suppression of your specific call.
  19. ]]
  20. metatable.move_to = function(self, pos, ...)
  21. if minetestd.services.teleport_callback.enabled and self:is_player() then
  22. for _,call in pairs(minetestd.teleport_callbacks) do
  23. if call(self, self:get_pos(), pos) then return end
  24. end
  25. end
  26. mto(self,pos,...)
  27. end
  28. metatable.moveto = metatable.move_to --???
  29. metatable.set_pos = function(self, pos, ...)
  30. if minetestd.services.teleport_callback.enabled and self:is_player() then
  31. for _,call in pairs(minetestd.teleport_callbacks) do
  32. if call(self, self:get_pos(), pos) then return end
  33. end
  34. end
  35. sps(self,pos,...)
  36. end
  37. metatable.setpos = metatable.set_pos
  38. end
  39. )
  40. end