krakaen

Carpenter Recipe Program 2

Apr 19th, 2019
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. RECIPES = require("RECIPES")
  2. local sides = require("sides")
  3. local component = require("component")
  4. local event = require("event")
  5. local tank = component.ender_tank
  6.  
  7. local currentCraft = ""
  8.  
  9. -- This will be filled automatically by the code, no need to modify it
  10. local transposerList = {
  11.     items = {
  12.         id = "",
  13.         side = ""  
  14.     },
  15.     fluids = {
  16.             id = "",   
  17.             side = "",
  18.             output = ""
  19.     }
  20. }
  21.  
  22. function tablelength(T)
  23.     local count = 0
  24.     for _ in pairs(T) do count = count + 1 end
  25.     return count
  26. end
  27.  
  28. function getCurrentRecipeIndex(items) -- items from inventory  
  29.     local isRecipeFound = false
  30.  
  31.     for i=1, tablelength(RECIPES) do
  32.         for j=1, tablelength(items) do
  33.             local damage = "0"
  34.             if items[j]["damage"] ~= nil then
  35.                 damage = items[j]["damage"]
  36.             end
  37.             local curRecipe = RECIPES[i]["item"][1]
  38.             if RECIPES[i]["item"][2] ~= "*" then
  39.                 curRecipe = curRecipe .. "." .. RECIPES[i]["item"][2]
  40.             end
  41.  
  42.             if string.match(items[j]["name"] .. "." .. math.floor(damage), curRecipe) then
  43.                 return i
  44.             end
  45.         end
  46.     end
  47.    
  48.     return nil
  49. end
  50.  
  51. function getInventoryItems()
  52.     local items = component.proxy(transposerList["items"]["id"]).getAllStacks(sides[transposerList["items"]["side"]]).getAll()                            
  53.     local realItems ={}
  54.     local increment = 1  
  55.    
  56.     for k,v in pairs(items) do
  57.         if v["name"] ~= "minecraft:air" then
  58.             realItems[increment] = v
  59.             increment = increment + 1
  60.         end
  61.     end
  62.    
  63.     return realItems
  64. end
  65.  
  66. function getTransposersInfo()
  67.     local transposers = component.list("transposer");
  68.     for id, data in pairs(transposers) do
  69.         getTransposerSides(id)
  70.     end
  71. end
  72.  
  73. function getTransposerSides(transposerId)
  74.     local possibleSides = {"bottom", "top", "back", "front", "right", "left"}
  75.     local transposer = component.proxy(transposerId)
  76.     local sides = {}
  77.    
  78.     for i=1, 6 do
  79.         if transposer.getInventoryName(i-1) ~= nil and transposer.getFluidInTank(i-1)["n"] < 1 then
  80.             transposerList["items"]["id"] = transposerId
  81.             transposerList["items"]["side"] = possibleSides[i]
  82.             print("Found inventory", transposerId, possibleSides[i])
  83.         elseif transposer.getFluidInTank(i-1)["n"] > 0 and transposer.getTankCapacity(i-1) == 16000 then
  84.             if transposer.getInventoryName(i-1) == nil  then
  85.                 transposerList["fluids"]["id"] = transposerId
  86.                 transposerList["fluids"]["side"] = possibleSides[i]
  87.                 print("Found ender tank", transposerId, possibleSides[i])  
  88.             end
  89.         elseif transposer.getFluidInTank(i-1)["n"] > 0 and transposer.getTankCapacity(i-1) ~= 16000 then
  90.             transposerList["fluids"]["output"] = possibleSides[i]
  91.             print("Found liquid output", transposerId, possibleSides[i])
  92.         end
  93.     end
  94. end
  95.  
  96. local function setTank(fluid)
  97.     local f,s,t = tonumber(fluid[1]),tonumber(fluid[2]),tonumber(fluid[3])
  98.     tank.setFrequency(f,s,t)
  99. end
  100.  
  101. function moveFluidToMachine(recipeIndex)
  102.     local fluid = RECIPES[recipeIndex]["fluid"]
  103.     local transposer = component.proxy(transposerList["fluids"]["id"])
  104.     local inputSide = transposerList["fluids"]["side"] 
  105.     local outputSide = transposerList["fluids"]["output"]
  106.     print(inputSide, outputSide)
  107.     setTank(fluid)    
  108.     if transposer.getTankLevel(sides[outputSide]) == 0 then
  109.         transposer.transferFluid(sides[inputSide], sides[outputSide], tonumber(fluid[4]))
  110.     end
  111.    
  112. end
  113.  
  114. function init()
  115.     getTransposersInfo()
  116. end
  117.  
  118. function loopDeLoop()  
  119.     while event.pull(1, "interrupted") == nil do
  120.         if getInventoryItems()[1] ~= nil then
  121.             local currentRecipeIndex = getCurrentRecipeIndex(getInventoryItems())  
  122.             if currentRecipeIndex == nil then
  123.                 print("that's not a recipe you dum dum, check your items")
  124.             else
  125.                 print(currentRecipeIndex)
  126.                 print("crafting : " .. RECIPES[currentRecipeIndex]["name"])
  127.                 moveFluidToMachine(currentRecipeIndex)
  128.             end
  129.         end
  130.         local event, address, arg1, arg2, arg3 = event.pull(1)
  131.         if type(address) == "string" and component.isPrimary(address) then
  132.             if event == "key_down" and arg2 == keyboard.keys.q then
  133.                 os.exit()
  134.             end
  135.         end
  136.     end
  137. end
  138.  
  139. init()
  140. loopDeLoop()
Add Comment
Please, Sign In to add comment