alesandreo

Energizing Orb

Feb 4th, 2021 (edited)
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.93 KB | None | 0 0
  1. --
  2. -- Created by IntelliJ IDEA.
  3. -- User: amcconaughey
  4. -- Date: 2/3/21
  5. -- Time: 2:09 PM
  6. -- To change this template use File | Settings | File Templates.
  7. --
  8.  
  9. crafting_station = peripheral.wrap("back")
  10. input_inventory = peripheral.wrap("left")
  11. output_inventory = peripheral.wrap("right")
  12.  
  13. recipes = {}
  14.  
  15. recipes["powah:steel_energized"] = {
  16.     inputs = {
  17.         {
  18.             count = 1,
  19.             name = "minecraft:iron_ingot"
  20.         },
  21.         {
  22.             count = 1,
  23.             name = "minecraft:gold_ingot"
  24.         }
  25.     },
  26.     outputs = {
  27.         {
  28.             count = 2,
  29.             name = "powah:steel_energized"
  30.         }
  31.     }
  32. }
  33.  
  34. function get_slot(inventory, name)
  35.     for slot_number, item_data in ipairs(inventory.list()) do
  36.         if item_data and item_data["name"] == name then
  37.             return slot_number
  38.         end
  39.     end
  40.     return nil
  41. end
  42.  
  43. function get_count(inventory, name)
  44.     local count = 0
  45.     for slot_number, item_data in ipairs(inventory.list()) do
  46.         if item_data and item_data.name == name then
  47.             count = count + item_data.count
  48.         end
  49.     end
  50.     print("Found "..count.." of "..name..".")
  51.     return count
  52. end
  53.  
  54. function check_stock(inventory,recipe)
  55.     if recipe and recipe.inputs then
  56.         for _,ingredient in ipairs(recipe.inputs) do
  57.             if get_count(inventory, ingredient.name) < ingredient.count then
  58.                 return false
  59.             end
  60.         end
  61.     end
  62.     return true
  63. end
  64.  
  65. function check_output(inventory, recipe)
  66.     if recipe and recipe.outputs then
  67.         for _,out_product in ipairs(recipe.outputs) do
  68.             if get_count(inventory, out_product.name) < out_product.count then
  69.                 return false
  70.             end
  71.         end
  72.     end
  73.     return true
  74. end
  75.  
  76. function check_empty(inventory)
  77.    for _,k in pairs(input_inventory.list()) do
  78.        return false
  79.    end
  80.    return true
  81. end
  82.  
  83. function transfer_item(source, destination, name, amount)
  84.     local amount_transferred = 0
  85.     print("Transferring "..amount.." "..name.." from "..peripheral.getName(source).." to "..peripheral.getName(destination))
  86.     if get_count(source, name) < amount then
  87.         error("Attempted to transfer more \""..name.."\" than the source contains.")
  88.         return nil
  89.     end
  90.     while amount_transferred < amount do
  91.         local amount_to_transfer = amount - amount_transferred
  92.         amount_transferred = amount_transferred + source.pushItems(peripheral.getName(destination), get_slot(source, name), amount_to_transfer)
  93.     end
  94. end
  95.  
  96. function insert_ingredients(in_inventory, cs_inv, recipe)
  97.     print("Inserting Ingredients from "..peripheral.getName(in_inventory).." to "..peripheral.getName(crafting_station))
  98.     if recipe and check_stock(in_inventory, recipe) then
  99.         for key,ingredient in ipairs(recipe.inputs) do
  100.             transfer_item(in_inventory, cs_inv, ingredient.name, ingredient.count)
  101.         end
  102.     end
  103.     return true
  104. end
  105.  
  106. function transfer_output(cs_inv, out_inv, recipe)
  107.     print("Transferring outputs from "..peripheral.getName(cs_inv).." to "..peripheral.getName(out_inv))
  108.     if recipe and check_output(cs_inv, recipe) then
  109.         for _,out_product in ipairs(recipe.outputs) do
  110.             transfer_item(cs_inv, out_inv, out_product.name, out_product.count)
  111.         end
  112.     end
  113. end
  114.  
  115. function craft_recipe(recipe)
  116.     while check_stock(input_inventory, recipe) or not check_empty(crafting_station) do
  117.         if check_empty(crafting_station) then
  118.             if check_stock(input_inventory, recipe) then
  119.                 insert_ingredients(input_inventory, crafting_station, recipe)
  120.             end
  121.         else
  122.             if check_output(crafting_station, recipe) then
  123.                 transfer_output(crafting_station, output_inventory, recipe)
  124.             end
  125.         end
  126.         os.sleep(10)
  127.     end
  128. end
  129.  
  130.  
  131. test_recipe = recipes["powah:steel_energized"]
  132. craft_recipe(test_recipe)
Add Comment
Please, Sign In to add comment