Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local fs = require("filesystem")
- local shell = require("shell")
- local event = require("event")
- local unicode = require("unicode")
- local term = require("term")
- local g = component.gpu
- local me = component.me_interface
- if not fs.exists("/lib/oc.lua") then
- shell.execute("wget https://www.dropbox.com/s/cgfytt8g174a6vs/libPrecraft.lua?dl=1 /lib/oc.lua")
- end
- local oc = require("oc")
- if not fs.exists("/home/BD.txt") then
- oc.savef("BD.txt", {})
- end
- --------------------Настройки--------------------
- local CBUTTON, CDRAW, CTEXT = 0x00ffff, 0x004575, 0xD38FFE -- текст кнопок, рамки, осн текст
- local debug = true -- вкл дебаг (меню с недостающими крафтами и нехватной ресурсов)
- local ADM = {['KReaTlVNuY']=true, ['4ertik_']=true} -- доступы к нажатиям
- local scroll = 1 -- начало скролла
- local precraft = true -- флаг осн цикла
- local go = true -- включить идет ли проверка вещей
- local WIDTH, HEIGHT = 80, 50
- --------------------Настройки--------------------
- local inputFind = ""
- local guiPath, debugLog1, debugLog2 = {}, {}, {}
- if debug then WIDTH, HEIGHT = 160, 50 end
- g.setResolution(WIDTH,HEIGHT)
- g.setForeground(CTEXT)
- local function Clear() g.setBackground(0x000000) g.setForeground(0xD38FFE) g.fill(1,4,WIDTH,HEIGHT-4," ") end
- local function DrawScrollContent()
- if guiPath[#guiPath]=="start" or guiPath[#guiPath] == "stop" or guiPath[#guiPath] == "go" then
- for i = scroll, maxscroll do
- if content[i] then
- local k = i - scroll
- if i == posSelect then
- g.setBackground(0x1e1e1e)
- g.fill(11, 7 k, 58, 1, " ")
- g.set(13,7 k, tostring(i.. ". "..content[i].name))
- g.set(45, 7 k, tostring(content[i].count))
- g.set(60, 7 k, tostring(content[i].craftSize))
- g.setBackground(0x000000)
- else
- g.setBackground(0x000000)
- g.fill(11, 7 k, 58, 1, " ")
- g.set(13,7 k, tostring(i.. ". "..content[i].name))
- g.set(45, 7 k, tostring(content[i].count))
- g.set(60, 7 k, tostring(content[i].craftSize))
- end
- end
- end
- end
- end
- local function ScrollContent()
- local cont = {}
- if inputFind ~= nil and inputFind ~= "" then
- for key, data in pairs(content) do
- if unicode.lower(data.name):find(unicode.lower(inputFind)) then
- table.insert(cont, content[key])
- end
- end
- return cont
- else
- return content
- end
- end
- local function eScroll(_,_,_,_,zs,nick)
- if ADM[nick] then
- for key in pairs(scrolls) do
- for i = 1, #scrolls[key].ScrollIn do
- if scrolls[key].ScrollIn[i] == guiPath[#guiPath] then
- if not zs then
- scroll = 1
- elseif zs == 1 and scroll > 1 then
- scroll = scroll - 1
- elseif zs == -1 and maxscroll < #content then
- scroll = scroll 1
- end
- maxscroll = scroll scrolls[key].h - 1
- scrolls[key].action()
- content = ScrollContent()
- oc.drawscroll(scrolls[key].x, scrolls[key].y, scrolls[key].w, scrolls[key].h, scroll, #content, 0x1e1e1e, 0x004575)
- g.fill(scrolls[key].xGui 1,scrolls[key].yGui 1,scrolls[key].wGui-4,scrolls[key].hGui-2, " ")
- DrawScrollContent()
- end
- end
- end
- end
- end
- local function Main()
- if guiPath[#guiPath] == "start" or guiPath[#guiPath] == "stop" or guiPath[#guiPath] == "go" then
- g.set(13, 5, "Название предмета:")
- g.set(45, 5, "Кол-во:")
- g.set(60, 5, "Крафт:")
- g.set(20,43, "Для поиска введите название..")
- oc.right(71,50, "Всего "..#dataItems.. " крафтов")
- g.fill(85,1, 75, 51, " ")
- for i = 1, #debugLog1 do
- g.set(85, 4 i, tostring(debugLog1[i]))
- end
- for i = 1, #debugLog2 do
- g.set(85, 26 i, tostring(debugLog2[i]))
- end
- elseif guiPath[#guiPath] == "change" then
- g.set(13, 10, "Название предмета: "..dataItems[posSelect].name)
- g.set(13, 12, "id предмета: "..tostring(dataItems[posSelect].id))
- g.set(13, 14, "dmg предмета: "..tostring(dataItems[posSelect].dmg))
- g.set(13, 16, "Количество поддержания: "..tostring(dataItems[posSelect].count))
- g.set(13, 18, "Размер крафта: "..tostring(dataItems[posSelect].craftSize))
- end
- end
- local function DrawScrolls()
- for key in pairs(scrolls) do
- for i = 1, #scrolls[key].ScrollIn do
- if scrolls[key].ScrollIn[i] == guiPath[#guiPath] then
- scrolls[key].action()
- eScroll(_,_,_,_,nil,"KReaTlVNuY")
- oc.drawscroll(scrolls[key].x, scrolls[key].y, scrolls[key].w, scrolls[key].h, scroll, #content, 0x1e1e1e, 0x004575)
- oc.drawbutton(scrolls[key].xGui,scrolls[key].yGui,scrolls[key].wGui,scrolls[key].hGui,CBUTTON,CDRAW,"")
- end
- end
- end
- end
- local function DrawButtons()
- for button in pairs(buttons) do
- for i = 1, #buttons[button].ButtonIn do
- if buttons[button].ButtonIn[i] == guiPath[#guiPath] and buttons[button].visible then
- oc.drawbutton(buttons[button].x,buttons[button].y,buttons[button].w, buttons[button].h, buttons[button].ctext, buttons[button].cbutton, buttons[button].text)
- end
- end
- end
- end
- local function toGui(gui, clear)
- if clear then
- guiPath[#guiPath] = nil
- end
- if guiPath[#guiPath] ~= gui then
- guiPath[#guiPath 1] = gui
- end
- dataq, dataItems = nil, nil
- dataItems = oc.loadf("BD.txt")
- Clear()
- Main()
- DrawButtons()
- DrawScrolls()
- end
- local function Back(to)
- if to then
- if to <= #guiPath then for i = 1, to do table.remove(guiPath, #guiPath) end end
- else
- table.remove(guiPath, #guiPath)
- end
- posSelect = nil
- Clear()
- Main()
- DrawButtons()
- DrawScrolls()
- end
- local function AddLogs(nameTable, log)
- if nameTable == "ore" then
- if #debugLog1 > 19 then table.remove(debugLog1, 1) end
- table.insert(debugLog1, log)
- else
- if #debugLog2 > 19 then table.remove(debugLog2, 1) end
- table.insert(debugLog2, log)
- end
- Main()
- end
- local function Check()
- for i = 1, #dataq do
- if not go then break end
- if not dataq[i].craft or dataq[i].craft.isDone() or dataq[i].craft.isCanceled() then
- dataq[i].craft = nil
- local itemsMe = me.getItemDetail({id = dataq[i].id, dmg = dataq[i].dmg})
- if itemsMe then
- local delta = dataq[i].count - itemsMe.basic().qty
- if delta > dataq[i].craftSize then delta = dataq[i].craftSize end
- if delta > 0 then
- local cpus = me.getCpus()
- for j = 1, #cpus do
- if not cpus[j].busy and cpus[j].storage == dataq[i].cpu then
- local craftables = me.getCraftables({name = dataq[i].id, damage = dataq[i].dmg})
- if craftables.n >= 1 then
- dataq[i].craft = craftables[1].request(delta, false, tostring(dataq[i].cpu))
- --AddLogs("ore", delta.. " "..dataq[i].count)
- if debug then
- local cancel, cancelmsg = dataq[i].craft.isCanceled()
- if not cancel and cancelmsg then
- if cancelmsg == "computing" then
- AddLogs("ore", dataq[i].name.. " не хватает ресурсов " .. cancelmsg)
- end
- elseif cancelmsg == "request failed (missing resources?)" then
- AddLogs("ore", dataq[i].name.. " не хватает ресурсов " .. cancelmsg)
- elseif cancelmsg then
- AddLogs("ore", dataq[i].name .. " ".. cancelmsg)
- end
- os.sleep(0.5)
- break
- end
- elseif debug then
- AddLogs("recipe", dataq[i].name.. " не имеет рецепта")
- end
- break
- end
- end
- end
- end
- end
- end
- os.sleep(1)
- AddLogs("ore", " ")
- AddLogs("recipe", " ")
- end
- local function AddItem()
- changeitem = true
- if me.getStackInSlot(1) then
- g.fill(1,43, WIDTH, 1, " ")
- g.set(14,43,"Введите название предмета:") term.setCursor(40,43)
- local name = tostring(io.read()) g.fill(1,43,80,1, " ")
- g.set(14,43,"Введите кол-во поддержания:") term.setCursor(41,43)
- local count = tonumber(io.read()) g.fill(1,43,80,1, " ")
- g.set(14,43,"Введите макс. объём крафта:") term.setCursor(41,43)
- local craftSize = tonumber(io.read()) g.fill(1,43,80,1, " ")
- table.insert(dataItems, {name=name, id = me.getStackInSlot(1).id,dmg = me.getStackInSlot(1).dmg, count = count, craftSize = craftSize, cpu = 65536})
- oc.savef("BD.txt", dataItems)
- else
- g.set(14, 43, "Предмет не установлен в 1-й слот интерфейса")
- os.sleep(2)
- g.fill(1, 43, WIDTH, 1, " ")
- end
- Main()
- changeitem = false
- end
- local function RemoveItem()
- if posSelect and dataItems then
- for i = 1, #dataItems do
- if dataItems[i].name == content[posSelect].name then
- table.remove(dataItems, i)
- oc.savef("BD.txt", dataItems)
- eScroll(_,_,_,_,nil,"KReaTlVNuY")
- posSelect = nil
- return true
- end
- end
- end
- end
- local function EditItem()
- if posSelect and dataItems then
- for i = 1, #dataItems do
- if dataItems[i].name == content[posSelect].name then
- posSelect = i
- toGui("change")
- return true
- end
- end
- end
- end
- local function ChangeItem(action)
- if posSelect and dataItems then
- g.fill(1,45, WIDTH, 1, " ")
- if action == "changeName" then
- g.set(14,45,"Введите название предмета:") term.setCursor(40,45)
- dataItems[posSelect].name = tostring(io.read())
- elseif action == "changeCount" then
- g.set(14,45,"Введите кол-во поддержания:") term.setCursor(41,45)
- dataItems[posSelect].count = tonumber(io.read())
- elseif action == "changeCraftSize" then
- g.set(14,45,"Введите макс. объём крафта:") term.setCursor(41,45)
- dataItems[posSelect].craftSize = tonumber(io.read())
- end
- oc.savef("BD.txt", dataItems)
- g.fill(1, 45, WIDTH, 1, " ")
- Main()
- end
- end
- local function eButton(_,_,x,y,_, nick)
- if ADM[nick] then
- for button in pairs(buttons) do
- if x >= buttons[button].x and x <= buttons[button].endX and y >= buttons[button].y and y <= buttons[button].endY then
- for i = 1, #buttons[button].ButtonIn do
- if buttons[button].ButtonIn[i] == guiPath[#guiPath] then
- yTouch = y
- buttons[button].action()
- return true
- end
- end
- end
- end
- end
- end
- local function InputWrite(_,_,key1, key2,nick)
- if ADM[nick] and not changeitem then
- for key in pairs(scrolls) do
- for i = 1, #scrolls[key].ScrollIn do
- if scrolls[key].ScrollIn[i] == guiPath[#guiPath] then
- local y = scrolls[key].iwY
- g.fill(20,y, 30, 1, " ")
- if key1 == 8 then -- backspace
- inputFind = unicode.sub(inputFind, 1, -2)
- elseif key1 == 0 and key2 == 211 then -- delete
- inputFind = ""
- elseif key1 ~= 0 then -- keyboard
- if inputFind == nil or inputFind == "" then
- inputFind = unicode.char(key1)
- elseif unicode.len(inputFind) < 49 then
- inputFind = inputFind .. unicode.char(key1)
- end
- end
- if unicode.len(inputFind) < 30 and unicode.len(inputFind) > 0 then
- g.set(20,y, tostring(inputFind))
- elseif unicode.len(inputFind) >= 30 then
- g.set(20,y, tostring(inputFind:sub(unicode.len(inputFind)-29, unicode.len(inputFind))))-- отображение последних 20 символов
- else
- g.set(20,y, "Для поиска введите название..")
- end
- eScroll(_,_,_,_,nil,"KReaTlVNuY")
- end
- end
- end
- end
- end
- local function initButtons()
- for button in pairs(buttons) do
- buttons[button].endX = buttons[button].x buttons[button].w - 1
- buttons[button].endY = buttons[button].y buttons[button].h - 1
- end
- end
- local function LoadSystem()
- g.fill(1,1,WIDTH, HEIGHT, " ")
- dataItems = oc.loadf("BD.txt")
- initButtons()
- etouch = event.listen("touch", eButton)
- escroll = event.listen("scroll", eScroll)
- ekeydown = event.listen("key_down", InputWrite)
- toGui("start")
- dataq = dataItems
- return true
- end
- buttons = {
- LOGO = {ButtonIn = {"start", "go", "stop", "change"}, visible = true, x = 10, y = 1, w = 62, h = 3, cbutton = 0x004575, ctext = 0x00ffff, text = "PreCraft", action = function() end},
- Exit = {ButtonIn = {"start", "go","stop", "change"}, visible = false, x = 1, y = 1, w = 1, h = 1, cbutton = 0x004575, ctext = 0x00ffff, text = "", action = function() g.setResolution(160,50) event.cancel(etouch) event.cancel(ekeydown) event.cancel(escroll) precraft = false end},
- Select = {ButtonIn = {"stop"}, visible = false, x = 11, y = 7, w = 60, h = 35, text = "", action = function() posSelect = yTouch scroll - 7 DrawScrollContent() end},
- Go = {ButtonIn = {"start","go"}, visible = true, x = 14, y = 47, w = 24, h = 3, cbutton = 0x28C730, ctext = 0x28C730, text = "Go", action = function() end},
- GoTrue = {ButtonIn = {"stop"}, visible = true, x = 14, y = 47, w = 24, h = 3, cbutton = 0x00ffff, ctext = 0x00ffff, text = "Go", action = function() toGui("go") dataq = nil dataq = dataItems go = true end},
- Stop = {ButtonIn = {"stop"}, visible = true, x = 43, y = 47, w = 24, h = 3, cbutton = 0xff0000, ctext = 0xff0000, text = "Stop", action = function() end},
- StopTrue = {ButtonIn = {"start", "go"}, visible = true, x = 43, y = 47, w = 24, h = 3, cbutton = 0x00ffff, ctext = 0x00ffff,text = "Stop", action = function() toGui("stop") go = false end},
- Add = {ButtonIn = {"stop"}, visible = true, x = 20, y = 45, w = 10, h = 1, cbutton = nil, ctext = 0x00ffff, text = "[Добавить]", action = function() AddItem() end},
- Change = {ButtonIn = {"stop"}, visible = true, x = 36, y = 45, w = 10, h = 1, cbutton = nil, ctext = 0x00ffff, text = "[Изменить]", action = function() EditItem() end},
- Remove = {ButtonIn = {"stop"}, visible = true, x = 54, y = 45, w = 9, h = 1, cbutton = nil, ctext = 0x00ffff, text = "[Удалить]", action = function() RemoveItem() end},
- -- сhange
- changeName = {ButtonIn = {"change"}, visible = true, x = 14, y = 35, w = 53, h = 3, cbutton = 0x004575, ctext = 0x00ffff, text = "Изменить название", action = function() ChangeItem("changeName") end},
- changeCount = {ButtonIn = {"change"}, visible = true, x = 14, y = 38, w = 53, h = 3, cbutton = 0x004575, ctext = 0x00ffff, text = "Изменить количество поддержания", action = function() ChangeItem("changeCount") end},
- changeCraftSize = {ButtonIn = {"change"}, visible = true, x = 14, y = 41, w = 53, h = 3, cbutton = 0x004575, ctext = 0x00ffff, text = "Изменит размер крафта", action = function() ChangeItem("changeCraftSize") end},
- changeBack = {ButtonIn = {"change"}, visible = true, x = 14, y = 47, w = 53, h = 3, cbutton = 0x004575, ctext = 0x00ffff, text = "Назад", action = function() Back() end},
- }
- scrolls = {
- Buy = {ScrollIn = {"start", "go", "stop"},iwY = 43, xGui = 10 , yGui = 6, wGui = 62, hGui = 37, x = 70, y = 7, w = 1, h = 35, action = function() content = dataItems end},
- }
- function loop()
- if LoadSystem() then
- while precraft do
- if go then
- g.set(1,1,"check")
- Check()
- else
- g.set(1,1,"sleep")
- end
- os.sleep(1)
- end
- end
- end
- ok, err = pcall(loop)
- if not ok then
- event.cancel(etouch)
- event.cancel(escroll)
- event.cancel(ekeydown)
- computer.shutdown(true)
- end
Advertisement
Add Comment
Please, Sign In to add comment