Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local computer = require("computer")
- local event = require("event")
- local term = require("term")
- local shell = require("shell")
- local fs = require("filesystem")
- local unicode = require("unicode")
- local serial = require("serialization")
- local data = component.database
- local gpu = component.gpu
- local transposer = component.transposer
- require("durexdb")
- local dataBase = DurexDatabase:new()
- local storages = {}
- local selectors = {}
- local items = {}
- local addressToSelector = {}
- gpu.setResolution(80,30)
- local page = 0
- local sizeOfPage = 26
- local items_on_the_screen = {}
- function loadSelectors()
- file = io.open(shell.getWorkingDirectory() .. "/selectors.lua", "r")
- local tempSelectors = serial.unserialize(file:read(999999))
- file:close()
- file = io.open(shell.getWorkingDirectory() .. "/addressToSelector.lua", "r")
- local addresses = serial.unserialize(file:read(999999))
- file:close()
- for i=1,#tempSelectors do
- selectors[i] = component.proxy(tempSelectors[i])
- end
- for k,v in pairs(addresses) do
- addressToSelector[k] = component.proxy(v)
- end
- end
- function saveSelectors()
- local addresses = {}
- for k,v in pairs( addressToSelector) do
- addresses[k] = v.address
- end
- local tempSelectors = {}
- for i=1,#selectors do
- tempSelectors[i] = selectors[i].address
- end
- file = io.open(shell.getWorkingDirectory() .. "/selectors.lua", "w")
- file:write(serial.serialize(tempSelectors))
- file:close()
- file = io.open(shell.getWorkingDirectory() .. "/addressToSelector.lua", "w")
- file:write(serial.serialize(addresses))
- file:close()
- end
- function showItems()
- for i=1,#selectors do
- selectors[i].setSlots({})
- end
- local selectorNumber = 1
- local indexSelector = 1
- for i=1,#items_on_the_screen do
- local item = {}
- item.id,item.dmg = parseToIdAndDamage(items_on_the_screen[i][3])
- selectors[selectorNumber].setSlot(indexSelector,item)
- indexSelector = indexSelector + 1
- if (indexSelector == 10 )then
- indexSelector = 1
- selectorNumber = selectorNumber + 1
- end
- end
- end
- function customizeSelectors()
- local x = 1
- local tempStorages = component.list('openperipheral_selector')
- local tempSelectors = {}
- for k,v in pairs(tempStorages) do
- component.setPrimary(v,k)
- while(component.isAvailable('openperipheral_selector') == false) do
- os.sleep(0.05)
- end
- tempSelectors[x] = component.openperipheral_selector
- x=x+1
- end
- for i=1,#tempSelectors do
- print('selector #'..i)
- local _,_,a = event.pull('slot_click')
- local realA = ''
- for j=1,#tempSelectors do
- if (tempSelectors[j].getSlot(1)) then
- selectors[i] = tempSelectors[j]
- addressToSelector[a] = tempSelectors[j]
- break
- end
- end
- end
- end
- function ItemToString(name,dmg)
- local item = {}
- item.name = name
- item.dmg = dmg
- return serial.serialize(item)
- end
- function getClickedItems(slot,address)
- local item = addressToSelector[address].getSlot(slot)
- local count = 0
- local tempName = ItemToString(item.id,item.dmg)
- local id = item.id
- local damage = item.dmg
- for k,v in pairs(items_on_the_screen) do
- if (k == tempName) then
- count = v[2]
- end
- end
- if (count == 0) then return end
- gpu.setBackground(0xbbbbbb)
- gpu.fill(28,10,24,8,' ')
- gpu.setBackground(0x111111)
- gpu.fill(30,11,20,6,' ')
- gpu.setForeground(0xffffff)
- gpu.set(35,11,"Max count")
- gpu.set(37,12,tostring(count))
- gpu.set(34,14,"Input count")
- gpu.setBackground(0x666666)
- gpu.setForeground(0xffffff)
- gpu.fill(37,15,5,1,' ')
- local str = ''
- while true do
- local _,_,asci = event.pull('key_down')
- if (asci == 13) then
- if (str == '' or tonumber(str) == 0) then
- drawItems()
- break
- else
- getItem(id,damage,tonumber(str))
- drawItems()
- break
- end
- elseif (asci == 8 ) then
- gpu.fill(37,15,5,1,' ')
- str = unicode.sub(str,1,unicode.len(str)-1)
- gpu.set(37,15,str)
- elseif (asci>=48 and asci<=57) then
- str = str .. unicode.char(asci)
- gpu.set(37,15,str)
- end
- end
- end
- function parseToIdAndDamage(name)
- local id_with_damage = name
- local words = {}
- local id = ''
- for w in (id_with_damage .. ":"):gmatch("([^:]*):") do
- table.insert(words, w)
- end
- for i=1,#words-1 do
- id = id .. words[i]
- if (i<#words-1) then
- id = id..':'
- end
- end
- local damage = tonumber(words[#words])
- return id,damage
- end
- loadSelectors()
- function drawItems()
- items_on_the_screen = getAllItemsFromDataBase()
- gpu.setBackground(0x111111)
- gpu.fill(23,3,56,27,' ')
- local i = 1
- local sleep = page * 26
- for k,v in pairs(items_on_the_screen) do
- if sleep > 0 then
- sleep = sleep - 1
- else
- if (i == 27) then
- break
- end
- v[3] = i
- if i%2 == 1 then
- gpu.setBackground(0x333333)
- else
- gpu.setBackground(0x555555)
- end
- gpu.setForeground(0xffffff)
- gpu.fill(25,i+2,52,1,' ')
- gpu.set(25,i+2,v[1])
- gpu.set(60,i+2,tostring(v[2]))
- gpu.setForeground(0x00ff00)
- gpu.set(74,i+2,'Get')
- gpu.setForeground(0xff0000)
- gpu.set(73,i+2,'N')
- i = i + 1
- end
- end
- end
- function drawInterface()
- setData()
- gpu.setBackground(0xbbbbbb)
- term.clear()
- gpu.setBackground(0x111111)
- gpu.fill(3,2,19,16,' ')
- gpu.fill(23,2,56,28,' ')
- gpu.setForeground(0xffffff)
- gpu.fill(3,27,9,3,' ')
- gpu.set(5,28,'<----')
- gpu.fill(13,27,9,3,' ')
- gpu.set(15,28,'---->')
- gpu.setForeground(0xff0000)
- gpu.set(25,2,'Name')
- gpu.set(60,2,'Count')
- gpu.set(72,2,'Query')
- gpu.setForeground(0xffffff)
- gpu.fill(3,23,19,3,' ')
- gpu.set(7,24,'Clear chest')
- gpu.fill(3,19,19,3,' ')
- gpu.set(7,20,'Show Items')
- drawItems()
- end
- function setData()
- dataBase:setData(items)
- end
- function getAllItemsFromDataBase()
- return dataBase:getAllItems()
- end
- function saveItems()
- file = io.open(shell.getWorkingDirectory() .. "/Chest.lua", "w")
- file:write(serial.serialize(items))
- file:close()
- end
- function loadItems()
- file = io.open(shell.getWorkingDirectory() .. "/Chest.lua", "r")
- items = serial.unserialize(file:read(999999))
- file:close()
- end
- function loadItemNames()
- dataBase:loadItemsNames()
- end
- function customizeStorage()
- local x = 1
- local tempStorages = component.list('transposer')
- local isTransposer = true
- while (isTransposer) do
- for k,v in pairs(tempStorages) do
- isTransposer = false
- component.setPrimary(v,k)
- while(component.isAvailable('transposer') == false) do
- os.sleep(0.05)
- end
- if (component.transposer.getStackInSlot(1,1)) then
- component.transposer.transferItem(1,0,64,1,1)
- storages[x] = component.transposer
- isTransposer = true
- x=x+1
- break
- end
- end
- end
- end
- customizeStorage()
- loadItemNames()
- function getAllItems()
- for i=1,#storages do
- items[i] = {}
- for j=1,3 do
- items[i][j] = {}
- getItemsOfStorage(i,j)
- end
- end
- end
- function getItemsOfStorage(x,y)
- for i=1,storages[x].getInventorySize(y+1) do
- local tempItem = storages[x].getStackInSlot(y+1,i)
- if (tempItem) then
- items[x][y][i]=tempItem
- else
- items[x][y][i]= {}
- items[x][y][i].damage =0
- items[x][y][i].name = "0"
- items[x][y][i].size = "0"
- end
- end
- end
- function getItemFromSlot(storageX,side,index,count)
- storages[storageX].transferItem(side,1,count,index)
- if (side ~= 0) then
- local temp_item = storages[storageX].getStackInSlot(side,index)
- if (temp_item) then
- items[storageX][side-1][index].size = temp_item.size
- end
- end
- if (storageX>1) then
- getItemFromSlot(storageX-1,0,1,count)
- end
- end
- function setNameToItem(id,name)
- dataBase:setNameForItem(id,name)
- end
- function getItem(id,damage,count)
- local slots = dataBase:getItems(id,damage,count)
- if not slots then
- return
- end
- for i=1,#slots do
- getItemFromSlot(slots[i][1],slots[i][2],slots[i][3],slots[i][4])
- end
- saveItems()
- setData()
- end
- function transferItemBack(slot,chest_x,side,index,count,level)
- if (level < chest_x) then
- storages[level].transferItem(1,0,count,slot,1)
- transferItemBack(1,chest_x,side,index,count,level+1)
- else
- storages[level].transferItem(1,side,count,slot,index)
- local tempItem = storages[chest_x].getStackInSlot(side,index)
- items[chest_x][side-1][index].size = tempItem.size
- items[chest_x][side-1][index].damage = tempItem.damage
- items[chest_x][side-1][index].name = tempItem.name
- items[chest_x][side-1][index].name = tempItem.name
- items[chest_x][side-1][index].name = tempItem.name
- saveItems()
- end
- end
- function pushItems()
- local availableSlots = dataBase:getAvailableSlot()
- local caret = 1
- for i=1,storages[1].getInventorySize(1) do
- local tempItem = storages[1].getStackInSlot(1,i)
- if (tempItem) then
- if (tempItem.size < tempItem.maxSize) then
- storages[1].store(1,i,data.address,1)
- local slots = dataBase:getNotFullSlot(tempItem.name,tempItem.damage,tempItem.maxSize)
- local flag = true
- for j=1,#slots do
- if (storages[slots[j][1]].compareStackToDatabase(slots[j][2],slots[j][3],data.address,1,true)) then
- local count = tempItem.maxSize - storages[slots[j][1]].getStackInSlot(slots[j][2],slots[j][3]).size
- transferItemBack(i,slots[j][1],slots[j][2],slots[j][3],count,1)
- tempItem.size = tempItem.size - count
- if (tempItem.size <=0) then
- flag = false
- break
- end
- end
- end
- if flag then
- slot = availableSlots[caret]
- caret = caret + 1
- transferItemBack(i,slot[1],slot[2],slot[3],64,1)
- end
- else
- slot = availableSlots[caret]
- caret = caret + 1
- transferItemBack(i,slot[1],slot[2],slot[3],64,1)
- end
- end
- end
- saveItems()
- setData()
- end
- loadItems()
- drawInterface()
- function isClicked(x1,y1,x2,y2,x,y)
- return (x>=x1 and y>=y1 and x<=x2 and y<=y2)
- end
- while true do
- local e,c,x,y,_,p = event.pull(3)
- if e == 'touch' then
- if (isClicked(3,27,11,29,x,y)) then
- page = page - 1
- drawItems()
- elseif (isClicked(13,27,21,29,x,y)) then
- page = page + 1
- drawItems()
- elseif (isClicked(3,23,21,25,x,y)) then
- pushItems()
- drawItems()
- elseif (isClicked(74,3,76,2+sizeOfPage,x,y)) then
- for k,v in pairs(items_on_the_screen) do
- if (v[3] == y-2) then
- local tempItemWithStats = serial.unserialize(k)
- local id = tempItemWithStats.name
- local damage = tonumber(tempItemWithStats.dmg)
- gpu.setBackground(0xbbbbbb)
- gpu.fill(28,10,24,8,' ')
- gpu.setBackground(0x111111)
- gpu.fill(30,11,20,6,' ')
- gpu.setForeground(0xffffff)
- gpu.set(35,11,"Max count")
- gpu.set(37,12,tostring(v[2]))
- gpu.set(34,14,"Input count")
- gpu.setBackground(0x666666)
- gpu.setForeground(0xffffff)
- gpu.fill(37,15,5,1,' ')
- local str = ''
- while true do
- local _,_,asci = event.pull('key_down')
- if (asci == 13) then
- if (str == '' or tonumber(str) == 0) then
- drawItems()
- break
- else
- getItem(id,damage,tonumber(str))
- drawItems()
- break
- end
- elseif (asci == 8 ) then
- gpu.fill(37,15,5,1,' ')
- str = unicode.sub(str,1,unicode.len(str)-1)
- gpu.set(37,15,str)
- elseif (asci>=48 and asci<=57) then
- str = str .. unicode.char(asci)
- gpu.set(37,15,str)
- end
- end
- break
- end
- end
- elseif (isClicked(73,3,73,2+sizeOfPage,x,y)) then
- local id_with_damage = items_on_the_screen[y-2][3]
- if (y-2)%2 == 1 then
- gpu.setBackground(0x333333)
- else
- gpu.setBackground(0x555555)
- end
- gpu.fill(25,y,32,1,' ')
- local str = ''
- while true do
- local _,_,asci = event.pull('key_down')
- if (asci == 13) then
- if (str == '') then
- drawItems()
- break
- else
- setNameToItem(id_with_damage,str)
- drawItems()
- break
- end
- elseif (asci == 8 ) then
- gpu.fill(25,y,32,1,' ')
- str = unicode.sub(str,1,unicode.len(str)-1)
- gpu.set(25,y,str)
- elseif (asci ~= 0 ) then
- str = str .. unicode.char(asci)
- gpu.set(25,y,str)
- end
- end
- elseif (isClicked(3,19,21,21,x,y)) then
- showItems()
- end
- elseif (e=='slot_click') then
- getClickedItems(c,x)
- end
- end
- saveItems()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement