Merge pull request #2080 from millennIumAMbiguity/master

Added range argument to farm
This commit is contained in:
Leijurv 2021-01-29 17:41:08 -08:00 committed by GitHub
commit a9f497981f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 5 deletions

View File

@ -17,7 +17,29 @@
package baritone.api.process; package baritone.api.process;
import net.minecraft.util.math.BlockPos;
public interface IFarmProcess extends IBaritoneProcess { 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);}
} }

View File

@ -18,14 +18,23 @@
package baritone.command.defaults; package baritone.command.defaults;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.cache.IWaypoint;
import baritone.api.command.Command; import baritone.api.command.Command;
import baritone.api.command.datatypes.ForWaypoints;
import baritone.api.command.exception.CommandException; import baritone.api.command.exception.CommandException;
import baritone.api.command.argument.IArgConsumer; 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.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
import static baritone.api.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
public class FarmCommand extends Command { public class FarmCommand extends Command {
public FarmCommand(IBaritone baritone) { public FarmCommand(IBaritone baritone) {
@ -34,8 +43,30 @@ public class FarmCommand extends Command {
@Override @Override
public void execute(String label, IArgConsumer args) throws CommandException { public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0); args.requireMax(2);
baritone.getFarmProcess().farm(); 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"); 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.", "The farm command starts farming nearby plants. It harvests mature crops and plants new ones.",
"", "",
"Usage:", "Usage:",
"> farm" "> farm - farms every crop it can find.",
"> farm <range> - farm crops within range from the starting position.",
"> farm <range> <waypoint> - farm crops within range from waypoint."
); );
} }
} }

View File

@ -61,6 +61,9 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro
private List<BlockPos> locations; private List<BlockPos> locations;
private int tickCount; private int tickCount;
private int range;
private BlockPos center;
private static final List<Item> FARMLAND_PLANTABLE = Arrays.asList( private static final List<Item> FARMLAND_PLANTABLE = Arrays.asList(
Items.BEETROOT_SEEDS, Items.BEETROOT_SEEDS,
Items.MELON_SEEDS, Items.MELON_SEEDS,
@ -97,7 +100,13 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro
} }
@Override @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; active = true;
locations = null; locations = null;
} }
@ -191,6 +200,11 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro
List<BlockPos> bonemealable = new ArrayList<>(); List<BlockPos> bonemealable = new ArrayList<>();
List<BlockPos> openSoulsand = new ArrayList<>(); List<BlockPos> openSoulsand = new ArrayList<>();
for (BlockPos pos : locations) { 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); IBlockState state = ctx.world().getBlockState(pos);
boolean airAbove = ctx.world().getBlockState(pos.up()).getBlock() instanceof BlockAir; boolean airAbove = ctx.world().getBlockState(pos.up()).getBlock() instanceof BlockAir;
if (state.getBlock() == Blocks.FARMLAND) { if (state.getBlock() == Blocks.FARMLAND) {