paramus

Storage system

Jan 5th, 2026 (edited)
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.19 KB | Gaming | 0 0
  1. -- This slab of code has no documentation, no coments, and makes no sense, in the event of needing to fix it
  2. -- may the lord be with you, not even I, it's creator, understand how it works, I only know it works,
  3. -- so if you have entered here to optimize or fix something, you have my most sincere condolences.
  4.  
  5. local modem = peripheral.find("modem") or error("No modem attached", 0)
  6. local inputChest = peripheral.wrap("minecraft:chest_8") or error("No input chest found", 0)
  7. local crafter = peripheral.wrap("turtle_0")
  8.  
  9. local inputChetName = peripheral.getName(inputChest)
  10. local query = ""
  11. local page = 1
  12. local craftMode = false
  13. local chests = {}
  14. local items = {}
  15. local buttons = {}
  16.  
  17. function Draw()
  18.     -- cache the buttons data
  19.     buttons = {}
  20.     local iy=2
  21.     local counter = 1
  22.     for name, amount in pairs(items) do
  23.         local index = string.find(name, ":")
  24.         name = string.sub(name,index+1,string.len(name))
  25.  
  26.         if (string.find(name, query) ~= nil) then
  27.             if (counter >= page) then
  28.                 amount = tostring(amount)
  29.                
  30.                 table.insert(buttons, {name=name, amount=amount})
  31.                 iy=iy+1
  32.                 if (iy>19) then
  33.                     break
  34.                 end
  35.                
  36.             end
  37.         end
  38.         counter=counter+1
  39.     end
  40.    
  41.     term.setBackgroundColor(colors.white)
  42.     term.clear()
  43.     term.setTextColor(colors.black)
  44.    
  45.     -- Page
  46.     if (page > 1) then
  47.         term.setCursorPos(51,2)
  48.         term.write("\24")
  49.     end    
  50.     if (page < #items-17) then
  51.         term.setCursorPos(51,18)
  52.         term.write("\25")
  53.     end
  54.    
  55.     -- Scrollbar
  56.     if (#items-17 > 1) then
  57.         local scrollbarSize = (18/#items)*16
  58.         local scrollbarPos = (page/#items)+3
  59.         paintutils.drawLine(51,scrollbarPos, 51,17, colors.orange)
  60.         --paintutils.drawLine(51,scrollbarPos, 51,scrollbarSize+2, colors.orange)
  61.     end
  62.    
  63.     -- item buttons
  64.     for i, button in ipairs(buttons) do
  65.         paintutils.drawLine(2,i+1, 46,i+1, colors.yellow)
  66.         term.setCursorPos(2,i+1)
  67.         term.write(button.name)
  68.         -- Quantity
  69.         term.setCursorPos(47-string.len(button.amount),i+1)
  70.         term.write(button.amount)
  71.         -- x64
  72.         paintutils.drawLine(48,i+1, 50,i+1, colors.green)
  73.         term.setCursorPos(48,i+1)
  74.         term.write("x64")
  75.     end
  76.    
  77.     term.setTextColor(colors.white)
  78.     -- Clear button
  79.     paintutils.drawLine(47,1, 51,1, colors.red)
  80.     term.setCursorPos(47,1)
  81.     term.write("Clear")
  82.    
  83.     -- query
  84.     paintutils.drawLine(1,1, 46,1, colors.black)
  85.     term.setCursorPos(1,1)
  86.     print(query)
  87.     term.setCursorPos(string.len(query)+1,1)
  88.     term.setCursorBlink(true)
  89. end
  90.  
  91. function GetItems()    
  92.     items = {}
  93.     chests = modem.getNamesRemote()
  94.     for i, c in ipairs(chests) do
  95.         if (c ~= inputChetName and string.find(c, "chest")) then
  96.             local chest = peripheral.wrap(c)
  97.             for slot, item in pairs(chest.list()) do
  98.                 if (items[item.name] == nil) then
  99.                     items[item.name]=item.count
  100.                 else
  101.                     items[item.name]=items[item.name] + item.count
  102.                 end
  103.             end
  104.         end
  105.     end
  106. end
  107.  
  108. function KeyProcess(key)
  109.     if (keys.getName(key) ==  "backspace") then
  110.         query = string.sub(query,1,string.len(query)-1)
  111.     elseif (keys.getName(key) ==  "space") then
  112.         query = query .. "_"
  113.     elseif (string.len(keys.getName(key)) == 1) then
  114.         query = query .. keys.getName(key)
  115.     end
  116. end
  117.  
  118. function PullItem(name,amount)
  119.     for i, c in ipairs(chests) do
  120.         if (string.find(c, "chest")) then
  121.             local chest = peripheral.wrap(c)
  122.             for slot, item in pairs(chest.list()) do
  123.                 if (item.name == name) then
  124.                     chest.pushItems(inputChetName, slot, amount)
  125.                     return
  126.                 end
  127.             end
  128.         end
  129.     end
  130. end
  131.  
  132. function StoreItems()
  133.     for slot, item in pairs(inputChest.list()) do
  134.         if (item ~= nil) then
  135.             for i, c in ipairs(chests) do
  136.                 if (c ~= inputChetName and string.find(c, "chest")) then
  137.                     local amount = inputChest.pushItems(c, slot)
  138.                     if (amount == item.count) then
  139.                         break
  140.                     end
  141.                 end
  142.             end
  143.         end
  144.     end
  145. end
  146.  
  147. function Click(x,y)
  148.     if (y==1 and x>=47) then
  149.         StoreItems()
  150.         return
  151.     end
  152.    
  153.     local iy=2
  154.     for name, amount in pairs(items) do
  155.         if (string.find(name, query) ~= nil) then
  156.             if (iy==y and x>=48) then
  157.                 PullItem(name,64)
  158.                 return
  159.             elseif (iy==y) then
  160.                 PullItem(name,1)
  161.                 return
  162.             end
  163.             iy=iy+1
  164.         end
  165.     end
  166. end
  167.  
  168. function CraftItem(quantity)
  169.     local recipeScheme = {"", "", "",
  170.                           "", "", "",
  171.                           "", "", ""}
  172.     -- here we need to recover the recipe from the JSON file (One file per recipe? Nah)
  173.    
  174.     local aditioner = 0
  175.     for i, item in ipairs(recipeScheme) do
  176.         -- to ignore the slots not used to craft
  177.         if (i == 4 or i == 8) then
  178.             aditioner = aditioner + 1
  179.         end
  180.        
  181.         local slot = i + aditioner
  182.         chest.pushItems(crafter, fromslot, quantity, slot)
  183.     end
  184.     modem.transmit(1, 2, "CRAFT")
  185. end
  186.  
  187. --main
  188. GetItems()
  189. Draw()
  190. parallel.waitForAny(
  191.     function()
  192.         while (true) do
  193.             local event, key, is_held = os.pullEvent("key")
  194.             KeyProcess(key)
  195.             Draw()
  196.         end
  197.     end,
  198.     function()
  199.         while (true) do
  200.             local event, dir, x, y = os.pullEvent("mouse_scroll")
  201.             page = page + dir
  202.             if (page < 1) then
  203.                 page = 1
  204.             elseif (#buttons > 18) then
  205.                 page = #buttons
  206.                 Draw()
  207.             else
  208.                 Draw()
  209.             end
  210.         end
  211.     end,
  212.     function()
  213.         while (true) do
  214.             local event, button, x, y = os.pullEvent("mouse_click")
  215.             Click(x,y)
  216.             GetItems()
  217.             Draw()
  218.         end
  219.     end
  220. )
Add Comment
Please, Sign In to add comment