Advertisement
Maro919

Teleporter API

Mar 1st, 2025 (edited)
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.94 KB | Gaming | 0 0
  1. local expect = dofile("rom/modules/main/cc/expect.lua")
  2.  
  3. local TP = {}
  4.  
  5. function TP:init(sProtocol, bEndpoint)
  6.     expect(1, sProtocol, "string")
  7.     expect(2, bEndpoint, "boolean", "nil")
  8.     self.destinations = {}
  9.     self.forced = {}
  10.     self.protocol = sProtocol
  11.     self.endpoint = bEndpoint or false
  12. end
  13.  
  14. function TP:register(sName)
  15.     expect(1, sName, "string")
  16.     self.name = sName
  17.     rednet.host(self.protocol, self.name)
  18.     rednet.broadcast({
  19.         sType = "lookup response",
  20.         sProtocol = self.protocol,
  21.         sHostname = self.name,
  22.     }, "dns")
  23. end
  24.  
  25. function TP:unregister()
  26.     rednet.unhost(self.protocol)
  27. end
  28.  
  29. function TP:force(nId, sName)
  30.     expect(1, nId, "number")
  31.     expect(2, sName, "string")
  32.     self.forced[nId] = sName
  33. end
  34.  
  35. function TP:unforce(nId)
  36.     expect(1, nId, "number")
  37.     self.forced[nId] = nil
  38. end
  39.  
  40. function TP:teleport(nId)
  41.     expect(1, nId, "number")
  42.     if self.forced[nId] or self.destinations[nId] and rednet.lookup(self.protocol, self.destinations[nId]) then
  43.         os.queueEvent("tp_trigger")
  44.         rednet.send(nId, nil, self.protocol)
  45.     end
  46. end
  47.  
  48. function TP:refreshList()
  49.     self.destinations = {}
  50.     for k, v in pairs(self.forced) do
  51.         self.destinations[k] = v
  52.     end
  53.  
  54.     rednet.broadcast({
  55.         sType = "lookup",
  56.         sProtocol = self.protocol,
  57.     }, "dns")
  58. end
  59.  
  60. function TP:getList()
  61.     return self.destinations
  62. end
  63.  
  64. function TP.daemon()
  65.     while true do
  66.         local event, id, msg, prot = os.pullEventRaw("rednet_message")
  67.         if prot == TP.protocol and TP.endpoint then
  68.             sleep(2)
  69.             os.queueEvent("tp_trigger")
  70.         elseif prot == "dns" and type(msg) == "table" and msg.sType == "lookup response" and msg.sProtocol == TP.protocol and type(msg.sHostname) == "string" then
  71.             TP.destinations[id] = msg.sHostname
  72.             os.queueEvent("tp_dns")
  73.         end
  74.     end
  75. end
  76.  
  77. return TP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement