Advertisement
eniallator

mControl - Farm Turtle Plugin

Jan 23rd, 2016
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. local version = "1.2.2"
  2.  
  3. local function validateItem(itemName)
  4.   if itemName == 'minecraft:wheat_seeds' or itemName == 'minecraft:carrot' or itemName == 'minecraft:potato' then
  5.     return true
  6.   end
  7. end
  8.  
  9. local function validateBlock(blockName)
  10.   if blockName == 'minecraft:wheat' or blockName == 'minecraft:carrots' or blockName == 'minecraft:potatoes' then
  11.     return true
  12.   end
  13. end
  14.  
  15. local function replaceCrop()
  16.   local success, blockBelow = turtle.inspectDown()
  17.  
  18.   if success and validateBlock(blockBelow.name) and blockBelow.metadata == 7 then
  19.     turtle.digDown()
  20.     turtle.placeDown()
  21.   end
  22. end
  23.  
  24. local move = {
  25.   forward = function()
  26.     while not turtle.forward() do
  27.       turtle.dig()
  28.       turtle.attack()
  29.     end
  30.   end,
  31.  
  32.   up = function()
  33.     while not turtle.up() do
  34.       turtle.digUp()
  35.       turtle.attackUp()
  36.     end
  37.   end,
  38.  
  39.   down = function()
  40.     while not turtle.down() do
  41.       turtle.digDown()
  42.       turtle.attackDown()
  43.     end
  44.   end
  45. }
  46.  
  47. local function checkSeeds()
  48.   seedCount = 0
  49.  
  50.   for i = 1, 16 do
  51.     item = turtle.getItemDetail(i)
  52.  
  53.     if item and validateItem(item.name) then
  54.       seedCount = seedCount + item.count
  55.     end
  56.   end
  57.  
  58.   if seedCount >= 16 then
  59.     return true
  60.   end
  61. end
  62.  
  63. local function getSeeds()
  64.   firstSeed = nil
  65.   gotSeed = false
  66.  
  67.   for i = 1, 16 do
  68.     item = turtle.getItemDetail(i)
  69.  
  70.     if item and validateItem(item.name) then
  71.       if firstSeed then
  72.         if turtle.getItemCount(firstSeed) >= 16 then
  73.           turtle.select(firstSeed)
  74.           break
  75.  
  76.         else
  77.           turtle.select(i)
  78.           turtle.transferTo(firstSeed)
  79.         end
  80.  
  81.       else
  82.         firstSeed = i
  83.       end
  84.     end
  85.   end
  86.  
  87.   if turtle.getItemCount(firstSeed) >= 16 then
  88.     turtle.select(firstSeed)
  89.   end
  90. end
  91.  
  92. local function checkLog(dir)
  93.   local success, blockData = turtle['inspect' .. (dir and dir or '')]()
  94.  
  95.   if success and blockData.name:find('minecraft:log') then
  96.     return true
  97.   end
  98. end
  99.  
  100. local function chopTree()
  101.   if checkLog('Down') then
  102.     turtle.digDown()
  103.     local height = 0
  104.  
  105.     while checkLog('Up') do
  106.       height = height + 1
  107.       move.up()
  108.     end
  109.  
  110.     for i=1, height do
  111.       move.down()
  112.     end
  113.   end
  114. end
  115.  
  116. local function line(numBlocks, update)
  117.   for x = 1, tonumber(numBlocks) - 1 do
  118.     move.forward()
  119.     update()
  120.   end
  121. end
  122.  
  123. local function grid(dim, update)
  124.   update()
  125.  
  126.   for y = 1, tonumber(dim[2]) - 1 do
  127.     line(dim[1], update)
  128.  
  129.     if y % 2 == 0 then
  130.       turtle.turnLeft()
  131.       move.forward()
  132.       update()
  133.       turtle.turnLeft()
  134.  
  135.     else
  136.       turtle.turnRight()
  137.       move.forward()
  138.       update()
  139.       turtle.turnRight()
  140.     end
  141.   end
  142.  
  143.   line(dim[2], update)
  144. end
  145.  
  146. local function validateNum(num, lower, upper)
  147.   if tonumber(num) then
  148.     local lowerCondition = lower and tonumber(lower) and tonumber(num) >= lower or not lower
  149.     local upperCondition = upper and tonumber(upper) and tonumber(num) <= upper or not upper
  150.    
  151.     if lowerCondition and upperCondition then
  152.       return true
  153.     end
  154.   end
  155. end
  156.  
  157. status = {harvest = 1, treefarm = 1}
  158.  
  159. helpTable = {harvest = 'Harvest a custom size field of crops', treefarm = 'Harvest a grid of trees'}
  160.  
  161. functionTable = {
  162.   harvest = function(item, id, prot)
  163.     seeds = checkSeeds()
  164.  
  165.     if seeds and validateNum(item[1], 1, 40) and validateNum(item[2], 1, 40) then
  166.       rednet.send(id, 'Harvesting a field of crops', prot)
  167.       getSeeds()
  168.       grid(item, replaceCrop)
  169.  
  170.     elseif not seeds then
  171.       rednet.send(id, 'Error: i need atleast 16 seeds in me to operate', prot)
  172.  
  173.     else
  174.       rednet.send(id, 'Usage: Harvest [arg1] [arg2] where both arguments must be numbers from 1-40', prot)
  175.     end
  176.   end,
  177.  
  178.   treefarm = function(item, id, prot)
  179.     if validateNum(item[1], 1, 40) and validateNum(item[2], 1, 40) then
  180.       rednet.send(id, 'Chopping down a grid of trees', prot)
  181.       move.forward()
  182.       grid(item, chopTree)
  183.  
  184.     else
  185.       rednet.send(id, 'Usage: treefarm [arg1] [arg2] where both arguments must be numbers from 1-40', prot)
  186.     end
  187.   end
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement