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