CelticCoder

turtleChestTake

Nov 15th, 2023 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.22 KB | None | 0 0
  1. -- Check if there's a block in front of the turtle
  2. if turtle.detect() then
  3.     -- If there's a block, check if it's a chest
  4.     local success, data = turtle.inspect()
  5.    
  6.     if success then
  7.         -- 'inspect()' returns true if it succeeded and provides data about the block
  8.         if data.name == "minecraft:chest" then
  9.             print("There is a chest in front of the turtle")
  10.            
  11.             -- 'data' contains information about the block
  12.            
  13.             -- Take items from the chest starting from slot 2 (excluding slot 1)
  14.             local slotsToFill = 32  -- Number of slots to attempt to fill
  15.             for slot = 1, slotsToFill do
  16.                 if turtle.getItemCount(slot) == 0 then
  17.                     -- If the current slot is empty, attempt to take items
  18.                     if turtle.suck(slot) then
  19.                         print("Took items from slot " .. slot)
  20.                     else
  21.                         print("Error")
  22.                     end
  23.                 end
  24.             end
  25.             turtle.select(1)
  26.         else
  27.             print("There is a block, but it's not a chest")
  28.         end
  29.     else
  30.         print("Failed to inspect")
  31.     end
  32. else
  33.     print("There's nothing in front of the turtle")
  34. end
  35.  
Add Comment
Please, Sign In to add comment