fishermedders

Item Stocker

Jun 26th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. --Functions
  2.  
  3. function fResetTerminal()
  4.   term.clear()
  5.   term.setCursorPos(1, 1)
  6. end
  7.  
  8. function fGetConfig()
  9.   if not fs.exists("items.tbl") then
  10.     tFile = fs.open("items.tbl", "w")
  11.     tFile.write("{}")
  12.     tFile.close()
  13.   end
  14.   tFile = fs.open("items.tbl", "r")
  15.   tConfig = textutils.unserialize(tFile.readAll())
  16.   tFile.close()
  17. end
  18.  
  19. function fSaveConfig()
  20.   tFile = fs.open("items.tbl", "w")
  21.   tFile.write(textutils.serialize(tConfig))
  22.   tFile.close()
  23. end
  24.  
  25. function fGetItemsFromName(sName)
  26.   tAvailable = p.listAvailableItems()
  27.   tReturn = {}
  28.   for i = 1,#tAvailable do
  29.     if tAvailable[i].isCraftable then
  30.       tCurrentItem = {["name"]=tAvailable[i].name,["damage"]=tAvailable[i].damage}
  31.       --print(textutils.serialize(tCurrentItem))
  32.       tCurrentItemObject = p.findItem(tCurrentItem)
  33.       tMeta = tCurrentItemObject.getMetadata()
  34.       print(tMeta.displayName)
  35.       if string.match(string.lower(tMeta.displayName), string.lower(sName)) then
  36.         print("MATCH")
  37.         table.insert(tReturn, tMeta)
  38.       end
  39.     end
  40.   end
  41.   return tReturn
  42. end
  43.  
  44. function fGetItemFromMeta(sName, iDamage)
  45.   tAvailable = p.listAvailableItems()
  46.   for i = 1,#tAvailable do
  47.     if tAvailable[i].name == sName and tAvailable[i].damage == iDamage then
  48.       return tAvailable[i]
  49.     end
  50.   end
  51.   return nil
  52. end
  53.  
  54. function fProcessorAvailable()
  55.   tCpus = p.getCraftingCPUs()
  56.   for i = 1,#tCpus do
  57.     if tCpus[i].busy == false then
  58.       return true
  59.     end
  60.   end
  61.   return false
  62. end
  63.  
  64. --Main Loops
  65.  
  66. function fUserInput()
  67.   while true do
  68.   fResetTerminal()
  69.   print("AE2 Autocrafting Stock Program by Fisher")
  70.   print("The Crafting daemon is running in the background of this terminal. This interface is simply to edit what is crafted.")
  71.   print("")
  72.   print("Type a command to start")
  73.   print("1. list")
  74.   print("2. add")
  75.   print("3. remove")
  76.   sInput = read()
  77.   if sInput == "list" then
  78.   end
  79.   if sInput == "add" then
  80.     print("Adding Item.")
  81.     print("Type the namespaced item ID (minecraft:stone)")
  82.     sName = read()
  83.     repeat
  84.       print("Type the damage value of the item (0)")
  85.       sDamage = read()
  86.       if not tonumber(sDamage) then
  87.         print("That is not a number!")
  88.       end
  89.     until(tonumber(sDamage))
  90.     if fGetItemFromMeta(sName, tonumber(sDamage)) then
  91.       tItem = fGetItemFromMeta(sName, tonumber(sDamage))
  92.       if tItem.isCraftable then
  93.         tItemMeta = p.findItem(tItem).getMetadata()
  94.         repeat
  95.           print("How many \"" .. tItemMeta.displayName .. "\" do you wish to keep in stock?")
  96.           sAmount = read()
  97.           if not tonumber(sAmount) then
  98.             print("That is not a number!")
  99.           end
  100.         until(tonumber(sAmount))
  101.         print("Keeping " .. sAmount .. " of \"" .. tItemMeta.displayName .. "\" in stock.")
  102.         tItemMeta["stockAmount"] = tonumber(sAmount)
  103.         table.insert(tConfig, tItemMeta)
  104.         fSaveConfig()
  105.         print("Hit Enter to return to the main menu.")
  106.         read()
  107.       else
  108.         print("This item isn't craftable in your system (no pattern)")
  109.       end
  110.     else
  111.       print("This item isn't in your system at all.")
  112.       read()
  113.     end
  114.   end
  115.   end
  116. end
  117.  
  118. function fCraftingDaemon()
  119.   while true do
  120.     for i = 1,#tConfig do
  121.       if tJobs[tConfig[i].name .. "-" .. tConfig[i].damage] ~= null then
  122.         if tJobs[tConfig[i].name .. "-" .. tConfig[i].damage].isFinished() then
  123.           tJobs[tConfig[i].name .. "-" .. tConfig[i].damage] = nil
  124.         end
  125.       end
  126.       if p.findItem(tConfig[i]).getMetadata().count < tConfig[i].stockAmount then
  127.         if tJobs[tConfig[i].name .. "-" .. tConfig[i].damage] == null then
  128.           if fProcessorAvailable() then
  129.             tCraftingObject = p.findItem(tConfig[i]).craft(1)
  130.             tJobs[tConfig[i].name .. "-" .. tConfig[i].damage] = tCraftingObject
  131.             tSize = {term.getSize()}
  132.             tPrevPos = {term.getCursorPos()}
  133.             term.setCursorPos(1,tSize[2])
  134.             if iColor == colors.gray then
  135.               iColor = colors.lightGray
  136.             else
  137.               iColor = colors.gray
  138.             end
  139.             term.setTextColor(iColor)
  140.             term.write("Crafting 1 " .. tConfig[i].displayName .. ".                                                          ")
  141.             term.setTextColor(colors.white)
  142.             term.setCursorPos(tPrevPos[1], tPrevPos[2])
  143.           end
  144.         end
  145.       end
  146.     end
  147.     sleep(0.1)
  148.   end
  149. end
  150.  
  151. --The Main loop
  152.  
  153. fGetConfig()
  154. p = peripheral.wrap("right")
  155. tJobs = {}
  156. iColor = colors.gray
  157. while true do
  158.   parallel.waitForAll(fUserInput, fCraftingDaemon)
  159. end
Add Comment
Please, Sign In to add comment