JereTheJuggler

createPowerPlant.lua

Nov 29th, 2021 (edited)
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.00 KB | None | 0 0
  1. local FurnaceFillLevel = 2
  2. local ItemNames={
  3.     arcaneCrystal="forbidden_arcanus:arcane_crystal",
  4.     arcaneCrystalDust="forbidden_arcanus:arcane_crystal_dust",
  5.     emptyBucket="minecraft:bucket",
  6.     lavaBucket="minecraft:lava_bucket"
  7. }
  8. local PeripheralTypes={
  9.     basin="create:basin",
  10.     blastFurnace="minecraft:blast_furnace",
  11.     drawer="standardDrawer"
  12. }
  13. local FurnaceSlots={
  14.     top=1,
  15.     bottom=2,
  16.     output=3
  17. }
  18. local BasinSlots={
  19.     input=1,
  20.     output=10
  21. }
  22. local Peripherals={
  23.     basins={},
  24.     furnaces={},
  25.     emptyBucketDrawer=nil,
  26.     lavaBucketDrawer=nil,
  27.     crystalDrawer=nil,
  28.     setCrystalDrawer=function(self,name)
  29.         self.crystalDrawer = self.wrap(name)
  30.         local items = self.crystalDrawer.list()
  31.         for slot,item in pairs(items) do
  32.             if item.name == ItemNames.arcaneCrystal then
  33.                 self.crystalDrawer.crystalSlot = slot
  34.             elseif item.name == ItemNames.arcaneCrystalDust then
  35.                 self.crystalDrawer.dustSlot = slot
  36.             end
  37.         end
  38.         if self.crystalDrawer.crystalSlot == nil then
  39.             error("Couldn't find slot for arcane crystals")
  40.         elseif self.crystalDrawer.dustSlot == nil then
  41.             error("Couldn't find slot for crystal dust")
  42.         end
  43.     end,
  44.     wrap=function(name)
  45.         local perip = peripheral.wrap(name)
  46.         perip.name = name
  47.         return perip
  48.     end,
  49.     wrapAll=function(self)
  50.         for _,name in ipairs(peripheral.getNames()) do
  51.             local t = peripheral.getType(name)
  52.             if t == PeripheralTypes.blastFurnace then
  53.                 table.insert(self.furnaces,self.wrap(name))
  54.             elseif t == PeripheralTypes.basin then
  55.                 table.insert(self.basins,self.wrap(name))
  56.             elseif t == PeripheralTypes.drawer then
  57.                 local p = self.wrap(name)
  58.                 if p.size() == 2 then
  59.                     self.crystalDrawer = p
  60.                     local items = p.list()
  61.                     for slot,item in pairs(items) do
  62.                         if item.name == ItemNames.arcaneCrystal then
  63.                             self.crystalDrawer.crystalSlot = slot
  64.                         elseif item.name == ItemNames.arcaneCrystalDust then
  65.                             self.crystalDrawer.dustSlot = slot
  66.                         end
  67.                     end
  68.                     if self.crystalDrawer.crystalSlot == nil then
  69.                         error("Couldn't find slot for arcane crystals")
  70.                     elseif self.crystalDrawer.dustSlot == nil then
  71.                         error("Couldn't find slot for crystal dust")
  72.                     end
  73.                 else
  74.                     local i = p.getItemDetail(1)
  75.                     if i ~= nil then
  76.                         if i.name == ItemNames.lavaBucket then
  77.                             self.lavaBucketDrawer = p
  78.                         elseif i.name == ItemNames.emptyBucket then
  79.                             self.emptyBucketDrawer = p
  80.                         end
  81.                     end
  82.                 end
  83.             end
  84.         end
  85.         if self.lavaBucketDrawer == nil then
  86.             error("Couldn't find lava bucket drawer")
  87.         elseif self.emptyBucketDrawer == nil then
  88.             error("Couldn't find empty bucket drawer")
  89.         elseif self.crystalDrawer == nil then
  90.             error("Couldn't find arcane crystal drawer")
  91.         elseif #self.basins == 0 then
  92.             error("Couldn't find any basins")
  93.         elseif #self.furnaces == 0 then
  94.             error("Couldn't find any blast furnaces")
  95.         end
  96.     end
  97. }
  98. local Processes={
  99.     handleLava=function()
  100.         local emptyFurnace = function(furnace)
  101.             furnace.pushItems(Peripherals.emptyBucketDrawer.name,FurnaceSlots.bottom)
  102.         end
  103.         local addLavaToFurnace = function(furnace)
  104.             Peripherals.lavaBucketDrawer.pushItems(furnace.name,1,1,FurnaceSlots.bottom)
  105.         end
  106.         while true do
  107.             for _,furnace in ipairs(Peripherals.furnaces) do
  108.                 local item = furnace.list()[FurnaceSlots.bottom]
  109.                 if item == nil or item.count == 0 then
  110.                     addLavaToFurnace(furnace)
  111.                 elseif item.name == ItemNames.emptyBucket then
  112.                     emptyFurnace(furnace)
  113.                     addLavaToFurnace(furnace)
  114.                 end
  115.             end
  116.             sleep(10)
  117.         end
  118.     end,
  119.     handleCrystals=function()
  120.         local emptyBasin = function(basin)
  121.             basin.pushItems(Peripherals.crystalDrawer.name,BasinSlots.output,nil,Peripherals.crystalDrawer.crystalSlot)
  122.         end
  123.         local fillFurnace = function(f,c)
  124.             if not pcall(function()
  125.                 Peripherals.crystalDrawer.pushItems(f.name,Peripherals.crystalDrawer.crystalSlot,c,FurnaceSlots.top)
  126.             end) then
  127.                 term.setTextColor(colors.red)
  128.                 print("Errored filling furnace "..f.name)
  129.                 print("Rewrapping crystal drawer")
  130.                 term.setTextColor(colors.white)
  131.                 Peripherals:setCrystalDrawer(Peripherals.crystalDrawer.name)
  132.             end
  133.         end
  134.         while true do
  135.             for _,basin in ipairs(Peripherals.basins) do
  136.                 emptyBasin(basin)
  137.             end
  138.             for _,furnace in ipairs(Peripherals.furnaces) do
  139.                 local items = furnace.list()
  140.                 if items[FurnaceSlots.top] == nil or items[FurnaceSlots.top].count < FurnaceFillLevel then
  141.                     local fillCount = FurnaceFillLevel
  142.                     if items[FurnaceSlots.top] ~= nil then
  143.                         fillCount = FurnaceFillLevel - items[FurnaceSlots.top].count
  144.                     end
  145.                     fillFurnace(furnace,fillCount)
  146.                 end
  147.             end
  148.             sleep(.5)
  149.         end
  150.     end,
  151.     handleDust=function()
  152.         local fillBasin = function(basin,count)
  153.             if not pcall(function()
  154.                 Peripherals.crystalDrawer.pushItems(basin.name,Peripherals.crystalDrawer.dustSlot,count)
  155.             end) then
  156.                 term.setTextColor(colors.red)
  157.                 print("Errored filling basin "..basin.name)
  158.                 print("Rewrapping crystal drawer")
  159.                 term.setTextColor(colors.white)
  160.                 Peripherals:setCrystalDrawer(Peripherals.crystalDrawer.name)
  161.             end
  162.         end
  163.         local emptyFurnace = function(furnace)
  164.             furnace.pushItems(Peripherals.crystalDrawer.name,FurnaceSlots.output)
  165.         end
  166.         while true do
  167.             for _,furnace in ipairs(Peripherals.furnaces) do
  168.                 emptyFurnace(furnace)
  169.             end
  170.             local dustCount
  171.             if not pcall(function()
  172.                 dustCount = Peripherals.crystalDrawer.list()[Peripherals.crystalDrawer.dustSlot].count
  173.             end) then
  174.                 term.setTextColor(colors.red)
  175.                 print("Errored getting dust count in drawer")
  176.                 print("Rewrapping crystal drawer")
  177.                 term.setTextColor(colors.white)
  178.                 Peripherals:setCrystalDrawer(Peripherals.crystalDrawer.name)
  179.             else
  180.                 local dustPerBasin = math.ceil(dustCount / #Peripherals.basins)
  181.                 for _,basin in ipairs(Peripherals.basins) do
  182.                     local items = basin.list()
  183.                     local existingCount = 0
  184.                     if items[BasinSlots.input] ~= nil then
  185.                         existingCount = items[BasinSlots.input].count
  186.                     end
  187.                     if existingCount < 64 then
  188.                         fillBasin(basin,math.min(
  189.                             dustPerBasin,
  190.                             64 - existingCount
  191.                         ))
  192.                     end
  193.                 end
  194.             end
  195.             sleep(.1)
  196.         end
  197.     end
  198. }
  199.  
  200. Peripherals:wrapAll()
  201. parallel.waitForAll(
  202.     Processes.handleLava,
  203.     Processes.handleCrystals,
  204.     Processes.handleDust
  205. )
Add Comment
Please, Sign In to add comment