diff --git a/src/api/java/baritone/api/process/IFarmProcess.java b/src/api/java/baritone/api/process/IFarmProcess.java index 6ced16fb..224307af 100644 --- a/src/api/java/baritone/api/process/IFarmProcess.java +++ b/src/api/java/baritone/api/process/IFarmProcess.java @@ -17,7 +17,29 @@ package baritone.api.process; +import net.minecraft.util.math.BlockPos; + public interface IFarmProcess extends IBaritoneProcess { - void farm(); + /** + * Begin to search for crops to farm with in specified aria + * from specified location. + * + * @param range The distance from center to farm from + * @param pos The center position to base the range from + */ + void farm(int range, BlockPos pos); + + /** + * Begin to search for nearby crops to farm. + */ + default void farm() {farm(0, null);} + + /** + * Begin to search for crops to farm with in specified aria + * from the position the command was executed. + * + * @param range The distance to search for crops to farm + */ + default void farm(int range) {farm(range, null);} } diff --git a/src/main/java/baritone/command/defaults/FarmCommand.java b/src/main/java/baritone/command/defaults/FarmCommand.java index 786d0124..1903f89f 100644 --- a/src/main/java/baritone/command/defaults/FarmCommand.java +++ b/src/main/java/baritone/command/defaults/FarmCommand.java @@ -18,14 +18,23 @@ package baritone.command.defaults; import baritone.api.IBaritone; +import baritone.api.cache.IWaypoint; import baritone.api.command.Command; +import baritone.api.command.datatypes.ForWaypoints; import baritone.api.command.exception.CommandException; import baritone.api.command.argument.IArgConsumer; +import baritone.api.command.exception.CommandInvalidStateException; +import baritone.api.command.helpers.Paginator; +import baritone.api.pathing.goals.Goal; +import baritone.api.pathing.goals.GoalBlock; +import baritone.api.utils.BetterBlockPos; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; +import static baritone.api.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX; + public class FarmCommand extends Command { public FarmCommand(IBaritone baritone) { @@ -34,8 +43,30 @@ public class FarmCommand extends Command { @Override public void execute(String label, IArgConsumer args) throws CommandException { - args.requireMax(0); - baritone.getFarmProcess().farm(); + args.requireMax(2); + int range = 0; + BetterBlockPos origin = null; + //range + if (args.has(1)) { + range = args.getAs(Integer.class); + } + //waypoint + if (args.has(1)){ + IWaypoint[] waypoints = args.getDatatypeFor(ForWaypoints.INSTANCE); + IWaypoint waypoint = null; + switch (waypoints.length) { + case 0: + throw new CommandInvalidStateException("No waypoints found"); + case 1: + waypoint = waypoints[0]; + break; + default: + throw new CommandInvalidStateException("Multiple waypoints were found"); + } + origin = waypoint.getLocation(); + } + + baritone.getFarmProcess().farm(range, origin); logDirect("Farming"); } @@ -55,7 +86,9 @@ public class FarmCommand extends Command { "The farm command starts farming nearby plants. It harvests mature crops and plants new ones.", "", "Usage:", - "> farm" + "> farm - farms every crop it can find.", + "> farm - farm crops within range from the starting position.", + "> farm - farm crops within range from waypoint." ); } } diff --git a/src/main/java/baritone/process/FarmProcess.java b/src/main/java/baritone/process/FarmProcess.java index 407157b3..a8806d2b 100644 --- a/src/main/java/baritone/process/FarmProcess.java +++ b/src/main/java/baritone/process/FarmProcess.java @@ -61,6 +61,9 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro private List locations; private int tickCount; + private int range; + private BlockPos center; + private static final List FARMLAND_PLANTABLE = Arrays.asList( Items.BEETROOT_SEEDS, Items.MELON_SEEDS, @@ -97,7 +100,13 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro } @Override - public void farm() { + public void farm(int range, BlockPos pos) { + if (pos == null) { + center = baritone.getPlayerContext().playerFeet(); + } else { + center = pos; + } + this.range = range; active = true; locations = null; } @@ -191,6 +200,11 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro List bonemealable = new ArrayList<>(); List openSoulsand = new ArrayList<>(); for (BlockPos pos : locations) { + //check if the target block is out of range. + if (range != 0 && pos.getDistance(center.getX(), center.getY(), center.getZ()) > range) { + continue; + } + IBlockState state = ctx.world().getBlockState(pos); boolean airAbove = ctx.world().getBlockState(pos.up()).getBlock() instanceof BlockAir; if (state.getBlock() == Blocks.FARMLAND) {