Skip to content

Commit 4848d05

Browse files
committed
Add crop whitelist to #farm (farming and pickup)
1 parent 71aa05c commit 4848d05

File tree

2 files changed

+117
-41
lines changed

2 files changed

+117
-41
lines changed

src/api/java/baritone/api/Settings.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -982,26 +982,44 @@ public final class Settings {
982982
public final Setting<Integer> farmMaxScanSize = new Setting<>(256);
983983

984984
/**
985-
* Prioritize gathering farm related drops
985+
* Prioritize pickup crop drops before another farm task
986986
*/
987-
public final Setting<Boolean> farmPrioritizeGather = new Setting<>(false);
987+
public final Setting<Boolean> farmPrioritizePickup = new Setting<>(false);
988988

989989
/**
990990
* true = farm farm farm, i don't want to stop!
991-
* false = allow farm to stop or fail
991+
* false = allow farm to stop or fail
992992
*/
993993
public final Setting<Boolean> farmContinuously = new Setting<>(false);
994994

995995
/**
996-
* How much farming task is enough for the Continuous Farm to resume?
996+
* How much farm task queue is enough for Baritone to resume the continuous farm?
997+
* {@link #farmContinuously}
997998
*/
998999
public final Setting<Integer> farmContinuouslyThreshold = new Setting<>(16);
9991000

10001001
/**
1001-
* Time interval (in seconds) for the Continuous Farm to check if threshold is fulfilled?
1002+
* How long Baritone should wait (in seconds) to check if continuous farm threshold is fulfilled?
1003+
* {@link #farmContinuously}
10021004
*/
10031005
public final Setting<Long> farmContinuouslyIntervalSecs = new Setting<>(TimeUnit.MINUTES.toSeconds(2));
10041006

1007+
/**
1008+
* Farm whitelist, only interact with crop that is on the {@link #farmWhitelist} list
1009+
*/
1010+
public final Setting<Boolean> farmEnableWhitelist = new Setting<>(false);
1011+
1012+
/**
1013+
* Crop block that Baritone is allowed to farm and collect
1014+
* {@link #farmEnableWhitelist}
1015+
*/
1016+
1017+
public final Setting<List<Block>> farmWhitelist = new Setting<>(new ArrayList<>(Arrays.asList(
1018+
Blocks.WHEAT,
1019+
Blocks.POTATOES,
1020+
Blocks.CARROTS
1021+
)));
1022+
10051023
/**
10061024
* When the cache scan gives less blocks than the maximum threshold (but still above zero), scan the main world too.
10071025
* <p>

src/main/java/baritone/process/FarmProcess.java

Lines changed: 94 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@
5656
import net.minecraft.world.phys.HitResult;
5757
import net.minecraft.world.phys.Vec3;
5858

59-
import java.util.ArrayList;
60-
import java.util.Arrays;
61-
import java.util.List;
62-
import java.util.Optional;
59+
import java.util.*;
6360
import java.util.function.Predicate;
6461

6562
public final class FarmProcess extends BaritoneProcessHelper implements IFarmProcess {
@@ -102,6 +99,31 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro
10299
Blocks.CACTUS.asItem()
103100
);
104101

102+
private static final Map<Block, Item> CROP_TO_PLANTABLE = new HashMap<>();
103+
static {
104+
CROP_TO_PLANTABLE.put(Blocks.BEETROOTS, Items.BEETROOT_SEEDS);
105+
CROP_TO_PLANTABLE.put(Blocks.WHEAT, Items.WHEAT_SEEDS);
106+
CROP_TO_PLANTABLE.put(Blocks.CARROTS, Items.CARROT);
107+
CROP_TO_PLANTABLE.put(Blocks.POTATOES, Items.POTATO);
108+
CROP_TO_PLANTABLE.put(Blocks.MELON, Items.MELON_SEEDS);
109+
CROP_TO_PLANTABLE.put(Blocks.PUMPKIN, Items.PUMPKIN_SEEDS);
110+
}
111+
112+
private static final Map<Block, List<Item>> CROP_TO_DROPPED = new HashMap<>();
113+
static {
114+
CROP_TO_DROPPED.put(Blocks.BEETROOTS, Arrays.asList(Items.BEETROOT, Items.BEETROOT_SEEDS));
115+
CROP_TO_DROPPED.put(Blocks.WHEAT, Arrays.asList(Items.WHEAT, Items.WHEAT_SEEDS));
116+
CROP_TO_DROPPED.put(Blocks.CARROTS, List.of(Items.CARROT));
117+
CROP_TO_DROPPED.put(Blocks.POTATOES, List.of(Items.POTATO));
118+
CROP_TO_DROPPED.put(Blocks.MELON, Arrays.asList(Items.MELON_SEEDS, Items.MELON_SLICE));
119+
CROP_TO_DROPPED.put(Blocks.PUMPKIN, Arrays.asList(Items.PUMPKIN, Items.PUMPKIN_SEEDS));
120+
CROP_TO_DROPPED.put(Blocks.NETHER_WART, List.of(Items.NETHER_WART));
121+
CROP_TO_DROPPED.put(Blocks.COCOA, List.of(Items.COCOA_BEANS));
122+
CROP_TO_DROPPED.put(Blocks.SUGAR_CANE, List.of(Items.SUGAR_CANE));
123+
CROP_TO_DROPPED.put(Blocks.BAMBOO, List.of(Items.BAMBOO));
124+
CROP_TO_DROPPED.put(Blocks.CACTUS, List.of(Items.CACTUS));
125+
}
126+
105127
public FarmProcess(Baritone baritone) {
106128
super(baritone);
107129
}
@@ -202,26 +224,82 @@ public boolean readyToHarvest(Level world, BlockPos pos, BlockState state) {
202224
private boolean readyForHarvest(Level world, BlockPos pos, BlockState state) {
203225
for (Harvest harvest : Harvest.values()) {
204226
if (harvest.block == state.getBlock()) {
205-
return harvest.readyToHarvest(world, pos, state);
227+
if (!Baritone.settings().farmEnableWhitelist.value) {
228+
return harvest.readyToHarvest(world, pos, state);
229+
} else {
230+
for (Block whitelist : Baritone.settings().farmWhitelist.value) {
231+
if ((harvest.block.equals(whitelist)) && (harvest.block == state.getBlock())) {
232+
return harvest.readyToHarvest(world, pos, state);
233+
}
234+
}
235+
}
206236
}
207237
}
208238
return false;
209239
}
210240

211241
private boolean isPlantable(ItemStack stack) {
212-
return FARMLAND_PLANTABLE.contains(stack.getItem());
242+
if (!Baritone.settings().farmEnableWhitelist.value) {
243+
return FARMLAND_PLANTABLE.contains(stack.getItem());
244+
}
245+
if (!FARMLAND_PLANTABLE.contains(stack.getItem())) {
246+
return false;
247+
} else {
248+
List<Item> whitelistedFarmlandPlantable = new ArrayList<>();
249+
for (Block whitelist : Baritone.settings().farmWhitelist.value) {
250+
if (!(CROP_TO_PLANTABLE.get(whitelist) == null) && (CROP_TO_PLANTABLE.get(whitelist)).equals(stack.getItem())) {
251+
whitelistedFarmlandPlantable.add(CROP_TO_PLANTABLE.get(whitelist));
252+
}
253+
}
254+
return whitelistedFarmlandPlantable.contains(stack.getItem());
255+
}
213256
}
214257

215258
private boolean isBoneMeal(ItemStack stack) {
216259
return !stack.isEmpty() && stack.getItem().equals(Items.BONE_MEAL);
217260
}
218261

219262
private boolean isNetherWart(ItemStack stack) {
220-
return !stack.isEmpty() && stack.getItem().equals(Items.NETHER_WART);
263+
if (!Baritone.settings().farmEnableWhitelist.value) {
264+
return !stack.isEmpty() && stack.getItem().equals(Items.NETHER_WART);
265+
} else {
266+
return !stack.isEmpty() && stack.getItem().equals(Items.NETHER_WART) && Baritone.settings().farmWhitelist.value.contains(Blocks.NETHER_WART);
267+
}
221268
}
222269

223270
private boolean isCocoa(ItemStack stack) {
224-
return !stack.isEmpty() && stack.getItem().equals(Items.COCOA_BEANS);
271+
if (!Baritone.settings().farmEnableWhitelist.value) {
272+
return !stack.isEmpty() && stack.getItem().equals(Items.COCOA_BEANS);
273+
} else {
274+
return !stack.isEmpty() && stack.getItem().equals(Items.COCOA_BEANS) && Baritone.settings().farmWhitelist.value.contains(Blocks.COCOA);
275+
}
276+
}
277+
278+
private List<Goal> whereAreTheDrops() {
279+
List<Goal> pickup = new ArrayList<>();
280+
for (Entity entity : ctx.entities()) {
281+
//check if the pickupGoals is out of range.
282+
if (range != 0 && entity.blockPosition().distSqr(center) > range * range) {
283+
continue;
284+
}
285+
if (entity instanceof ItemEntity && entity.onGround()) {
286+
ItemEntity ei = (ItemEntity) entity;
287+
if (PICKUP_DROPPED.contains(ei.getItem().getItem())) {
288+
if (!Baritone.settings().farmEnableWhitelist.value) {
289+
// +0.1 because of farmland's 0.9375 and soulsand's 0.875 dummy height lol
290+
pickup.add(new GoalBlock(new BetterBlockPos(entity.position().x, entity.position().y + 0.125, entity.position().z)));
291+
} else {
292+
for (Block whitelist : Baritone.settings().farmWhitelist.value) {
293+
if (!(CROP_TO_DROPPED.get(whitelist) == null) && (CROP_TO_DROPPED.get(whitelist)).contains(ei.getItem().getItem())) {
294+
// +0.1 because of farmland's 0.9375 and soulsand's 0.875 dummy height lol
295+
pickup.add(new GoalBlock(new BetterBlockPos(entity.position().x, entity.position().y + 0.125, entity.position().z)));
296+
}
297+
}
298+
}
299+
}
300+
}
301+
}
302+
return pickup;
225303
}
226304

227305
@Override
@@ -297,23 +375,11 @@ public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
297375
// Farm Continuously: Block input while paused
298376
if (!isPaused()) {
299377
// Check if item gather is prioritized
300-
if (Baritone.settings().farmPrioritizeGather.value) {
301-
List<Goal> pickupGoals = new ArrayList<>();
302-
for (Entity entity : ctx.entities()) {
303-
//check if the pickupGoals is out of range.
304-
if (range != 0 && entity.blockPosition().distSqr(center) > range * range) {
305-
continue;
306-
}
307-
if (entity instanceof ItemEntity && entity.onGround()) {
308-
ItemEntity ei = (ItemEntity) entity;
309-
if (PICKUP_DROPPED.contains(ei.getItem().getItem())) {
310-
// +0.1 because of farmland's 0.9375 and soulsand's 0.875 dummy height lol
311-
pickupGoals.add(new GoalBlock(new BetterBlockPos(entity.position().x, entity.position().y + 0.125, entity.position().z)));
312-
}
313-
}
314-
}
378+
if (Baritone.settings().farmPrioritizePickup.value) {
379+
List<Goal> pickupGoals = whereAreTheDrops();
315380
if (!(pickupGoals.isEmpty())) {
316-
return new PathingCommand(new GoalComposite(pickupGoals.toArray(new Goal[0])), PathingCommandType.SET_GOAL_AND_PATH);
381+
GoalComposite pickupGoalComposite = new GoalComposite(pickupGoals.toArray(new Goal[0]));
382+
return new PathingCommand(pickupGoalComposite, PathingCommandType.SET_GOAL_AND_PATH);
317383
}
318384
}
319385
baritone.getInputOverrideHandler().clearAllKeys();
@@ -428,18 +494,10 @@ public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
428494
goalz.add(new GoalBlock(pos));
429495
}
430496
}
431-
for (Entity entity : ctx.entities()) {
432-
//check if the pickupGoals is out of range.
433-
if (range != 0 && entity.blockPosition().distSqr(center) > range * range) {
434-
continue;
435-
}
436-
if (entity instanceof ItemEntity && entity.onGround()) {
437-
ItemEntity ei = (ItemEntity) entity;
438-
if (PICKUP_DROPPED.contains(ei.getItem().getItem())) {
439-
// +0.1 because of farmland's 0.9375 and soulsand's 0.875 dummy height lol
440-
goalz.add(new GoalBlock(new BetterBlockPos(entity.position().x, entity.position().y + 0.125, entity.position().z)));
441-
}
442-
}
497+
List<Goal> pickupGoals = whereAreTheDrops();
498+
if (!(pickupGoals.isEmpty())) {
499+
GoalComposite pickupGoalComposite = new GoalComposite(pickupGoals.toArray(new Goal[0]));
500+
goalz.add(pickupGoalComposite);
443501
}
444502
if (goalz.isEmpty() && !isPaused()) {
445503
if (!Baritone.settings().farmContinuously.value) {

0 commit comments

Comments
 (0)