Advertisement
krakaen

Carpenter Recipe Program

Apr 19th, 2019
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.14 KB | None | 0 0
  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 RECIPES[i]["name"]
  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.         elseif transposer.getFluidInTank(i-1)["n"] > 0 or transposer.getTankCapacity(i-1) == 16000 then
  83.             local fluid = transposer.getFluidInTank(i-1)[1]
  84.            
  85.             if transposer.getInventoryName(i-1) == nil  then
  86.                 transposerList["fluids"]["id"] = transposerId
  87.                 transposerList["fluids"]["side"] = possibleSides[i]    
  88.             end
  89.         elseif transposer.getFluidInTank(i-1)["n"] > 0 OR transposer.getTankCapacity(i-1) ~= 16000 then
  90.             transposerList["fluids"]["output"] = transposerId
  91.         end
  92.     end
  93. end
  94.  
  95. function getOutputLocation(transposer)
  96.     if transposer.getInventoryName(sides.up) == "actuallyadditions:block_phantom_liquiface" then
  97.         return sides.up
  98.     elseif transposer.getInventoryName(sides.down) == "actuallyadditions:block_phantom_liquiface" then
  99.         return sides.down
  100.     end
  101.     return nil
  102. end
  103.  
  104. function moveFluidToMachine(recipeIndex)
  105.     local fluid = RECIPES[recipeName]["fluid"]
  106.     local transposer = component.proxy(transposerList["fluids"]["id"])
  107.     local inputSide = transposerList["fluids"]["side"] 
  108.     local outputSide = transposerList["fluids"]["output"]
  109.    
  110.     if transposer.getTankLevel(outputSide) == 0 then
  111.         transposer.transferFluid(sides[inputSide], outputSide, fluidAmount)
  112.     end
  113.    
  114. end
  115.  
  116. function init()
  117.     getTransposersInfo()
  118. end
  119.  
  120. function loopDeLoop()  
  121.     while event.pull(1, "interrupted") == nil do
  122.         if getInventoryItems()[1] ~= nil then
  123.             local currentRecipeIndex = getCurrentRecipeIndex(getInventoryItems())  
  124.             if currentRecipeIndex == nil then
  125.                 print("that's not a recipe you dum dum, check your items")
  126.             else
  127.                 print(currentRecipeIndex)
  128.                 print("crafting : " .. RECIPES[currentRecipeIndex]["name"])
  129.                 moveFluidToMachine(currentRecipeIndex)
  130.             end
  131.         end
  132.         local event, address, arg1, arg2, arg3 = event.pull(1)
  133.         if type(address) == "string" and component.isPrimary(address) then
  134.             if event == "key_down" and arg2 == keyboard.keys.q then
  135.                 os.exit()
  136.             end
  137.         end
  138.     end
  139. end
  140.  
  141. init()
  142. loopDeLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement