lui2k_

ATM10 - ME Terminal to drawer back to ME Terminal

Jul 26th, 2025
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.41 KB | Gaming | 0 0
  1. -- Wrap peripherals
  2. local me = peripheral.wrap("bottom")  -- ME Bridge below
  3. local drawer = peripheral.wrap("left") -- Drawer to the left
  4. local monitor = peripheral.find("monitor")  -- Find connected monitor
  5.  
  6. -- Setup monitor
  7. monitor.setTextScale(0.5)
  8. monitor.clear()
  9. local line = 1
  10.  
  11. -- Helper function to log to monitor with timestamp
  12. local function log(message)
  13.     local time = textutils.formatTime(os.time(), true)
  14.     monitor.setCursorPos(1, line)
  15.     monitor.write("[" .. time .. "] " .. message)
  16.     line = line + 1
  17.     if line > 20 then
  18.         monitor.scroll(1)
  19.         line = 20
  20.     end
  21. end
  22.  
  23. -- Example item name (Stone Essence from Mystical Agriculture)
  24. -- This will be overridden by whatever is in the drawer
  25. local exampleItem = "mysticalagriculture:stone_essence"
  26.  
  27. -- Detect item stored in drawer
  28. local drawerContents = drawer.list()
  29. local itemName = nil
  30.  
  31. for _, item in pairs(drawerContents) do
  32.     itemName = item.name
  33.     break  -- only one slot in 1x1 drawer
  34. end
  35.  
  36. if not itemName then
  37.     log("No item found in drawer.")
  38.     return
  39. end
  40.  
  41. log("🔍 Detected item: " .. itemName .. " (e.g., " .. exampleItem .. ")")
  42.  
  43. -- Step 1: Extract all matching items from ME into drawer
  44. local items = me.getItems()
  45. for _, item in pairs(items) do
  46.     if item.name == itemName then
  47.         local remaining = item.amount
  48.         while remaining > 0 do
  49.             local toExtract = math.min(remaining, 64)
  50.             local extracted = me.exportItem({
  51.                 name = item.name,
  52.                 count = toExtract
  53.             }, "left")
  54.  
  55.             log("Extracted " .. extracted .. " " .. itemName .. " to drawer")
  56.             remaining = remaining - extracted
  57.             if extracted == 0 then break end
  58.         end
  59.     end
  60. end
  61.  
  62. -- Step 2: Re-insert all matching items from drawer back to ME
  63. for slot, item in pairs(drawer.list()) do
  64.     if item.name == itemName then
  65.         local remaining = item.count
  66.         while remaining > 0 do
  67.             local toInsert = math.min(remaining, 64)
  68.             local inserted = me.importItem({
  69.                 name = item.name,
  70.                 count = toInsert
  71.             }, "left", slot)
  72.  
  73.             log("Inserted " .. inserted .. " " .. itemName .. " from drawer slot " .. slot)
  74.             remaining = remaining - inserted
  75.             if inserted == 0 then break end
  76.         end
  77.     end
  78. end
  79.  
  80. log("Cycle complete for item: " .. itemName)
  81.  
Advertisement
Add Comment
Please, Sign In to add comment