Advertisement
Lanzr

ae2_auto_press

Mar 22nd, 2025 (edited)
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.00 KB | None | 0 0
  1. device_inscriber = peripheral.find("ae2:inscriber")
  2. device_container = peripheral.find("inventory",function (name, device)
  3.     newStr = peripheral.getType(name)
  4.     return newStr ~= "ae2:inscriber"
  5. end)
  6.  
  7. local slotMap = {
  8.     ["ae2:logic_processor_press"] = 1,
  9.     ["ae2:calculation_processor_press"] = 1,
  10.     ["ae2:engineering_processor_press"] = 1,
  11.     ["ae2:silicon_press"] = 1,
  12.     ["ae2:printed_logic_processor"] = 1,
  13.     ["ae2:printed_calculation_processor"] = 1,
  14.     ["ae2:printed_engineering_processor"] = 1,
  15.     ["ae2:printed_silicon"] = 2,
  16.     ["minecraft:redstone"] = 3,
  17.     ["minecraft:gold_ingot"] = 3,
  18.     ["ae2:certus_quartz_crystal"] = 3,
  19.     ["minecraft:diamond"] = 3,
  20.     ["ae2:silicon"] = 3,
  21.    
  22. }
  23.  
  24. local recipes = {
  25.     ["ae2:printed_logic_processor"] = {
  26.         ["ae2:logic_processor_press"] = {
  27.             slot = {}
  28.         },
  29.         ["minecraft:gold_ingot"] = {
  30.             slot = {}
  31.         }
  32.     },
  33.     ["ae2:printed_calculation_processor"] = {
  34.         ["ae2:calculation_processor_press"] = {
  35.             slot = {}
  36.         },
  37.         ["ae2:certus_quartz_crystal"] = {
  38.             slot = {}
  39.         }
  40.     },
  41.     ["ae2:printed_engineering_processor"] = {
  42.         ["ae2:engineering_processor_press"] = {
  43.             slot = {}
  44.         },
  45.         ["minecraft:diamond"] = {
  46.             slot = {}
  47.         }
  48.     },
  49.     ["ae2:printed_silicon"] = {
  50.         ["ae2:silicon_press"] = {
  51.             slot = {}
  52.         },
  53.         ["ae2:silicon"] = {
  54.             slot = {}
  55.         }
  56.     },
  57.     ["ae2:logic_processor"] = {
  58.         ["ae2:printed_logic_processor"] = {
  59.             slot = {}
  60.         },
  61.         ["minecraft:redstone"] = {
  62.             slot = {}
  63.         },
  64.         ["ae2:printed_silicon"] = {
  65.             slot = {}
  66.         }
  67.     },
  68.     ["ae2:calculation_processor"] = {
  69.         ["ae2:printed_calculation_processor"] = {
  70.             slot = {}
  71.         },
  72.         ["minecraft:redstone"] = {
  73.             slot = {}
  74.         },
  75.         ["ae2:printed_silicon"] = {
  76.             slot = {}
  77.         }
  78.     },
  79.     ["ae2:engineering_processor"] = {
  80.         ["ae2:printed_engineering_processor"] = {
  81.             slot = {}
  82.         },
  83.         ["minecraft:redstone"] = {
  84.             slot = {}
  85.         },
  86.         ["ae2:printed_silicon"] = {
  87.             slot = {}
  88.         }
  89.     }
  90. }
  91. function itemMatch(index,itemStack)
  92.     for recipeName, recipe in pairs(recipes) do
  93.         for itemName, slot in pairs(recipe) do
  94.             if itemStack.name == itemName then
  95.                 table.insert(slot, index)
  96.             end
  97.         end
  98.     end
  99. end
  100.  
  101. function clearInscriberOutput()
  102.     -- 4 是输出槽
  103.     device_inscriber.pushItems(peripheral.getName(device_container),4)
  104. end
  105. function clearInscriber()
  106.     -- 4 是输出槽
  107.     for i = 1, 4, 1 do
  108.         device_inscriber.pushItems(peripheral.getName(device_container),i)
  109.     end
  110. end
  111.  
  112. function clearRecipe(recipeName)
  113.     for itemName, slot in pairs(recipes[recipeName]) do
  114.         slot = {}
  115.     end
  116. end
  117.  
  118. function clearAll()
  119.     for recipeName, recipe in pairs(recipes) do
  120.         clearRecipe(recipeName)
  121.     end
  122. end
  123.  
  124. function pushToInscriber(index,targetIndex)
  125.     device_container.pushItems(peripheral.getName(device_inscriber),index,64,targetIndex)    
  126. end
  127.  
  128. function checkInscriberIndexIsEmpty(index)
  129.     if device_inscriber.list()[index] == nil then
  130.         return true
  131.     else
  132.         return false
  133.     end
  134.    
  135. end
  136.  
  137. function craft(recipeName)
  138.     -- 有很多个slot ,必须其中一个为空才返回
  139.     -- itemName 有很多个,每次循环都是其中的一个,
  140.     target_recipe = recipes[recipeName]
  141.     print("craft: ",recipeName)
  142.     for itemName, slot in pairs(target_recipe) do
  143.         -- 要先转送
  144.         index =  table.remove(slot,1)
  145.         pushToInscriber(index,slotMap[itemName])
  146.     end
  147.  
  148.     retFlag = false
  149.     repeat
  150.         retFlag = false
  151.         -- 槽转送以后且槽为空时才清空slot
  152.         for itemName, slot in pairs(target_recipe) do
  153.             -- 要先转送
  154.             if #slot == 0 then
  155.                 if checkInscriberIndexIsEmpty(slotMap[itemName]) then -- 容器和目标槽位都为空
  156.                     print("slot ",slotMap[itemName])
  157.                     print("itemName ",itemName)
  158.                     print("clear",recipeName)
  159.                     clearRecipe(recipeName)
  160.                     clearInscriber()
  161.                     return
  162.                 else -- 容器为空,目标不为空
  163.                 end
  164.             else
  165.                 if checkInscriberIndexIsEmpty(slotMap[itemName]) then -- 容器不为空,目标为空
  166.                     print("ise: ",itemName.." "..slotMap[itemName])
  167.                     index = table.remove(slot,1)
  168.                     pushToInscriber(index,slotMap[itemName])
  169.                     clearInscriberOutput()
  170.                 else -- 容器不为空,目标不为空
  171.                 end
  172.             end
  173.         end
  174.         sleep(5)
  175.         -- print("loop again")
  176.     until retFlag
  177.  
  178. end
  179.  
  180. function recipeExist()
  181.     for recipeName, recipe in pairs(recipes) do
  182.         craftFlag = true
  183.         for itemName, slot in pairs(recipe) do
  184.             if #slot == 0 then
  185.                 craftFlag = false
  186.                 break
  187.             end
  188.         end
  189.         if craftFlag then -- 满足配方,合成
  190.             craft(recipeName)
  191.         end
  192.         clearRecipe(recipeName)
  193.     end
  194.    
  195. end
  196. -- 大循环遍历
  197. function mainloop()
  198.     retFlag = true
  199.     repeat
  200.         -- 初始化阶段
  201.         retFlag = true
  202.         containerList = device_container.list() -- 遍历容器所有物品
  203.         for index, itemStack in pairs(containerList) do
  204.             if itemStack ~=nil then
  205.                 itemMatch(index,itemStack)
  206.             end
  207.         end
  208.         if recipeExist() then -- 判断是否有配方存在并且合成配方
  209.             retFlag = false
  210.         end
  211.         print("exist ",retFlag)
  212.         clearAll()
  213.     until retFlag
  214. end
  215. clearInscriber()
  216. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement