Advertisement
i6_quadcore

recipe

Feb 22nd, 2025
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.91 KB | None | 0 0
  1. local utils = require("utils")
  2.  
  3. return function(basalt, parentFrame, cookbook)
  4.     local frame = parentFrame:addFrame()
  5.  
  6.     local editing = nil
  7.  
  8.     local pattern = {}
  9.  
  10.     local itemLabel = label(frame, "Enter item ID", 3, 2)
  11.  
  12.     local itemInput = frame
  13.         :addInput()
  14.         :setPosition(3, 3)
  15.         :setSize(34, 1)
  16.         :setDefaultText("minecraft: ...")
  17.  
  18.     local itemList = frame
  19.         :addList()
  20.         :setPosition(3, 5)
  21.         :setSize(34, 10)
  22.         :setBackground(colors.black)
  23.         :setSelectionColor(colors.white)
  24.         :addItem("<none>", colors.black, colors.white)
  25.  
  26.     local usedColors = {}
  27.  
  28.     function addListItem(itemID)
  29.         local count = itemList:getItemCount()
  30.         if (count == 10) then
  31.             toast(frame, "Maximum items exceeded.", colors.red)
  32.             return 0
  33.         end
  34.  
  35.         local i = (count - 1) % colorCount + 1
  36.         while usedColors[i] do
  37.             i = i + 1
  38.         end
  39.         usedColors[i] = true
  40.         local currentColor = indexToColor[i]
  41.         itemList:addItem(itemID, colors.black, currentColor)
  42.         itemList:selectItem(count + 1)
  43.         return currentColor
  44.     end
  45.  
  46.     -- TODO: selection not updated when switching focus to grid immediately
  47.     itemInput:onLoseFocus(
  48.         function()
  49.             local itemName = itemInput:getValue()
  50.             if(#itemName > 0) then
  51.                 addListItem(itemName)
  52.                 itemInput:setValue("")
  53.             end
  54.         end)
  55.  
  56.     local gridIndex = 0
  57.     local gridSelector = {}
  58.     for pos = 1, 9 do
  59.         xOffset = (pos-1)%3*4
  60.         yOffset = math.floor((pos-1)/3)*3
  61.         gridSelector[pos] = frame:addPane()
  62.             :setSize(3, 2)
  63.             :setPosition(39 + xOffset, 3 + yOffset)
  64.             :onClick(
  65.                 function()
  66.                     local itemIndex = itemList:getItemIndex()
  67.                     if (itemIndex == nil) then return end
  68.  
  69.                     if (itemIndex == 1) then
  70.                         gridSelector[pos]:setBackground(colors.black)
  71.                         pattern[pos] = nil
  72.                         return
  73.                     end
  74.  
  75.                     local item = itemList:getItem(itemIndex)
  76.                     pattern[pos] = item.text
  77.                     gridSelector[pos]:setBackground(item.fgCol)
  78.                 end
  79.             )
  80.     end
  81.  
  82.     local recipeLabel = label(frame, "Enter result ID", 3, 16)
  83.     label(frame, "Qty", 34, 16)
  84.  
  85.     local recipeNameInput = frame
  86.         :addInput()
  87.         :setPosition(3, 17)
  88.         :setSize(30, 1)
  89.         :setDefaultText("minecraft: ...")
  90.  
  91.     local recipeCountInput = frame
  92.         :addInput()
  93.         :setPosition(34, 17)
  94.         :setSize(3, 1)
  95.         :setDefaultText("1")
  96.         :setInputType("number")
  97.         :setInputLimit(2)
  98.        
  99.  
  100.     function clearGrid()
  101.         for pos=1, 9 do gridSelector[pos]:setBackground(colors.black) end
  102.         pattern = {}
  103.         usedColors = {}
  104.     end
  105.  
  106.     function clearAll()
  107.         clearGrid()
  108.         itemList:clear():addItem("<none>", colors.black, colors.white)
  109.         recipeNameInput:setValue("")
  110.         recipeCountInput:setValue("")
  111.        
  112.         editing = false
  113.         removeStatus()
  114.     end
  115.  
  116.     local clearGridButton = label(frame, "  Clear ", 3, 17)
  117.         :setPosition(40, 12)
  118.         :setBackground(colors.gray)
  119.         :setForeground(colors.red)
  120.         :onClick(clearGrid)
  121.    
  122.     local clearAllButton = label(frame, " Clear all ", 3, 17)
  123.         :setPosition(39, 14)
  124.         :setBackground(colors.gray)
  125.         :setForeground(colors.red)
  126.         :onClick(clearAll)
  127.  
  128.     local commitButton = label(frame, " Save recipe ", 3, 17)
  129.         :setPosition(38, 17)
  130.         :setBackground(colors.gray)
  131.         :setForeground(colors.green)
  132.         :onClick(function()
  133.             local recipeName = recipeNameInput:getValue()
  134.             if (#recipeName == 0) then
  135.                 toast(frame, "Enter ID of resulting item.", colors.red)
  136.             else
  137.                 local count = tonumber("0"..recipeCountInput:getValue())
  138.  
  139.                 cookbook[recipeName] = {['pattern'] = pattern, ['count'] = count}
  140.  
  141.                 if (editing) then
  142.                     cookbook[editing] = nil
  143.                     editing = recipeName
  144.                 end
  145.  
  146.                 saveCookbook(cookbook)
  147.                 toast(frame, "Recipe saved successfully.", colors.blue)
  148.             end
  149.         end)
  150.  
  151.     function onKeyHandler(self, event, key)
  152.         if (key == keys.delete and itemList:isFocused()) then
  153.             local removeIndex = itemList:getItemIndex()
  154.             if (removeIndex == nil or removeIndex == 1) then return end
  155.            
  156.             local removeItem = itemList:getItem(removeIndex)
  157.  
  158.             usedColors[colorToIndex[removeItem.fgCol]] = nil
  159.             itemList:removeItem(removeIndex)
  160.  
  161.             for pos = 1, 9 do
  162.                 if (recipe[pos] == removeItem.text) then
  163.                     gridSelector[pos]:setBackground(colors.black)
  164.                     recipe[pos] = nil
  165.                 end
  166.             end
  167.  
  168.             -- Select nearest item in list
  169.             if (removeIndex - 1 == itemList:getItemCount()) then removeIndex = removeIndex - 1 end
  170.             itemList:selectItem(removeIndex)
  171.         end
  172.     end
  173.     frame:onKey(onKeyHandler)
  174.  
  175.     function populate(newItem, newRecipe)
  176.         editing = newItem
  177.         status(frame, "Editing", colors.blue)
  178.         clearAll()
  179.         recipeNameInput:setValue(newItem)
  180.         recipeCountInput:setValue(newRecipe.count)
  181.         pattern = deepCopy(newRecipe.pattern)
  182.  
  183.         itemColors = {}
  184.  
  185.         for pos, item in pairs(pattern) do
  186.             if itemColors[item] == nil then
  187.                 itemColors[item] = addListItem(item)
  188.             end
  189.             gridSelector[pos]:setBackground(itemColors[item])
  190.         end
  191.        
  192.     end
  193.  
  194.     return frame, populate
  195. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement