View difference between Paste ID: 2Pvr9RnY and b2fc4912
SHOW: | | - or go back to the newest paste.
1
-- Adapted from Marval's pastebin b2fc4912 but converted to English via ChatGPT
2-
Z = {1,2,3,5,6,7,9,10,11}
2+
-- All credit goes to Marval.  This v.5 is untested as of yet, and likely needs some post-AI tweaking.
3-
x={}
3+
4-
tCraft={}
4+
-- Initialize the peripheral analyzer on the right side of the turtle
5-
function oddaj()
5+
6-
for i=1,16 do
6+
7
-- Define crafting slot positions
8-
if turtle.getItemCount(i)>0 then
8+
local craftingSlots = {1,2,3,5,6,7,9,10,11}
9-
turtle.select(i)
9+
10-
turtle.drop()
10+
-- Initialize the tables
11
local x = {}
12
local tCraft = {}
13
14-
function zlicz(recka2)
14+
-- Function to drop all items from the turtle's inventory
15-
ZLICZ={}
15+
local function dropAllItems()
16-
for i=1,9 do 
16+
    for i = 1, 16 do
17-
ZLICZ[i]={ID,META,COUNT}
17+
        if turtle.getItemCount(i) > 0 then
18
            turtle.select(i)
19-
for i=1,9 do
19+
            turtle.drop()
20-
if recka2[i][1] == nil then
20+
        end
21-
else
21+
    end
22-
ZLICZ[1].ID = recka2[i][1]
22+
23-
ZLICZ[1].META = recka2[i][2]
23+
24-
ZLICZ[1].COUNT = 1
24+
-- Function to count items in the crafting grid
25-
w = i
25+
local function countItems(craftingGrid)
26-
break
26+
    local countList = {}
27
    for i = 1, 9 do 
28
        countList[i] = {ID = nil, META = nil, COUNT = 0}
29-
k = 1
29+
    end
30-
for i = w+1,9 do
30+
31-
	check = false
31+
    -- Find the first non-empty slot
32-
	if recka2[i][1] == nil then
32+
    local startIndex = 0
33-
	else
33+
    for i = 1, 9 do
34-
	X = recka2[i][1]
34+
        if craftingGrid[i][1] ~= nil then
35-
	Y = recka2[i][2]
35+
            countList[1].ID = craftingGrid[i][1]
36-
	--print("Wyraz nr " ..i.. " wynosi " ..X.."")
36+
            countList[1].META = craftingGrid[i][2]
37-
		for j = 1,k do
37+
            countList[1].COUNT = 1
38-
			if X == ZLICZ[j].ID and Y == ZLICZ[j].META then 
38+
            startIndex = i
39-
				ZLICZ[j].COUNT = ZLICZ[j].COUNT + 1  
39+
            break
40-
				--print("Zwiekszam ilosc liczb "..X.." o jeden")
40+
        end
41-
				check = true
41+
    end
42-
			end
42+
43-
		
43+
    -- Count the rest of the items
44-
		end
44+
    local itemCount = 1
45-
	if check == false then
45+
    for i = startIndex + 1, 9 do
46-
		k = k+1
46+
        local itemFound = false
47-
		ZLICZ[k].ID = X
47+
        if craftingGrid[i][1] ~= nil then
48-
		ZLICZ[k].META = Y
48+
            local itemId = craftingGrid[i][1]
49-
		--print("Dopisuje element o nr " ..k.. " o wartosci "..X.."")
49+
            local itemMeta = craftingGrid[i][2]
50-
		ZLICZ[k].COUNT = 1
50+
51-
		
51+
            for j = 1, itemCount do
52-
	end
52+
                if itemId == countList[j].ID and itemMeta == countList[j].META then 
53-
	end
53+
                    countList[j].COUNT = countList[j].COUNT + 1
54
                    itemFound = true
55-
return ZLICZ,k
55+
                    break
56
                end
57-
function sprawdz(dane,rekordy)
57+
            end
58-
sprawdzenie = true
58+
59-
for i=1,rekordy do
59+
            if not itemFound then
60-
slot = 0
60+
                itemCount = itemCount + 1
61-
X = 0
61+
                countList[itemCount].ID = itemId
62-
Y = 0
62+
                countList[itemCount].META = itemMeta
63-
ilosc = 0
63+
                countList[itemCount].COUNT = 1
64-
--print("Poszukuje bloku o ID rownym " ..dane[i].ID.. "")
64+
            end
65-
repeat
65+
        end
66-
slot = slot + 1
66+
    end
67-
X = analyzer.getBlockIdAt(slot)
67+
68-
Y = analyzer.getBlockMetadataAt(slot)
68+
    return countList, itemCount
69-
if X == dane[i].ID and dane[i].META == nil then
69+
70-
ilosc = ilosc + analyzer.getBlockCountAt(slot)
70+
-- Function to check if all required items are available
71-
elseif X == dane[i].ID and Y == dane[i].META then 
71+
local function checkAvailability(items, recordCount)
72-
--print("znalazlem w slocie "..slot.." blok o ID rownym "..ID.."") 
72+
    local isAvailable = true
73-
ilosc = ilosc + analyzer.getBlockCountAt(slot)
73+
    local missingItem = nil
74
75-
until slot == 109
75+
    for i = 1, recordCount do
76-
if ilosc < dane[i].COUNT then
76+
        local slot = 0
77-
sprawdzenie = false
77+
        local itemId = 0
78-
if dane[i].META == nil then
78+
        local itemMeta = 0
79-
brak = dane[i].ID
79+
        local itemCount = 0
80-
else
80+
81-
brak = ""..dane[i].ID..":"..dane[i].META..""
81+
        repeat
82
            slot = slot + 1
83-
print("braklo itemu o ID: "..dane[i].ID.." w liczbie: "..dane[i].COUNT - ilosc.."")
83+
            itemId = analyzer.getBlockIdAt(slot)
84
            itemMeta = analyzer.getBlockMetadataAt(slot)
85
86-
return sprawdzenie, brak
86+
            if itemId == items[i].ID and (items[i].META == nil or itemMeta == items[i].META) then
87
                itemCount = itemCount + analyzer.getBlockCountAt(slot)
88-
function craft(recka3)
88+
            end
89-
for i=1,9 do
89+
        until slot == 109
90-
if recka3[i][1] == nil then
90+
91-
else
91+
        if itemCount < items[i].COUNT then
92-
slot = 0
92+
            isAvailable = false
93-
repeat
93+
            missingItem = items[i].META == nil and items[i].ID or (items[i].ID .. ":" .. items[i].META)
94-
slot = slot +1
94+
            print("Missing item ID: " .. missingItem .. " in quantity: " .. items[i].COUNT - itemCount)
95-
X = analyzer.getBlockIdAt(slot)
95+
            break
96-
Y = analyzer.getBlockMetadataAt(slot)
96+
        end
97-
until (X == recka3[i][1] and recka3[i][2] == nil) or (X == recka3[i][1] and Y == recka3[i][2])
97+
    end
98-
--print("Znalazlem w slocie "..slot.."")
98+
99-
ilosc = analyzer.getBlockCountAt(slot)
99+
    return isAvailable, missingItem
100-
turtle.select(Z[i])
100+
101-
analyzer.takeAt(slot)
101+
102-
turtle.drop(ilosc - 1)
102+
-- Function to perform the crafting operation
103
local function craft(items)
104
    for i = 1, 9 do
105-
turtle.craft()
105+
        if items[i][1] ~= nil then
106-
oddaj()
106+
            local slot = 0
107
            repeat
108-
function craftuj(recka)
108+
                slot = slot + 1
109-
print("craftuje "..b.."")
109+
                local itemId = analyzer.getBlockIdAt(slot)
110-
a = false
110+
                local itemMeta = analyzer.getBlockMetadataAt(slot)
111-
while not a do
111+
            until (itemId == items[i][1] and (items[i][2] == nil or itemMeta == items[i][2]))
112-
a,b = sprawdz(zlicz(recka))
112+
113-
if a == false then
113+
            local itemCount = analyzer.getBlockCountAt(slot)
114-
print("braklo "..b.."")
114+
            turtle.select(craftingSlots[i])
115-
b = tostring(b)
115+
            analyzer.takeAt(slot)
116-
crafting()
116+
            turtle.drop(itemCount - 1)
117-
a = false
117+
        end
118-
else
118+
    end
119-
craft(recka)
119+
120
    turtle.craft()
121
    dropAllItems()
122
end
123-
function crafting()
123+
124-
if tCraft[b] then
124+
-- Function to manage the crafting process
125-
    craftuj(tCraft[b])
125+
local function manageCrafting(recipe)
126-
	else
126+
    print("Crafting " .. b)
127-
	print("Braklo: "..b.."") os.pullEvent()
127+
    local isReadyToCraft = false
128-
  end
128+
129
    while not isReadyToCraft do
130-
function craftujemy()
130+
        local countList, _ = countItems(recipe)
131-
odczyt = io.open ("receptury","r")
131+
        isReadyToCraft, missingItem = checkAvailability(countList)
132-
while true do -- odczyt recek
132+
133-
tekst=odczyt:read()
133+
        if not isReadyToCraft then
134-
if tekst == nil then break end
134+
            print("Missing " .. missingItem)
135-
item1, x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9] = string.match(tekst, "([%w%:]+)=([%w%:]+);([%w%:]+);([%w%:]+);([%w%:]+);([%w%:]+);([%w%:]+);([%w%:]+);([%w%:]+);([%w%:]+)")
135+
            missingItem = tostring(missingItem)
136-
tCraft[item1]={}
136+
            -- Assuming 'crafting' is a function that handles missing items
137-
for i=1,9 do
137+
            crafting()
138-
if(x[i] == "0") then
138+
        else
139-
tCraft[item1][i]={nil}
139+
            craft(recipe)
140-
elseif(string.find(x[i],":")) then
140+
        end
141-
id,meta = string.match(x[i],"(%d+):(%d+)")
141+
    end
142-
tCraft[item1][i]={tonumber(id),tonumber(meta)}
142+
143-
else
143+
144-
tCraft[item1][i]={tonumber(x[i])}
144+
-- Function to handle crafting based on user input
145
local function crafting()
146
    if tCraft[b] then
147
        manageCrafting(tCraft[b])
148-
odczyt:close()
148+
    else
149-
print("Co mam craftowac? Podaj ID lub ID:META")
149+
        print("Missing: " .. b)
150-
b = read()
150+
        os.pullEvent() -- Wait for an event, possibly user input
151-
b1 = b
151+
    end
152-
print("ile? ")
152+
153-
c = read()
153+
154-
for zz=1,c do
154+
-- Function to start the crafting process
155-
crafting()
155+
local function startCrafting()
156-
b = b1
156+
    local file = io.open("receptury", "r") -- Open the recipe file
157
    while true do
158
        local line = file:read()
159-
function menu()
159+
        if line == nil then break end
160-
term.clear()
160+
161-
opcja = 2
161+
        -- Parse the recipe line
162-
term.setCursorPos(1,1)
162+
        local item, recipeParts = string.match(line, "([%w%:]+)=(.+)")
163-
term.write("Ultimate Crafting Program")
163+
164-
term.setCursorPos(1,2)
164+
        -- Store the parsed recipe
165-
term.write("[ ] Craftuj")
165+
        tCraft[item] = {}
166-
term.setCursorPos(1,3)
166+
        for i = 1, 9 do
167-
term.write("[ ] Dodaj recepture przez skanowanie")
167+
            local part = select(i, string.match(recipeParts, "([^;]+)"))
168-
term.setCursorPos(1,4)
168+
            if part == "0" then
169-
term.write("[ ] Dodaj recepture przez wpisanie")
169+
                tCraft[item][i] = {nil}
170
            elseif string.find(part, ":") then
171-
function odswiez()
171+
                local id, meta = string.match(part, "(%d+):(%d+)")
172-
for i=2,10 do
172+
                tCraft[item][i] = {tonumber(id), tonumber(meta)}
173-
term.setCursorPos(2,i)
173+
            else
174-
term.write(" ")
174+
                tCraft[item][i] = {tonumber(part)}
175
            end
176-
term.setCursorPos(2,opcja)
176+
        end
177-
term.write("X")
177+
    end
178
    file:close()
179-
function zapisz()
179+
180-
print("Uloz recepture w inventory turtla")
180+
    -- Ask user for the item to craft and the quantity
181-
os.pullEvent()
181+
    print("What to craft? Enter ID or ID:META")
182-
x={}
182+
    b = read()
183-
for i=1,9 do
183+
    local originalItem = b
184-
turtle.select(Z[i]) -- wybierz slot
184+
    print("How many?")
185-
x[i] = {} -- stworz tablice
185+
    local quantity = tonumber(read())
186-
id=analyzer.getBlockId() -- pobierz id
186+
187-
if(id == false) then -- jesli nie ma nic
187+
    -- Craft the specified quantity of the item
188-
x[i].ID = nil -- przypisz nil
188+
    for zz = 1, quantity do
189-
else
189+
        crafting()
190-
x[i].ID = id -- przypisz id
190+
        b = originalItem
191-
x[i].META = analyzer.getBlockMetadata() -- przypisz meta
191+
    end
192
end
193
194-
z={}
194+
-- Function to display the main menu
195-
for i=1,9 do
195+
local function displayMenu()
196-
if x[i].ID == nil then -- jezli id = nil
196+
    term.clear() -- Clear the terminal screen
197-
z[i] = 0; -- przypisz 0
197+
    local option = 2 -- Initialize the default selected option
198-
elseif x[i].META == nil then -- jezeli meta = nil
198+
199-
z[i] = x[i].ID -- przypisz samo id
199+
    -- Set the cursor position and write menu options
200-
else -- w przeciwnym wypadku
200+
    term.setCursorPos(1, 1)
201-
z[i] = ""..x[i].ID..":"..x[i].META.."" -- wpisz ID:META
201+
    term.write("Ultimate Crafting Program")
202
    term.setCursorPos(1, 2)
203
    term.write("[ ] Start Crafting") -- "Craftuj" translates to "Start Crafting"
204-
turtle.craft()
204+
    term.setCursorPos(1, 3)
205-
turtle.select(11)
205+
    term.write("[ ] Add Recipe by Scanning") -- "Dodaj recepture przez skanowanie"
206-
item = analyzer.getBlockId()..":"..analyzer.getBlockMetadata()
206+
    term.setCursorPos(1, 4)
207-
zapis = io.open ("receptury","a") -- otworz plik
207+
    term.write("[ ] Add Recipe Manually") -- "Dodaj recepture przez wpisanie"
208-
zapis:write(string.format("%s=%s;%s;%s;%s;%s;%s;%s;%s;%s\n",item,z[1],z[2],z[3],z[4],z[5],z[6],z[7],z[8],z[9]))
208+
209-
zapis:close()
209+
210
-- Function to refresh the menu display
211-
function podaj()
211+
local function refreshMenu()
212-
term.clear()
212+
    -- Assume 'option' is a global or externally managed variable
213-
print("podaj recepture wedlug skladni: ")
213+
    for i = 2, 10 do
214-
print("ID:META=ID:META;ID:META; ... ;ID:META")
214+
        term.setCursorPos(2, i)
215-
print("mozna podawac rowniez ID bez podania Metadanych")
215+
        term.write(" ")
216-
nowa=read()
216+
    end
217-
zapis = io.open ("receptury","a") -- otworz plik
217+
    term.setCursorPos(2, option) -- Update the display to show the selected option
218-
zapis:write(nowa.."\n")
218+
    term.write("X")
219-
zapis:close()
219+
220
221-
menu()
221+
-- Function to scan and save a new crafting recipe
222-
odswiez()
222+
local function saveScannedRecipe()
223
    print("Arrange the recipe in the turtle's inventory")
224-
przycisk,kod = os.pullEvent()
224+
    os.pullEvent() -- Wait for an event, likely user arrangement of items
225-
if przycisk == "key" and kod == 208 and opcja < 4 then
225+
    local recipe = {}
226-
opcja=opcja+1
226+
    
227
    for i = 1, 9 do
228-
if przycisk == "key" and kod == 200 and opcja > 2 then
228+
        turtle.select(craftingSlots[i]) -- Select slot
229-
opcja = opcja - 1
229+
        local id = analyzer.getBlockId() -- Get block id
230
        if id then
231-
if przycisk == "key" and kod == 28 then
231+
            recipe[i] = {ID = id, META = analyzer.getBlockMetadata()} -- Assign id and meta
232-
	term.clear()
232+
        else
233-
	term.setCursorPos(1,1)
233+
            recipe[i] = {ID = nil} -- Assign nil if empty
234-
	if opcja == 2 then
234+
        end
235-
	craftujemy()
235+
    end
236-
	elseif opcja == 3 then
236+
237-
	zapisz()
237+
    -- Convert the recipe to a string format
238-
	elseif opcja == 4 then 
238+
    local recipeString = {}
239-
	podaj()
239+
    for i = 1, 9 do
240-
	end
240+
        if recipe[i].ID == nil then
241-
	menu()
241+
            recipeString[i] = "0"
242-
	end
242+
        elseif recipe[i].META == nil then
243-
odswiez()
243+
            recipeString[i] = tostring(recipe[i].ID)
244-
end
244+
        else
245
            recipeString[i] = recipe[i].ID .. ":" .. recipe[i].META
246
        end
247
    end
248
249
    turtle.craft()
250
    turtle.select(11)
251
    local craftedItem = analyzer.getBlockId() .. ":" .. analyzer.getBlockMetadata()
252
    local file = io.open("receptury", "a") -- Open file for appending
253
    file:write(string.format("%s=%s;%s;%s;%s;%s;%s;%s;%s;%s\n", craftedItem, unpack(recipeString)))
254
    file:close()
255
end
256
257
-- Function to manually input and save a new recipe
258
local function inputAndSaveRecipe()
259
    term.clear()
260
    print("Enter the recipe in the format: ")
261
    print("ID:META=ID:META;ID:META; ... ;ID:META")
262
    print("You can also input IDs without metadata")
263
    local newRecipe = read()
264
265
    local file = io.open("receptury", "a") -- Open file for appending
266
    file:write(newRecipe .. "\n")
267
    file:close()
268
end
269
270
-- Main loop for menu navigation and action selection
271
displayMenu()
272
refreshMenu()
273
274
while true do
275
    local event, keyCode = os.pullEvent("key")
276
277
    -- Navigate through the menu
278
    if keyCode == 208 and option < 4 then -- Arrow Down
279
        option = option + 1
280
    elseif keyCode == 200 and option > 2 then -- Arrow Up
281
        option = option - 1
282
    end
283
284
    -- Select a menu option
285
    if keyCode == 28 then -- Enter key
286
        term.clear()
287
        term.setCursorPos(1, 1)
288
        if option == 2 then
289
            startCrafting()
290
        elseif option == 3 then
291
            saveScannedRecipe()
292
        elseif option == 4 then
293
            inputAndSaveRecipe()
294
        end
295
        displayMenu()
296
        refreshMenu()
297
    end
298
end
299