Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --recipe recorder
- local slots = {5, 6, 7, 9, 10, 11, 13, 14, 15, 16} --- slots to use for crafting
- local craftingRecipes = {}
- local keySlot = 16
- local keyItem = turtle.getItemDetail(keySlot)
- shaka = require("API")
- brain = shaka.readFile("brainID")
- function contains(table, val)
- for i=1,#table do
- if table[i] == val then
- if val ~=16 then
- return true
- end
- end
- end
- return false
- end
- function displayInventory()
- local filledColor = colors.green
- local emptyColor = colors.white
- local blueColor = colors.blue
- local orangeColor = colors.orange
- local line = " "
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.black)
- -- print(line)
- for y = 1, 4 do
- for x = 1, 4 do
- local i = (y - 1) * 4 + x
- local item = turtle.getItemDetail(i)
- if contains(slots, i) then
- term.setBackgroundColor(blueColor)
- elseif i == 16 then
- term.setBackgroundColor(orangeColor)
- else
- term.setBackgroundColor(emptyColor)
- end
- if item then
- if contains(slots, i) == false and i ~= 16 then
- term.setBackgroundColor(colors.red)
- else
- term.setBackgroundColor(filledColor)
- end
- end
- io.write(" ")
- term.setBackgroundColor(colors.black)
- io.write(" ")
- end
- print("")
- print(line)
- end
- -- term.setCursorPos(1, 1)
- -- term.setBackgroundColor(colors.black)
- end
- if keyItem then
- local file = fs.open("craftingRecipes", "r")
- if file then
- craftingRecipes = textutils.unserialize(file.readAll())
- file.close()
- end
- craftingRecipes[keyItem.name] = {}
- craftingRecipes[keyItem.name].keyCount = keyItem.count
- for i = 1, #slots do
- local slot = slots[i]
- local item = turtle.getItemDetail(slot)
- if item and slot ~= keySlot then
- if not craftingRecipes[keyItem.name][item.name] then
- craftingRecipes[keyItem.name][item.name] = {}
- end
- table.insert(craftingRecipes[keyItem.name][item.name], {count = item.count, slot = slot})
- end
- end
- rednet.send(brain, "saveItem " ..keyItem.name, "stockpileupdate")
- local senderID, msg, protocol = rednet.receive("stockPileConfirm", 0.5)
- term.clear()
- term.setCursorPos(1,1)
- if msg and senderID == brain then
- if msg == "nope" then
- term.setTextColor(colors.yellow)
- term.write("A recipe for ")
- term.setTextColor(colors.red)
- term.write(">"..keyItem.name.."<")
- term.setTextColor(colors.yellow)
- print(" is already saved for this turtle.")
- term.setTextColor(colors.green)
- print("\nDo you want to overwrite it? y/n")
- term.setTextColor(colors.white)
- answer = read()
- if answer == "y" then
- local id = os.getComputerID()
- local msg = table.concat({id, keyItem.name, "0"}, " ")
- rednet.send(brain, msg, "tableChange")
- local sender, message, protocol = rednet.receive("tableConfirm", 0.5)
- if sender then
- rednet.send(brain, "saveItem " ..keyItem.name, "stockpileupdate")
- local senderID, reply = rednet.receive("stockPileConfirm", 0.5)
- if reply == "gotcha" then
- term.setTextColor(colors.lime)
- print("\nSuccess!\n")
- term.setTextColor(colors.white)
- print("Changed recipe for", keyItem.name.. ".")
- file = fs.open("craftingRecipes", "w")
- file.write(textutils.serialize(craftingRecipes))
- file.close()
- sleep(2)
- os.reboot()
- else
- term.setTextColor(colors.red)
- print("Something went wrong.")
- print("Press any key to continue..")
- os.pullEvent("key")
- os.reboot()
- end
- end
- else
- term.setTextColor(colors.gray)
- textutils.slowPrint("Rebooting..")
- sleep(1)
- os.reboot()
- end
- elseif msg == "gotcha" then
- term.setTextColor(colors.lime)
- print("Success!\n")
- term.setTextColor(colors.white)
- print("Added ", keyItem.name, " to the crafting list.")
- file = fs.open("craftingRecipes", "w")
- file.write(textutils.serialize(craftingRecipes))
- file.close()
- sleep(1)
- os.reboot()
- end
- else
- shaka.changeColors(colors.orange, colors.black)
- term.clear()
- shaka.centerText("Not connected to brain!", 5)
- shaka.centerText("Press any key to continue..", 7)
- os.pullEvent("key")
- os.reboot()
- end
- else
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1, 10)
- print("Put crafting result in orange slot, make sure to use the correct amount.")
- print("Put recipe in blue slots.")
- print("Enter to save. Any other key to cancel.")
- while true do
- displayInventory()
- local eventData = {os.pullEvent()}
- event = eventData[1]
- key = eventData[2]
- if event == "turtle_inventory" then
- displayInventory()
- elseif event == "key" and (key == keys.enter or key == keys.c) then
- shell.run("recipeRecorder")
- return
- elseif event == "key" and (key ~= keys.enter or key ~= keys.c) then
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.yellow)
- textutils.slowPrint("Exiting.")
- sleep(1)
- os.reboot()
- elseif event == "mouse_click" then
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.yellow)
- textutils.slowPrint("Exiting.")
- sleep(1)
- os.reboot()
- end
- end
- end
Add Comment
Please, Sign In to add comment