Advertisement
Shaka01

meBridgeRestocker

Oct 27th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | None | 0 0
  1. -- File paths and ME Bridge setup
  2. local thresholdFile = "thresholds.txt"
  3. local meBridge = peripheral.wrap("front") -- Change to the actual side of the ME Bridge
  4. local editing = false
  5.  
  6. -- Load thresholds from file
  7. local function loadThresholds()
  8.     local thresholds = {}
  9.     if not fs.exists(thresholdFile) then
  10.         print("Threshold file not found. Creating a new one.")
  11.         local file = fs.open(thresholdFile, "w")
  12.         file.close()
  13.         return thresholds
  14.     end
  15.    
  16.     local file = fs.open(thresholdFile, "r")
  17.     while true do
  18.         local line = file.readLine()
  19.         if not line then break end
  20.         local name, threshold = line:match("(.+)=(%d+)")
  21.         if name and threshold then
  22.             thresholds[name] = tonumber(threshold)
  23.         end
  24.     end
  25.     file.close()
  26.     return thresholds
  27. end
  28.  
  29. -- Save thresholds to file
  30. local function saveThresholds(thresholds)
  31.     local file = fs.open(thresholdFile, "w")
  32.     for name, threshold in pairs(thresholds) do
  33.         file.writeLine(name .. "=" .. threshold)
  34.     end
  35.     file.close()
  36. end
  37.  
  38. -- Set a new threshold for an item
  39. local function setThreshold(name, threshold)
  40.     local thresholds = loadThresholds()
  41.     thresholds[name] = threshold
  42.     saveThresholds(thresholds)
  43. end
  44.  
  45. -- Prompt user to set a threshold based on item in turtle slot
  46. local function promptThreshold()
  47.     -- Check if there's an item in the first slot
  48.     local item = turtle.getItemDetail(1)
  49.     if item then
  50.         local itemName = item.name
  51.         print("Detected item:", itemName)
  52.         print("Enter the threshold for this item:")
  53.         local threshold = tonumber(read())
  54.         term.clear()
  55.         term.setCursorPos(1, 1)
  56.         if threshold then
  57.             setThreshold(itemName, threshold)
  58.             print("Threshold for", itemName, "set to", threshold)
  59.         else
  60.             print("Invalid input. Please enter a number.")
  61.         end
  62.         term.setTextColor(colors.green)
  63.         textutils.slowPrint("Success!", 4)
  64.         term.setTextColor(colors.white)
  65.         sleep(1)
  66.         term.clear()
  67.     else
  68.         term.clear()
  69.         term.setCursorPos(1, 1)
  70.         print("Place item in slot 1 and hit enter.")
  71.     end
  72. end
  73.  
  74. -- Open thresholds.txt in edit mode
  75. local function editThresholdsFile()
  76.     editing = true
  77.     term.clear()
  78.     term.setCursorPos(1, 1)
  79.     shell.run("edit", thresholdFile) -- Open the editor
  80.     editing = false
  81. end
  82.  
  83. -- Check inventory and craft items if below threshold
  84. local function checkAndCraft()
  85.     local thresholds = loadThresholds()
  86.     local items = meBridge.listItems()
  87.  
  88.     for name, threshold in pairs(thresholds) do
  89.         local item = meBridge.getItem({name = name})
  90.         local count = item and item.amount or 0
  91.  
  92.         if count < threshold then
  93.             local crafting, text = meBridge.isItemCrafting({name = name})
  94.             if crafting == false then
  95.                 term.setTextColor(colors.yellow)
  96.                 print(">".. name.. "<")
  97.                 local success = meBridge.craftItem({name = name, count = threshold - count})
  98.                 if success then
  99.                     term.setTextColor(colors.green)
  100.                     print("Success.")
  101.                     term.setTextColor(colors.white)
  102.                 else
  103.                     term.setTextColor(colors.red)
  104.                     print("Fail:", name)
  105.                     term.setTextColor(colors.white)
  106.                 end
  107.             end
  108.         end
  109.     end
  110. end
  111.  
  112. -- Main loop with parallel execution for key detection and crafting
  113. local function handleKeyPress()
  114.     while true do
  115.         local event, key = os.pullEvent("key")
  116.         if key == keys.space then
  117.             editThresholdsFile()   -- Open the editor
  118.         elseif key == keys.enter then
  119.             promptThreshold()       -- Prompt user to set threshold based on turtle slot 1
  120.         end
  121.         sleep(0.1)  -- Short sleep to prevent overwhelming the event loop
  122.     end
  123. end
  124.  
  125. local function runCraftingCheck()
  126.     while true do
  127.         if editing == false then
  128.             checkAndCraft()         -- Continuously check if any items need to be crafted
  129.         end
  130.         sleep(1)   -- Adjust sleep time as needed
  131.     end
  132. end
  133.  
  134. -- Run both functions in parallel
  135. parallel.waitForAny(handleKeyPress, runCraftingCheck)
  136.  
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement