Advertisement
Bunny_bt

Drawer system

Feb 12th, 2023 (edited)
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.83 KB | None | 0 0
  1. local chest = peripheral.find('minecraft:chest')
  2. local controller = peripheral.wrap('bottom')
  3. local controllerData = {} --slot, name, count
  4. local nativeSize = { term.getSize() }
  5. local log = {} --recent requests, delete this?
  6. local terminal = window.create(term.native(), 1, 1, nativeSize[1] / 2, nativeSize[2])
  7. local history = window.create(term.native(), nativeSize[1] / 2 + 1, 1, nativeSize[1] / 2, nativeSize[2])
  8.  
  9. --TODO
  10. --info might not be getting refreshed
  11. --sort controllerData by count
  12.  
  13. function PullData()
  14.     log = {}
  15.     term.clear()
  16.     term.setCursorPos(1, 1)
  17.     print('Pulling Data...')
  18.     for i = 1, controller.size() do
  19.         local itemData = controller.getItemMeta(i)
  20.         if itemData ~= nil then
  21.             table.insert(controllerData, { slot = i, name = string.lower(itemData.displayName), count = itemData.count })
  22.         end
  23.     end
  24.     print('Complete!')
  25. end
  26.  
  27. function Request()
  28.     local input = string.lower(read())
  29.     print('Pulling data...')
  30.     local words = {}
  31.     local amount
  32.     --split words and amount
  33.     for word in string.gmatch(input, "%S+") do
  34.         if tonumber(word) ~= nil then
  35.             amount = tonumber(word)
  36.         else
  37.             table.insert(words, word)
  38.         end
  39.     end
  40.     if amount == nil then
  41.         amount = 1
  42.     end
  43.  
  44.     --Attemps to find exact match with input and displayName
  45.     for index, entry in pairs(controllerData) do
  46.         local controllerWords = {}
  47.         for word in string.gmatch(entry.name, "%S+") do
  48.             table.insert(controllerWords, word)
  49.         end
  50.         if #words == #controllerWords then
  51.             for i = 1, #words, 1 do
  52.                 for i = 1, #controllerWords, 1 do
  53.                     if string.find(words[i], controllerWords[i]) == nil then --breaks until all words match
  54.                         break
  55.                     elseif i == #words then
  56.                         --print(entry.name, 'found.')
  57.                         chest.pullItems(peripheral.getName(controller), entry.slot, amount)
  58.                         controllerData[index].count = entry.count - amount
  59.                         for k, v in ipairs(log) do
  60.                             if v.name == entry.name then table.remove(log, k) end
  61.                         end
  62.                         table.insert(log, controllerData[index])
  63.                         return
  64.                     end
  65.                 end
  66.             end
  67.         end
  68.     end
  69.     --Finds appoximate match if above fails
  70.     for index, entry in pairs(controllerData) do
  71.         for i = 1, #words, 1 do
  72.             if string.find(entry.name, words[i]) == nil then --breaks until all words match
  73.                 break
  74.             elseif i == #words then
  75.                 --print(entry.name, 'found.')
  76.                 chest.pullItems(peripheral.getName(controller), entry.slot, amount)
  77.                 controllerData[index].count = entry.count - amount
  78.                 for k, v in ipairs(log) do
  79.                     if v.name == entry.name then table.remove(log, k) end
  80.                 end
  81.                 table.insert(log, controllerData[index])
  82.                 return
  83.             end
  84.         end
  85.         if index == #controllerData then
  86.             term.clear()
  87.             term.setCursorPos(1, 1)
  88.             print(input, 'not in storage :(\n')
  89.             sleep(1)
  90.         end
  91.     end
  92. end
  93.  
  94. function PullEvent(event, timer)
  95.     local tiemrID = os.startTimer(timer)
  96.     local evt = { os.pullEvent() }
  97.     if evt[1] == event then
  98.         return true
  99.     elseif evt[1] == 'timer' and evt[2] == tiemrID then
  100.         return false
  101.     elseif evt[1] == 'mouse_scroll' then
  102.         scrollAmount = scrollAmount + evt[2]
  103.     end
  104. end
  105.  
  106. --Init stuff
  107. term.clear()
  108. term.setCursorPos(1, 1)
  109. PullData()
  110. scrollAmount = nativeSize[2] - #controllerData - 1
  111.  
  112. --Main
  113. while true do
  114.     term.clear()
  115.     history.clear()
  116.  
  117.     --Writes info to right side
  118.     history.setCursorPos(1, 1)
  119.     term.redirect(history)
  120.     for k = 1, #controllerData + scrollAmount, 1 do
  121.         if #controllerData + scrollAmount < nativeSize[2] then --if scrolled too high
  122.             scrollAmount = scrollAmount + 1
  123.             k = 1
  124.         elseif #controllerData + scrollAmount > #controllerData then --if scrolled too low
  125.             scrollAmount = scrollAmount - 1
  126.             k = #controllerData
  127.         end
  128.         if controllerData[k] ~= nil then --Lists info
  129.             print(controllerData[k].count, controllerData[k].name)
  130.         end
  131.     end
  132.    
  133.     term.redirect(terminal)
  134.     paintutils.drawLine(nativeSize[1] / 2, 1, nativeSize[1] / 2, nativeSize[2], colors.gray)
  135.     term.setBackgroundColor(colors.black)
  136.     term.setCursorPos(1, 1)
  137.     term.write("Request Item: ")
  138.    
  139.     local isEvent = PullEvent("key", 300)
  140.     if isEvent == true then Request() elseif isEvent == false then PullData() end
  141. end
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement