Advertisement
melzneni

Turti lib better peripherals

Sep 24th, 2023 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. local api = {}
  2.  
  3. function api.findPeripherals(filter)
  4. local peripheralObjects = {}
  5. for _, name in pairs(peripheral.getNames()) do
  6. local type = name
  7. local i = name:find("_")
  8. if i then
  9. type = name:sub(1, i - 1)
  10. end
  11. if filter == nil or string.lower(type) == string.lower(filter) then
  12. table.insert(peripheralObjects,
  13. instantiateClass("Peripheral", name)
  14. )
  15. end
  16. end
  17. return toTurtiArray(peripheralObjects)
  18. end
  19.  
  20. function api.wrapPeripheral(side)
  21. return instantiateClass("Peripheral", side)
  22. end
  23.  
  24. local PERIPHERAL_CLASS_TYPE = {
  25. key = "Peripheral",
  26. to_string = function(p, context)
  27. return "Peripheral(" .. context.name .. ")"
  28. end,
  29. methods = {
  30. call = function(p, context, ...)
  31. return peripheral.call(context.name, ...)
  32. end,
  33. getName = function(p, context)
  34. return peripheral.getName(peripheral.wrap(context.name))
  35. end
  36. },
  37. getters = {
  38. name = function(p, context)
  39. return context.name
  40. end
  41. },
  42. init = function(p, name)
  43. return { name = name }
  44. end
  45. }
  46.  
  47. return {
  48. name = "betterPeripherals",
  49. api = api,
  50. classes = {
  51. PERIPHERAL_CLASS_TYPE
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement