Advertisement
gelatine87

ElementalCraft Binder Automation

May 14th, 2024 (edited)
52
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.17 KB | Gaming | 0 0
  1. -- Define peripheral sides
  2. local SIDES = {
  3.     TOP = "top",
  4.     BOTTOM = "bottom",
  5.     LEFT = "left",
  6.     RIGHT = "right",
  7.     FRONT = "front",
  8.     BACK = "back"
  9. }
  10.  
  11. -- Define peripheral names
  12. local PERIPHERAL_NAMES = {
  13.     INPUT = "minecraft:barrel",
  14.     BINDER = {
  15.         REGULAR = "elementalcraft:binder",
  16.         IMPROVED = "elementalcraft:binder_improved"
  17.     },
  18.     OUTPUT = "minecraft:barrel"
  19. }
  20.  
  21. -- Function to check for a peripheral
  22. local function findPeripheral(peripheralName, side)
  23.     local peripheralObject = peripheral.wrap(side)
  24.     if not peripheralObject or peripheral.getType(side) ~= peripheralName then
  25.         error("Peripheral '" .. peripheralName .. "' not found on side: " .. side)
  26.     end
  27.     return peripheralObject
  28. end
  29.  
  30. -- Function to retrieve peripherals
  31. local function retrievePeripherals()
  32.     local output = findPeripheral(PERIPHERAL_NAMES.OUTPUT, SIDES.LEFT)
  33.     local input = findPeripheral(PERIPHERAL_NAMES.INPUT, SIDES.BOTTOM)
  34.     local binder = findPeripheral(PERIPHERAL_NAMES.BINDER.IMPROVED, SIDES.RIGHT) or findPeripheral(PERIPHERAL_NAMES.BINDER.REGULAR, SIDES.RIGHT)
  35.     return output, input, binder
  36. end
  37.  
  38. -- Retrieve peripherals
  39. local output, input, binder = retrievePeripherals()
  40.  
  41. -- Constants
  42. local SLEEP_DURATION = 0.1 -- Adjust sleep time as needed
  43.  
  44. -- Recipe list
  45. local recipeList = {
  46.     {
  47.         name = "elementalcraft:fireite_ingot",
  48.         ingredients = {
  49.             { "minecraft:netherite_ingot",        1 },
  50.             { "elementalcraft:swift_alloy_ingot", 1 },
  51.             { "elementalcraft:springaline_shard", 1 },
  52.             { "elementalcraft:purecrystal",       1 }
  53.         }
  54.     },
  55.     {
  56.         name = "elementalcraft:reservoir_fire",
  57.         ingredients = {
  58.             { "elementalcraft:container",         1 },
  59.             { "elementalcraft:springaline_glass", 1 },
  60.             { "elementalcraft:purecrystal",       1 },
  61.             { "elementalcraft:pristine_fire_gem", 1 }
  62.         }
  63.     }
  64. }
  65.  
  66. -- Function for error handling with line number indication
  67. local function errorWithPosition(message)
  68.     local _, line = pcall(error, "", 2) -- Allows retrieving current line number
  69.     term.clear()
  70.     term.setCursorPos(1, 1)
  71.     error("Error in line " .. line .. ": " .. message)
  72. end
  73.  
  74. -- Function to display a message to the user
  75. local function showMessage(message)
  76.     term.clear()
  77.     term.setCursorPos(1, 1)
  78.     print(message)
  79. end
  80.  
  81. -- Function to check if the binder is empty
  82. local function isBinderEmpty()
  83.     local binderItems = binder.list()
  84.     return next(binderItems) == nil
  85. end
  86.  
  87. -- Function to search for an item in input
  88. local function findItemInInput(itemName, itemList)
  89.     for slot, item in pairs(itemList) do
  90.         if item.name == itemName then
  91.             return slot, item
  92.         end
  93.     end
  94.     return nil, nil
  95. end
  96.  
  97. -- Function to find a recipe
  98. local function findRecipe(recipeName)
  99.     for _, r in ipairs(recipeList) do
  100.         if r.name == recipeName then
  101.             return r
  102.         end
  103.     end
  104.     return nil
  105. end
  106.  
  107. -- Function to check if enough materials are available
  108. local function enoughMaterialsAvailable(recipe, inputList)
  109.     for _, ingredient in ipairs(recipe.ingredients) do
  110.         local ingredientName, requiredCount = ingredient[1], ingredient[2]
  111.         local slot, item = findItemInInput(ingredientName, inputList)
  112.         if not slot or not item or item.count < requiredCount then
  113.             return false
  114.         end
  115.     end
  116.     return true
  117. end
  118.  
  119. -- Function to push items to the binder
  120. local function pushItemToBinder(recipe, inputList)
  121.     for _, ingredient in ipairs(recipe.ingredients) do
  122.         local ingredientName, requiredCount = ingredient[1], ingredient[2]
  123.         local slot = findItemInInput(ingredientName, inputList)
  124.         input.pushItems(peripheral.getName(binder), slot, requiredCount)
  125.     end
  126. end
  127.  
  128. -- Updated function to transfer items to the binder
  129. local function pushItemsToBinder(recipeName, inputList)
  130.     local recipe = findRecipe(recipeName)
  131.     if not recipe then
  132.         errorWithPosition("Recipe not found: " .. recipeName)
  133.         return
  134.     end
  135.  
  136.     if enoughMaterialsAvailable(recipe, inputList) then
  137.         pushItemToBinder(recipe, inputList)
  138.     end
  139. end
  140.  
  141. -- Function to push item to the output peripheral only if there's exactly one item left in the binder
  142. local function pushItemToOutput(container)
  143.     local binderItems = binder.list()
  144.     local itemCount = 0
  145.     for _, item in ipairs(binderItems) do
  146.         itemCount = itemCount + item.count
  147.     end
  148.  
  149.     if itemCount == 1 then
  150.         local success, err = binder.pushItems(container, 1)
  151.         if not success then
  152.             errorWithPosition("Failed to push item: " .. err)
  153.         end
  154.     end
  155. end
  156.  
  157. -- Main function to check for available recipes and start crafting
  158. local function main()
  159.     while true do
  160.         local crafting = false -- Flag to check if a recipe is currently being crafted
  161.         for _, recipe in ipairs(recipeList) do
  162.             if not isBinderEmpty() then -- Check if the binder is not empty
  163.                 for _, binderItem in ipairs(binder.list()) do
  164.                     if binderItem.name == recipe.name then
  165.                         showMessage("Crafting " .. recipe.name)
  166.                         pushItemToOutput(peripheral.getName(output))
  167.                         crafting = true -- A recipe is currently being crafted
  168.                         break -- Break out of the recipe loop if a recipe is being crafted
  169.                     end
  170.                 end
  171.             elseif enoughMaterialsAvailable(recipe, input.list()) then -- Check if enough materials are available
  172.                 showMessage("Crafting " .. recipe.name)
  173.                 pushItemsToBinder(recipe.name, input.list())
  174.                 crafting = true -- A recipe is currently being crafted
  175.                 break -- Break out of the recipe loop if a recipe is being crafted
  176.             end
  177.         end
  178.         if not crafting then -- If no recipe is currently being crafted
  179.             showMessage("No recipe is currently being crafted.")
  180.         end
  181.         os.sleep(SLEEP_DURATION)
  182.     end
  183. end
  184.  
  185. -- Call function to check and craft recipes
  186. main()
Advertisement
Comments
  • gelatine87
    14 days (edited)
    # Lua 2.00 KB | 0 0
    1. To use this you need to change a few things.
    2.  
    3. 1.
    4. -- Define peripheral names
    5. local PERIPHERAL_NAMES = {
    6.     INPUT = "minecraft:barrel",
    7.     BINDER = {
    8.         REGULAR = "elementalcraft:binder",
    9.         IMPROVED = "elementalcraft:binder_improved"
    10.     },
    11.     OUTPUT = "minecraft:barrel"
    12. }
    13.  
    14. INPUT & OUTPUT-Name could be different. If so change the "minecraft:barrel" to your choosen inventory.
    15.  
    16. ==================================================================================================================
    17. 2.
    18. -- Function to retrieve peripherals
    19. local function retrievePeripherals()
    20.     local output = findPeripheral(PERIPHERAL_NAMES.OUTPUT, SIDES.LEFT)
    21.     local input = findPeripheral(PERIPHERAL_NAMES.INPUT, SIDES.BOTTOM)
    22.     local binder = findPeripheral(PERIPHERAL_NAMES.BINDER.IMPROVED, SIDES.RIGHT) or findPeripheral(PERIPHERAL_NAMES.BINDER.REGULAR, SIDES.RIGHT)
    23.     return output, input, binder
    24. end
    25.  
    26. The direction where the INPUT, OUTPUT and BINDER are. Change "SIDES.LEFT" depending on the peripheral.
    27.  
    28. ==================================================================================================================
    29. 3.
    30. -- Recipe list
    31. local recipeList = {
    32.     {
    33.         name = "elementalcraft:fireite_ingot",
    34.         ingredients = {
    35.             { "minecraft:netherite_ingot",        1 },
    36.             { "elementalcraft:swift_alloy_ingot", 1 },
    37.             { "elementalcraft:springaline_shard", 1 },
    38.             { "elementalcraft:purecrystal",       1 }
    39.         }
    40.     },
    41.     {
    42.         name = "elementalcraft:reservoir_fire",
    43.         ingredients = {
    44.             { "elementalcraft:container",         1 },
    45.             { "elementalcraft:springaline_glass", 1 },
    46.             { "elementalcraft:purecrystal",       1 },
    47.             { "elementalcraft:pristine_fire_gem", 1 }
    48.         }
    49.     }
    50. }
    51.  
    52. The actual recipe you are want to craft. "name" = the Output-tag of your crafted item. "ingredients" are the materials to craft the output item you want to craft but you need to write it in the right order.
Add Comment
Please, Sign In to add comment
Advertisement