Advertisement
Marikc0

[DEV] Marik's CC/OP API

Nov 21st, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.40 KB | None | 0 0
  1. -- Marik's CC/OpenPeripherals API
  2. -- ComputerCraft/OpenPeripherals Base Monitoring Program for MC 1.6.4
  3. -- Thanks: Fogger, @EsperNet: #OpenMods, #computercraft @Freenode: #lua
  4.  
  5. function pLoad()
  6.     machines = peripheral.getNames() -- load a list of peripherals into a table
  7.     table.sort(machines) -- so it displays in non-random manner
  8.  
  9. return machines
  10. end
  11.  
  12. function clearPos(x, y, side)
  13. -- clears a specific line @ x, y and side
  14.  
  15.     m = peripheral.wrap(side)
  16.  
  17.     m.setCursorPos(x,y)
  18.     m.clearLine()
  19.  
  20. end
  21.  
  22. function appendString(side, color, text, clear, x, y)
  23. -- side: check program config (ie: left, right, aux_1)
  24. -- color: computercraft color codes
  25. -- text: the text to append
  26. -- clear: true or false (clear the line when you append the string?)
  27. -- x or y: if either is zero it uses the getCursorPos() x or y
  28.  
  29.     local m = peripheral.wrap(side)
  30.     local a,b = m.getCursorPos()
  31.    
  32.     if x == 0 and y ~= 0 then
  33.         m.setCursorPos(a,y)
  34.     elseif x ~= 0 and y == 0 then
  35.         -- set x position
  36.         m.setCursorPos(x,b)
  37.     elseif x ~= 0 and y ~= 0 then
  38.         m.setCursorPos(x,y)
  39.     elseif x == 0 and y == 0 then
  40.         m.setCursorPos(a,b)
  41.     end
  42.    
  43.     m.setTextColor(color)
  44.    
  45.     if clear == true then
  46.         m.clearLine()
  47.     end
  48.  
  49.     m.write(text)
  50.  
  51. end
  52.  
  53. function cString(x, y, color, side, text)
  54. -- x, y: position coords
  55. -- color: computercraft color codes
  56. -- side: check program config (ie: left, right, aux_1)
  57. -- text: any string
  58.  
  59. local m = peripheral.wrap(side)
  60.  
  61.     m.setCursorPos(x,y)
  62.     m.setTextColor(color)
  63.     m.write(text)
  64.    
  65. end
  66.  
  67. function getTank(tankPeriph)
  68.  
  69. local tableInfo = tankPeriph.getTankInfo("unknown")[1]
  70.  
  71. local fluidRaw, fluidName, fluidAmount, fluidCapacity, fluidID
  72.  
  73.     fluidRaw = tableInfo.rawName
  74.     fluidName = tableInfo.name
  75.     fluidAmount = tableInfo.amount
  76.     fluidCapacity = tableInfo.capacity
  77.     fluidID = tableInfo.id
  78.  
  79. return fluidRaw, fluidName, fluidAmount, fluidCapacity, fluidID
  80. end
  81.  
  82. function getBuckets(fluidAmount)
  83. -- Convert millibuckets to buckets
  84.  
  85.     fluidBuckets = fluidAmount / 1000
  86.  
  87. return fluidBuckets
  88. end
  89.  
  90. function numOfMachines()
  91.  
  92.     local machines = peripheral.getNames()
  93.     count = #machines
  94.     table.sort(machines)
  95.     n = 0
  96.    
  97.     for i=1, #machines do
  98.         if string.find(machines[i], "mfsu")
  99.         or string.find(machines[i], "mfe")
  100.         or string.find(machines[i], "cesu")  
  101.         or string.find(machines[i], "batbox") then
  102.             n = n + 1
  103.         end
  104.     end
  105.    
  106. return n, count
  107. end
  108.  
  109. function comma(number)
  110. -- Formats long numbers with commas
  111.    
  112.     if string.len(number) < 6 then
  113.         -- leave as is
  114.     elseif string.len(number) == 6 then
  115.         number = string.gsub(number, "^(-?%d+)(%d%d%d)", '%1,%2')
  116.     elseif string.len(number) >= 7 and string.len(number) <= 9 then
  117.         number = string.gsub(number, "^(-?%d+)(%d%d%d)(%d%d%d)", '%1,%2,%3')
  118.     elseif string.len(number) == 10 then -- need to fix
  119.         number = string.gsub(number, "^(-?%d+)(%d%d%d)(%d%d%d)(%d%d%d)", '%1,%2,%3,%4')
  120.     else
  121.         -- nothing
  122.     end
  123.  
  124. return number
  125. end
  126.  
  127. function itemRename(itemName)
  128. -- Shortens some long IC2/MFR/AE names to a more readable format
  129. -- This could be done with a lot less lines, but I'm still working on it.
  130.  
  131.     if (string.find(itemName, "ic2.item",1) == 1) then
  132.         itemName = string.sub(itemName, 9)
  133.         itemName = removeDots(itemName)
  134.  
  135.             if string.find(itemName, "%s") then
  136.                 -- If there's a space in the itemName like "Iron Ore" don't do anything to it!
  137.                 -- It's "probably" correct.
  138.             else
  139.                 -- It's probably "CrushedIronOre" or something similar.
  140.                 -- Fix it.
  141.                 itemName = spaceAtSecondUpper(itemName)
  142.             end        
  143.  
  144.     elseif (string.find(itemName, "ic2.",1) == 1) then
  145.         itemName = string.sub(itemName, 5)
  146.         itemName = removeDots(itemName)
  147.  
  148.             if string.find(itemName, "%s") then
  149.                 -- end
  150.             else
  151.                 itemName = spaceAtSecondUpper(itemName)
  152.             end    
  153.  
  154.     elseif (string.find(itemName, "tile.mfr.",1) == 1) then
  155.         itemName = string.sub(itemName, 10)
  156.     elseif (string.find(itemName, "appeng.materials.",1) == 1)
  157.         or (string.find(itemName, "AppEng.Materials.",1) == 1) then
  158.         itemName = string.sub(itemName, 18)
  159.         itemName = removeDots(itemName)
  160.         itemName = spaceAtSecondUpper(itemName)
  161.     elseif (string.find(itemName, "item.mfr.",1) == 1) then
  162.         itemName = string.sub(itemName, 10)
  163.         itemName = removeDots(itemName)
  164.     elseif (string.find(itemName, "rubberwood.log.name",1) == 1) then
  165.         itemName = removeDots(itemName)
  166.     elseif (string.find(itemName, "item.openperipheral",1) == 1) then
  167.         itemName = string.sub(itemName, 21)
  168.         itemName = removeDots(itemName)
  169.         itemName = spaceAtSecondUpper(itemName)
  170.     end
  171.  
  172.  
  173. return itemName
  174. end
  175.  
  176. function removeDots(itemName)
  177. -- remove any "." in itemName and replaces with spaces
  178. -- also remove any occurence of "name" in the ore
  179.  
  180.     itemName = string.gsub(itemName, "%.", " ") -- change rubber.raw.name to "rubber raw name"
  181.     itemName = string.gsub(itemName, "%name", "") -- change "rubber raw name" to "rubber raw"
  182.     itemName = string.gsub(itemName, "block", "") -- remove "block" from the name
  183.  
  184. return itemName
  185. end
  186.  
  187. function spaceAtSecondUpper(itemName)
  188. -- Insert a space before each occurence of a capital letter
  189. -- Remove the leading space
  190. -- This is to change ore names like "PurifiedCrushedCopperOre" to "Purified Crushed Copper Ore"
  191.  
  192.     itemName = itemName:gsub("(%S)(%u)", "%1 %2")
  193.     itemName = itemName:gsub("^%s*(.-)%s*$", "%1")
  194.  
  195. return itemName
  196. end
  197.  
  198. function GPSinfo()
  199.  
  200. local x,y,z = gps.locate(5)
  201.     if x == nil then
  202.         return false
  203.     else
  204.         return true
  205.     end
  206. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement