Advertisement
Stekeblad

FluxedSeeds

Jan 13th, 2016
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 39.42 KB | None | 0 0
  1. -- Made by Stekeblad
  2. -- Last updated June 2016
  3.  
  4. -- This is a computer craft program for automating the mod FluxedCrystals2 by Jaredlll08
  5. -- You will need the open peripherals mods to be able to use the program
  6. -- Build with the following mod versions:
  7. -- ComputerCraft 1.75
  8. -- OpenPeripheralsAddons 1.7.10 - 0.4
  9. -- OpenPeripheralsCore 1.7.10 - 1.2
  10. -- OpenPeripheralsIntegration 1.7.10 - 0.3
  11.  
  12. -- First some variables...
  13. local args = {...}
  14. local SeedTable={}
  15. local settings = {}
  16. local x, y = term.getSize()
  17. local printDmgValues = false
  18. local invInput
  19. local invOutput
  20. if settings.outputType == "chest" then
  21.   local nrOutSlots
  22. end
  23. local slot = nil
  24. local lastEmpty = 0
  25. local nrInpSlots -- two variables for the same thing? Don't want to look into that now....
  26. local inpChestSlots
  27.  
  28. --Then some function in almost alphabetical order
  29. -- (It works, and not super 100% rubbish names!)
  30.  
  31. function about() -- prints little about-information
  32.   term.clear()
  33.   term.setCursorPos(1,1)
  34.   print("Your version is 2.5 of FluxedSeeds")
  35.   print("For the program to work, you will need the openPeripherals mods in order to connect chests and refiners to your computer network.")
  36.   print("\nFluxedSeeds is created by Stekeblad and is intended to be used together with the mod fluxedCrystals2 by Jaredlll08.")
  37.   print("\n\tPress enter for more...")
  38.   read()
  39.   term.clear()
  40.   term.setCursorPos(1,1)
  41.   print("Things NOT made by Stekeblad in this program are two parts in the function for downloading default configurations,")
  42.   print("\nThe download function downloads the seed information from the creator's gitHub.")
  43.   print("The data is then passed to a JSON decoder made by ElvishJerricco.")
  44.   print("This two things can I not take credits for!")
  45.   print("\n\tPress enter for more...")
  46.   read()
  47.   term.clear()
  48.   term.setCursorPos(1,1)
  49.   print("The program is made to work in a computer or on a monitor, both on normal and advanced systems.")
  50.   print("Beacuse FluxedCrystals supports adding, editing and removing seeds in its configuration file, FluxedSeeds is built so its easy to edit")
  51.   print("what the program knows about the different seed types so it matches your configuration file.")
  52.   print("\nFor more information I recommend you to find this programs thread on computercraft's forum")
  53.   print("\n\tPress enter to finnish...")
  54.   read()
  55.   return
  56. end
  57.  
  58. function checkIfInTable() --almost ignore-able
  59.   return SeedTable[slot.dmg]["needed"]
  60. end
  61.  
  62. --checkMove
  63. -- returns 0 if there is not enougth items in stack to move
  64. -- returns -1 if no free space in output inventory is found
  65. -- returns 1 on successful move
  66. -- returns 2 on non-fluxedCrystals-shards or shard not in SeedTable
  67. function checkMove(slot, i)
  68.   if (slot.id ~= "fluxedCrystals:smoothShard"
  69.   and slot.id ~= "fluxedCrystals:roughShard") then
  70.     return 2
  71.   end
  72.   if not pcall(checkIfInTable) then
  73.     return 2
  74.   end
  75.   if(SeedTable[slot.dmg]["needed"] <= slot.qty) then
  76.     if settings.outputType == "chest" then
  77.       local oldLastEmpty = lastEmpty
  78.       local outSlot = "notARealSlot"
  79.       while outSlot ~= nil do
  80.         lastEmpty = lastEmpty + 1
  81.         if lastEmpty == oldLastEmpty then return -1 end
  82.         if lastEmpty > nrOutSlots then
  83.           lastEmpty = lastEmpty - nrOutSlots
  84.         end
  85.         outSlot = invOutput.getStackInSlot(lastEmpty)  
  86.         sleep(0.05)        
  87.       end
  88.       local status, error = pcall(invInput.pushItemIntoSlot(settings.outputDir,
  89.       i, SeedTable[slot.dmg]["needed"], lastEmpty))
  90.       if not status then error("Failed to move items, check connection to output chest") end
  91.       return 1
  92.     else
  93.       --> settings.outputType == "refiner"
  94.       if invOutput.getStackInSlot(1) == nil then
  95.         invInput.pushItemIntoSlot(settings.outputDir,
  96.         i, SeedTable[slot.dmg]["needed"], 1)
  97.         return 1
  98.       else
  99.         return -1
  100.       end
  101.     end
  102.   else
  103.     return 0
  104.   end
  105. end
  106.  
  107.  
  108.  
  109. function editSeedTable() -- change the SeedtTble-file
  110.   if settings.alwaysEditFile == "true" then
  111.     shell.run("edit FluxedSeeds_SeedTable")
  112.     return
  113.   end
  114.   local SeedSetup = true
  115.   while SeedSetup do
  116.     term.clear()
  117.     print("Enter the input inventory slot number for the item to add, edit or remove, slot 1 is in top left corner")
  118.     print("You can also write \"back\" to save and exit")                                                         -- add, edit, delete without the item? NO! (isInFile)
  119.     print("or \"file\" if you are an advanced user to edit the seed table file directly" )
  120.     term.write ("input?: ")
  121.     local badInp = true, usrInp
  122.     while badInp do
  123.       usrInp = read()
  124.       local numCheck = tonumber(usrInp)
  125.       if numCheck ~= nil then
  126.         if numCheck > invInput.getInventorySize()  then -- To find witch one that errored, had bad luck guessing earlier
  127.           print("Slot number must be a whole number between 1 and "..invInput.getInventorySize())
  128.         elseif numCheck < 1 then
  129.           print("Slot number must be a whole number between 1 and "..invInput.getInventorySize())        
  130.         elseif numCheck ~= math.floor(numCheck) then
  131.           print("Slot number must be a whole number between 1 and "..invInput.getInventorySize())        
  132.         else
  133.           if invInput.getStackInSlot(numCheck) == nil then
  134.             print("No item in slot ".. numCheck)
  135.           else
  136.             if (invInput.getStackInSlot(numCheck).id ~= "fluxedCrystals:smoothShard"
  137.             and invInput.getStackInSlot(numCheck).id ~= "fluxedCrystals:roughShard") then
  138.               print("Item is not a smooth or rough shard from Fluxed Crystals!")
  139.             else
  140.               badInp = false
  141.               print("")
  142.             end
  143.           end
  144.         end
  145.       elseif usrInp ~= "back" and usrInp ~= "file" then
  146.         print("Invalid command!")
  147.       else
  148.         badInp = false
  149.         print("")
  150.       end -- if numCheck ~= nil then
  151.     end -- while badInp do
  152.    
  153.     if tonumber(usrInp) then
  154.       local slot = invInput.getStackInSlot(usrInp)
  155.       if(SeedTable[slot.dmg]) then -- seed in table already
  156.         while usrInp ~= "back" do
  157.           print("\nFound: [".. slot.dmg .."] name: " .. SeedTable[slot.dmg]["name"] .. ", needed: " .. SeedTable[slot.dmg]["needed"])
  158.           print("Name of item in selected slot: " .. slot.display_name)
  159.           print("\nItem already in Seed Table. Type \"delete\", \"edit_name\", \"edit_needed\" or \"back\": ")
  160.           usrInp = read()
  161.           if usrInp ~= "back" and usrInp ~= "edit_name" and usrInp ~= "edit_needed" and usrInp ~= "delete" then
  162.             term.write("Invalid command! Enter again: ")
  163.             usrInp = read()
  164.           elseif usrInp == "delete" then
  165.             SeedTable[slot.dmg] = nil
  166.             print("Deleted")
  167.             usrInp = "back"
  168.           elseif usrInp == "edit_name" then
  169.             term.write("Enter new name: ")
  170.             SeedTable[slot.dmg]["name"] = read()
  171.             print("Name changed")
  172.             usrInp = "back"
  173.           elseif usrInp == "edit_needed" then
  174.             term.write("Enter new amount needed: ")
  175.             usrInp = tonumber(read())
  176.             if (usrInp == nil) then
  177.               print("Input not a number!")
  178.             elseif (usrInp ~= math.floor(usrInp)) then
  179.               print("Decimal number not allowed!")
  180.             elseif (usrInp > 64 or usrInp < 1) then
  181.               print ("Needed amount needs to be atleast one but not more then 64 !")
  182.             else
  183.               SeedTable[slot.dmg]["needed"] = usrInp
  184.               print("Amount needed has been changed!")
  185.             end
  186.           end
  187.         end -- while usrInp ~= "back" do
  188.          print("Going back...")
  189.          sleep(3)
  190.          usrInp = "notBack"
  191.       else -- a seed, but not in table
  192.         print("Seed type not in table, type \"add\" or \"back\"")
  193.         print("Name of item in selected slot: " .. slot.display_name)
  194.         term.write("?: ")
  195.         usrInp = read()
  196.         while usrInp ~= "back" and usrInp ~= "add" do
  197.           term.write("invalid command! Enter again: ")
  198.           usrInp = read()
  199.         end
  200.         print("")
  201.         if usrInp == "add" then
  202.           print("What do you want to call this item? This will be displayed instead of its real name.")
  203.           print("Suggesting something short, eg. \"iron\" instead of \"Smooth Iron Shard\"")
  204.           term.write("Enter name: ")
  205.           local name1 = read()
  206.           print("How many \"".. name1 .. "\" is needed in the gem refiner for one refining operation? (a whole number in range 1-64)")
  207.           term.write("Enter amount needed: ")
  208.           local needed1 = tonumber(read())
  209.           if needed1 ~= math.floor(needed1) or needed1 < 1 or needed1 > 64 then
  210.             needed1  = nil
  211.           end
  212.           while needed1 == nil do
  213.             print("Invalid amount!\n Enter a whole number in range 1-64")
  214.             term.write("Enter amount: ")
  215.             needed1 = read()
  216.             if needed1 ~= math.floor(needed1) or needed1 < 1 or needed1 > 64 then
  217.               needed1  = nil
  218.             end
  219.           end
  220.           SeedTable[slot.dmg] = {name = name1, needed = tonumber(needed1)}
  221.           print("New seed added to seed table")
  222.           sleep(3)
  223.          
  224.         end
  225.         usrInp = "notBack"
  226.       end -- [else] if(SeedTable[slot.dmg]) then
  227.     end -- if tonumber(usrInp) then (else already fixed in input loop)
  228.    
  229.     if usrInp == "file" then
  230.       term.clear()
  231.      print("Making changes in SeedTable file may break the program! Be careful.")
  232.       print("The program will close then you have saved the changes and closed the file, changes will be applied next time the program starts")
  233.       print("The format for the table is as follows:")
  234.       print("[\'dmgValue\'] = {name = 'nameOnScreen', \"needed\" = 'nrItemsNeddedInRefiner'},")
  235.       print("\tPress enter to open file...")
  236.       read()
  237.       shell.run("edit FluxedSeeds_SeedTable")
  238.       error("Edit in seedTable, restart the program to apply changes!")
  239.     end
  240.    
  241.     if usrInp == "back" then
  242.       print("Saving changes to SeedTable file...")
  243.       local file = fs.open("FluxedSeeds_SeedTable", "w")
  244.       file.write(textutils.serialize(SeedTable))
  245.       file.close()
  246.       print("Saved")
  247.       SeedSetup = false
  248.     end
  249.   end -- while SeedSetup do
  250.  
  251.   --print("I'm getting bored making it possible to make changes from inside the program...")
  252.   --print("Make the changes on your own, heres the file\n\tpress enter to open...")
  253.   --read()
  254.   --shell.run("edit FluxedSeeds_SeedTable")
  255. end
  256.  
  257.  
  258.  
  259. function printStack(slot, result) -- prints whats in a specific slot, used by 'main'
  260.   if slot ~= nil then
  261.     if result == 0 then
  262.       print(slot.qty .." x " ..SeedTable[slot.dmg]["name"])
  263.     elseif result == 1 then
  264.       print(slot.qty .." x " ..SeedTable[slot.dmg]["name"]
  265.       .. "    moved " .. SeedTable[slot.dmg]["needed"])
  266.     else -- not supported item in chest or a shard not in SeedTable
  267.       if(term.isColor()) then term.setTextColor(colors.red)
  268.       else term.setTextColor(colors.lightGray) end
  269.       if (slot.id ~= "fluxedCrystals:smoothShard"
  270.       and slot.id ~= "fluxedCrystals:roughShard") then
  271.         print(slot.qty .. " x " .. slot.display_name .. " can't be processed!")
  272.       else
  273.         print(slot.qty .. " x " .. slot.display_name .. " not in table!")
  274.       end
  275.       term.setTextColor(colors.white)
  276.     end
  277.   end
  278. end
  279.  
  280.  
  281.  
  282. function printTableIds() -- prints the dmgValue and name of the items in SeedTable to be compared with the in-game seeds
  283.   local printed = 0
  284.   local tableSize = table.maxn(SeedTable)
  285.   print("\n\n")
  286.  
  287.   for i = 1, tableSize do
  288.     if printed == 10 and i ~= tableSize then
  289.       printed = 0
  290.       print("    press enter for more...")
  291.       read()
  292.       print("\n\n")
  293.     end
  294.     if pcall(function() return SeedTable[i]["needed"] end) then -- index exists
  295.       print("dmg: " .. i .. ", name: " .. SeedTable[i]["name"] .. "\t required: " .. SeedTable[i]["needed"])
  296.       printed = printed + 1
  297.     end
  298.   end
  299.   print("\nEverything printed, press enter to exit...")
  300.   read()
  301.   return
  302. end
  303.  
  304.  
  305.  
  306. function runningMenu() -- shows little menu then pressing any key in normal running mode
  307.   term.clear()
  308.   term.setCursorPos(2,1)
  309.   print("\t\tMENU")
  310.   print("\n\"setup\" for settings menu")
  311.   print("\"seeds\" for SeedTable menu")
  312.   print("\"about\" to show about text")
  313.   print("\"exit\" to terminate program (also ctrl+T)")
  314.   print("anything else continues program")
  315.   term.write("input: ")
  316.   local usrInp = read()
  317.   usrInp = string.lower(usrInp)
  318.   if usrInp == "setup" then setup() end
  319.   if usrInp == "seeds" then editSeedTable() end
  320.   if usrInp == "about" then about() end
  321.   if usrInp == "exit" then error("Program terminated by user") end
  322. end
  323.  
  324.  
  325.  
  326. function setupMain() -- used by setup
  327.   term.clear()
  328.   term.setCursorPos(1,1)
  329.   print("\tSetup menu")
  330.   print("Type 1-5, \"file\" or \"back\"")
  331.   term.write("1. Input")
  332.   term.setCursorPos(x - #settings.input - 2, 3)
  333.   term.write(settings.input .. " " .. testInventory(settings.input))
  334.   term.setCursorPos(1, 4)
  335.   term.write("2. Output")
  336.   term.setCursorPos(x - #settings.output - 2, 4)
  337.   term.write(settings.output .. " " .. testInventory(settings.output))
  338.   term.setCursorPos(1,5)
  339.   term.write("3. Output Type")
  340.   term.setCursorPos(x - #settings.outputType - 2, 5)
  341.   term.write(settings.outputType)
  342.   term.setCursorPos(1,6)
  343.   term.write("4. Output Direction")
  344.   term.setCursorPos(x - #settings.outputDir - 2, 6)
  345.   term.write(settings.outputDir)
  346.   term.setCursorPos(1, 7)
  347.   term.write("5. Sleep time (sec)")
  348.   term.setCursorPos(x - 3, 7)
  349.   term.write(settings.sleepTime)
  350.   term.setCursorPos(1, 8)
  351.   print("file. edit the settings file directly (only for advanced users)")
  352.   print("back. exit settings configuration")
  353.   term.write("Choose an alternative: ")
  354.   local errInp = true
  355.   while errInp do
  356.     usrInp = read()
  357.     if(usrInp ~= "1" and usrInp ~= "2" and usrInp ~= "3" and usrInp ~= "4"
  358.     and usrInp ~= "5" and usrInp ~= "file" and usrInp ~= "back") then
  359.       term.write("Bad input, try again: ")
  360.     else
  361.       errInp = false
  362.     end
  363.   end
  364.   return usrInp
  365. end
  366.  
  367.  
  368.  
  369. function setup() -- Changing the settings
  370.   if (settings.alwaysEditFile == "true") then
  371.     shell.run("edit FluxedSeeds_settings")
  372.     return
  373.   end
  374.   local usrInp
  375.   local inSetup = true
  376.   while inSetup do
  377.     usrInp = setupMain()
  378.     term.clear()
  379.     term.setCursorPos(1,2)  
  380.    
  381.     if (usrInp == "1") then
  382.       while usrInp ~= "back" do
  383.         term.setCursorPos(1,2)
  384.         term.write("1. Input")
  385.         local strLen = tonumber(string.len(settings.input))
  386.         term.setCursorPos((x - strLen - 2), 2)
  387.         term.write(settings.input)
  388.         term.setCursorPos(1,4)
  389.         if testInventory(settings.input) == "!" then
  390.           print ("THE CURRENT INVENTORY COULD NOT BE ACCESSED!!!")
  391.         end
  392.         local test1, test2 = peripheral.find("chest")
  393.         if(test1 == nil) then
  394.           print("No chest found on network, if you have a special type of chest you can enter its network name below")
  395.         elseif(test2 == nil) then
  396.           print("Only one normal chest found on network, type \"find\" or its name to set this one to input chest")
  397.           print("Found chest: \""..test1.."\"")
  398.         end
  399.         print("Enter \"back\" or the network name of the input inventory (eg. \"chest_0\"): ")
  400.         usrInp = read()
  401.         if (usrInp ~= "find" and usrInp ~= "back") then
  402.           local testRes = testInventory(usrInp)
  403.           if testRes == "!" then
  404.             print("Cant connect to that inventory!")
  405.             print("...")
  406.             read()
  407.           else
  408.             print("Inventory " .. usrInp .. " is OK, saving it!")
  409.             settings.input = usrInp
  410.             usrInp = "back"
  411.           end
  412.           if usrInp == "find" then
  413.             settings.input = test1
  414.             if not settings.outputType == "refiner" and test2 == nil then
  415.               print("Changing output type to refiner beacuse only one chest was found, this is easy to change if needed")
  416.               settings.outputType = "refiner"
  417.             end
  418.             usrInp = "back"
  419.           end
  420.         end
  421.         sleep(3)
  422.         term.clear()
  423.       end -- while usrInp ~= "back" do
  424.       usrInp = "1"
  425.     end -- if usrInp == "1" then
  426.    
  427.    
  428.     if (usrInp == "2") then
  429.       while usrInp ~= "back" do
  430.         term.setCursorPos(1,2)
  431.         term.write("2. Output")
  432.         local strLen = tonumber(string.len(settings.output))
  433.         term.setCursorPos(x - strLen - 2, 2)
  434.         term.write(settings.output .. " " .. testInventory(settings.output))
  435.         term.setCursorPos(1,4)
  436.         print("It's recommended to look at \"Output Type\" (alt 3) before changing \"Output\" (this one)")
  437.         if testInventory(settings.output) == "!" then
  438.           print ("THE CURRENT INVENTORY COULD NOT BE ACCESSED!!!")
  439.         end
  440.         local test1, test2 = peripheral.find("gem_refiner")
  441.         if test1 and not test2 then
  442.           print("one gem refiner was found on the network, type \"find\" or its name to set it as output")
  443.           print("found refiner: \""..test1.."\"")
  444.         end
  445.         print("Enter \"back\" or the network name of the output inventory, a chest or a gem refiner (eg. \"chest_1\" or \"gem_refiner_0\")")
  446.         usrInp = read()
  447.         if (usrInp ~= "find" and usrInp ~= "back") then
  448.           local testRes = testInventory(usrInp)
  449.           if testRes == "!" then
  450.             print("Cant connect to that inventory!")
  451.             print("...")
  452.             read()
  453.           else
  454.             print("Inventory " .. usrInp .. " is OK, saving it!")
  455.             settings.output = usrInp
  456.             usrInp = "back"
  457.           end
  458.         elseif usrInp == "find" then
  459.           print("Finding a output, assuming input is configured to be a connected chest on this network...")
  460.           if test1 then
  461.             settings.output = test1
  462.             print ("Found and saved " .. test1)
  463.             settings.outputType = "refiner"
  464.             usrInp = "back"
  465.           else
  466.             local ch1, ch2, ch3 = peripheral.find("chest")
  467.             if ch3 then
  468.               print("More then one possible output chest found, can not choose automaticly")
  469.             elseif ch2 then
  470.               if ch1 == settings.input then
  471.                 settings.output = ch2
  472.                 print("Found and saved ".. ch2)
  473.                 settings.outputType = "chest"
  474.                 usrInp = "back"
  475.               end
  476.               if ch2 == settings.input then
  477.                 settings.output = ch1
  478.                 print("Found and saved ".. ch1)
  479.                 settings.outputType = "chest"
  480.                 usrInp = "back"
  481.               end
  482.             end
  483.           end
  484.           if not usrInp == "back" then
  485.             print("More then one possible output chest found, can not choose automaticly")
  486.           end
  487.         end
  488.         sleep(3)
  489.         term.clear()
  490.       end -- while usrInp ~= "back" do
  491.       usrInp = "2"
  492.     end -- if (usrInp == "2") then
  493.    
  494.     if usrInp == "3" then
  495.       while usrInp ~= "back" do
  496.         term.setCursorPos(1,2)
  497.         term.write("3. Output Type")
  498.         local strLen = tonumber(string.len(settings.outputType))
  499.         term.setCursorPos(x - strLen - 2, 2)
  500.         term.write(settings.outputType)
  501.         term.setCursorPos(1,4)
  502.         print("type \"back\" to go back or select type of output inventory, \"refiner\" or \"chest\": ")
  503.         usrInp = read()
  504.         if usrInp == "refiner" then
  505.           print("Setting type of output inventory to refiner")
  506.           settings.outputType = "refiner"
  507.           usrInp = "back"
  508.         elseif usrInp == "chest" then
  509.           print("Setting type of output inventory to chest")
  510.           settings.outputType = "chest"
  511.           usrInp = "back"
  512.         elseif usrInp ~= "back" then
  513.           print("Invalid choice!")
  514.         end
  515.         sleep(3)
  516.         term.clear()
  517.       end -- while usrInp ~= "back" do
  518.       usrInp = "3"
  519.     end -- if usrInp == "3" then
  520.    
  521.     if usrInp == "4" then
  522.       while usrInp ~= "back" do
  523.         term.setCursorPos(1,2)
  524.         term.write("4. Output Direction")
  525.         local strLen = tonumber(string.len(settings.outputDir))
  526.         term.setCursorPos(x - strLen - 2, 2)
  527.         term.write(settings.outputDir)
  528.         term.setCursorPos(1, 4)
  529.         print ("Looking from INPUT, in witch direction is OUTPUT?")
  530.         print ("Type: \"down\", \"up\", \"north\", \"south\", \"west\", \"east\" or \"back\"")
  531.         usrInp = read()
  532.         if (usrInp == "down" or usrInp == "up" or usrInp == "north"
  533.         or usrInp == "south" or usrInp == "west" or usrInp == "east") then
  534.           settings.outputDir = usrInp
  535.           print("Output Direction set to "..usrInp)
  536.           usrInp = "back"
  537.         elseif usrInp ~= "back" then
  538.           print("Not a valid direction!")
  539.         end
  540.         sleep(3)
  541.         term.clear()
  542.       end -- while usrInp ~= "back" do
  543.       usrInp = "4"
  544.     end -- usrInp == "4" then
  545.    
  546.     if usrInp == "5" then
  547.       while usrInp ~= "back" do
  548.         term.setCursorPos(1,2)
  549.         term.write("5. Sleep time (sec)")
  550.         local strLen = string.len(tostring(settings.sleepTime))
  551.         term.setCursorPos(x - strLen - 2, 2)
  552.         term.write(settings.sleepTime)
  553.         term.setCursorPos(1, 4)
  554.         print("The time to wait (in seconds) between checks of the content in input chest (default 2)")
  555.         term.write("Enter number of seconds to wait or \"back\": ")
  556.         usrInp = read()
  557.         if tonumber(usrInp) and tonumber(usrInp) > 0 then
  558.           settings.sleepTime = tonumber(usrInp)
  559.           print("waiting time changed to "..usrInp)
  560.           usrInp = "back"
  561.         elseif usrInp ~= "back" then
  562.           print("Input not a number or smaller then zero")
  563.         end
  564.         sleep(3)
  565.         term.clear()
  566.       end -- while usrInp ~= "back" do
  567.       usrInp = "5"
  568.     end -- if usrInp == "5" then
  569.    
  570.     if usrInp == "file" then
  571.       print("Making changes in settings file may break the program! Be careful.")
  572.       print("The program will close then you have saved the changes and closed the file, changes will be applied next time the program starts")
  573.       print("\tPress enter to open file...")
  574.       read()
  575.       shell.run("edit FluxedSeeds_settings")
  576.       error("\nTerminated to reload settings, please restart the program")
  577.     end
  578.    
  579.     if usrInp == "back" then
  580.       print("Saving changes to settings file...")
  581.       local file = fs.open("FluxedSeeds_settings", "w")
  582.       file.write(textutils.serialize(settings))
  583.       file.close()
  584.       print("saved!")
  585.       sleep(3)
  586.       inSetup = false
  587.     end
  588.   end -- while inSetup do
  589.   return
  590. end -- function setup()
  591.  
  592.  
  593.  
  594. function testInventory(inv) -- used by testMovingItems
  595.   if peripheral.isPresent(inv) then return "" else return "!" end
  596. end
  597.  
  598.  
  599.  
  600. function testMovingItems() --Tests that the inventories are next to each other
  601.   print("A test will be performed to verify that the configuration of input and output inventory is correctly made. Make sure to turn off everything that may add or remove items in this inventories!\n--Press any key then ready--")
  602.   os.pullEvent("key")
  603.   invInput.condenseItems() -- Sorts the chest
  604.   sleep(0.1)
  605.   if settings.output then
  606.     invOutput.condenseItems()
  607.     sleep(0.1)
  608.     if (invInput.getStackInSlot(1) ~= nil
  609.     and invOutput.getStackInSlot(invOutput.getInventorySize()) == nil) then --Chest not empty and other not full
  610.       local pushedItem = invInput.getStackInSlot(1) -- saves info about the stack pushing from    
  611.       local nrPushed
  612.       local status, err = pcall( function () nrPushed = invInput.pushItemIntoSlot(settings.outputDir, 1, 1, invOutput.getInventorySize()) end )--pushing
  613.       sleep(0.1)
  614.       if (err) then
  615.         error("\nNo Inventory in specified direction off input inventory!\nCheck input chest name, direction or try swapping input/output inventory names")
  616.       end
  617.       if (nrPushed == 1) then -- push successful
  618.         local recieveditem = invOutput.getStackInSlot(invOutput.getInventorySize()) --gets recieved item
  619.         if (recieveditem.display_name == pushedItem.display_name) then --compares
  620.           outInvType = "chest"
  621.           invInput.pullItemIntoSlot(settings.outputDir,
  622.             invOutput.getInventorySize(), 1, 1) -- Pull it back
  623.           sleep(0.1)
  624.           if (invInput.getStackInSlot(1).qty == pushedItem.qty
  625.           and invInput.getStackInSlot(1).display_name == pushedItem.display_name) then
  626.             -- The item got back to input chest again! All OK!
  627.             print("\n\nNo problems with your setup found!\n--Continues in 5 seconds--")
  628.             sleep(5)
  629.             return
  630.           else
  631.             error("Test successful, but failed to return test item to input chest!")
  632.           end
  633.         else
  634.           error("\nItem pushed but something else found in target inventory!\nTried to push one "
  635.                 .. pushedItem.display_name .. "\nbut found instead " .. recieveditem.display_name ..
  636.                 "\nReson: Outside disturbance")
  637.         end -- if (recieveditem.display_name == pushedItem.display_name) then --compares
  638.       else -- if (nrPushed == 1) --> nrPushed = 0
  639.         error("\nSpace in target inventory was available but could not move anything!"..
  640.               "\nTried to move: " .. pushedItem.display_name .. "\nReson: probably wrong name on input or output chest")
  641.       end -- if (nrPushed == 1)
  642.     else
  643.       error("\nInput chest is empty or output chest is full, can't continue test!")
  644.     end -- if (invInput.getStackInSlot(1) ~= nil --Chest not empty and other not full
  645.   else -- Test as a refiner
  646.     if gemRef.getStackInSlot(1) == nil then --test move to refiner
  647.       local pushedItem = invInput.getStackInSlot(1)
  648.       if (pushedItem ~= nil) then
  649.         local nrPushed = invInput.pushItemIntoSlot(settings.outputDir, 1, 1, 1)
  650.         sleep(0.1)
  651.         if (nrPushed == 1) then
  652.           local recieveditem = gemRef.getStackInSlot(1)
  653.           if(recieveditem ~= nil and recieveditem.display_name == pushedItem.display_name) then
  654.             outInvType = "refiner"
  655.             invInput.pullItemIntoSlot(settings.outputDir, 1, 1, 1)
  656.             sleep(0.1)
  657.             if (invInput.getStackInSlot(1).qty == pushedItem.qty
  658.           and invInput.getStackInSlot(1).display_name == pushedItem.display_name) then
  659.               -- Test successful
  660.               print("\n\nNo problems with your setup found!\n--Continues in 5 seconds--")
  661.               sleep(5)
  662.               return
  663.             else
  664.               error("Test successful, but failed to return test item to input chest!")
  665.             end
  666.           else
  667.             if (recieveditem == nil) then
  668.               error("\nItem pushed but not recieved by 'refiner'\nTried to move: "..pushedItem.display_name..
  669.                     "\nReson: Pushed to something else (check output chest direction and source inventory)!")
  670.             else -- pushed item ~= recieved item
  671.               error("\nItem pushed,  but found something else in target inventory!\nTried to push one "
  672.                 .. pushedItem.display_name .. "\nbut found instead " .. recieveditem.display_name .."\nReson: Outside disturbance")
  673.             end
  674.           end
  675.         else-- if (nrPushed == 1) --> nrPushed = 0
  676.           error("\nSpace in target inventory was available but could not move anything!"..
  677.               "\nTried to move: " .. pushedItem.display_name .. "\nReson: probably wrong input/output or item not a smooth shard!")
  678.         end -- if (nrPushed == 1)
  679.       else
  680.         error("\nCan't test! No items in input chest to test on or testing on another chest, check name!")
  681.       end --if (pushedItem ~= nil)
  682.     else
  683.       error("\nGem Refiner is not empty! Please move the shards to input chest and try again!")
  684.     end -- if gemRef.getStackInSlot(1) == nil
  685.   end -- if settings.output
  686. end -- function testMovingItems()
  687.  
  688.  
  689.  
  690. function quickSettingsCheck()
  691.   local invalidChestSetup = false
  692.   if not pcall(function() return settings.input end) then
  693.     print("Could not find the input inventory on the network!")
  694.     invalidChestSetup = true
  695.   elseif not peripheral.isPresent(settings.input) then
  696.     print("Could not find the input inventory on the network!")
  697.     invalidChestSetup = true
  698.   end
  699.   if not pcall(function() return settings.output end) then
  700.     print("Could not find the output inventory on the network!")
  701.     invalidChestSetup = true
  702.   elseif not peripheral.isPresent(settings.output) then
  703.     print("Could not find the output inventory on the network!")
  704.     invalidChestSetup = true
  705.   end
  706.   if not pcall(function() return settings.outputType end) then
  707.     print("Error in settings file, invalid outputType")
  708.     invalidChestSetup = true  
  709.   elseif (settings.outputType ~= "chest" and settings.outputType ~= "refiner") then
  710.     print("Error in settings file, invalid outputType")
  711.     invalidChestSetup = true  
  712.   end
  713.   if not pcall(function() return settings.sleepTime end) then
  714.     print("Error in settings file, Invalid sleepTime!")
  715.     invalidChestSetup = true
  716.   elseif not tonumber(settings.sleepTime) then
  717.     print("Error in settings file, Invalid sleepTime!")
  718.     invalidChestSetup = true
  719.   elseif settings.sleepTime < 0 then
  720.       print("Error in settings file, Invalid sleepTime!")
  721.     invalidChestSetup = true
  722.   end
  723.   if not pcall(function() return settings.outputDir end) then
  724.     print("Error in settings file, Invalid output direction!")
  725.     invalidChestSetup = true  
  726.   elseif(settings.outputDir ~= "up" and settings.outputDir ~= "down" and settings.outputDir ~= "north"
  727.   and settings.outputDir ~= "south" and settings.outputDir ~= "west" and settings.outputDir ~= "east") then
  728.     print("Error in settings file, Invalid output direction!")
  729.     invalidChestSetup = true
  730.   end
  731.  
  732.   if not pcall(function() return settings.alwaysEditFile end) then
  733.     settings.alwaysEditFile = "false"
  734.   end
  735.  
  736.   if invalidChestSetup then
  737.     print("Redirecting to setup, press enter...")
  738.     read()
  739.     setup()
  740.   end
  741. end
  742.  
  743.  
  744. --
  745. ----------------------------------
  746. -- Startup point
  747.  
  748.  
  749.  
  750. for i, v in ipairs(args) do
  751.   args[i] = v
  752. end
  753.  
  754. if (fs.exists("FluxedSeeds_settings")) then -- loads settings, checks for errors in settings and creates default settings
  755.   local settingsFile = fs.open("FluxedSeeds_settings", "r")
  756.   settings = textutils.unserialize(settingsFile.readAll())
  757.   -- print(textutils.serialize(settings)) -- Debug code
  758.   settingsFile.close()
  759.   quickSettingsCheck()
  760. else
  761.   print("No settings file found, generating default...")
  762.   settings = {
  763.     outputDir = "down",
  764.     input = "chest_0",
  765.     output = "gem_refiner_0",
  766.     outputType = "refiner",
  767.     sleepTime = 2,
  768.     alwaysEditFile = "false",
  769.   }
  770.   local settingsFile = fs.open("FluxedSeeds_settings", "w")
  771.   settingsFile.write(textutils.serialize(settings))
  772.   settingsFile.close()
  773.   print("Press enter to start configuration...")
  774.   read()
  775.   setup()
  776. end
  777. -- should not be like this...
  778. invInput = peripheral.wrap(settings.input)
  779. if not invInput then error("Could not wrap input inventory as a peripheral, check your cables and program settings.") end
  780. invOutput = peripheral.wrap(settings.output)
  781. if not invOutput then error("Could not wrap output inventory as a peripheral, check your cables and program settings.") end
  782.  
  783. if (fs.exists("FluxedSeeds_SeedTable")) then -- loads SeedTable, creates empty then none and can download default
  784.   local seedFile = fs.open("FluxedSeeds_SeedTable", "r")
  785.   local seedData = seedFile.readAll()
  786.   SeedTable = textutils.unserialize(seedData)
  787.   seedFile.close()
  788. else
  789.   -- Download from gitHub?
  790.   function getLatestDefaultSeedConfigs()
  791.     print("No seed information found on your computer.\n")
  792.     print("Do you want to download the newest seed files from the creators gitHub or maybe a custom one?")
  793.     print("\nThis is recomended if your config file and default are the same or only slightly different or if your pack has a own seed file with a public link")
  794.     print("This requires http to be enabled in computercraft config (enabled as default).")
  795.     print("\nSeed information can be manually changed, added and removed by running the program with the argument \"SeedTable\"\n? ")
  796.     local badAns = true, answer
  797.     while badAns do
  798.       answer = read()
  799.       if string.lower(answer) == "yes" or answer == "no" then badAns = false
  800.       else print ("Download seed information? Answer with \"yes\" or \"no\": ") end
  801.     end
  802.     if answer == "yes" then
  803.       os.loadAPI("json")
  804.       if not json then
  805.         print("A needed api is not found!")
  806.         term.write("Download Json API by ElvishJerricco?: ")
  807.         badAns = true
  808.         while badAns do
  809.           answer = read()
  810.           if string.lower(answer) == "yes" or answer == "no" then badAns = false
  811.           else term.write ("Answer with \"yes\" or \"no\": ") end
  812.         end
  813.         if answer == "yes" then
  814.           print("\nDownloading json API...")
  815.           shell.run("pastebin get 4nRg9CHU json")
  816.           os.loadAPI("json")
  817.           print("json API has been downloaded")
  818.         else
  819.           print("API will not be downloaded, will not be able to download information. Creating empty seed table to be filled manually.")
  820.           local seedFile = fs.open("FluxedSeeds_SeedTable", "w")
  821.           seedFile.write("{}")
  822.           seedFile.close()
  823.           return
  824.         end        
  825.       end
  826.       print("\n\nWhat do you want to download?\n1. Latest GitHub version from the creator\n2. A custom file from pastebin \n3. A custom file from somewhere else")
  827.       print("4. Nothing, terminate program")
  828.       term.write("input your choise: ")
  829.       badAns = true
  830.       while badAns do
  831.         answer = tonumber(read())
  832.         if answer then
  833.           if answer < 1 or answer > 4 or answer ~= math.floor(answer) then
  834.             print("invalid input")
  835.           else
  836.             badAns = false
  837.           end
  838.         else
  839.           print("Invalid input")
  840.         end
  841.       end
  842.      
  843.       local onlineSeeds = nil
  844.       if answer == 1 then
  845.         print("Downloading default...")
  846.         onlineSeeds = http.get("https://raw.githubusercontent.com/jaredlll08/Fluxed-Crystals-Reborn/master/src/main/resources/assets/fluxedcrystals/misc/crystal.json")
  847.         if not onlineSeeds then
  848.           print ("\nDownload failed!")
  849.           print("Try again later or skip this and add manually")
  850.           return
  851.         else
  852.           print("Download Finished!")
  853.         end  
  854.       elseif answer == 2 then
  855.         print("\n\tDownload from pastebin.\nNote that the file needs to look like the one in your config-folder (name, seedId, refinerAmount)")
  856.         term.write("Enter pastebin code: ")
  857.         local code = read()
  858.         print("Trying to download paste...")
  859.         onlineSeeds = http.get("http://pastebin.com/raw/"..code)
  860.         if not onlineSeeds then
  861.           print("\nDownload failed!")
  862.           print("Check your pastebin code")
  863.         end
  864.       elseif answer == 3 then
  865.         print("\n\tDownload from somewhere else\nNote that the file needs to look like the one in your config-folder (name, seedId, refinerAmount)")
  866.         print("The link needs to point to raw text, check your URL twice, no checks for where the link points will be made!")
  867.         term.write("Enter full url to seed file: ")
  868.         local url = read()
  869.         print("Connecting to " .. url .. "...")
  870.         onlineSeeds = http.get(url)
  871.         if not onlineSeeds then
  872.           print("Download failed!")
  873.           print("Did you double-check the url?")
  874.         end
  875.       elseif answer == 4 then
  876.         error("Terminated by user")
  877.       else
  878.         error("Unknown alternative selected")
  879.       end
  880.      
  881.       print("Processing retrieved data...")
  882.       onlineSeeds = onlineSeeds.readAll()
  883.       onlineSeeds = json.decode(onlineSeeds)
  884.       for i = 1, table.getn(onlineSeeds.seeds) do
  885.         local status, err = pcall(function () SeedTable[onlineSeeds.seeds[i]["seedID"]] = {name = onlineSeeds.seeds[i]["name"], needed = onlineSeeds.seeds[i]["refinerAmount"]} end)
  886.         if not status then
  887.           error("error processing row " .. i .. " of " ..  table.getn(onlineSeeds.seeds) .. " in downloaded seedtable, error msg: "..err)
  888.         end
  889.       end
  890.      
  891.       local seedFile = fs.open("FluxedSeeds_SeedTable", "w")
  892.       seedFile.write(textutils.serialize(SeedTable))
  893.       seedFile.close()
  894.       print("SeedTable created, saved and ready to be used")
  895.       print("press enter to exit")
  896.       read()
  897.       return
  898.     else
  899.       print("Don't dowloading\nCreating empty seed table to be filled manually")
  900.       local seedFile = fs.open("FluxedSeeds_SeedTable", "w")
  901.       seedFile.write("{}")
  902.       seedFile.close()
  903.       sleep(3)
  904.       return
  905.     end
  906.   end
  907.   getLatestDefaultSeedConfigs()
  908. end
  909.  
  910.  
  911.  
  912. if settings.outputType == "chest" then
  913.   nrOutSlots = invOutput.getInventorySize()
  914. end
  915. nrInpSlots = invInput.getInventorySize()  -- two variables for the same thing? Don't want to look into that now....
  916. inpChestSlots = invInput.getInventorySize()
  917.  
  918.  
  919.  
  920.   if (#args>1) then
  921.     print("FluxedSeeds takes one argument on startup, not more")
  922.     args[1] = "nil"
  923.   end
  924. if args[1] == "start" then args[1] = ":)"
  925. elseif (args[1] == "dmgValues") then printDmgValues = true
  926. elseif (args[1] == "setup") then setup()
  927. elseif (args[1] == "testSetup") then testMovingItems()
  928. elseif (args[1] == "SeedTable") then editSeedTable()
  929. elseif (args[1] == "about") then about(); return
  930. elseif (args[1] == "showTable") then printTableIds(); return
  931. else
  932.   term.clear()
  933.   print("Following program arguments are available:")
  934.   print("  \"start\" starts the program")
  935.   print("  \"dmgValues\" prints the name and damage value on all items in input inventory")
  936.   print("  \"setup\" configure input/output etc")
  937.   print("  \"testSetup\" will try to help you if input and output is not configured correctly")
  938.   print("  \"SeedTable\" will take you to a menu for editing the SeedTable")
  939.   print("  \"about\" will show more details about the program, how its used as well as credits")
  940.   print("  \"showTable\" prints ID, number of items needed and name of items in SeedTable")
  941.   return -1
  942. end
  943.  
  944. if(printDmgValues) --triggered by one argument
  945. then
  946.   local allEmpty = true
  947.   for i=1, inpChestSlots, 1 do
  948.     local slot = invInput.getStackInSlot(i)
  949.     if(slot) then
  950.       print(slot.display_name.. " has dmg value ".. slot.dmg)
  951.       allEmpty = false
  952.     end
  953.   end
  954.   if allEmpty then print("No items in inventory") end
  955.   return
  956. end
  957.  
  958.  
  959.  
  960.  
  961. ----------------------------------------------------------
  962. --      'main'    It's here the program normally runs
  963.  
  964. local result
  965. local allowMove
  966. print("\n\nStarting in ".. settings.sleepTime .. " second(s)")
  967. local event, timerIdOrKey, myTimerID, blocked = true
  968. while true do
  969.   blocked = true
  970.   myTimerID = os.startTimer(settings.sleepTime)
  971.   while blocked do
  972.     event, timerIdOrKey = os.pullEvent()
  973.     if (event == "timer" and myTimerID == timerIdOrKey) then blocked = false end
  974.     if(event == "key") then
  975.       runningMenu()
  976.       blocked = false
  977.     end
  978.   end
  979.   invInput.condenseItems()
  980.   if (settings.outputType == "chest") then invOutput.condenseItems() end
  981.   term.clear()
  982.   term.setCursorPos(1,1)
  983.   local i = 1
  984.   allowMove = true
  985.   for i = 1, inpChestSlots, 1 do
  986.     slot = invInput.getStackInSlot(i)
  987.     if slot and allowMove then
  988.       result = checkMove(slot, i)
  989.       if result == -1 then
  990.         allowMove = false
  991.         result = 0
  992.       end
  993.     end
  994.     printStack(slot, result)
  995.   end
  996. end -- End of program loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement