Wheat Farmer (intermediate)
This will show you code for a wheat farmer, meaning that it finds the closest fully grown wheat blocks, moves to them, breaks them, and finally replants them.
FarmTask.cs
Task responsible for finding, breaking, and replanting wheat.
Commented Code
Raw Code
1
public class FarmTask : ITask, ITickListener
2
{
3
// Add additional costs to Jumping in order to discourage
4
// the bot from jumping on our crops.
5
private static MapOptions MO = new MapOptions()
6
{
7
AdditionalWeights = new MapOptionWeights()
8
{
9
JumpGap = 25,
10
JumpUp = 25
11
}
12
};
13
14
// https://docs.minecraftbot.com/api/utility/locationblacklistcollection
15
private static LocationBlacklistCollection BLACKLIST = LocationBlacklistCollection.CreateGlobal(3, // blacklist location globally after 3 bots fail to reach it.
16
600000, 1); // blacklist location for 10 minutes.
17
18
private const int GROWN_WHEAT_METADATA = 7;
19
private const int WHEAT_ID = 59;
20
private const int SEED_ID = 295;
21
22
public override bool Exec() {
23
return !Context.Player.IsDead() &&
24
!Context.Player.State.Eating && !Context.Player.State.EatRequestQueued && // do not run if we are about to eat/are eating.
25
!Inventory.IsFull() && Context.Containers.GetOpenWindow() == null; // do not run if a chest is open or our inventory is full, as in that case the store task is running.
26
}
27
28
public async Task OnTick() {
29
var block = await Context.World.FindClosest(64, 8, // search 64x64x8 area
30
WHEAT_ID, CpuMode.Medium_Usage,
31
consideredBlock => consideredBlock.GetMetadata() == GROWN_WHEAT_METADATA // filter to only fully grown blocks (determined by metadata).
32
&& !BLACKLIST.IsBlocked(Context, consideredBlock.GetLocation())); // filter by blacklist as well.
33
34
if (block == null)
35
// We didn't find any grown wheat blocks close to the bot.
36
return;
37
38
if ((await block.MoveTo(MO).Task).Result == MoveResultType.Completed) {
39
// Successfully moved to block, attempt to mine it.
40
var mineAction = await block.Dig();
41
await mineAction.DigTask;
42
43
// Attempt to replant at the position that we mined at.
44
if(await Inventory.Select(SEED_ID))
45
await block.PlaceAt(); // attempt to place the seeds at the block that we just mined.
46
}
47
else {
48
// Could not path to the block, add it to our temporary block.
49
BLACKLIST.AddToBlockCounter(Context, block.GetLocation());
50
}
51
}
52
}
Copied!
1
public class FarmTask : ITask, ITickListener
2
{
3
private static MapOptions MO = new MapOptions()
4
{
5
AdditionalWeights = new MapOptionWeights() { JumpGap = 25, JumpUp = 25 }
6
};
7
private static LocationBlacklistCollection BLACKLIST = LocationBlacklistCollection.CreateGlobal(3, 600000, 1);
8
9
private const int GROWN_WHEAT_METADATA = 7;
10
private const int WHEAT_ID = 59;
11
private const int SEED_ID = 295;
12
13
public override bool Exec() {
14
return !Context.Player.IsDead() &&
15
!Context.Player.State.Eating && !Context.Player.State.EatRequestQueued &&
16
!Inventory.IsFull() && Context.Containers.GetOpenWindow() == null;
17
}
18
19
public async Task OnTick() {
20
var block = await Context.World.FindClosest(64, 8,
21
WHEAT_ID, CpuMode.Medium_Usage,
22
consideredBlock => consideredBlock.GetMetadata() == GROWN_WHEAT_METADATA
23
&& !BLACKLIST.IsBlocked(Context, consideredBlock.GetLocation()));
24
25
if (block == null) return;
26
27
if ((await block.MoveTo(MO).Task).Result == MoveResultType.Completed) {
28
var mineAction = await block.Dig();
29
await mineAction.DigTask;
30
31
if(await Inventory.Select(SEED_ID))
32
await block.PlaceAt();
33
else {
34
BLACKLIST.AddToBlockCounter(Context, block.GetLocation());
35
}
36
}
37
}
Copied!
StoreTask.cs
Task responsible for storing items into the nearest non-full chest when the bot's inventory is full.
Commented Code
Raw Code
1
public class StoreTask : ITask, ITickListener
2
{
3
public override bool Exec() {
4
return !Context.Player.IsDead() &&
5
!Context.Player.State.Eating && !Context.Player.State.EatRequestQueued && // do not run if we are about to eat/are eating.
6
(Inventory.IsFull() || Context.Containers.GetOpenWindow() != null); // run when our inventory is full or we have a chest open.
7
}
8
9
public async Task OnTick() {
10
11
// Locate the nearest chests.
12
var chestMap = Context.Functions.CreateChestMap();
13
await chestMap.UpdateChestList();
14
15
// Attempt to open a chest that is not full.
16
var window = await chestMap.Open(ChestStatus.NotFull);
17
if (window == null) return;
18
19
// In this case we deposite all of our items, however
20
// you could extend this by not storing food.
21
await window.Deposit();
22
await window.Close();
23
}
24
}
Copied!
1
public class StoreTask : ITask, ITickListener
2
{
3
public override bool Exec() {
4
return !Context.Player.IsDead() &&
5
!Context.Player.State.Eating && !Context.Player.State.EatRequestQueued &&
6
(Inventory.IsFull() || Context.Containers.GetOpenWindow() != null);
7
}
8
9
public async Task OnTick() {
10
11
var chestMap = Context.Functions.CreateChestMap();
12
await chestMap.UpdateChestList();
13
14
var window = await chestMap.Open(ChestStatus.NotFull);
15
if (window == null) return;
16
17
await window.Deposit();
18
await window.Close();
19
}
20
}
Copied!
WheatFarmerExample.zip
125KB
Binary
Full Plugin Source Code
Last modified 1yr ago
Copy link