Shaka01

craftTurtle_recipeRecorder_dependancy

Feb 11th, 2023 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. --recipe recorder
  2.  
  3. local slots = {5, 6, 7, 9, 10, 11, 13, 14, 15, 16} --- slots to use for crafting
  4. local craftingRecipes = {}
  5.  
  6. local keySlot = 16
  7. local keyItem = turtle.getItemDetail(keySlot)
  8. shaka = require("API")
  9. brain = shaka.readFile("brainID")
  10.  
  11. function contains(table, val)
  12.   for i=1,#table do
  13.     if table[i] == val then
  14.     if val ~=16 then
  15.       return true
  16.      end
  17.     end
  18.   end
  19.   return false
  20. end
  21.  
  22.  
  23.  
  24. function displayInventory()
  25.   local filledColor = colors.green
  26.   local emptyColor = colors.white
  27.   local blueColor = colors.blue
  28.   local orangeColor = colors.orange
  29.   local line = "              "
  30.   term.setCursorPos(1, 1)
  31.   term.setBackgroundColor(colors.black)
  32.   -- print(line)
  33.   for y = 1, 4 do
  34.     for x = 1, 4 do
  35.       local i = (y - 1) * 4 + x
  36.       local item = turtle.getItemDetail(i)
  37.       if contains(slots, i) then
  38.         term.setBackgroundColor(blueColor)
  39.       elseif i == 16 then
  40.         term.setBackgroundColor(orangeColor)
  41.       else
  42.         term.setBackgroundColor(emptyColor)
  43.       end
  44.       if item then
  45.         if contains(slots, i) == false and i ~= 16 then
  46.         term.setBackgroundColor(colors.red)
  47.         else
  48.         term.setBackgroundColor(filledColor)
  49.         end
  50.       end
  51.       io.write("  ")
  52.       term.setBackgroundColor(colors.black)
  53.       io.write(" ")
  54.     end
  55.     print("")
  56.     print(line)
  57.   end
  58.   -- term.setCursorPos(1, 1)
  59.   -- term.setBackgroundColor(colors.black)
  60. end
  61.  
  62.  
  63. if keyItem then
  64.   local file = fs.open("craftingRecipes", "r")
  65.   if file then
  66.     craftingRecipes = textutils.unserialize(file.readAll())
  67.     file.close()
  68.   end
  69.  
  70.   craftingRecipes[keyItem.name] = {}
  71.  
  72.   craftingRecipes[keyItem.name].keyCount = keyItem.count
  73.   for i = 1, #slots do
  74.     local slot = slots[i]
  75.     local item = turtle.getItemDetail(slot)
  76.     if item and slot ~= keySlot then
  77.       if not craftingRecipes[keyItem.name][item.name] then
  78.         craftingRecipes[keyItem.name][item.name] = {}
  79.       end
  80.       table.insert(craftingRecipes[keyItem.name][item.name], {count = item.count, slot = slot})
  81.     end
  82.   end
  83.  
  84.  
  85.     rednet.send(brain, "saveItem " ..keyItem.name, "stockpileupdate")
  86.     local senderID, msg, protocol = rednet.receive("stockPileConfirm", 0.5)
  87.     term.clear()
  88.     term.setCursorPos(1,1)
  89.  
  90.     if msg and senderID == brain then
  91.         if msg == "nope" then
  92.             term.setTextColor(colors.yellow)
  93.             term.write("A recipe for ")
  94.             term.setTextColor(colors.red)
  95.             term.write(">"..keyItem.name.."<")
  96.             term.setTextColor(colors.yellow)
  97.             print(" is already saved for this turtle.")
  98.             term.setTextColor(colors.green)
  99.             print("\nDo you want to overwrite it? y/n")
  100.             term.setTextColor(colors.white)
  101.             answer = read()
  102.             if answer == "y" then
  103.                 local id = os.getComputerID()
  104.                 local msg = table.concat({id, keyItem.name, "0"}, " ")
  105.                 rednet.send(brain, msg, "tableChange")
  106.                 local sender, message, protocol = rednet.receive("tableConfirm", 0.5)
  107.                 if sender then
  108.                     rednet.send(brain, "saveItem " ..keyItem.name, "stockpileupdate")
  109.                     local senderID, reply = rednet.receive("stockPileConfirm", 0.5)
  110.                     if reply == "gotcha" then
  111.                         term.setTextColor(colors.lime)
  112.                         print("\nSuccess!\n")
  113.                         term.setTextColor(colors.white)
  114.                         print("Changed recipe for", keyItem.name.. ".")
  115.                         file = fs.open("craftingRecipes", "w")
  116.                         file.write(textutils.serialize(craftingRecipes))
  117.                         file.close()
  118.                         sleep(2)
  119.                         os.reboot()
  120.                     else
  121.                         term.setTextColor(colors.red)
  122.                         print("Something went wrong.")
  123.                         print("Press any key to continue..")
  124.                         os.pullEvent("key")
  125.                         os.reboot()
  126.                     end
  127.                 end    
  128.             else
  129.                 term.setTextColor(colors.gray)
  130.                 textutils.slowPrint("Rebooting..")
  131.                 sleep(1)
  132.                 os.reboot()
  133.             end
  134.         elseif msg == "gotcha" then
  135.             term.setTextColor(colors.lime)
  136.             print("Success!\n")
  137.             term.setTextColor(colors.white)
  138.             print("Added ", keyItem.name, " to the crafting list.")
  139.             file = fs.open("craftingRecipes", "w")
  140.             file.write(textutils.serialize(craftingRecipes))
  141.             file.close()
  142.             sleep(1)
  143.             os.reboot()
  144.         end
  145.     else
  146.         shaka.changeColors(colors.orange, colors.black)
  147.         term.clear()
  148.         shaka.centerText("Not connected to brain!", 5)
  149.         shaka.centerText("Press any key to continue..", 7)
  150.         os.pullEvent("key")
  151.         os.reboot()
  152.     end
  153.  
  154.  
  155.  
  156. else
  157.  
  158.     term.clear()
  159.     term.setCursorPos(1,1)
  160.     term.setBackgroundColor(colors.black)
  161.     term.setCursorPos(1, 10)
  162.     print("Put crafting result in orange slot, make sure to use the correct amount.")
  163.     print("Put recipe in blue slots.")
  164.     print("Enter to save. Any other key to cancel.")
  165.  
  166.     while true do
  167.       displayInventory()
  168.       local eventData = {os.pullEvent()}
  169.       event = eventData[1]
  170.       key = eventData[2]
  171.       if event == "turtle_inventory" then
  172.         displayInventory()
  173.       elseif event == "key" and (key == keys.enter or key == keys.c) then
  174.         shell.run("recipeRecorder")
  175.         return
  176.       elseif event == "key" and (key ~= keys.enter or key ~= keys.c) then
  177.         term.clear()
  178.         term.setCursorPos(1,1)
  179.         term.setTextColor(colors.yellow)
  180.         textutils.slowPrint("Exiting.")
  181.         sleep(1)
  182.         os.reboot()
  183.       elseif event == "mouse_click" then
  184.         term.clear()
  185.         term.setCursorPos(1,1)
  186.         term.setTextColor(colors.yellow)
  187.         textutils.slowPrint("Exiting.")
  188.         sleep(1)
  189.         os.reboot()
  190.       end
  191.     end
  192. end
Add Comment
Please, Sign In to add comment