joserobjr

compressor.lua

Feb 3rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 30.89 KB | None | 0 0
  1. package.loaded["autocompressor/gui"] = nil
  2. package.loaded["autocompressor/crafting_unit"] = nil
  3. local crafting_unit = require("autocompressor/crafting_unit")
  4. local checkTime = crafting_unit.checkTime
  5. local sides = require("sides")
  6. local component = require("component")
  7. local gpu = component.gpu
  8. local originalResX, originalResY = gpu.getResolution()
  9. gpu.setResolution(80, 25)
  10. local gui = require("autocompressor/gui")
  11. local event = require("event")
  12. local beep = require("computer").beep
  13. local unicode = require("unicode")
  14. local serialization = require("serialization")
  15. local fs = require("filesystem")
  16.  
  17. gui.checkVersion(2, 5)
  18.  
  19. local save = function() end
  20.  
  21. ---@type table<string, autocompressor.crafting_unit>
  22. local unities = {}
  23.  
  24. local nameGui = gui.newGui("center", "center", 40, 10, true, "Adicionando Transpositor")
  25. local nameLabel1 = gui.newLabel(nameGui, "center", 2, "Dê um nome para o transpositor")
  26. local nameLabel2 = gui.newLabel(nameGui, "center", 3, "")
  27. local nameLabel3 = gui.newLabel(nameGui, 10, 5, "Nome:")
  28. local nameText = gui.newText(nameGui, 16, 5, 12, "")
  29. local nameRendering = false
  30. local nameAdd = gui.newButton(nameGui, 7, 8, " Adicionar ", function () end)
  31. local nameClose = gui.newButton(nameGui, 21, 8, " Cancelar ", function ()
  32.     nameRendering = false
  33.     gui.closeGui(nameGui)
  34. end)
  35.  
  36. local addPatternGui = gui.newGui("center", 3, 41, 21, true, "Adicionando Nova Receita")
  37. local addPatternRendering = false
  38. gui.newLabel(addPatternGui, 1, 2, "Produto criado:")
  39. local addPatternProductName = gui.newText(addPatternGui, 16, 2, 32, "", nil, 16)
  40. gui.newLabel(addPatternGui, 32, 2,"Qntd:")
  41.  
  42. local function numberOnly (window, field, text)
  43.     if text == nil then text = "1" else text = tostring(text) end
  44.     local changed = text:gsub("%D", ""):gsub("0(%d)", "%1")
  45.     if #changed == 0 or changed == "0" then
  46.         changed = "1"
  47.     end
  48.     if changed ~= text then
  49.         gui.setText(window, field, changed)
  50.     end
  51. end
  52.  
  53. local addPatternAmount = gui.newText(addPatternGui, 37, 2, 3, "1", numberOnly, 3)
  54. local addPatternGridFrames = {}
  55. for y = 0, 2 do
  56.     for x = 0, 2 do
  57.         table.insert(addPatternGridFrames,
  58.             gui.newFrame(addPatternGui, 1 + x*13, 4 + y*5, 13, 5)
  59.         )
  60.     end
  61. end
  62. local addPatternGrid = {}
  63. for y = 0, 2 do
  64.     for x = 0, 2 do
  65.         table.insert(addPatternGrid,
  66.             gui.newMultiLineLabel(addPatternGui, 1 + x*13 + 1, 4 + y*5 + 1, 11, 3, "")
  67.         )
  68.     end
  69. end
  70. ---@class autocompressor.gui.addpattern.modify
  71. ---@field quantityField
  72.  
  73. local addPatternModify = {}
  74. for y = 0, 2 do
  75.     for x = 0, 2 do
  76.         table.insert(addPatternModify,
  77.             gui.newText(addPatternGui, 1 + x*13 + 5, 4 + y*5, 3, "0", numberOnly)
  78.         )
  79.     end
  80. end
  81.  
  82. local addPatternAdd = gui.newButton(addPatternGui, 7, 19, " Adicionar ", function () end)
  83. gui.newButton(addPatternGui, 21, 19, " Cancelar ", function ()
  84.     addPatternRendering = false
  85.     gui.closeGui(addPatternGui)
  86. end)
  87.  
  88. local changeTransposerGui = gui.newGui("center", 3, 41, 20, true, "Alterando...")
  89. local changeTransposerNameLabel = gui.newLabel(changeTransposerGui, 2, 2, "Nome do transpositor: ")
  90. local changeTransposerName = gui.newText(changeTransposerGui, 24, 2, 12, "", nil, 15)
  91. --local changeTransposerRecipesLabel = gui.newLabel(changeTransposerGui, 2, 4, "Receitas")
  92. local changeTransposerList = gui.newList(changeTransposerGui, 2, 4, 22, 15, {}, nil, "Receitas")
  93. local changeTransposerRunning = false
  94. local changeTransposerClose = gui.newButton(changeTransposerGui, 26, 18, "  Fechar   ", function ()
  95.     changeTransposerRunning = false
  96. end)
  97. local changeTransposerAdd = gui.newButton(changeTransposerGui, 26, 4,    " Adicionar ")
  98. local changeTransposerModify = gui.newButton(changeTransposerGui, 26, 6, "  Alterar  ")
  99. local changeTransposerRemove = gui.newButton(changeTransposerGui, 26, 8, "  Remover  ")
  100.  
  101. -- Begin: Menu definitions
  102. local function createWindow()
  103.     ---@class autocompressor.gui.main.window
  104.     ---@field transposers table<string,autocompressor.gui.main.transposer>
  105.     ---@field scrollBar number
  106.     local window = { transposers = {} }
  107.  
  108.     local function recreateGui()
  109.         window.transposers = {}
  110.  
  111.         window.mainGui = gui.newGui(1, 2, 79, 23, false)
  112.  
  113.         window.exitButton = gui.newButton(window.mainGui, 73, 23, "sair", function(guiID, id)
  114.             local result = gui.getYesNo("", "Você tem certeza que deseja sair?", "")
  115.             if result == true then
  116.                 gpu.setResolution(originalResX, originalResY)
  117.                 gpu.setBackground(0x000000)
  118.                 gpu.setForeground(0xFFFFFF)
  119.                 gpu.fill(1, 1, originalResX, originalResY, " ")
  120.                 require("term").clear()
  121.                 os.exit()
  122.             end
  123.         end)
  124.  
  125.         window.col = 0
  126.         window.lin = 1
  127.     end
  128.  
  129.     recreateGui()
  130.     function window.nextBlock()
  131.         local col = window.col
  132.         local lin = window.lin
  133.  
  134.         if col >= 4 then
  135.             if lin >= 2 then
  136.                 return false
  137.             else
  138.                 lin = lin + 1
  139.                 col = 1
  140.             end
  141.         else
  142.             col = col + 1
  143.         end
  144.  
  145.         window.col = col
  146.         window.lin = lin
  147.         return true
  148.     end
  149.  
  150.     function window.createHorizontalScroll()
  151.         window.scrollBar = gui.newVSlider(window.mainGui, 1, 21, 74, 1, 1, 1, function(guiID, vSliderID, value)
  152.  
  153.         end)
  154.  
  155.         return window.scrollBar
  156.     end
  157.  
  158.     ---@param amount number
  159.     ---@param name string
  160.     ---@return boolean
  161.     local function validateProductName(name, amount)
  162.         if unicode.len(name) < 3 or unicode.len(name) > 32 then
  163.             gui.showError("O nome do produto digitado", "tem que ter", "de 3 letras a 32 caractéres", true)
  164.             return true
  165.         end
  166.  
  167.         if amount == 0 or amount > 999 then
  168.             gui.showError("", "Invalid amount", true)
  169.             return true
  170.         end
  171.         return false
  172.     end
  173.  
  174.     ---@param unit autocompressor.crafting_unit
  175.     function window.createTransposerBlock(line, col, unit)
  176.         local mainGui = window.mainGui
  177.         ---@class autocompressor.gui.main.transposer
  178.         local block = {}
  179.         local frameX = (col - 1) * 20
  180.         local frameY = 1 + (line - 1) * 10
  181.         block.frame = gui.newFrame(mainGui, frameX, frameY, 20, 10, unit.name:sub(1,12))
  182.  
  183.         block.btn_learn = gui.newButton(mainGui, frameX + 1, frameY + 1, "+", function(guiID, buttonID)
  184.             local inputs = unit:getInputs()
  185.             ---@class autocompressor.grid.result
  186.             ---@field chest autocompressor.chest
  187.             ---@field grid table<number, _item_stack>
  188.  
  189.             ---@type autocompressor.grid.result[]
  190.             local gridsFound = {}
  191.             for side, chest in pairs(inputs) do
  192.                 local grid = chest:getGridStacks()
  193.                 for slot, stack in pairs(grid) do
  194.                     if stack.name == "minecraft:stick" or stack.size <= 0 then
  195.                         grid[slot] = nil
  196.                     end
  197.                 end
  198.  
  199.                 if next(grid) ~= nil then
  200.                     table.insert(gridsFound, {chest=chest, grid=grid})
  201.                 end
  202.             end
  203.  
  204.             if not next(gridsFound) then
  205.                 gui.showError("Nenhum padrão foi encontrado", "em nenhum baú conectado ao", "transpositor "..unit.name, true)
  206.                 return
  207.             end
  208.  
  209.             local selection
  210.             if #gridsFound > 1 then
  211.                 gui.showError("", "Não implementado", "", true)
  212.                 return
  213.             else
  214.                 selection = gridsFound[1]
  215.             end
  216.  
  217.             local minStack = math.huge
  218.             for i = 1, 9 do
  219.                 ---@type _item_stack
  220.                 local stack = selection.grid[i]
  221.                 local label
  222.                 local title
  223.                 if not stack then
  224.                     title = nil
  225.                     label = ""
  226.                 else
  227.                     title = stack.size.."x"
  228.                     if stack.size < minStack then
  229.                         minStack = stack.size
  230.                     end
  231.                     if string.len(stack.label) > 0 then
  232.                         label = stack.label
  233.                     else
  234.                         label = stack.name
  235.                     end
  236.                 end
  237.                 addPatternGui[addPatternAmount].text = minStack
  238.                 addPatternGui[addPatternProductName].text = ""
  239.                 addPatternGui[addPatternGridFrames[i]].text = title
  240.                 addPatternGui[addPatternAdd].text = "[ Adicionar ]"
  241.                 addPatternGui.text = "|Adicionando Nova Receita|"
  242.                 addPatternGui[addPatternGrid[i]].text = label
  243.                 for i, fieldId in ipairs(addPatternModify) do
  244.                     addPatternGui[fieldId].visible = false
  245.                 end
  246.             end
  247.  
  248.             addPatternGui[addPatternAdd].func = function()
  249.                 local name = addPatternGui[addPatternProductName].text
  250.                 local amount = tonumber(addPatternGui[addPatternAmount].text)
  251.                 if validateProductName(name, amount) then
  252.                     return
  253.                 end
  254.  
  255.                 ---@type autocompressor.chest
  256.                 local chest = selection.chest
  257.                 if chest:createGrid(selection.grid, name, amount) then
  258.                     save()
  259.                     gui.showMsg("A receita para criar", name:sub(1,30), "foi salva com sucesso.")
  260.                 end
  261.  
  262.                 addPatternRendering = false
  263.             end
  264.  
  265.             addPatternGui[addPatternProductName].func = nil
  266.  
  267.             addPatternRendering = true
  268.             gui.displayGui(addPatternGui)
  269.             while addPatternRendering do
  270.                 gui.runGui(addPatternGui)
  271.             end
  272.             gui.closeGui(addPatternGui)
  273.         end)
  274.  
  275.         block.btn_modify = gui.newButton(mainGui, frameX + 5, frameY + 1, "Mudar", function(guiID, buttonID)
  276.             changeTransposerGui.text = "Alterando o transpositor "..unit.name
  277.             changeTransposerGui[changeTransposerName].text = unit.name
  278.  
  279.             local reverse = {}
  280.             local function updateList(refresh)
  281.                 ---@type string[]
  282.                 local list = {}
  283.                 reverse = {}
  284.                 for side, chest in pairs(unit:getInputs()) do
  285.                     for k, grid in pairs(chest.grids) do
  286.                         local label = grid.amount.."x"..grid.product
  287.                         table.insert(list, label)
  288.                         table.insert(reverse, {grid=grid, chest=chest, index=k})
  289.                     end
  290.                 end
  291.                 changeTransposerGui[changeTransposerList].entries = list
  292.                 if refresh then
  293.                     gui.displayWidget(changeTransposerGui, changeTransposerList)
  294.                 end
  295.             end
  296.  
  297.             updateList()
  298.  
  299.             changeTransposerGui[changeTransposerAdd].func = function(...)
  300.                 mainGui[block.btn_learn].func(...)
  301.                 updateList(true)
  302.             end
  303.  
  304.             changeTransposerGui[changeTransposerModify].func = function()
  305.                 local selection = gui.getSelected(changeTransposerGui, changeTransposerList)
  306.                 if not selection then
  307.                     gui.showError("", "Nenhuma receita selecionada", true)
  308.                     return
  309.                 end
  310.                 selection = reverse[selection]
  311.                 ---@type autocompressor.grid
  312.                 local grid = selection.grid
  313.  
  314.                 addPatternGui.text = "|Modificando a Receita "..grid.product.."|"
  315.                 --                                  "[ Adicionar ]"
  316.                 addPatternGui[addPatternAdd].text = "[ Modificar ]"
  317.                 for k, index in pairs(addPatternGridFrames) do
  318.                     local item = grid.matrix[k]
  319.                     if item then
  320.                         addPatternGui[index].text = "   "
  321.                         addPatternGui[addPatternModify[k]].text = tostring(item.amount)
  322.                         addPatternGui[addPatternModify[k]].visible = true
  323.                         addPatternGui[addPatternGrid[k]].text = item.name
  324.                     else
  325.                         addPatternGui[index].text = nil
  326.                         addPatternGui[addPatternModify[k]].visible = false
  327.                         addPatternGui[addPatternGrid[k]].text = ""
  328.                     end
  329.                 end
  330.  
  331.                 addPatternGui[addPatternProductName].text = grid.product
  332.                 addPatternGui[addPatternAmount].text = tostring(grid.amount)
  333.                 addPatternGui[addPatternAdd].func = function ()
  334.                     local name = addPatternGui[addPatternProductName].text
  335.                     local amount = tonumber(addPatternGui[addPatternAmount].text)
  336.                     if validateProductName(name, amount) then
  337.                         return
  338.                     end
  339.  
  340.                     local oldName = grid.product
  341.                     grid.product = name
  342.                     grid.amount = tonumber(amount)
  343.                     for k, index in pairs(addPatternModify) do
  344.                         local value = tonumber(addPatternGui[index].text)
  345.                         local item = grid.matrix[k]
  346.                         if item then
  347.                             item.amount = value
  348.                         end
  349.                     end
  350.                     save()
  351.                     gui.showMsg("A receita de ", oldName, "foi alterada com sucesso.")
  352.                     addPatternRendering = false
  353.                 end
  354.  
  355.                 addPatternRendering = true
  356.                 gui.displayGui(addPatternGui)
  357.                 while addPatternRendering do
  358.                     gui.runGui(addPatternGui)
  359.                 end
  360.                 gui.closeGui(addPatternGui)
  361.                 updateList(true)
  362.             end
  363.  
  364.             changeTransposerGui[changeTransposerName].func = function(frame,widget,value)
  365.                 value = tostring(value)
  366.                 if value:len() < 3 then
  367.                     gui.showMsg("O nome não foi alterado", "porque ele precisa ter",
  368.                     "no mínimo 3 letras")
  369.                     frame[widget].text = unit.name
  370.                 else
  371.                     unit.name = value
  372.                     save()
  373.                     local transposer = window.transposers[unit.address]
  374.                     if transposer then
  375.                         mainGui[transposer.frame].text = value
  376.                     end
  377.                 end
  378.             end
  379.  
  380.             changeTransposerGui[changeTransposerRemove].func = function()
  381.                 local selection = gui.getSelected(changeTransposerGui, changeTransposerList)
  382.                 if not selection then
  383.                     gui.showError("", "Nenhuma receita selecionada", true)
  384.                     return
  385.                 end
  386.                 selection = reverse[selection]
  387.                 ---@type autocompressor.grid
  388.                 local grid = selection.grid
  389.                 ---@type autocompressor.chest
  390.                 local chest = selection.chest
  391.  
  392.                 if gui.getYesNo("Você está prestes a remover a receita", grid.product, "Tem certeza?") then
  393.                     table.remove(chest.grids, selection.index)
  394.                     save()
  395.                     gui.showMsg("Você removeu a receita de", grid.product, "com sucesso")
  396.                     updateList(true)
  397.                     return
  398.                 end
  399.             end
  400.  
  401.             gui.displayGui(changeTransposerGui)
  402.             changeTransposerRunning = true
  403.             while changeTransposerRunning do
  404.                 gui.runGui(changeTransposerGui)
  405.             end
  406.         end)
  407.  
  408.         block.btn_del = gui.newButton(mainGui, frameX + 14, frameY + 1, "Del", function(guiID, buttonID)
  409.             if gui.getYesNo("Você tem certeza que quer apagar", "o transpositor "..unit.name.."?", "") then
  410.                 unities[unit.address] = nil
  411.                 save()
  412.                 window.recreateGui()
  413.             end
  414.         end)
  415.  
  416.         block.lbl_status = gui.newLabel(mainGui, frameX + 1, frameY + 3, "Esperando...", 0xc0c0c0, 0x0)
  417.  
  418.         block.lbl_processing = {
  419.             gui.newLabel(mainGui, frameX + 1, frameY + 4, "", 0xc0c0c0, 0xffffff),
  420.             gui.newLabel(mainGui, frameX + 1, frameY + 5, "", 0xc0c0c0, 0xffffff),
  421.             gui.newLabel(mainGui, frameX + 1, frameY + 6, "", 0xc0c0c0, 0xffffff),
  422.             gui.newLabel(mainGui, frameX + 1, frameY + 7, "", 0xc0c0c0, 0xffffff),
  423.             gui.newLabel(mainGui, frameX + 1, frameY + 8, "", 0xc0c0c0, 0xffffff),
  424.         }
  425.  
  426.         function block.update()
  427.             mainGui[block.frame].text = unit.name:sub(1,12)
  428.             local status = unit.status
  429.             local success, compressors = pcall(function () return unit:getCompressors() end)
  430.  
  431.             if not success then
  432.                 status = "Offline"
  433.                 compressors = {}
  434.             elseif status == "init" then
  435.                 status = "Online"
  436.             end
  437.  
  438.             local tbl = mainGui[block.lbl_status]
  439.             tbl.text = string.format("%-16s", status)
  440.  
  441.             gui.displayWidget(mainGui, block.lbl_status)
  442.  
  443.             local len = crafting_unit.tablelength(compressors)
  444.  
  445.             for i = len + 1, 5 do
  446.                 mainGui[block.lbl_processing[i]].text = ""
  447.                 gui.displayWidget(mainGui, block.lbl_processing[i])
  448.             end
  449.  
  450.             local i = 0
  451.             for k, compressor in pairs(compressors) do
  452.                 i = i + 1
  453.                 if i >= 5 then break end
  454.                 status = compressor.status
  455.                 if status == "waiting" then
  456.                     status = "Disponível"
  457.                 end
  458.                 block.setProcessingLabel(i, status)
  459.             end
  460.         end
  461.  
  462.         function block.setProcessingLabel(index, text)
  463.             mainGui[block.lbl_processing[index]].text = text:sub(1, 18)
  464.             gui.displayWidget(mainGui, block.lbl_processing[index])
  465.         end
  466.  
  467.         window.transposers[unit.address] = block
  468.         return block
  469.     end
  470.  
  471.     ---@param window autocompressor.gui.main.window
  472.     function window.createInsertionBlock(line, column)
  473.         line = line or window.lin
  474.         column = column or window.col
  475.         local mainGui = window.mainGui
  476.         local frameX = (column - 1) * 20
  477.         local frameY = 1 + (line - 1) * 10
  478.  
  479.         local block = {}
  480.         block.frame = gui.newFrame(mainGui, frameX, frameY, 20, 10, "Criar Novo")
  481.  
  482.         block.lbl_err = gui.newMultiLineLabel(mainGui, frameX + 1, frameY + 2, 18, 5, "Nenhum transpositor novo que seja válido foi encontrado", nil, 0xAA0000)
  483.  
  484.         local unused = {}
  485.         local inserted = {}
  486.  
  487.         --block.lbl_name = gui.newLabel(mainGui, frameX+2, frameY+7, "[ Nome: ", 0x0000FF, 0xFFFFFF)
  488.         --block.txt_name = gui.newText(mainGui, frameX+9, frameY+7, 10, "")
  489.         --block.lbl_name2 = gui.newLabel(mainGui, frameX+19, frameY+7, "]", 0x0000FF, 0xFFFFFF)
  490.  
  491.  
  492.         local function isValid(addr)
  493.             ---@type component.transposer
  494.             local proxy = component.proxy(addr)
  495.             if not proxy then
  496.                 return false
  497.             end
  498.             local hasCompressor = false
  499.             local hasInv = false
  500.             for i, v in ipairs(sides) do
  501.                 local n = sides[v]
  502.                 local size = proxy.getInventorySize(n)
  503.                 if size == 12 or size == 11 or size == 7 then
  504.                     hasCompressor = true
  505.                 elseif size ~= nil then
  506.                     hasInv = true
  507.                 end
  508.  
  509.                 if hasCompressor and hasInv then
  510.                     return true
  511.                 end
  512.             end
  513.             return false
  514.         end
  515.  
  516.         block.create = gui.newButton(mainGui, frameX+1, frameY+8, " Registrar Novo ", function ()
  517.             local selected = mainGui[block.list].selected
  518.             local short = unused[selected]
  519.             local addr = component.get(short, "transposer")
  520.             if not addr then
  521.                 beep()
  522.                 for k, v in pairs(inserted) do
  523.                     if v == short then
  524.                         addr = k
  525.                         break
  526.                     end
  527.                 end
  528.                 if not addr then addr = short end
  529.                 gui.showError("O transpositor não é válido!", addr, "Ele não está conectado ao computador", true)
  530.             elseif not isValid(addr) then
  531.                 beep()
  532.                 gui.showError("O transpositor não é válido!", addr, "Ele não tem uma prensa ou um baú", true)
  533.             else
  534.                 nameGui[nameLabel2].text = addr
  535.                 nameGui[nameText].text = ""
  536.                 nameRendering = true
  537.                 nameGui[nameAdd].func = function()
  538.                     local name = nameGui[nameText].text
  539.                     if unicode.len(name) < 3 then
  540.                         gui.showError("O nome digitado é muito pequeno",name,"Digite um nome com mais de 3 letras", true)
  541.                     elseif unicode.len(name) > 12 then
  542.                         gui.showError("O nome digitado é muito grande",name,"Digite um nome com até 12 letras", true)
  543.                     else
  544.                         --gui.showError("Não implementado","","", true)
  545.                         nameRendering = false
  546.                         gui.closeGui(nameGui)
  547.  
  548.                         if not isValid(addr) then
  549.                             gui.showError("O transpositor não é válido!", addr, "Ele foi invalidado ao ser adicionado")
  550.                         else
  551.                             local unit = crafting_unit.wrap(addr, name)
  552.                             unities[addr] = unit
  553.                             save()
  554.  
  555.                             mainGui[block.lbl_err].visible = false
  556.                             mainGui[block.list].visible = false
  557.                             mainGui[block.create].visible = false
  558.                             mainGui[block.frame].visible = false
  559.  
  560.                             window.createTransposerBlock(line, column, unit)
  561.                             if window.nextBlock() then
  562.                                 window.createInsertionBlock()
  563.                             end
  564.                         end
  565.                     end
  566.                 end
  567.                 gui.displayGui(nameGui)
  568.                 while nameRendering do
  569.                     gui.runGui(nameGui)
  570.                 end
  571.             end
  572.         end)
  573.  
  574.  
  575.         local possible = component.list("transposer")
  576.         local lastPossible
  577.         local lastUnusedIndex
  578.         function block.findUnused()
  579.             local index = next(unused, lastUnusedIndex)
  580.             if not index then
  581.                 lastUnusedIndex = nil
  582.             else
  583.                 local fullAddr = component.get(unused[index], "transposer")
  584.                 if not fullAddr or window.transposers[fullAddr] or not isValid(fullAddr) then
  585.                     for addr, short in pairs(inserted) do
  586.                         if short == unused[index] then
  587.                             inserted[addr] = nil
  588.                             break
  589.                         end
  590.                     end
  591.                     table.remove(unused, index)
  592.                 else
  593.                     lastUnusedIndex = index
  594.                 end
  595.             end
  596.  
  597.             local success, addr = pcall(next, possible, lastPossible)
  598.             if not addr then
  599.                 lastPossible = nil
  600.                 possible = component.list("transposer")
  601.             else
  602.                 lastPossible = addr
  603.                 local online = component.get(addr, "transposer")
  604.                 if online and not inserted[addr] and not window.transposers[addr] and isValid(addr) then
  605.                     local short = addr:sub(1,17)
  606.                     inserted[addr] = short
  607.                     table.insert(unused, short)
  608.                 end
  609.             end
  610.  
  611.             return unused
  612.         end
  613.  
  614.         block.list = gui.newList(mainGui, frameX + 1, frameY + 1, 18, 7, unused, function ()
  615.  
  616.         end, "Transpositor")
  617.  
  618.         function block.setListVisible(visible)
  619.             mainGui[block.list].visible = visible
  620.             --mainGui[block.lbl_name].visible = visible
  621.             --mainGui[block.lbl_name2].visible = visible
  622.             --mainGui[block.txt_name].visible = visible
  623.             mainGui[block.create].visible = visible
  624.             mainGui[block.lbl_err].visible = not visible
  625.         end
  626.  
  627.         function block.renderList()
  628.             if mainGui[block.list].visible then
  629.                 gui.displayWidget(mainGui, block.list)
  630.                 --gui.displayWidget(mainGui, block.lbl_name)
  631.                 --gui.displayWidget(mainGui, block.lbl_name2)
  632.                 --gui.displayWidget(mainGui, block.txt_name)
  633.                 gui.displayWidget(mainGui, block.create)
  634.             else
  635.                 gui.displayGui(mainGui)
  636.             end
  637.         end
  638.  
  639.         function block.update()
  640.             block.findUnused()
  641.             mainGui[block.list].entries = unused
  642.  
  643.             block.setListVisible(next(unused) ~= nil)
  644.             block.renderList()
  645.         end
  646.  
  647.         window.insertionBlock = block
  648.         return block
  649.     end
  650.  
  651.     function window.recreateGui()
  652.         recreateGui()
  653.  
  654.         for addr, unit in pairs(unities) do
  655.             if window.nextBlock() then
  656.                 window.createTransposerBlock(window.lin, window.col, unit)
  657.             end
  658.         end
  659.  
  660.         if window.nextBlock() then
  661.             window.createInsertionBlock()
  662.         end
  663.     end
  664.  
  665.     window.recreateGui()
  666.  
  667.     return window
  668. end
  669.  
  670. ---@return autocompressor.save.unit[]
  671. local function exportData()
  672.     local data = {}
  673.     for addr, unit in pairs(unities) do
  674.         ---@class autocompressor.save.unit
  675.         ---@field inputs autocompressor.save.input[]
  676.         local unitData = {addr=addr, name=unit.name, inputs={}}
  677.         table.insert(data, unitData)
  678.  
  679.         for side, chest in pairs(unit:getInputs()) do
  680.             ---@class autocompressor.save.input
  681.             ---@field grids autocompressor.save.grid[]
  682.             local inputData = {side=side, grids={}}
  683.             table.insert(unitData.inputs, inputData)
  684.  
  685.             for k, grid in pairs(chest.grids) do
  686.                 ---@class autocompressor.save.grid
  687.                 ---@field matrix table<number, autocompressor.save.item>
  688.                 local gridData = {product=grid.product, amount=grid.amount, matrix={}}
  689.                 table.insert(inputData.grids, gridData)
  690.  
  691.                 for slot, item in pairs(grid.matrix) do
  692.                     ---@class autocompressor.save.item
  693.                     local itemData = {id=item.id,metadata=item.metadata,amount=item.amount,name=item.name}
  694.                     gridData.matrix[slot] = itemData
  695.                 end
  696.             end
  697.         end
  698.     end
  699.  
  700.     return data
  701. end
  702.  
  703.  
  704. ---@param save autocompressor.save.unit[]
  705. ---@return autocompressor.crafting_unit[]
  706. local function importData(save)
  707.     local loaded = {}
  708.     for i, unitData in ipairs(save) do
  709.         local unit = crafting_unit.wrap(unitData.addr, unitData.name)
  710.         loaded[unitData.addr] = unit
  711.         local chests = unit:getInputs()
  712.         for i, inputData in ipairs(unitData.inputs) do
  713.             local chest = chests[inputData.side]
  714.             for i, gridData in ipairs(inputData.grids) do
  715.                 local fakeStacks = {}
  716.                 for slot, item in pairs(gridData.matrix) do
  717.                     ---@type _item_stack
  718.                     local stack = {name=item.id,size=item.amount,label=item.name,damage=item.metadata,hasTag=false}
  719.                     fakeStacks[slot] = stack
  720.                 end
  721.                 chest:createGrid(fakeStacks, gridData.product, gridData.amount)
  722.             end
  723.         end
  724.     end
  725.     return loaded
  726. end
  727.  
  728. save = function ()
  729.     local data = serialization.serialize(exportData())
  730.     fs.makeDirectory("/var")
  731.     local file = io.open("/var/autocompressor.save", "w")
  732.     file:write(data)
  733.     file:close()
  734. end
  735.  
  736. local function load()
  737.     if fs.exists("/var/autocompressor.save") then
  738.         local file = io.open("/var/autocompressor.save", "r")
  739.         local data = file:read("*all")
  740.         file:close()
  741.         unities = importData(serialization.unserialize(data))
  742.     end
  743. end
  744.  
  745. -- End: Menu definitions
  746.  
  747. load()
  748.  
  749. local window = createWindow()
  750.  
  751. gui.clearScreen()
  752. gui.setTop("Automação da Prensa")
  753. gui.setBottom("Criado por youtube.com/ProgramandoMinecraft")
  754.  
  755. gui.displayGui(window.mainGui)
  756.  
  757. local updateInsertion = true
  758.  
  759. local service = coroutine.create(function ()
  760.     while true do
  761.         checkTime("Service Loop", function ()
  762.             for addr, unit in pairs(unities) do
  763.                 for k, chest in pairs(unit:getInputs()) do
  764.                     checkTime("scanPatterns", chest.scanPatterns, chest)
  765.                     coroutine.yield()
  766.                     --checkTime("processDelayedMoves", chest.processDelayedMoves, chest)
  767.                 end
  768.  
  769.                 for k, compressor in pairs(unit:getCompressors()) do
  770.                     checkTime("updateStatus", compressor.updateStatus, compressor)
  771.                     coroutine.yield()
  772.                 end
  773.             end
  774.         end )
  775.         coroutine.yield()
  776.     end
  777. end)
  778.  
  779. local p
  780. local update = coroutine.create(function ()
  781.     while true do
  782.         --[[
  783.         local s, i, v = pcall(next, window.transposers, p)
  784.         if not s or i == nil then
  785.             p = nil
  786.         else
  787.             if p == nil then
  788.                 p = 1
  789.             else
  790.                 p = p + 1
  791.             end
  792.             v.update()
  793.             --require("computer").beep()
  794.         end
  795.         coroutine.yield()
  796.         ]]--
  797.  
  798.         for addr, transposer in pairs(window.transposers) do
  799.             transposer.update()
  800.             coroutine.yield()
  801.         end
  802.     end
  803. end)
  804.  
  805. --[[
  806. thread.create(function ()
  807.     while true do
  808.         checkTime("Service Loop", function ()
  809.             for addr, unit in pairs(unities) do
  810.                 for k, chest in pairs(unit:getInputs()) do
  811.                     checkTime("scanPatterns", chest.scanPatterns, chest)
  812.                     checkTime("processDelayedMoves", chest.processDelayedMoves, chest)
  813.                 end
  814.  
  815.                 for k, compressor in pairs(unit:getCompressors()) do
  816.                     checkTime("updateStatus", compressor.updateStatus, compressor)
  817.                 end
  818.             end
  819.         end )
  820.     end
  821. end)
  822. ]]--
  823.  
  824.  
  825. -- Main loop
  826. while true do
  827.     gui.runGui(window.mainGui)
  828.     if updateInsertion then
  829.         updateInsertion = false
  830.         --require("computer").beep()
  831.         window.insertionBlock.update()
  832.         event.timer(3, function () updateInsertion = true end, 1)
  833.     end
  834.  
  835.     coroutine.resume(service)
  836.     coroutine.resume(update)
  837. end
Add Comment
Please, Sign In to add comment