Advertisement
Shaka01

crafter autocraft editor 2024 - dependancy download

Dec 3rd, 2024 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.41 KB | None | 0 0
  1. -- File to store the list of items
  2. local filename = "items_list.lua"
  3. local turtleName = "turtle_2"
  4. local shaka = require("API")
  5. local fromInventories = shaka.readFile("inventories") or {}
  6.  
  7. -- Save list to file
  8. local function saveList(list)
  9.     local file = fs.open(filename, "w")
  10.     if file then
  11.         file.write(textutils.serialize(list))
  12.         file.close()
  13.     else
  14.         error("Failed to save list.")
  15.     end
  16. end
  17.  
  18. -- Read list from file
  19. local function readList()
  20.     if not fs.exists(filename) then return {} end
  21.     local file = fs.open(filename, "r")
  22.     local content = file.readAll()
  23.     file.close()
  24.     local success, result = pcall(load("return " .. content))
  25.     return success and result or {}
  26. end
  27.  
  28. -- Display the list with minimal UI
  29. local function displayList(list, startIndex, selectedIndex)
  30.     term.clear()
  31.     term.setCursorPos(1, 1)
  32.     print("W/S to scroll, [e]: edit, [i]: update from turtle, [q]: quit:")
  33.  
  34.     local endIndex = math.min(startIndex + 9, #list)
  35.     for i = startIndex, endIndex do
  36.         local item = list[i]
  37.         if i == selectedIndex then
  38.             term.setTextColor(colors.yellow)
  39.             print("> " .. item.name .. " (x" .. item.amount .. ")")
  40.         else
  41.             term.setTextColor(colors.white)
  42.             print("  " .. item.name .. " (x" .. item.amount .. ")")
  43.         end
  44.     end
  45.  
  46.     -- Display scrolling indicators
  47.     term.setTextColor(colors.gray)
  48.     if startIndex > 1 then
  49.         print("\n(Scroll up for more...)")
  50.     end
  51.     if endIndex < #list then
  52.         print("\n(Scroll down for more...)")
  53.     end
  54.     term.setTextColor(colors.white)
  55. end
  56.  
  57. -- Scan turtle inventory and update the list
  58. local function scanTurtleInventory(list)
  59.     for i = 1, 16 do
  60.         local item = turtle.getItemDetail(i)
  61.         if item then
  62.             local found = false
  63.             for _, entry in ipairs(list) do
  64.                 if entry.name == item.name then
  65.                     entry.amount = entry.amount + item.count
  66.                     found = true
  67.                     break
  68.                 end
  69.             end
  70.             if not found then
  71.                 table.insert(list, {name = item.name, amount = item.count})
  72.             end
  73.         end
  74.     end
  75.     saveList(list) -- Save updated list
  76. end
  77.  
  78. -- Main program logic
  79. local function main()
  80.     local list = readList()
  81.     local startIndex = 1
  82.     local selectedIndex = 1
  83.  
  84.     while true do
  85.         displayList(list, startIndex, selectedIndex)
  86.  
  87.         local event, key = os.pullEvent("key")
  88.         if key == keys.q then
  89.             os.reboot()
  90.             break -- Quit the program
  91.         elseif key == keys.w or key == keys.up then
  92.             if selectedIndex > 1 then
  93.                 selectedIndex = selectedIndex - 1
  94.                 if selectedIndex < startIndex then
  95.                     startIndex = startIndex - 1
  96.                 end
  97.             end
  98.         elseif key == keys.s or key == keys.down then
  99.             if selectedIndex < #list then
  100.                 selectedIndex = selectedIndex + 1
  101.                 if selectedIndex > startIndex + 9 then
  102.                     startIndex = startIndex + 1
  103.                 end
  104.             end
  105.         elseif key == keys.i then
  106.             term.clear()
  107.             term.setCursorPos(1, 1)
  108.             print("Scanning turtle inventory and updating list...")
  109.             scanTurtleInventory(list)
  110.             print("Inventory updated.")
  111.             sleep(1)
  112.         elseif key == keys.e then
  113.             term.clear()
  114.             term.setCursorPos(1, 1)
  115.             local item = list[selectedIndex]
  116.             shaka.changeColors(colors.gray, colors.black)
  117.             term.clearLine()
  118.             print("Editing item: ")
  119.             shaka.changeColors(colors.black, colors.yellow)
  120.             print(shaka.prettyName(item.name))
  121.             shaka.changeColors(colors.black, colors.white)
  122.             shaka.nextLine()
  123.             write("Enter new amount: ")
  124.             shaka.changeColors(colors.black, colors.yellow)
  125.             sleep(0.3)
  126.             local newAmount = tonumber(read())
  127.             if newAmount and newAmount > 0 then
  128.                 item.amount = newAmount
  129.                 saveList(list)
  130.                 shaka.changeColors(colors.black, colors.green)
  131.                 print("Item updated.")
  132.             else
  133.                 shaka.changeColors(colors.black, colors.red)
  134.                 print("Invalid amount.")
  135.             end
  136.             sleep(1)
  137.         end
  138.     end
  139. end
  140.  
  141. -- Run the program
  142. main()
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement