Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...};
- function table:contains(value, recursive)
- recursive = recursive or false
- for key, table_value in pairs(self) do
- if recursive and type(table_value) == "table" then
- local contains = table.contains(table_value, value);
- if contains then return true end
- end
- if table_value == value then
- return true;
- end
- end
- return false;
- end
- local function findItemInInventory(item)
- for i = 1, 16 do
- local itemDetails = turtle.getItemDetail(i);
- if itemDetails and itemDetails.name == item then
- return i;
- end
- end
- return -1;
- end
- local function isBlockPartOfTree(blockData)
- local treeBlockTags = {
- "minecraft:logs",
- --"minecraft:leaves"
- }
- for _, tag in pairs(treeBlockTags) do
- if blockData.tags and blockData.tags[tag] then
- return true;
- end
- end
- return false;
- end
- local function fellTree()
- turtle.dig();
- turtle.forward();
- local hasBlock, data = turtle.inspectDown();
- turtle.digDown();
- local heightOfTree = 0;
- while hasBlock do
- hasBlock, data = turtle.inspectUp();
- if not hasBlock or not isBlockPartOfTree(data) then
- break;
- end
- turtle.digUp();
- turtle.up();
- for i = 1, 3 do
- turtle.turnRight();
- turtle.dig();
- end
- turtle.turnRight();
- heightOfTree = heightOfTree + 1;
- end
- for i = 1, heightOfTree do
- turtle.down();
- end
- end
- local function plantSapling()
- local saplingItemSlot = findItemInInventory("minecraft:spruce_sapling");
- if saplingItemSlot == -1 then
- return;
- end
- turtle.select(saplingItemSlot);
- turtle.placeDown();
- end
- local function checkIfEnabled()
- return redstone.getInput("top");
- end
- local function getFirstEmptySlot()
- for i=1, 16 do
- if turtle.getItemCount(i) == 0 then
- return i
- end
- end
- return -1
- end
- local function getTotalItemCount(item)
- local totalCount = 0;
- for i=1, 16 do
- local itemDetail = turtle.getItemDetail(i);
- if itemDetail and itemDetail.name == item then
- totalCount = totalCount + turtle.getItemCount(i);
- end
- end
- return totalCount;
- end
- local function throwawayItem()
- turtle.turnRight();
- turtle.drop()
- turtle.turnLeft();
- end
- local function pickupSaplings(amountNeeded)
- local emptySlot = getFirstEmptySlot();
- while emptySlot == -1 do
- print("No empty slot for saplings!");
- sleep(3);
- emptySlot = getFirstEmptySlot();
- end
- turtle.select(emptySlot);
- local amountNeededLeft = amountNeeded;
- local maxTries = 20;
- while maxTries > 0 and amountNeeded > 0 do
- turtle.suckDown(math.min(amountNeededLeft, 64));
- local itemDetail = turtle.getItemDetail();
- if itemDetail then
- if itemDetail.name ~= "minecraft:spruce_sapling" then
- throwawayItem();
- end
- end
- amountNeededLeft = amountNeeded - getTotalItemCount("minecraft:spruce_sapling", true);
- maxTries = maxTries - 1;
- end
- if amountNeededLeft > 0 then
- print("Not enough saplings, still farming.");
- end
- end
- local function dropLogs()
- for i=1, 16 do
- turtle.select(i);
- local itemDetail = turtle.getItemDetail();
- if itemDetail then
- if itemDetail.name == "minecraft:spruce_log" then
- local couldDrop = turtle.dropDown();
- if not couldDrop then
- throwawayItem();
- end
- else
- throwawayItem();
- end
- end
- end
- end
- local function printHelp()
- print("USAGE: treefarm <amountOfTrees> <amountOfRows>");
- print("DESCRIPTION:");
- print(" - <amountOfTrees> The amount of trees to farm in a single row.");
- print(" - <amountOfRows> The amount of rows to farm.");
- end
- local function parseCliArgs(cliArgs)
- if #cliArgs < 2 then
- printHelp();
- end
- local amountOfTrees = tonumber(cliArgs[1]);
- local amountOfRows = tonumber(cliArgs[2]);
- if amountOfTrees and amountOfRows then
- return {
- amountOfTrees = amountOfTrees,
- amountOfRows = amountOfRows,
- }
- else
- printHelp();
- return nil;
- end
- end
- local function fellFarm(amountOfTrees, amountOfRows)
- for i = 1, amountOfRows do
- for j = 1, amountOfTrees do
- fellTree();
- end
- local turnDirection = turtle.turnRight;
- if i%2 == 0 then
- turnDirection = turtle.turnLeft;
- end
- turtle.dig();
- turtle.forward();
- if i ~= amountOfRows then
- turnDirection();
- turtle.dig();
- turtle.forward();
- turtle.dig();
- turtle.forward();
- turnDirection();
- end
- end
- end
- local function plantSaplingsInFarm(amountOfTrees, amountOfRows)
- for i = 1, amountOfRows do
- for j = 1, amountOfTrees do
- turtle.dig();
- turtle.forward();
- plantSapling();
- end
- local turnDirection = turtle.turnRight;
- if i%2 == 0 then
- turnDirection = turtle.turnLeft;
- end
- turtle.dig();
- turtle.forward();
- if i ~= amountOfRows then
- turnDirection();
- turtle.dig()
- turtle.forward();
- turtle.dig()
- turtle.forward();
- turnDirection();
- end
- end
- end
- local function farm(amountOfTrees, amountOfRows);
- while true do
- while not checkIfEnabled() do
- sleep(10)
- end
- pickupSaplings(amountOfTrees*amountOfRows);
- turtle.forward();
- turtle.forward();
- turtle.forward();
- turtle.forward();
- plantSaplingsInFarm(amountOfTrees, amountOfRows);
- turtle.turnRight();
- turtle.turnRight();
- sleep(30);
- fellFarm(amountOfTrees, amountOfRows);
- turtle.forward();
- turtle.forward();
- turtle.forward();
- turtle.forward();
- turtle.turnLeft();
- turtle.turnLeft();
- dropLogs();
- sleep(30);
- end
- end
- local function main()
- local cliArgs = parseCliArgs(args);
- if not cliArgs then
- return;
- end
- local amountOfTrees = cliArgs.amountOfTrees;
- local amountOfRows = cliArgs.amountOfRows;
- farm(amountOfTrees, amountOfRows);
- end
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement