rkinasz

peripheralUtils.lua

Feb 7th, 2022
1,642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.56 KB | None | 0 0
  1. --list of periperherals already in use
  2. local usedPeripherals = {}
  3. local error = true
  4.  
  5. --checks the list of used peripherals
  6. local function isInUse (p)
  7.   for k,v in pairs(usedPeripherals) do
  8.     if v == p then
  9.       return true
  10.     end
  11.   end
  12.   return false
  13. end
  14.  
  15. --detects and returns the side the peripheral is on
  16. function detectPeripheral (name)
  17.     for k,v in pairs(redstone.getSides()) do
  18.         if not isInUse(v) and peripheral.getType(v) == name then
  19.              return v
  20.         end
  21.     end
  22.  
  23.     for k,v in pairs (peripheral.getNames()) do
  24.       if string.find(v, name) and not isInUse(v) then
  25.         return v
  26.       end
  27.     end
  28.  
  29.     if error then
  30.       print("Cannot find peripheral: " .. name)
  31.     end
  32.  
  33.     return nil
  34. end
  35.  
  36. --returns a peripheral handler
  37. function wrapPeripheral (name, lock)
  38.   local p = detectPeripheral(name)
  39.   if p then
  40.     if lock then
  41.       table.insert(usedPeripherals, p)
  42.     end
  43.     return peripheral.wrap(p)
  44.   else
  45.     return nil
  46.   end
  47. end
  48.  
  49.  
  50. function setError (b)
  51.   error = b
  52. end
  53.  
  54. --returns a list of peripheral objects of given type
  55. function wrapMultiplePeripherals (type)
  56.   peripheralUtils.setError(false)
  57.   local p = {}
  58.   local t = peripheralUtils.wrapPeripheral(type, true)
  59.   while t do
  60.     table.insert(p, t)
  61.     t = peripheralUtils.wrapPeripheral(type, true)
  62.   end
  63.   peripheralUtils.setError(true)
  64.   return p
  65. end
  66.  
  67.  
  68. --removes peripheral name from used peripherals
  69. function unlock (p)
  70.   for k,v in pairs (usedPeripherals) do
  71.     if v == p then
  72.       table.remove(k)
  73.     end
  74.   end
  75. end
  76.  
Advertisement
Add Comment
Please, Sign In to add comment