Merge pull request #2080 from millennIumAMbiguity/master
Added range argument to farm
This commit is contained in:
		| @@ -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);} | ||||
| } | ||||
|   | ||||
| @@ -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 <range> - farm crops within range from the starting position.", | ||||
|                 "> farm <range> <waypoint> - farm crops within range from waypoint." | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -61,6 +61,9 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro | ||||
|     private List<BlockPos> locations; | ||||
|     private int tickCount; | ||||
|  | ||||
|     private int range; | ||||
|     private BlockPos center; | ||||
|  | ||||
|     private static final List<Item> 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<BlockPos> bonemealable = new ArrayList<>(); | ||||
|         List<BlockPos> 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) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user