Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Adapted from Marval's pastebin b2fc4912 but converted to English via ChatGPT
- -- All credit goes to Marval. This v.5 is untested as of yet, and likely needs some post-AI tweaking.
- -- Initialize the peripheral analyzer on the right side of the turtle
- local analyzer = peripheral.wrap("right")
- -- Define crafting slot positions
- local craftingSlots = {1,2,3,5,6,7,9,10,11}
- -- Initialize the tables
- local x = {}
- local tCraft = {}
- -- Function to drop all items from the turtle's inventory
- local function dropAllItems()
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- -- Function to count items in the crafting grid
- local function countItems(craftingGrid)
- local countList = {}
- for i = 1, 9 do
- countList[i] = {ID = nil, META = nil, COUNT = 0}
- end
- -- Find the first non-empty slot
- local startIndex = 0
- for i = 1, 9 do
- if craftingGrid[i][1] ~= nil then
- countList[1].ID = craftingGrid[i][1]
- countList[1].META = craftingGrid[i][2]
- countList[1].COUNT = 1
- startIndex = i
- break
- end
- end
- -- Count the rest of the items
- local itemCount = 1
- for i = startIndex + 1, 9 do
- local itemFound = false
- if craftingGrid[i][1] ~= nil then
- local itemId = craftingGrid[i][1]
- local itemMeta = craftingGrid[i][2]
- for j = 1, itemCount do
- if itemId == countList[j].ID and itemMeta == countList[j].META then
- countList[j].COUNT = countList[j].COUNT + 1
- itemFound = true
- break
- end
- end
- if not itemFound then
- itemCount = itemCount + 1
- countList[itemCount].ID = itemId
- countList[itemCount].META = itemMeta
- countList[itemCount].COUNT = 1
- end
- end
- end
- return countList, itemCount
- end
- -- Function to check if all required items are available
- local function checkAvailability(items, recordCount)
- local isAvailable = true
- local missingItem = nil
- for i = 1, recordCount do
- local slot = 0
- local itemId = 0
- local itemMeta = 0
- local itemCount = 0
- repeat
- slot = slot + 1
- itemId = analyzer.getBlockIdAt(slot)
- itemMeta = analyzer.getBlockMetadataAt(slot)
- if itemId == items[i].ID and (items[i].META == nil or itemMeta == items[i].META) then
- itemCount = itemCount + analyzer.getBlockCountAt(slot)
- end
- until slot == 109
- if itemCount < items[i].COUNT then
- isAvailable = false
- missingItem = items[i].META == nil and items[i].ID or (items[i].ID .. ":" .. items[i].META)
- print("Missing item ID: " .. missingItem .. " in quantity: " .. items[i].COUNT - itemCount)
- break
- end
- end
- return isAvailable, missingItem
- end
- -- Function to perform the crafting operation
- local function craft(items)
- for i = 1, 9 do
- if items[i][1] ~= nil then
- local slot = 0
- repeat
- slot = slot + 1
- local itemId = analyzer.getBlockIdAt(slot)
- local itemMeta = analyzer.getBlockMetadataAt(slot)
- until (itemId == items[i][1] and (items[i][2] == nil or itemMeta == items[i][2]))
- local itemCount = analyzer.getBlockCountAt(slot)
- turtle.select(craftingSlots[i])
- analyzer.takeAt(slot)
- turtle.drop(itemCount - 1)
- end
- end
- turtle.craft()
- dropAllItems()
- end
- -- Function to manage the crafting process
- local function manageCrafting(recipe)
- print("Crafting " .. b)
- local isReadyToCraft = false
- while not isReadyToCraft do
- local countList, _ = countItems(recipe)
- isReadyToCraft, missingItem = checkAvailability(countList)
- if not isReadyToCraft then
- print("Missing " .. missingItem)
- missingItem = tostring(missingItem)
- -- Assuming 'crafting' is a function that handles missing items
- crafting()
- else
- craft(recipe)
- end
- end
- end
- -- Function to handle crafting based on user input
- local function crafting()
- if tCraft[b] then
- manageCrafting(tCraft[b])
- else
- print("Missing: " .. b)
- os.pullEvent() -- Wait for an event, possibly user input
- end
- end
- -- Function to start the crafting process
- local function startCrafting()
- local file = io.open("receptury", "r") -- Open the recipe file
- while true do
- local line = file:read()
- if line == nil then break end
- -- Parse the recipe line
- local item, recipeParts = string.match(line, "([%w%:]+)=(.+)")
- -- Store the parsed recipe
- tCraft[item] = {}
- for i = 1, 9 do
- local part = select(i, string.match(recipeParts, "([^;]+)"))
- if part == "0" then
- tCraft[item][i] = {nil}
- elseif string.find(part, ":") then
- local id, meta = string.match(part, "(%d+):(%d+)")
- tCraft[item][i] = {tonumber(id), tonumber(meta)}
- else
- tCraft[item][i] = {tonumber(part)}
- end
- end
- end
- file:close()
- -- Ask user for the item to craft and the quantity
- print("What to craft? Enter ID or ID:META")
- b = read()
- local originalItem = b
- print("How many?")
- local quantity = tonumber(read())
- -- Craft the specified quantity of the item
- for zz = 1, quantity do
- crafting()
- b = originalItem
- end
- end
- -- Function to display the main menu
- local function displayMenu()
- term.clear() -- Clear the terminal screen
- local option = 2 -- Initialize the default selected option
- -- Set the cursor position and write menu options
- term.setCursorPos(1, 1)
- term.write("Ultimate Crafting Program")
- term.setCursorPos(1, 2)
- term.write("[ ] Start Crafting") -- "Craftuj" translates to "Start Crafting"
- term.setCursorPos(1, 3)
- term.write("[ ] Add Recipe by Scanning") -- "Dodaj recepture przez skanowanie"
- term.setCursorPos(1, 4)
- term.write("[ ] Add Recipe Manually") -- "Dodaj recepture przez wpisanie"
- end
- -- Function to refresh the menu display
- local function refreshMenu()
- -- Assume 'option' is a global or externally managed variable
- for i = 2, 10 do
- term.setCursorPos(2, i)
- term.write(" ")
- end
- term.setCursorPos(2, option) -- Update the display to show the selected option
- term.write("X")
- end
- -- Function to scan and save a new crafting recipe
- local function saveScannedRecipe()
- print("Arrange the recipe in the turtle's inventory")
- os.pullEvent() -- Wait for an event, likely user arrangement of items
- local recipe = {}
- for i = 1, 9 do
- turtle.select(craftingSlots[i]) -- Select slot
- local id = analyzer.getBlockId() -- Get block id
- if id then
- recipe[i] = {ID = id, META = analyzer.getBlockMetadata()} -- Assign id and meta
- else
- recipe[i] = {ID = nil} -- Assign nil if empty
- end
- end
- -- Convert the recipe to a string format
- local recipeString = {}
- for i = 1, 9 do
- if recipe[i].ID == nil then
- recipeString[i] = "0"
- elseif recipe[i].META == nil then
- recipeString[i] = tostring(recipe[i].ID)
- else
- recipeString[i] = recipe[i].ID .. ":" .. recipe[i].META
- end
- end
- turtle.craft()
- turtle.select(11)
- local craftedItem = analyzer.getBlockId() .. ":" .. analyzer.getBlockMetadata()
- local file = io.open("receptury", "a") -- Open file for appending
- file:write(string.format("%s=%s;%s;%s;%s;%s;%s;%s;%s;%s\n", craftedItem, unpack(recipeString)))
- file:close()
- end
- -- Function to manually input and save a new recipe
- local function inputAndSaveRecipe()
- term.clear()
- print("Enter the recipe in the format: ")
- print("ID:META=ID:META;ID:META; ... ;ID:META")
- print("You can also input IDs without metadata")
- local newRecipe = read()
- local file = io.open("receptury", "a") -- Open file for appending
- file:write(newRecipe .. "\n")
- file:close()
- end
- -- Main loop for menu navigation and action selection
- displayMenu()
- refreshMenu()
- while true do
- local event, keyCode = os.pullEvent("key")
- -- Navigate through the menu
- if keyCode == 208 and option < 4 then -- Arrow Down
- option = option + 1
- elseif keyCode == 200 and option > 2 then -- Arrow Up
- option = option - 1
- end
- -- Select a menu option
- if keyCode == 28 then -- Enter key
- term.clear()
- term.setCursorPos(1, 1)
- if option == 2 then
- startCrafting()
- elseif option == 3 then
- saveScannedRecipe()
- elseif option == 4 then
- inputAndSaveRecipe()
- end
- displayMenu()
- refreshMenu()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement