Advertisement
Marlingaming

Generator Manager

Nov 22nd, 2022 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. local Modem = peripheral.wrap("back")
  2. local Eng = peripheral.wrap("minecraft:furnace_1")
  3. local FuelStore = peripheral.wrap("minecraft:chest_1")
  4. local TChest = peripheral.wrap("minecraft:chest_2")
  5.  
  6. local Delay = 3--for Item toggle
  7.  
  8. local UpTimes = {5,22}
  9.  
  10. local FuelI = {"minecraft:charcoal",5}--(set as false for no fuel). itemtag, targetamount
  11. local A_Pwr = {1,43200,false,false,false}--GenRole(1-Main,2-Backup,3-starter),MaxPwr(perhour),State,Run,usesUptimes
  12.  
  13. local ID = 1
  14.  
  15. local function Switch()
  16. local Items = TChest.list()
  17. if Items[1].count > 0 then TChest.pushItems(Eng,1) else TChest.pullItems(Eng,1) end
  18. end
  19.  
  20. local function CheckEng()
  21. local list = Eng.list()
  22. local Fuel = list[2].count
  23. if Fuel < FuelI[2] and A_Pwr[4] == true then
  24. for slot, item in pairs(FuelStore.list()) do
  25. if item.name == FuelI[1] and Fuel < FuelI[2] then FuelStore.pushItems(Eng,1,1) Fuel = Fuel + 1 end
  26. end
  27. end
  28. return Fuel
  29. end
  30.  
  31. local function Action(D)
  32. local Ac = textutils.unserialize(D)
  33. local Response = {}
  34. if (Ac[1][1] == "G" and Ac[1][2] ~= A_Pwr[1]) or (Ac[1][1] == "ID" and Ac[1][2] ~= ID) then
  35. print("msg not for unit")
  36. elseif Ac[2] == "shutdown" then
  37. A_Pwr[3] = false
  38. A_Pwr[4] = false
  39. Response = {true}
  40. elseif Ac[2] == "startup" then
  41. A_Pwr[3] = true
  42. Response = {true}
  43. elseif Ac[2] == "starter" then
  44. A_Pwr[3] = true
  45. A_Pwr[4] = true
  46. CheckEng()
  47. Switch()
  48. os.sleep(Ac[3])
  49. Switch()
  50. A_Pwr[3] = false
  51. A_Pwr[4] = false
  52. Response = {true}
  53. elseif Ac[2] == "status" then
  54. Response = {ID,A_Pwr[3],A_Pwr[4],A_Pwr[5],CheckEng()}
  55. end
  56. return textutils.serialize(Response)
  57. end
  58.  
  59. local function Loop()
  60. Modem.open(223)
  61. local S = os.setAlarm(os.time("ingame") + (Delay * 0.01))
  62. local E = os.setAlarm(os.time("ingame") + 1)
  63. while true do
  64. local a, b, c, d, e = os.pullEvent()
  65. if a == "alarm" and A_Pwr[4] == true then
  66. if b == S then
  67. Switch()
  68. S = os.setAlarm(os.time("ingame") + (Delay * 0.01))
  69. elseif b == E then
  70. CheckEng()
  71. E = os.setAlarm(os.time("ingame") + 1)
  72. end
  73. elseif a == "modem_message" then
  74. Modem.transmit(c,223,Action(e))
  75. else
  76. if os.time("ingame") > UpTimes[2] or os.time("ingame") < UpTimes[1] and A_Pwr[5] == true then A_Pwr[4] = false elseif A_Pwr[3] == true then A_Pwr[4] = true end
  77. end
  78. os.queueEvent("pass")
  79. end
  80. end
  81.  
  82. Loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement