Advertisement
Jetpack_Maniac

AutoBee OC

Jul 8th, 2017
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.30 KB | None | 0 0
  1. ---- AutoBee for OpenComputers ----
  2.  
  3. local running = true
  4. if pcall(function() dofile("autobeeCore.lua") end) == false then
  5.   if pcall(function() dofile("autobee/autobeeCore.lua") end) == false then
  6.     print("Missing core library, fetching from pastebin.")
  7.     shell.execute("pastebin get Vvckgdst autobeeCore.lua")
  8.   end
  9. end
  10.  
  11. -- Peripheral check
  12. function peripheralCheck()
  13.   local apiary = nil
  14.   for address, componentType in pairs(component.list()) do
  15.     -- this is the search for 1.7.10 Forestry apiaries
  16.     if string.find(componentType, "apiculture") and componentType:sub(21,21) == "0" then
  17.       apiary = address
  18.     -- this is the search for 1.10.2 Forestry apiaries and bee houses
  19.     elseif componentType == "bee_housing" then
  20.       apiary = address
  21.       print("WARNING: OpenComputers on 1.10.2 does not support necessary APIs. See https://github.com/jetpack-maniac/autobee for more information.")
  22.       os.exit()
  23.     end
  24.   end
  25.   if apiary == nil then
  26.     print("No apiaries found.  Closing program.")
  27.     os.exit()    
  28.   else
  29.     dependencyCheck(component.proxy(apiary))
  30.   end
  31. end
  32.  
  33. -- looks at a device and determines if it's a valid apiary, returns true or false
  34. function isApiary(address)
  35.   if address == nil then return false end
  36.   device = component.type(address)
  37.   -- 1.10.2 Apiaries
  38.   if string.find(device, "bee_housing") then -- TODO discriminate apiaries versus bee houses
  39.     return true
  40.   -- 1.7.10 Apiaries
  41.   elseif string.find(device, "apiculture") and device:sub(21,21) == "0" then
  42.     return true
  43.   else
  44.   return false
  45.   end
  46. end
  47.  
  48. -- examines peripherals and returns a valid apiary or nil
  49. function findApiary()
  50.   local devices = component.list()
  51.   for address, device in pairs(devices) do
  52.     if isApiary(address) then
  53.       return component.proxy(address)
  54.     else
  55.       return false
  56.     end
  57.   end
  58. end
  59.  
  60. -- Version Check
  61. if pcall(function() component = require("component") end) == true then
  62.   if component.proxy ~= nil then
  63.     version = "OpenComputers"
  64.     keyboard = require("keyboard")
  65.     event = require("event")
  66.     term = require("term")
  67.     peripheralCheck()
  68.     print("AutoBee running.")
  69.     print("Hold Ctrl+W to stop. Hold Ctrl+L to clear terminal.")
  70.   else
  71.     error("This version is for OpenComputers.  See https://github.com/jetpack-maniac/autobee for more details.")
  72.   end
  73. end
  74.  
  75. local apiaryTimerIDs = {}
  76.  
  77. --These next two functions are callbacks to enable OC events to work properly
  78. function deviceConnect(event, address)
  79.   addDevice(address)
  80. end
  81.  
  82. function deviceDisconnect(event, address)
  83.   removeDevices()
  84.   initDevices()
  85. end
  86.  
  87. function removeDevices()
  88.   for timerID, address in pairs(apiaryTimerIDs) do
  89.     event.cancel(address)
  90.   end
  91.   apiaryTimerIDs = {}
  92. end
  93.  
  94. function addDevice(address)
  95.   if address == nil then return false end
  96.   if isApiary(address) == true then
  97.     local apiary = Apiary(component.proxy(address), address)
  98.     apiaryTimerIDs[address] = event.timer(delay, function() apiary.checkApiary() end, math.huge)
  99.     print(size(apiaryTimerIDs).." apiaries connected.")
  100.     return true
  101.   end
  102.   return false
  103. end
  104.  
  105. function initDevices()
  106.   --Aux ignore in case the program crashed and listeners are still active
  107.   event.ignore("component_available",deviceConnect)
  108.   event.ignore("component_removed",deviceDisconnect)
  109.   local devices = component.list()
  110.   for address, device in pairs(devices) do
  111.     addDevice(address)
  112.   end
  113.   event.listen("component_added",deviceConnect)
  114.   event.listen("component_removed",deviceDisconnect)
  115. end
  116.  
  117. ----------------------
  118. -- The main loop
  119. ----------------------
  120.  
  121. if running == true then initDevices() end -- inits devices upon first run
  122.  
  123. while running do
  124.   if version == "OpenComputers" then
  125.     if keyboard.isKeyDown(keyboard.keys.w) and keyboard.isControlDown() then
  126.       event.ignore("component_available",deviceConnect)
  127.       event.ignore("component_removed",deviceDisconnect)
  128.       removeDevices()
  129.       print("AutoBee: Interrupt detected. Closing program.")
  130.       break
  131.     end
  132.     if keyboard.isKeyDown(keyboard.keys.l) and keyboard.isControlDown() then
  133.       term.clear()
  134.       print("AutoBee running.")
  135.       print("Hold Ctrl+W to stop. Hold Ctrl+L to clear terminal.")
  136.       print(size(apiaryTimerIDs).." apiaries connected.")
  137.     end
  138.     os.sleep(delay)
  139.   end
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement