Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local chest = peripheral.find('minecraft:chest')
- local controller = peripheral.wrap('bottom')
- local controllerData = {} --slot, name, count
- local nativeSize = { term.getSize() }
- local log = {} --recent requests, delete this?
- local terminal = window.create(term.native(), 1, 1, nativeSize[1] / 2, nativeSize[2])
- local history = window.create(term.native(), nativeSize[1] / 2 + 1, 1, nativeSize[1] / 2, nativeSize[2])
- --TODO
- --info might not be getting refreshed
- --sort controllerData by count
- function PullData()
- log = {}
- term.clear()
- term.setCursorPos(1, 1)
- print('Pulling Data...')
- for i = 1, controller.size() do
- local itemData = controller.getItemMeta(i)
- if itemData ~= nil then
- table.insert(controllerData, { slot = i, name = string.lower(itemData.displayName), count = itemData.count })
- end
- end
- print('Complete!')
- end
- function Request()
- local input = string.lower(read())
- print('Pulling data...')
- local words = {}
- local amount
- --split words and amount
- for word in string.gmatch(input, "%S+") do
- if tonumber(word) ~= nil then
- amount = tonumber(word)
- else
- table.insert(words, word)
- end
- end
- if amount == nil then
- amount = 1
- end
- --Attemps to find exact match with input and displayName
- for index, entry in pairs(controllerData) do
- local controllerWords = {}
- for word in string.gmatch(entry.name, "%S+") do
- table.insert(controllerWords, word)
- end
- if #words == #controllerWords then
- for i = 1, #words, 1 do
- for i = 1, #controllerWords, 1 do
- if string.find(words[i], controllerWords[i]) == nil then --breaks until all words match
- break
- elseif i == #words then
- --print(entry.name, 'found.')
- chest.pullItems(peripheral.getName(controller), entry.slot, amount)
- controllerData[index].count = entry.count - amount
- for k, v in ipairs(log) do
- if v.name == entry.name then table.remove(log, k) end
- end
- table.insert(log, controllerData[index])
- return
- end
- end
- end
- end
- end
- --Finds appoximate match if above fails
- for index, entry in pairs(controllerData) do
- for i = 1, #words, 1 do
- if string.find(entry.name, words[i]) == nil then --breaks until all words match
- break
- elseif i == #words then
- --print(entry.name, 'found.')
- chest.pullItems(peripheral.getName(controller), entry.slot, amount)
- controllerData[index].count = entry.count - amount
- for k, v in ipairs(log) do
- if v.name == entry.name then table.remove(log, k) end
- end
- table.insert(log, controllerData[index])
- return
- end
- end
- if index == #controllerData then
- term.clear()
- term.setCursorPos(1, 1)
- print(input, 'not in storage :(\n')
- sleep(1)
- end
- end
- end
- function PullEvent(event, timer)
- local tiemrID = os.startTimer(timer)
- local evt = { os.pullEvent() }
- if evt[1] == event then
- return true
- elseif evt[1] == 'timer' and evt[2] == tiemrID then
- return false
- elseif evt[1] == 'mouse_scroll' then
- scrollAmount = scrollAmount + evt[2]
- end
- end
- --Init stuff
- term.clear()
- term.setCursorPos(1, 1)
- PullData()
- scrollAmount = nativeSize[2] - #controllerData - 1
- --Main
- while true do
- term.clear()
- history.clear()
- --Writes info to right side
- history.setCursorPos(1, 1)
- term.redirect(history)
- for k = 1, #controllerData + scrollAmount, 1 do
- if #controllerData + scrollAmount < nativeSize[2] then --if scrolled too high
- scrollAmount = scrollAmount + 1
- k = 1
- elseif #controllerData + scrollAmount > #controllerData then --if scrolled too low
- scrollAmount = scrollAmount - 1
- k = #controllerData
- end
- if controllerData[k] ~= nil then --Lists info
- print(controllerData[k].count, controllerData[k].name)
- end
- end
- term.redirect(terminal)
- paintutils.drawLine(nativeSize[1] / 2, 1, nativeSize[1] / 2, nativeSize[2], colors.gray)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1, 1)
- term.write("Request Item: ")
- local isEvent = PullEvent("key", 300)
- if isEvent == true then Request() elseif isEvent == false then PullData() end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement