Advertisement
gravitowl

treefarm

Dec 3rd, 2024 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.74 KB | None | 0 0
  1. local args = {...};
  2.  
  3. function table:contains(value, recursive)
  4.     recursive = recursive or false
  5.     for key, table_value in pairs(self) do
  6.         if recursive and type(table_value) == "table" then
  7.             local contains = table.contains(table_value, value);
  8.             if contains then return true end
  9.         end
  10.  
  11.         if table_value == value then
  12.             return true;
  13.         end
  14.     end
  15.  
  16.     return false;
  17. end
  18.  
  19. local function findItemInInventory(item)
  20.     for i = 1, 16 do
  21.         local itemDetails = turtle.getItemDetail(i);
  22.  
  23.         if itemDetails and itemDetails.name == item then
  24.             return i;
  25.         end
  26.     end
  27.  
  28.     return -1;
  29. end
  30.  
  31. local function isBlockPartOfTree(blockData)
  32.     local treeBlockTags = {
  33.         "minecraft:logs",
  34.         --"minecraft:leaves"
  35.     }
  36.  
  37.     for _, tag in pairs(treeBlockTags) do
  38.         if blockData.tags and blockData.tags[tag] then
  39.             return true;
  40.         end
  41.     end
  42.  
  43.     return false;
  44. end
  45.  
  46. local function fellTree()
  47.     turtle.dig();
  48.     turtle.forward();
  49.  
  50.     local hasBlock, data = turtle.inspectDown();
  51.     turtle.digDown();
  52.  
  53.     local heightOfTree = 0;
  54.  
  55.     while hasBlock do
  56.         hasBlock, data = turtle.inspectUp();
  57.  
  58.         if not hasBlock or not isBlockPartOfTree(data) then
  59.             break;
  60.         end
  61.  
  62.         turtle.digUp();
  63.         turtle.up();
  64.  
  65.         for i = 1, 3 do
  66.             turtle.turnRight();
  67.             turtle.dig();
  68.         end
  69.  
  70.         turtle.turnRight();
  71.  
  72.         heightOfTree = heightOfTree + 1;
  73.     end
  74.  
  75.     for i = 1, heightOfTree do
  76.         turtle.down();
  77.     end
  78. end
  79.  
  80. local function plantSapling()
  81.     local saplingItemSlot = findItemInInventory("minecraft:spruce_sapling");
  82.  
  83.     if saplingItemSlot == -1 then
  84.         return;
  85.     end
  86.  
  87.     turtle.select(saplingItemSlot);
  88.     turtle.placeDown();
  89. end
  90.  
  91. local function checkIfEnabled()
  92.     return redstone.getInput("top");
  93. end
  94.  
  95. local function getFirstEmptySlot()
  96.     for i=1, 16 do
  97.         if turtle.getItemCount(i) == 0 then
  98.             return i
  99.         end
  100.     end
  101.    
  102.     return -1
  103. end
  104.  
  105. local function getTotalItemCount(item)
  106.     local totalCount = 0;
  107.     for i=1, 16 do
  108.         local itemDetail = turtle.getItemDetail(i);
  109.  
  110.         if itemDetail and itemDetail.name == item then
  111.             totalCount = totalCount + turtle.getItemCount(i);
  112.         end            
  113.     end
  114.  
  115.     return totalCount;
  116. end
  117.  
  118. local function throwawayItem()
  119.     turtle.turnRight();
  120.     turtle.drop()
  121.     turtle.turnLeft();
  122. end
  123.  
  124. local function pickupSaplings(amountNeeded)
  125.     local emptySlot = getFirstEmptySlot();
  126.     while emptySlot == -1 do
  127.         print("No empty slot for saplings!");
  128.         sleep(3);
  129.         emptySlot = getFirstEmptySlot();
  130.     end
  131.  
  132.     turtle.select(emptySlot);
  133.  
  134.     local amountNeededLeft = amountNeeded;
  135.     local maxTries = 20;
  136.  
  137.     while maxTries > 0 and amountNeeded > 0 do
  138.         turtle.suckDown(math.min(amountNeededLeft, 64));
  139.         local itemDetail = turtle.getItemDetail();
  140.  
  141.         if itemDetail then
  142.             if itemDetail.name ~= "minecraft:spruce_sapling" then
  143.                 throwawayItem();
  144.             end
  145.         end
  146.  
  147.         amountNeededLeft = amountNeeded - getTotalItemCount("minecraft:spruce_sapling", true);
  148.         maxTries = maxTries - 1;
  149.     end
  150.  
  151.     if amountNeededLeft > 0 then
  152.         print("Not enough saplings, still farming.");
  153.     end
  154. end
  155.  
  156. local function dropLogs()
  157.     for i=1, 16 do
  158.         turtle.select(i);
  159.  
  160.         local itemDetail = turtle.getItemDetail();
  161.  
  162.         if itemDetail then
  163.             if itemDetail.name == "minecraft:spruce_log" then
  164.                 local couldDrop = turtle.dropDown();
  165.  
  166.                 if not couldDrop then
  167.                     throwawayItem();
  168.                 end
  169.             else
  170.                 throwawayItem();
  171.             end
  172.         end
  173.  
  174.     end
  175. end
  176.  
  177. local function printHelp()
  178.     print("USAGE: treefarm <amountOfTrees> <amountOfRows>");
  179.  
  180.     print("DESCRIPTION:");
  181.     print(" - <amountOfTrees> The amount of trees to farm in a single row.");
  182.     print(" - <amountOfRows> The amount of rows to farm.");
  183. end
  184.  
  185. local function parseCliArgs(cliArgs)
  186.     if #cliArgs < 2 then
  187.         printHelp();
  188.     end
  189.  
  190.     local amountOfTrees = tonumber(cliArgs[1]);
  191.     local amountOfRows = tonumber(cliArgs[2]);
  192.  
  193.     if amountOfTrees and amountOfRows then
  194.         return {
  195.             amountOfTrees = amountOfTrees,
  196.             amountOfRows = amountOfRows,
  197.         }
  198.     else
  199.         printHelp();
  200.         return nil;
  201.     end
  202. end
  203.  
  204. local function fellFarm(amountOfTrees, amountOfRows)
  205.     for i = 1, amountOfRows do
  206.         for j = 1, amountOfTrees do
  207.             fellTree();
  208.         end
  209.  
  210.         local turnDirection = turtle.turnRight;
  211.         if i%2 == 0 then
  212.             turnDirection = turtle.turnLeft;
  213.         end
  214.  
  215.         turtle.dig();
  216.         turtle.forward();
  217.         if i ~= amountOfRows then
  218.             turnDirection();
  219.             turtle.dig();
  220.             turtle.forward();
  221.             turtle.dig();
  222.             turtle.forward();
  223.             turnDirection();
  224.         end
  225.     end
  226. end
  227.  
  228. local function plantSaplingsInFarm(amountOfTrees, amountOfRows)
  229.     for i = 1, amountOfRows do
  230.         for j = 1, amountOfTrees do
  231.             turtle.dig();
  232.             turtle.forward();
  233.             plantSapling();
  234.         end
  235.  
  236.         local turnDirection = turtle.turnRight;
  237.         if i%2 == 0 then
  238.             turnDirection = turtle.turnLeft;
  239.         end
  240.  
  241.         turtle.dig();
  242.         turtle.forward();
  243.         if i ~= amountOfRows then
  244.             turnDirection();
  245.             turtle.dig()
  246.             turtle.forward();
  247.             turtle.dig()
  248.             turtle.forward();
  249.             turnDirection();
  250.         end
  251.     end
  252. end
  253.  
  254. local function farm(amountOfTrees, amountOfRows);
  255.     while true do
  256.         while not checkIfEnabled() do
  257.             sleep(10)
  258.         end
  259.         pickupSaplings(amountOfTrees*amountOfRows);
  260.         turtle.forward();
  261.         turtle.forward();
  262.         turtle.forward();
  263.         turtle.forward();
  264.         plantSaplingsInFarm(amountOfTrees, amountOfRows);
  265.         turtle.turnRight();
  266.         turtle.turnRight();
  267.  
  268.         sleep(30);
  269.  
  270.         fellFarm(amountOfTrees, amountOfRows);
  271.         turtle.forward();
  272.         turtle.forward();
  273.         turtle.forward();
  274.         turtle.forward();
  275.         turtle.turnLeft();
  276.         turtle.turnLeft();
  277.         dropLogs();
  278.  
  279.         sleep(30);
  280.     end
  281.  
  282. end
  283.  
  284. local function main()
  285.     local cliArgs = parseCliArgs(args);
  286.  
  287.     if not cliArgs then
  288.         return;
  289.     end
  290.  
  291.     local amountOfTrees = cliArgs.amountOfTrees;
  292.     local amountOfRows = cliArgs.amountOfRows;
  293.  
  294.     farm(amountOfTrees, amountOfRows);
  295. end
  296.  
  297. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement