gelatine87 icon

ElementalCraft Binder Automation

gelatine87 | PRO | 05/17/24 06:01:43 PM UTC (Edited) | 0 ⭐ | 470 👁️ | Never ⏰ | [computercraft]
Lua |

6.17 KB

|

Gaming

|

0 👍

/

0 👎

-- Define peripheral sides
local SIDES = {
    TOP = "top",
    BOTTOM = "bottom",
    LEFT = "left",
    RIGHT = "right",
    FRONT = "front",
    BACK = "back"
}
 
-- Define peripheral names
local PERIPHERAL_NAMES = {
    INPUT = "minecraft:barrel",
    BINDER = {
        REGULAR = "elementalcraft:binder",
        IMPROVED = "elementalcraft:binder_improved"
    },
    OUTPUT = "minecraft:barrel"
}
 
-- Function to check for a peripheral
local function findPeripheral(peripheralName, side)
    local peripheralObject = peripheral.wrap(side)
    if not peripheralObject or peripheral.getType(side) ~= peripheralName then
        error("Peripheral '" .. peripheralName .. "' not found on side: " .. side)
    end
    return peripheralObject
end
 
-- Function to retrieve peripherals
local function retrievePeripherals()
    local output = findPeripheral(PERIPHERAL_NAMES.OUTPUT, SIDES.LEFT)
    local input = findPeripheral(PERIPHERAL_NAMES.INPUT, SIDES.BOTTOM)
    local binder = findPeripheral(PERIPHERAL_NAMES.BINDER.IMPROVED, SIDES.RIGHT) or findPeripheral(PERIPHERAL_NAMES.BINDER.REGULAR, SIDES.RIGHT)
    return output, input, binder
end
 
-- Retrieve peripherals
local output, input, binder = retrievePeripherals()
 
-- Constants
local SLEEP_DURATION = 0.1 -- Adjust sleep time as needed
 
-- Recipe list
local recipeList = {
    {
        name = "elementalcraft:fireite_ingot",
        ingredients = {
            { "minecraft:netherite_ingot",        1 },
            { "elementalcraft:swift_alloy_ingot", 1 },
            { "elementalcraft:springaline_shard", 1 },
            { "elementalcraft:purecrystal",       1 }
        }
    },
    {
        name = "elementalcraft:reservoir_fire",
        ingredients = {
            { "elementalcraft:container",         1 },
            { "elementalcraft:springaline_glass", 1 },
            { "elementalcraft:purecrystal",       1 },
            { "elementalcraft:pristine_fire_gem", 1 }
        }
    }
}
 
-- Function for error handling with line number indication
local function errorWithPosition(message)
    local _, line = pcall(error, "", 2) -- Allows retrieving current line number
    term.clear()
    term.setCursorPos(1, 1)
    error("Error in line " .. line .. ": " .. message)
end
 
-- Function to display a message to the user
local function showMessage(message)
    term.clear()
    term.setCursorPos(1, 1)
    print(message)
end
 
-- Function to check if the binder is empty
local function isBinderEmpty()
    local binderItems = binder.list()
    return next(binderItems) == nil
end
 
-- Function to search for an item in input
local function findItemInInput(itemName, itemList)
    for slot, item in pairs(itemList) do
        if item.name == itemName then
            return slot, item
        end
    end
    return nil, nil
end
 
-- Function to find a recipe
local function findRecipe(recipeName)
    for _, r in ipairs(recipeList) do
        if r.name == recipeName then
            return r
        end
    end
    return nil
end
 
-- Function to check if enough materials are available
local function enoughMaterialsAvailable(recipe, inputList)
    for _, ingredient in ipairs(recipe.ingredients) do
        local ingredientName, requiredCount = ingredient[1], ingredient[2]
        local slot, item = findItemInInput(ingredientName, inputList)
        if not slot or not item or item.count < requiredCount then
            return false
        end
    end
    return true
end
 
-- Function to push items to the binder
local function pushItemToBinder(recipe, inputList)
    for _, ingredient in ipairs(recipe.ingredients) do
        local ingredientName, requiredCount = ingredient[1], ingredient[2]
        local slot = findItemInInput(ingredientName, inputList)
        input.pushItems(peripheral.getName(binder), slot, requiredCount)
    end
end
 
-- Updated function to transfer items to the binder
local function pushItemsToBinder(recipeName, inputList)
    local recipe = findRecipe(recipeName)
    if not recipe then
        errorWithPosition("Recipe not found: " .. recipeName)
        return
    end
 
    if enoughMaterialsAvailable(recipe, inputList) then
        pushItemToBinder(recipe, inputList)
    end
end
 
-- Function to push item to the output peripheral only if there's exactly one item left in the binder
local function pushItemToOutput(container)
    local binderItems = binder.list()
    local itemCount = 0
    for _, item in ipairs(binderItems) do
        itemCount = itemCount + item.count
    end
 
    if itemCount == 1 then
        local success, err = binder.pushItems(container, 1)
        if not success then
            errorWithPosition("Failed to push item: " .. err)
        end
    end
end
 
-- Main function to check for available recipes and start crafting
local function main()
    while true do
        local crafting = false -- Flag to check if a recipe is currently being crafted
        for _, recipe in ipairs(recipeList) do
            if not isBinderEmpty() then -- Check if the binder is not empty
                for _, binderItem in ipairs(binder.list()) do
                    if binderItem.name == recipe.name then
                        showMessage("Crafting " .. recipe.name)
                        pushItemToOutput(peripheral.getName(output))
                        crafting = true -- A recipe is currently being crafted
                        break -- Break out of the recipe loop if a recipe is being crafted
                    end
                end
            elseif enoughMaterialsAvailable(recipe, input.list()) then -- Check if enough materials are available
                showMessage("Crafting " .. recipe.name)
                pushItemsToBinder(recipe.name, input.list())
                crafting = true -- A recipe is currently being crafted
                break -- Break out of the recipe loop if a recipe is being crafted
            end
        end
        if not crafting then -- If no recipe is currently being crafted
            showMessage("No recipe is currently being crafted.")
        end
        os.sleep(SLEEP_DURATION)
    end
end
 
-- Call function to check and craft recipes
main()

Comments

  • gelatine87 icon
    05/18/24 06:33:04 AM UTC (Edited)
    Lua |

    0 B

    |

    0 👍

    /

    0 👎

    To use this you need to change a few things.
     
    1.
    -- Define peripheral names
    local PERIPHERAL_NAMES = {
        INPUT = "minecraft:barrel",
        BINDER = {
            REGULAR = "elementalcraft:binder",
            IMPROVED = "elementalcraft:binder_improved"
        },
        OUTPUT = "minecraft:barrel"
    }
     
    INPUT & OUTPUT-Name could be different. If so change the "minecraft:barrel" to your choosen inventory.
     
    ==================================================================================================================
    2.
    -- Function to retrieve peripherals
    local function retrievePeripherals()
        local output = findPeripheral(PERIPHERAL_NAMES.OUTPUT, SIDES.LEFT)
        local input = findPeripheral(PERIPHERAL_NAMES.INPUT, SIDES.BOTTOM)
        local binder = findPeripheral(PERIPHERAL_NAMES.BINDER.IMPROVED, SIDES.RIGHT) or findPeripheral(PERIPHERAL_NAMES.BINDER.REGULAR, SIDES.RIGHT)
        return output, input, binder
    end
     
    The direction where the INPUT, OUTPUT and BINDER are. Change "SIDES.LEFT" depending on the peripheral.
     
    ==================================================================================================================
    3.
    -- Recipe list
    local recipeList = {
        {
            name = "elementalcraft:fireite_ingot",
            ingredients = {
                { "minecraft:netherite_ingot",        1 },
                { "elementalcraft:swift_alloy_ingot", 1 },
                { "elementalcraft:springaline_shard", 1 },
                { "elementalcraft:purecrystal",       1 }
            }
        },
        {
            name = "elementalcraft:reservoir_fire",
            ingredients = {
                { "elementalcraft:container",         1 },
                { "elementalcraft:springaline_glass", 1 },
                { "elementalcraft:purecrystal",       1 },
                { "elementalcraft:pristine_fire_gem", 1 }
            }
        }
    }
     
    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.