Wassaa

enchTurtle

Jun 25th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- enchantTurtle.lua
  2.  
  3. -- Wrap the input chest in front
  4. local frontChest = peripheral.wrap("front")
  5. if not frontChest then
  6.   error("❌ No chest detected in front of the turtle")
  7. end
  8.  
  9. -- Helper to test if the chest is empty
  10. local function isChestEmpty(chest)
  11.   -- chest.list() returns a sparse table of slots → items
  12.   return next(chest.list()) == nil
  13. end
  14.  
  15. -- Main sorting loop
  16. while not isChestEmpty(frontChest) do
  17.   -- Always work in turtle slot 1
  18.   turtle.select(1)
  19.   -- Grab exactly one item from the chest in front
  20.   -- (succeeds only if there was at least one)
  21.   if not turtle.suck(1) then
  22.     -- nothing left?
  23.     break
  24.   end
  25.  
  26.   -- Inspect what we just pulled
  27.   local tDetail = turtle.getItemDetail(1)
  28.   if tDetail and tDetail.enchantments and #tDetail.enchantments > 0 then
  29.     -- Enchanted → drop up
  30.     if not turtle.dropUp() then
  31.       error("Could not drop enchanted item above!")
  32.     end
  33.     print(("🔼 Enchanted: %s ×1"):format(tDetail.name))
  34.   else
  35.     -- Unenchanted → drop down
  36.     if not turtle.dropDown() then
  37.       error("Could not drop normal item below!")
  38.     end
  39.     print(("🔽 Normal:    %s ×1"):format(tDetail and tDetail.name or "<unknown>"))
  40.   end
  41. end
  42.  
  43. print("✅ All items sorted.")
Add Comment
Please, Sign In to add comment