Advertisement
remie92

Amazing automatic storage system

Jul 16th, 2024
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 KB | None | 0 0
  1. --imgur : https://imgur.com/a/dVXnwfh
  2.  
  3. monitor = peripheral.wrap("monitor_0")
  4.  
  5. inputName="minecraft:chest_15"
  6. outputName="minecraft:chest_14"
  7.  
  8. preCountPull=true
  9.  
  10.  
  11. inputChest=peripheral.wrap(inputName)
  12. outputChest=peripheral.wrap(outputName)
  13. storageChests={}
  14.  
  15. storage={}
  16.  
  17.  
  18. function main()
  19.     setupStorageChests()
  20.     wrong=checkError()
  21.  
  22.     compileData()
  23.  
  24.    
  25.     if(wrong~=nil) then
  26.         return wrong
  27.     end
  28.     monitorMain()
  29.  
  30.     while true do
  31.         local event, button, x, y = os.pullEvent( "monitor_touch" )
  32.         if(x<11 and y<3) then
  33.             push()
  34.         end
  35.         if(x>19 and y<3) then
  36.             pull()
  37.         end
  38.     end
  39.  
  40. end
  41.  
  42. function compileData()
  43.     storage={}
  44.     for i, v in ipairs(storageChests) do
  45.         table.insert(storage,i,v.list())
  46.     end
  47.     --save(storage,"data")
  48.     print("Reset compiled data")
  49. end
  50.  
  51.  
  52. function setupStorageChests()
  53.     storageChestsNames=peripheral.getNames()
  54.  
  55. for i, v in ipairs(storageChestsNames) do
  56.     if(v=="bottom") then
  57.         table.remove(storageChestsNames,i)
  58.     end
  59.   end
  60.   for i, v in ipairs(storageChestsNames) do
  61.     if(v=="top") then
  62.         table.remove(storageChestsNames,i)
  63.     end
  64.   end
  65.   for i, v in ipairs(storageChestsNames) do
  66.     if(v=="monitor_0") then
  67.         table.remove(storageChestsNames,i)
  68.     end
  69.   end
  70.   for i, v in ipairs(storageChestsNames) do
  71.     if(v==inputName) then
  72.         table.remove(storageChestsNames,i)
  73.     end
  74.   end
  75.   for i, v in ipairs(storageChestsNames) do
  76.     if(v==outputName) then
  77.         table.remove(storageChestsNames,i)
  78.     end
  79.   end
  80.  
  81.  
  82.   for i, v in ipairs(storageChestsNames) do
  83.         table.insert(storageChests,peripheral.wrap(v))
  84.   end
  85.  
  86. end
  87.  
  88.  
  89. function push()
  90.     print("Starting to push")
  91.     for i, v in pairs(inputChest.list()) do
  92.         --print(i, v.name)
  93.         index=findSlot()
  94.         print("Storing ",v.count,v.name," in chest",index)
  95.         storageChests[index].pullItems(peripheral.getName(inputChest),i)
  96.         table.insert(storage[index],v)
  97.         --inputChest.pushItems(peripheral.getName( storageChests[index]),i,64,slot)
  98.       end
  99.       print("Pushed everything")
  100.       compileData()
  101. end
  102. function pull()
  103.     print("Type name of item, for example: iron_ingot. don't use any prefixes like minecraft:iron_ingot")
  104.     name=read()
  105.  
  106.     if preCountPull then
  107.         counter=0
  108.         for i, v in ipairs(storage) do  --for every chest
  109.             checkChest=storage[i]
  110.             for index=1,54 do           --for every slot
  111.                 item=storage[i][index]
  112.                 if(item~=nil) then  --if this passes, we have a slot containing a item
  113.                     if name == remove_prefix(item.name) then --if this passes, we have the slot with the correct item!
  114.                         counter=counter+item.count
  115.                     end
  116.                    
  117.                 end
  118.             end
  119.         end
  120.         print("Type amount of item you need, (example: 78), maximum =",counter)
  121.     else
  122.         print("Type amount of item you need, (example: 78)")
  123.     end
  124.  
  125.  
  126.    
  127.     maxCount=tonumber(read())
  128.     ogCount=maxCount
  129.     print("Trying to pull",maxCount,"of",name)
  130.  
  131.     for i, v in ipairs(storage) do  --for every chest
  132.         checkChest=storage[i]
  133.         for index=1,54 do           --for every slot
  134.             item=storage[i][index]
  135.             if(item~=nil) then  --if this passes, we have a slot containing a item
  136.                 if name == remove_prefix(item.name) then --if this passes, we have the slot with the correct item!
  137.                     maxCount=maxCount-storageChests[i].pushItems(peripheral.getName(outputChest),index,maxCount)
  138.                 end
  139.                
  140.             end
  141.         end
  142.     end
  143.     print("Finished trying to pull")
  144.     print("Managed to pull",ogCount-maxCount,"items. This is",math.floor((ogCount-maxCount)/ogCount*100),"% of the actual amount")
  145.     compileData()
  146. end
  147.  
  148. function remove_prefix(input) --From ChatGPT
  149.     local result = input:match(":(.+)")
  150.     return result
  151. end
  152.  
  153. function findSlot()
  154.     for i, v in ipairs(storage) do
  155.         checkChest=storage[i]
  156.         for index=1,54 do
  157.             hasItem=storage[i][index]
  158.            
  159.             if(hasItem==nil) then
  160.                 return i
  161.             end
  162.         end
  163.       end
  164.       return nil,nil
  165. end
  166.  
  167.  
  168. function findSlot1()
  169.     for i, v in ipairs(storageChests) do
  170.         checkChest=storageChests[i]
  171.         for index=1,checkChest.size() do
  172.             hasItem=checkChest.list()[index]
  173.             if(hasItem==nil) then
  174.                 return i
  175.             end
  176.         end
  177.       end
  178.       return nil,nil
  179. end
  180.  
  181.  
  182. function checkError()
  183.     if(monitor==nil) then
  184.         return "NO MONITOR FOUND, Setup a 3x1 monitor setup above the input and output chests, and possibly change line 1 of the program"
  185.     end
  186.  
  187.     return nil
  188. end
  189. function monitorMain()
  190.     monitor.clear()
  191.     monitor.setCursorPos(1,1)
  192.     monitor.write( "Click here" )
  193.     monitor.setCursorPos(1,2)
  194.     monitor.write( "To input" )
  195.     monitor.setCursorPos(20,1)
  196.     monitor.write( "Click here" )
  197.     monitor.setCursorPos(21,2)
  198.     monitor.write( "To output" )
  199.     monitor.setCursorPos(13,3)
  200. end
  201.  
  202. function save(table,name)
  203.     local file = fs.open(name,"w")
  204.     file.write(textutils.serialize(table))
  205.     file.close()
  206.     end
  207.  
  208. print(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement