Advertisement
hendgo12

wrap

Apr 15th, 2021
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. --Simple Wrap 1.1
  2. --NuAoA
  3. local targs = {...}
  4. local comp_faces = {"front","back","left","right","top","bottom"}
  5. -- If peripheral type is passed, returns true and the face it was found on, if a face is passed, returns true and the type of peripheral on that face.
  6. function find(...)
  7. if #arg ~= 1 then
  8. error("[Wrap] Proper syntax is wrap.find(PeripheralType or ComputerFace)")
  9. else
  10. for i=1,#comp_faces do -- If a periphal type is passed
  11. if peripheral.isPresent(comp_faces[i]) and peripheral.getType(comp_faces[i]) == arg[1] then
  12. return true,comp_faces[i]
  13. end
  14. end
  15. for i=1,#comp_faces do -- If a face is passed look for peripheral
  16. if string.lower(arg[1]) == comp_faces[i] then
  17. if peripheral.isPresent(comp_faces[i]) then
  18. return true,peripheral.getType(comp_faces[i])
  19. end
  20. end
  21. end
  22. end
  23. return false,nil
  24. end
  25.  
  26. function attach(...) -- will search for perphial type or face and attach that peripheral. returns wrap function and bool(success)
  27. if #arg ~= 1 then error("Wrap: Proper syntax is wrap.attach(PeripheralType or ComputerFace)")
  28. else
  29. local bool,kind = find(arg[1])
  30. if bool then
  31. for i=1,#comp_faces do
  32. if kind == comp_faces[i] then
  33. return peripheral.wrap(kind),true
  34. end
  35. end
  36. return peripheral.wrap(arg[1]),true
  37. else
  38. print("[Wrap] No Peripheral Found for type/face: "..arg[1])
  39. return nil,false
  40. end
  41.  
  42. end
  43. end
  44.  
  45. --returns the unqiue name of a remote peripheral attached with a modem
  46. function getRemoteName(name)
  47. local modem,bool = attach("modem")
  48. if bool and not modem.isWireless() then
  49. local tab = modem.getNamesRemote()
  50. for k,v in pairs(tab) do
  51. if string.lower(string.match(v,"(.+)_%d")) == string.lower(name) then
  52. return v
  53. end
  54. end
  55. return nil
  56. else
  57. print("[Wrap] no wired modems found! ("..name..")")
  58. end
  59.  
  60. end
  61.  
  62. function scan(...)
  63. print("[Wrap] Scanning for peripherals...")
  64. for i = 1,#comp_faces do
  65. local bool,name = find(comp_faces[i])
  66. if bool then
  67. print("[Wrap] "..name.." found on face "..comp_faces[i])
  68. if name == "modem" then
  69. local p = peripheral.wrap(comp_faces[i])
  70. if p.isWireless() then
  71. print("[Wrap] ---> "..name.." is wireless")
  72. else
  73. local tab = p.getNamesRemote()
  74. for k,v in pairs(tab) do
  75. print("[Wrap] ---> "..v.." found in modem network")
  76. end
  77. end
  78. end
  79. end
  80. end
  81. end
  82.  
  83.  
  84. if #targs>0 then
  85. if targs[1] == "scan" then
  86. scan()
  87. elseif targs[1] == "getName" and #targs >1 then
  88. local rName = getRemoteName(targs[2])
  89. if rName ~= nil then
  90. print("[Wrap] The remote name is: "..rName)
  91. else
  92. print("[Wrap] No remote name was found")
  93. end
  94. else
  95. local bool, name = find(targs[1])
  96. if bool then
  97. print("[Wrap] "..name.." found on "..targs[1].." face.")
  98. else
  99. print("[Wrap] Nothing found.")
  100. end
  101. end
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement