MarcosKoco

API_Functions.lua

Sep 24th, 2021 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 KB | None | 0 0
  1. local System = os -- Used for System things like os.time() to System.time()
  2. local Screen = term -- Used for Screen thing on the turtle or computer like term.clear() to Screen.clear()
  3. local Per = peripheral -- Used shortcut for peripheral
  4. local Text = textutils -- Used shortcut for textilils
  5. local Pack = Text.serialize -- Makes a new command for packing ( shortcut used from local Text)
  6. local UnPack = Text.unserialize -- Makes a new command for unpacking ( shortcut used from local Text)
  7.  
  8. local w, h = Screen.getSize()
  9.  
  10. function getPerNames()
  11.    
  12.     local Data = Per.getNames()
  13.    
  14.     return Data
  15.    
  16. end
  17.  
  18. function FindDisk()
  19.    
  20.     SetColors(Screen, colors.white, colors.black)
  21.    
  22.     Screen.setCursorPos(math.floor((w - string.len("Finding Disk")) /2), (h-1))
  23.    
  24.     print("Finding Disk")
  25.    
  26.     SetColors(Screen, colors.black, colors.white)
  27.    
  28.     for i,name in pairs(Per.getNames()) do
  29.        
  30.         for j,method in pairs(Per.getMethods(name)) do
  31.            
  32.             if (method == 'isDiskPresent') then
  33.                
  34.                 return name
  35.                
  36.             end
  37.            
  38.         end
  39.        
  40.     end
  41.    
  42. end
  43.  
  44. function FindModem()
  45.    
  46.     SetColors(Screen, colors.white, colors.black)
  47.    
  48.     Screen.setCursorPos(math.floor((w - string.len("Finding Modem")) /2), (h-1))
  49.    
  50.     print("Finding Modem")
  51.    
  52.     SetColors(Screen, colors.black, colors.white)
  53.    
  54.     for i,name in pairs(Per.getNames()) do
  55.        
  56.         for j,method in pairs(Per.getMethods(name)) do
  57.            
  58.             if (method == 'isWireless') then
  59.                
  60.                 local M = Per.wrap(name)
  61.                
  62.                 if M.isWireless() == false then
  63.                    
  64.                     return name
  65.                    
  66.                 end
  67.                
  68.             end
  69.            
  70.         end
  71.        
  72.     end
  73.    
  74. end
  75.  
  76. function FindWirelessModem()
  77.    
  78.     SetColors(Screen, colors.white, colors.black)
  79.    
  80.     Screen.setCursorPos(math.floor((w - string.len("Finding Wireless Modem")) /2), (h-1))
  81.    
  82.     print("Finding Wireless Modem")
  83.    
  84.     SetColors(Screen, colors.black, colors.white)
  85.    
  86.     for i,name in pairs(Per.getNames()) do
  87.        
  88.         for j,method in pairs(Per.getMethods(name)) do
  89.            
  90.             if (method == 'isWireless') then
  91.                
  92.                 local M = Per.wrap(name)
  93.                
  94.                 if (M.isWireless) then
  95.                    
  96.                     return name
  97.                    
  98.                 end
  99.                
  100.             end
  101.            
  102.         end
  103.        
  104.     end
  105.    
  106. end
  107.  
  108. function FindMonitor()
  109.    
  110.     SetColors(Screen, colors.white, colors.black)
  111.    
  112.     Screen.setCursorPos(math.floor((w - string.len("Finding Monitor")) /2), (h-1))
  113.    
  114.     print("Finding Monitor")
  115.    
  116.     SetColors(Screen, colors.black, colors.white)
  117.    
  118.     for i,name in pairs(Per.getNames()) do
  119.        
  120.         for j,method in pairs(Per.getMethods(name)) do
  121.            
  122.             if (method == 'clear') then
  123.                
  124.                 return name
  125.                
  126.             end
  127.            
  128.         end
  129.        
  130.     end
  131.    
  132. end
  133.  
  134. function FindPrinter()
  135.    
  136.     SetColors(Screen, colors.white, colors.black)
  137.    
  138.     Screen.setCursorPos(math.floor((w - string.len("Finding Printer")) /2), (h-1))
  139.    
  140.     print("Finding Printer")
  141.    
  142.     SetColors(Screen, colors.black, colors.white)
  143.    
  144.     for i,name in pairs(Per.getNames()) do
  145.        
  146.         for j,method in pairs(Per.getMethods(name)) do
  147.            
  148.             if (method == 'newPage') then
  149.                
  150.                 return name
  151.                
  152.             end
  153.            
  154.         end
  155.        
  156.     end
  157.    
  158. end
  159.  
  160. function FindSpeaker()
  161.    
  162.     SetColors(Screen, colors.white, colors.black)
  163.    
  164.     Screen.setCursorPos(math.floor((w - string.len("Finding Speaker")) /2), (h-1))
  165.    
  166.     print("Finding Speaker")
  167.    
  168.     SetColors(Screen, colors.black, colors.white)
  169.    
  170.     for i,name in pairs(Per.getNames()) do
  171.        
  172.         for j,method in pairs(Per.getMethods(name)) do
  173.            
  174.             if (method == 'playSound') then
  175.                
  176.                 return name
  177.                
  178.             end
  179.            
  180.         end
  181.        
  182.     end
  183.    
  184. end
  185.  
  186. function DelFile(Path)
  187.  
  188.     if fs.exists(Path) then
  189.        
  190.         fs.delete(Path)
  191.        
  192.     end
  193.    
  194. end
  195.  
  196. function LoadFile(Path)
  197.    
  198.     if fs.exists(Path) then
  199.        
  200.         File = fs.open(Path, "r")
  201.         local Data = UnPack(File.readAll())
  202.         File.close()
  203.        
  204.         return Data
  205.        
  206.     end
  207.    
  208. end
  209.  
  210. function SaveFile(Path, For, Data)
  211.    
  212.     File = fs.open(Path, For)
  213.     File.write(Pack(Data))
  214.     File.close()
  215.    
  216. end
  217.  
  218. function SetColors(WhatScreen, Background, Text)
  219.    
  220.     WhatScreen.setBackgroundColor(Background)
  221.     WhatScreen.setTextColor(Text)
  222.    
  223. end
  224.  
  225. function SetTitle(WhatScreen, text)
  226.    w, h = WhatScreen.getSize()
  227.    WhatScreen.setCursorPos((w-string.len(text))/2+1, 1)
  228.    WhatScreen.write(text)
  229. end
  230.  
  231. function PadString(sText, iLen)
  232.    
  233.     local iTextLen = string.len(sText)
  234.    
  235.     -- Too short, pad
  236.    
  237.     if (iTextLen < iLen) then
  238.        
  239.         local iDiff = iLen - iTextLen
  240.        
  241.         return(sText..string.rep(" ",iDiff))
  242.        
  243.     end
  244.    
  245.    
  246.     -- Too long, trim
  247.    
  248.     if (iTextLen > iLen) then
  249.        
  250.         return(string.sub(sText,1,iLen))
  251.        
  252.     end
  253.    
  254.     -- Exact length
  255.    
  256.     return(sText)
  257.    
  258. end
  259.  
  260. function printCentered(WhatScreen,y,s)
  261.    
  262.     local x = math.floor((w - string.len(s)) /2)
  263.    
  264.     WhatScreen.setCursorPos(x,y)
  265.     WhatScreen.clearLine()
  266.     WhatScreen.write(s)
  267.    
  268. end
  269.  
  270. function printCenteredNC(WhatScreen,y,s)
  271.    
  272.     local x = math.floor((w - string.len(s)) /2)
  273.    
  274.     WhatScreen.setCursorPos(x,y)
  275.     WhatScreen.write(s)
  276.    
  277. end
  278.  
  279. function printQuart(WhatScreen,y,s)
  280.    
  281.     WhatScreen.setCursorPos(1,y)
  282.     WhatScreen.clearLine()
  283.     WhatScreen.write(s)
  284.    
  285. end
  286.  
  287. function printThreeQuart(WhatScreen,y,s)
  288.    
  289.     local x = math.floor(w - string.len(s))
  290.    
  291.     WhatScreen.setCursorPos(x,y)
  292.     WhatScreen.clearLine()
  293.     WhatScreen.write(s)
  294.    
  295. end
  296.  
  297. function printThreeQuartNC(WhatScreen,y,s)
  298.    
  299.     local x = math.floor(w - string.len(s))
  300.    
  301.     WhatScreen.setCursorPos(x,y)
  302.     WhatScreen.write(s)
  303.    
  304. end
Advertisement
Add Comment
Please, Sign In to add comment