View difference between Paste ID: 6r37Gs0i and yChp4pyq
SHOW: | | - or go back to the newest paste.
1
os.loadAPI("turtleChest.lua")
2
3
length = 9
4
width = 14
5
6
-- Define an array with plant names and their final growth stages
7
local plants = {
8
    ["minecraft:wheat"] = 7,
9
    ["minecraft:carrots"] = 7,
10
    ["minecraft:potatoes"] = 7,
11
    ["minecraft:beetroots"] = 3,
12
    ["minecraft:nether_wart"] = 3,
13
    ["minecraft:cocoa"] = 2,
14
    ["minecraft:pumpkin_stem"] = 7,
15
    ["minecraft:melon_stem"] = 7,
16
    -- Add more plants if needed
17
}
18
19
local seeds = {
20
    ["minecraft:wheat_seeds"] = 7,
21
    ["minecraft:carrots"] = 7,
22
    ["minecraft:potatoes"] = 7,
23
    ["minecraft:beetroots"] = 3,
24
    ["minecraft:pumpkin_stem"] = 7,
25
    ["minecraft:melon_stem"] = 7,
26
    ["minecraft:nether_wart"] = 3,
27
    ["minecraft:cocoa"] = 2,
28
    ["minecraft:sweet_berry_bush"] = 3,
29
}
30
31
local function findItem(itemName)
32
    for slot = 1, 16 do 
33
        local item = turtle.getItemDetail(slot)
34
        if item and item.name == itemName then
35
            return slot
36
        end
37
    end
38
    return -1
39
end
40
41
-- Function to check if the plant is fully grown
42
local function isFullyGrown(plantName, growthStage)
43
    -- Look up the plant in the array
44
    finalStage = plants[plantName]
45
    if finalStage then
46
        return growthStage == finalStage
47
    else
48
        return false
49
    end
50
end
51
52
function plantSeed()
53
	for seedName, _ in pairs(seeds) do
54
		slot = findItem(seedName)
55
		if slot ~= -1 then
56
			break
57
		end
58
	end
59
	if slot == -1 then
60
		return false
61
	else
62
		turtle.select(slot)
63
		turtle.placeDown()
64
		return true
65
	end
66
end
67
68
function harvest()
69
	turtle.digDown()
70
	if not plantSeed() then
71
		return false
72
	end
73
	return true
74
end
75
76
function growCheck()
77
	success, data = turtle.inspectDown()
78
	if success then
79
	    plantName = data.name
80
	    growthStage = data.state.age
81
	
82
	    if isFullyGrown(plantName, growthStage) then
83
			return harvest()
84
		end
85
	end
86
	return true
87
end
88
89
function toStart(width, length)
90
	if length % 2 == 1 then
91
 		turtle.turnLeft()
92
  		turtle.turnLeft()
93
  		for i = 1, width do
94
    		turtle.forward()
95
  		end
96
	end
97
	turtle.turnRight()
98
	for j = 2, length do
99
    	turtle.forward()
100
	end
101
	turtle.turnLeft()
102
end
103
104
function mineTop(width, length, plantName, growthStage)
105
  outofseed = false
106
  for i = 1, length do
107
    for j = 1, width do
108
	  if not growCheck() then
109
		outofseed = true
110
	  end
111
      turtle.dig()
112
	  turtle.forward()
113
    end
114
    if i < length then
115
      if i % 2 == 1 then
116
		if not growCheck() then
117
			outofseed = true
118
	    end
119
        turtle.turnRight()
120
        turtle.dig()
121
	    turtle.forward()
122
        turtle.turnRight()
123
      else
124
        if not growCheck() then
125
			outofseed = true
126
	  	end
127
		turtle.turnLeft()
128
        turtle.dig()
129
	    turtle.forward()
130
        turtle.turnLeft()
131
      end
132
    end
133
  end
134
  return outofseed
135
end
136
137
function farm(width, length, plantName, growthStage)
138
    mineTop(width, length, plantName, growthStage)
139
	outofseed = growCheck()
140
    toStart(width, length)
141
	turtleChest.insertAll()
142
	turtle.turnLeft()
143
end
144
145
check = 0
146
while true do
147
	-- Main program
148
	success, data = turtle.inspect()
149
	
150
	if success then
151
	    plantName = data.name
152
	    growthStage = data.state.age
153
	
154
	    if isFullyGrown(plantName, growthStage) then
155
   			check = check + 1
156
			if check == 1 then
157
				turtle.turnLeft()
158
			end
159
	    end
160
	end
161
	if check == 2 then
162
		check = 0
163
		turtle.up()
164
		farm(length-1, width, plantName, growthStage)
165
		turtle.down()
166
	end
167
end