Advertisement
bobmarley12345

TE induction furnace auto clear

Nov 15th, 2023 (edited)
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.46 KB | None | 0 0
  1. local INTERFACES_NAMES = {}
  2. local INTERFACES = {}
  3.  
  4. -- config loadable info
  5. local REDSTONE_TRIGGER_SIDES = {}
  6. local INTERFACE_PULL_DIRECTION = nil
  7.  
  8. function CanonicaliseSide(side)
  9.     if (side == nil or #side < 3 or #side > 6) then
  10.         return nil
  11.     end
  12.  
  13.     local s = string.lower(side)
  14.     if (s == "left" or s == "top" or s == "right" or s == "bottom" or s == "front" or s == "back") then
  15.         return s
  16.     end
  17.  
  18.     return nil
  19. end
  20.  
  21. function CanonicaliseNSWE(dir)
  22.     if (dir == nil or #dir < 3 or #dir > 6) then
  23.         return nil
  24.     end
  25.  
  26.     local s = string.lower(dir)
  27.     if (s == "north" or s == "east" or s == "south" or s == "west" or s == "up" or s == "down") then
  28.         return s
  29.     end
  30.  
  31.     return nil
  32. end
  33.  
  34. function SplitStringByComma(input_str)
  35.     return string.gmatch(input_str, "([^,]+)")
  36. end
  37.  
  38. function LoadConnectedInterfaces()
  39.     INTERFACES_NAMES = {}
  40.     INTERFACES = {}
  41.     local modem_connections = peripheral.getNames("back")
  42.     if (modem_connections == nil) then
  43.         return
  44.     end
  45.  
  46.     for k, peripheralName in pairs(modem_connections) do
  47.         if (tostring(peripheralName):find("me_interface") ~= nil) then
  48.             local methodTable = peripheral.wrap(peripheralName)
  49.             if (methodTable ~= nil) then
  50.                 table.insert(INTERFACES, methodTable)
  51.                 table.insert(INTERFACES_NAMES, peripheralName)
  52.             end
  53.         end
  54.     end
  55. end
  56.  
  57. function LoadConfigInfo()
  58.     if (not fs.exists("config")) then
  59.         return false
  60.     end
  61.  
  62.     local hFile = fs.open("config", "r")
  63.     if (hFile == nil) then
  64.         print("Failed to open config file")
  65.         return false
  66.     end
  67.  
  68.     local isFail = false
  69.     REDSTONE_TRIGGER_SIDES = {}
  70.     local rs_trig = hFile.readLine()
  71.     if (rs_trig ~= nil and #rs_trig > 0) then
  72.         for side in SplitStringByComma(rs_trig) do
  73.             local realSide = CanonicaliseSide(side)
  74.             if (realSide ~= nil) then
  75.                 table.insert(REDSTONE_TRIGGER_SIDES, realSide)
  76.             else
  77.                 print("Invalid side in config: " .. realSide)
  78.                 isFail = true
  79.             end
  80.         end
  81.     end
  82.  
  83.     INTERFACE_PULL_DIRECTION = nil
  84.     local itf_pull_dir_raw = hFile.readLine()
  85.     local itf_pull_dir = CanonicaliseNSWE(itf_pull_dir_raw)
  86.     if (itf_pull_dir ~= nil) then
  87.         INTERFACE_PULL_DIRECTION = itf_pull_dir
  88.     else
  89.         print("Invalid interface pull direction: " .. itf_pull_dir_raw)
  90.         isFail = true
  91.     end
  92.  
  93.     hFile.close()
  94.     return not isFail and #REDSTONE_TRIGGER_SIDES > 0 and INTERFACE_PULL_DIRECTION ~= nil
  95. end
  96.  
  97. function SaveConfigInfo()
  98.     local hFile = fs.open("config", "w")
  99.     if (hFile == nil) then
  100.         print("Failed to open config file for writing")
  101.         return
  102.     end
  103.  
  104.     if (REDSTONE_TRIGGER_SIDES ~= nil) then
  105.         local str = table.concat(REDSTONE_TRIGGER_SIDES, ",")
  106.         hFile.writeLine(str)
  107.         print("Writing to config: " .. str)
  108.     else
  109.         hFile.writeLine("")
  110.     end
  111.  
  112.     if (INTERFACE_PULL_DIRECTION ~= nil) then
  113.         hFile.writeLine(INTERFACE_PULL_DIRECTION)
  114.         print("Writing to config: " .. INTERFACE_PULL_DIRECTION)
  115.     else
  116.         hFile.writeLine("")
  117.     end
  118. end
  119.  
  120. function SetupConfig()
  121.     print("Input the sides on the computer that, when a redstone signal is received, will clear all induction furnaces")
  122.     print("(comma separated list of: top, bottom, left, right, front or back)")
  123.     term.write(">")
  124.  
  125.     local inputSides = {}
  126.     local sides = read()
  127.     for side in SplitStringByComma(sides) do
  128.         local realSide = CanonicaliseSide(side)
  129.         if (realSide == nil) then
  130.             print("Invalid side: " .. side)
  131.             return false
  132.         end
  133.        
  134.         table.insert(inputSides, realSide)
  135.     end
  136.  
  137.     print("Input the world direction of the induction furnaces, relative to the ME Interfaces")
  138.     print("(top, bottom, north, east, south, west)")
  139.     term.write(">")
  140.     local pull_dir_raw = read()
  141.     local pull_dir = CanonicaliseNSWE(pull_dir_raw)
  142.     if (pull_dir == nil) then
  143.         print("Invalid direction: " .. pull_dir_raw)
  144.         return false
  145.     end
  146.  
  147.     REDSTONE_TRIGGER_SIDES = inputSides
  148.     INTERFACE_PULL_DIRECTION = pull_dir
  149.     return true
  150. end
  151.  
  152. function OnRedstoneInput()
  153.     -- clear interfaces
  154.  
  155.     print("Clearing " .. #INTERFACES .. " induction furnaces at " .. INTERFACE_PULL_DIRECTION)
  156.     for i, itf in pairs(INTERFACES) do
  157.         itf.pullItem(INTERFACE_PULL_DIRECTION, 1)
  158.         itf.pullItem(INTERFACE_PULL_DIRECTION, 2)
  159.     end
  160. end
  161.  
  162. function Main()
  163.     if (not LoadConfigInfo()) then
  164.         while (not SetupConfig()) do end
  165.         SaveConfigInfo()
  166.     end
  167.    
  168.     LoadConnectedInterfaces()
  169.     print("Loaded " .. #INTERFACES .. " interfaces via modem connections")
  170.     print("TIP1: To reset the config file, terminate the program and run 'rm config'")
  171.     print("")
  172.     print("TIP2: The ME interface must also be placed on the output of the induction furnaces (red, yellow or orange sides)")
  173.  
  174.     while true do
  175.         os.pullEvent("redstone")
  176.  
  177.         local hasInput = false
  178.         for i, side in pairs(REDSTONE_TRIGGER_SIDES) do
  179.             if (rs.getInput(side)) then
  180.                 hasInput = true
  181.                 break
  182.             end
  183.         end
  184.  
  185.         if (hasInput) then
  186.             OnRedstoneInput()
  187.             print("[" .. os.clock() .. "] Redstone received!")
  188.         end
  189.     end
  190. end
  191.  
  192. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement