MineMcMine

ExternalCrafter

Apr 24th, 2022 (edited)
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.     Using: CC: Tweaked, Plethora
  3.     pastebin run sBTwAePK
  4.     pastebin get sBTwAePK startup
  5. ]]--
  6.  
  7. if pcall(os.loadAPI, "jLib/jFuncs") then
  8. else
  9.     shell.run("pastebin get kqBFGcfV jLib/jFuncs")
  10.     os.loadAPI("jLib/jFuncs")
  11. end
  12. if(fs.exists("jLib/ExternalCrafterRecipes")) then
  13.     fs.delete("jLib/ExternalCrafterRecipes")
  14. end
  15. shell.run("pastebin get a2DA84Hg jLib/ExternalCrafterRecipes")
  16.  
  17.  
  18. local inputChest = peripheral.wrap("back")
  19. local self = peripheral.wrap("left").getInventory()
  20.  
  21. local file = fs.open("jLib/ExternalCrafterRecipes", "r")
  22. local recipes = textutils.unserialize(file.readAll())
  23.  
  24. local function containsRecipe(items, recipe)
  25.     local needed = {}
  26.     for _, material in pairs(recipe) do
  27.         table.insert(needed, {["name"] = material.name, ["damage"] = material.damage, ["nbtHash"] = material.nbtHash, ["checked"] = false})
  28.     end
  29.  
  30.     local lItems = jFuncs.tableClone(items)
  31.  
  32.     local ret = {}
  33.     for _, material in pairs(needed) do
  34.         for i, item in pairs(lItems) do
  35.             if(item.name == material.name and item.damage == material.damage and item.nbtHash == material.nbtHash and not material.checked and item.count > 0) then
  36.                 table.insert(ret, { ["slot"] = i })
  37.                 material.checked = true
  38.                 item.count = item.count - 1
  39.             end
  40.         end
  41.     end
  42.  
  43.     for _, material in pairs(needed) do
  44.         if(not material.checked) then
  45.             return nil
  46.         end
  47.     end
  48.    
  49.     return ret
  50. end
  51.  
  52. local function firstValidRecipe(items)
  53.     local valid = nil
  54.     for i, recipe in pairs(recipes) do
  55.         valid = containsRecipe(items, recipe)
  56.         if(valid ~= nil) then
  57.             return i
  58.         end
  59.     end
  60.     return nil
  61. end
  62.  
  63. local function getFirst(toGet)
  64.     local allItems = inputChest.list()
  65.     for i, item in pairs(allItems) do
  66.         if(item.name == toGet.name and item.damage == toGet.damage and item.nbtHash == toGet.nbtHash) then
  67.             return i
  68.         end
  69.     end
  70.     return nil
  71. end
  72.  
  73. local function pullRecipe(recipe)
  74.     local slot
  75.     for i, material in pairs(recipe) do
  76.         slot = getFirst(material)
  77.         self.pullItems("back", slot, 1, math.floor(i + i/3 - 1/3))
  78.     end
  79. end
  80.  
  81. local function pushItems()
  82.     for i, _ in pairs(self.list()) do
  83.         self.pushItems("top", i)
  84.     end
  85. end
  86.  
  87.  
  88. while (true) do
  89.     local allItems = inputChest.list()
  90.     local recipeIndex = firstValidRecipe(allItems)
  91.     if(recipeIndex ~= nil) then
  92.         print("Pulling recipe: " .. recipeIndex)
  93.         pullRecipe(recipes[recipeIndex])
  94.         turtle.craft()
  95.         pushItems()
  96.     else
  97.         os.sleep(1)
  98.     end
  99. end
  100.  
  101.  
Add Comment
Please, Sign In to add comment