StarMaker23

AutoSearedFurnace_Counter_Lategame.lua

Mar 4th, 2023 (edited)
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.03 KB | None | 0 0
  1. ------------------------------------------------------- Main Code
  2. ---------------------- Get peripherals
  3.  
  4. Output_P = peripheral.find("storagedrawers:controllerslave")
  5. Input_P = peripheral.find("storagedrawers:controller")
  6. Furnace_P = peripheral.find("minecraft:tconstruct_seared_furnace")
  7. Monitor_P = peripheral.find("monitor")
  8. if Monitor_P then
  9.     term.redirect(Monitor_P)
  10.     Monitor_P.setTextScale(0.5)
  11. end
  12. term.setTextColor(colors.green)
  13. print('\n\n\n\n\n\n\n\n\n\n\n\n\nStarted Up...')
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. ------------------------------------------------------------------------------------------------------------------
  28. ------------------------------------------------------------------------------------------------------------------
  29. ------------------------------------------------------------------------------------------------------------------
  30. -------- print peripherals
  31. for _, X in ipairs (peripheral.getNames()) do
  32.     print (X)
  33. end
  34.  
  35.  
  36.  
  37.  
  38. function printSubTables(DATA)
  39.     if type(DATA) == 'table' then
  40.         for k,v in pairs(DATA) do
  41.             if type(v) == 'table' then
  42.                 printSubTables(DATA)
  43.             end
  44.            
  45.         end
  46.        
  47.     else
  48.         print(DATA)
  49.     end
  50.    
  51. end
  52.  
  53. ------- print peripheral methods
  54.  
  55. function PrintPeripheralMethods(peripheral_Q)
  56.    
  57.     for n, METHOD in pairs(peripheral.getMethods(peripheral.getName(peripheral_Q))) do
  58.         print(' -', n, METHOD)
  59.     end
  60. end
  61.  
  62.  
  63.  
  64. function DeepPrint (e)
  65.     -- if e is a table, we should iterate over its elements
  66.     if type(e) == "table" then
  67.         for k,v in pairs(e) do -- for every element in the table
  68.             print('  -'..k, ' = ', v)
  69.             DeepPrint(v)       -- recursively repeat the same procedure
  70.         end
  71.     end
  72. end
  73.  
  74.  
  75. function printTableKeysAndValues(Table, Depth)
  76.     for i in pairs(Table) do --For filled every slot in Input Inventory
  77.         TableEntry = Table[i]
  78.         for key, value in pairs(TableEntry) do
  79.             -- Add spaces infront of the text
  80.             TD = Depth or 0
  81.             for x=1,TD,1 do io.write(' ') end
  82.             -- print key and its value
  83.             print('["' .. key .. '"] and value of '.. tostring(value))
  84.         end
  85.         print('')
  86.     end
  87. end
  88.  
  89.  
  90. -- Monitor_P.setTextColor(colors.green)
  91. -- print('Output_P Inventory:')
  92. -- -- PrintPeripheralMethods(Input_P)
  93. -- DeepPrint(Output_P.list())
  94. -- Monitor_P.setTextColor(colors.blue)
  95. -- print('Input_P Inventory:')
  96. -- -- PrintPeripheralMethods(Input_P)
  97. -- DeepPrint(Input_P.list())
  98. -- Monitor_P.setTextColor(colors.red)
  99. -- print('Input_P Inventory:')
  100. -- -- PrintPeripheralMethods(Input_P)
  101. -- DeepPrint(Input_P.list())
  102. -- Monitor_P.setTextColor(colors.yellow)
  103. -- print('Furnace_P Inventory:')
  104. -- -- PrintPeripheralMethods(Input_P)
  105. -- DeepPrint(Furnace_P.list())
  106. -- Monitor_P.setTextColor(colors.white)
  107.  
  108.  
  109. function CatchAndPrintError(Func)
  110.     local ran, errorMsg = xpcall(Func, Func)
  111.     if not ran then
  112.         term.setTextColor(colors.red)
  113.        
  114.         print("\n  --ERROR, --->")
  115.         print(tostring(errorMsg))
  116.         print(type(errorMsg))
  117.         print('Terminated.')
  118.     end
  119. end
  120.  
  121. ------------------------------------------------------------------------------------------------------------------
  122. ------------------------------------------------------------------------------------------------------------------
  123. ------------------------------------------------------------------------------------------------------------------
  124.  
  125. function ValidSmeltable(ItemName)
  126.     if string.find(ItemName, 'Pulverized') or string.find(ItemName, 'Blend') or string.find(ItemName, 'Dust') or string.find(ItemName, 'Grit') then
  127.         return true
  128.     else
  129.         return false
  130.     end
  131. end
  132.  
  133. function ValidOre(ItemName)
  134.     if string.find(ItemName, 'Ore') or string.find(ItemName, 'ore') then
  135.         return true
  136.     else
  137.         return false
  138.     end
  139. end
  140.  
  141.  
  142.  
  143.  
  144.  
  145. --------------------------------------------------------------------------------
  146. --------------------------------------------------------------------------------
  147. --------------------------------------------------------------------------------
  148.  
  149. FurnaceItemSlotsCache = {}
  150. DustsItemSlotsCache = {}
  151. function SummarizeInventoryTable(TableToSummarize)
  152.     -- print('\n\nSummarizeInventoryTable9000 Reports:')
  153.     local TempCache = {}
  154.  
  155.     -- check if item type exists in TempCache, if not. add it!
  156.     for T_i in pairs(TableToSummarize) do
  157.         IN = TableToSummarize[T_i][1]
  158.         IA = TableToSummarize[T_i][2]
  159.         IsNewCacheEntry = true
  160.  
  161.         -- if entry for this item type was found in the Cache, add its value to the given entry.
  162.         for C_i in pairs(TempCache) do
  163.             if IN == TempCache[C_i][1] then
  164.                 IsNewCacheEntry = false
  165.                 -- print('match Found! Adding Value to existing match!')
  166.                 -- print('      + ', IA, IN)
  167.                 TempCache[C_i][2] = TempCache[C_i][2] + IA
  168.                
  169.             end
  170.         end
  171.  
  172.         -- if no entry for this item type was found in the Cache, add it as a new entry.
  173.         if IsNewCacheEntry then
  174.             NewCacheEntry = {IN, IA}
  175.             table.insert(TempCache, NewCacheEntry)
  176.         end
  177.        
  178.     end
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.     -- -- Table Reporter
  198.     -- print('\n - Table Reports:')
  199.     -- for i in pairs(TableToSummarize) do
  200.     --     IN = TableToSummarize[i][1]
  201.     --     IA = TableToSummarize[i][2]
  202.     --     print('   -', IN, IA)
  203.     -- end
  204.  
  205.     -- -- Cache Reporter
  206.     -- print('\n - Cache Reports:')
  207.     -- for i in pairs(TempCache) do
  208.     --     IN = TempCache[i][1]
  209.     --     IA = TempCache[i][2]
  210.     --     print('   -', IN, IA)
  211.     -- end
  212.  
  213.     return TempCache
  214. end
  215.  
  216.  
  217.  
  218.  
  219. function HandleFurnaceInventory()
  220.     FurnaceItemSlotsCache = {}
  221.     for SlotNum in pairs(Furnace_P.list()) do --For filled every slot in Input Inventory
  222.        
  223.         local ItemName = Furnace_P.getItemMeta(SlotNum)['displayName'] -- get the display name of item in slot
  224.        
  225.         -- if the item name contains a Valid key word
  226.         if string.find(ItemName, 'Ingot') then
  227.             local ItemAmount = Furnace_P.getItemMeta(SlotNum)['count'] -- get the item amount of item in slot
  228.             local MiniData = {ItemName, ItemAmount}
  229.             table.insert(FurnaceItemSlotsCache, MiniData)
  230.             Furnace_P.pushItems(peripheral.getName(Output_P), SlotNum)
  231.             -- print('Sending ', ItemName, ItemAmount, ' into Output')
  232.             io.write('.')
  233.         end
  234.     end
  235.  
  236.  
  237.     -- Print Summarization of the Inventory
  238.     print('')
  239.     DATA = SummarizeInventoryTable(FurnaceItemSlotsCache)
  240.     for i in pairs(DATA) do
  241.         IN = DATA[i][1]
  242.         IA = DATA[i][2]
  243.         print('   -'..IA..'- '..IN)
  244.     end
  245. end
  246.  
  247.  
  248.  
  249.  
  250.  
  251. function HandleDustsInventory()
  252.     DustsItemSlotsCache = {}
  253.  
  254.    
  255.     for SlotNum in pairs(Input_P.list()) do --For filled every slot in Input Inventory
  256.         local ItemName = Input_P.getItemMeta(SlotNum)['displayName'] -- get the display name of item in slot
  257.         -- if the item name contains a Valid key word
  258.         if ValidSmeltable(ItemName) then
  259.             local ItemAmount = Input_P.getItemMeta(SlotNum)['count'] -- get the item amount of item in slot
  260.             local MiniData = {ItemName, ItemAmount}
  261.             table.insert(DustsItemSlotsCache, MiniData)
  262.             Input_P.pushItems(peripheral.getName(Furnace_P), SlotNum)
  263.             -- print('Sending ', ItemName, ItemAmount, ' into furnace')
  264.             io.write('.')
  265.         else
  266.             term.setTextColor(colors.red)
  267.             print('\n  --ERROR, "'.. ItemName .. '" is not a valid dust type!')
  268.        
  269.         end
  270.     end
  271.     -- Print Summarization of the Inventory
  272.     print('')
  273.     DATA = SummarizeInventoryTable(DustsItemSlotsCache)
  274.     for i in pairs(DATA) do
  275.         IN = DATA[i][1]
  276.         IA = DATA[i][2]
  277.         print('   -'..IA..'- '..IN)
  278.     end
  279.  
  280. end
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290. -------------------- Run Item Processing Loop
  291.  
  292.  
  293. function RunLoop()
  294.  
  295.     os.sleep(1)
  296.     term.setTextColor(colors.blue)
  297.     print('\n\n\nExporting Ingots from Furnace...')
  298.     term.setTextColor(colors.lightBlue)
  299.     CatchAndPrintError(HandleFurnaceInventory)
  300.     --HandleFurnaceInventory()
  301.     term.setTextColor(colors.blue)
  302.     print('Done.')
  303.     os.sleep(1)
  304.    
  305.     term.setTextColor(colors.orange)
  306.     print('\n\n\nInserting Smeltables Into Furnace...')
  307.     term.setTextColor(colors.yellow)
  308.     -- HandleDustsInventory()
  309.     CatchAndPrintError(HandleDustsInventory)
  310.     term.setTextColor(colors.orange)
  311.     print('Done.')
  312.     os.sleep(1)
  313.  
  314.  
  315.  
  316.  
  317.  
  318.     term.setTextColor(colors.purple)
  319.     print('\n\n\nCycle Done, Sleeping for 10 seconds. - Elephant\n')
  320.     os.sleep(10)
  321. end
  322. while true do
  323.     RunLoop()
  324.    
  325.     -- term.setTextColor(colors.white)
  326.     -- print('Loop Begun')
  327.     -- CatchAndPrintError(RunLoop)
  328.     -- term.setTextColor(colors.white)
  329.     -- print('Loop End')
  330.    
  331. end
  332.  
  333.  
  334.  
  335. print('Code Finished for an unknown reason, Error(?).')
Advertisement
Add Comment
Please, Sign In to add comment