Clean up Optional bad practices

This commit is contained in:
Brady 2018-08-05 23:42:17 -05:00
parent aa635360a2
commit 23db615695
No known key found for this signature in database
GPG Key ID: 73A788379A197567
8 changed files with 66 additions and 58 deletions

View File

@ -4,26 +4,20 @@ import baritone.bot.behavior.Behavior;
import baritone.bot.event.events.TickEvent; import baritone.bot.event.events.TickEvent;
import baritone.bot.utils.Rotation; import baritone.bot.utils.Rotation;
import java.util.Optional;
public class LookBehavior extends Behavior { public class LookBehavior extends Behavior {
public static final LookBehavior INSTANCE = new LookBehavior(); public static final LookBehavior INSTANCE = new LookBehavior();
private LookBehavior() {
target = Optional.empty();
}
/** /**
* Target's values are as follows: * Target's values are as follows:
* *
* getFirst() -> yaw * getFirst() -> yaw
* getSecond() -> pitch * getSecond() -> pitch
*/ */
private Optional<Rotation> target; private Rotation target;
public void updateTarget(Rotation target) { public void updateTarget(Rotation target) {
this.target = Optional.of(target); this.target = target;
} }
@Override @Override
@ -33,11 +27,10 @@ public class LookBehavior extends Behavior {
@Override @Override
public void onPlayerUpdate() { public void onPlayerUpdate() {
if(target.isPresent()) { if (target != null) {
player().setPositionAndRotation(player().posX, player().posY, player().posZ, player().rotationYaw = target.getFirst();
target.get().getFirst(), target.get().getSecond()); player().rotationPitch = target.getSecond();
target = Optional.empty(); target = null;
} }
} }
} }

View File

@ -35,10 +35,10 @@ public final class LookBehaviorUtils implements Helper{
} }
public static Optional<Rotation> reachable(BlockPos pos) { public static Optional<Rotation> reachable(BlockPos pos) {
Optional possibleRotation = reachableCenter(pos); Optional<Rotation> possibleRotation = reachableCenter(pos);
if(possibleRotation.isPresent()) { if (possibleRotation.isPresent())
return possibleRotation; return possibleRotation;
}
IBlockState bstate = BlockStateInterface.get(pos); IBlockState bstate = BlockStateInterface.get(pos);
AxisAlignedBB bbox = bstate.getBoundingBox(mc.world, pos); AxisAlignedBB bbox = bstate.getBoundingBox(mc.world, pos);
for (Vec3d vecMult : BLOCK_SIDE_MULTIPLIERS) { for (Vec3d vecMult : BLOCK_SIDE_MULTIPLIERS) {
@ -49,14 +49,13 @@ public final class LookBehaviorUtils implements Helper{
double y = pos.getY() + yDiff; double y = pos.getY() + yDiff;
double z = pos.getZ() + zDiff; double z = pos.getZ() + zDiff;
possibleRotation = reachableRotation(pos, new Vec3d(x, y, z)); possibleRotation = reachableRotation(pos, new Vec3d(x, y, z));
if (possibleRotation.isPresent()) { if (possibleRotation.isPresent())
return possibleRotation; return possibleRotation;
} }
}
return Optional.empty(); return Optional.empty();
} }
private static RayTraceResult raytraceTowards(Rotation rotation) { private static RayTraceResult rayTraceTowards(Rotation rotation) {
double blockReachDistance = (double) mc.playerController.getBlockReachDistance(); double blockReachDistance = (double) mc.playerController.getBlockReachDistance();
Vec3d vec3 = mc.player.getPositionEyes(1.0F); Vec3d vec3 = mc.player.getPositionEyes(1.0F);
Vec3d vec31 = calcVec3dFromRotation(rotation); Vec3d vec31 = calcVec3dFromRotation(rotation);
@ -75,7 +74,7 @@ public final class LookBehaviorUtils implements Helper{
protected static Optional<Rotation> reachableRotation(BlockPos pos, Vec3d offset) { protected static Optional<Rotation> reachableRotation(BlockPos pos, Vec3d offset) {
Rotation rotation = Utils.calcRotationFromVec3d(mc.player.getPositionEyes(1.0F), Rotation rotation = Utils.calcRotationFromVec3d(mc.player.getPositionEyes(1.0F),
offset); offset);
RayTraceResult result = raytraceTowards(rotation); RayTraceResult result = rayTraceTowards(rotation);
if (result != null if (result != null
&& result.typeOfHit == RayTraceResult.Type.BLOCK && result.typeOfHit == RayTraceResult.Type.BLOCK
&& result.getBlockPos().equals(pos)) && result.getBlockPos().equals(pos))
@ -91,13 +90,12 @@ public final class LookBehaviorUtils implements Helper{
protected static Optional<Rotation> reachableCenter(BlockPos pos) { protected static Optional<Rotation> reachableCenter(BlockPos pos) {
Rotation rotation = Utils.calcRotationFromVec3d(mc.player.getPositionEyes(1.0F), Rotation rotation = Utils.calcRotationFromVec3d(mc.player.getPositionEyes(1.0F),
Utils.calcCenterFromCoords(pos, mc.world)); Utils.calcCenterFromCoords(pos, mc.world));
RayTraceResult result = raytraceTowards(rotation); RayTraceResult result = rayTraceTowards(rotation);
if (result != null if (result != null
&& result.typeOfHit == RayTraceResult.Type.BLOCK && result.typeOfHit == RayTraceResult.Type.BLOCK
&& result.getBlockPos().equals(pos)) && result.getBlockPos().equals(pos))
return Optional.of(rotation); return Optional.of(rotation);
return Optional.empty(); return Optional.empty();
} }
} }

View File

@ -108,10 +108,9 @@ public abstract class Movement implements Helper, MovementHelper {
* @return Status * @return Status
*/ */
public MovementStatus update() { public MovementStatus update() {
MovementState latestState = updateState(currentState); MovementState latestState = updateState(currentState);
latestState.getTarget().rotation.ifPresent(LookBehavior.INSTANCE::updateTarget); latestState.getTarget().getRotation().ifPresent(LookBehavior.INSTANCE::updateTarget);
//TODO calculate movement inputs from latestState.getGoal().position // TODO: calculate movement inputs from latestState.getGoal().position
// latestState.getTarget().position.ifPresent(null); NULL CONSUMER REALLY SHOULDN'T BE THE FINAL THING YOU SHOULD REALLY REPLACE THIS WITH ALMOST ACTUALLY ANYTHING ELSE JUST PLEASE DON'T LEAVE IT AS IT IS THANK YOU KANYE // latestState.getTarget().position.ifPresent(null); NULL CONSUMER REALLY SHOULDN'T BE THE FINAL THING YOU SHOULD REALLY REPLACE THIS WITH ALMOST ACTUALLY ANYTHING ELSE JUST PLEASE DON'T LEAVE IT AS IT IS THANK YOU KANYE
latestState.inputState.forEach((input, forced) -> { latestState.inputState.forEach((input, forced) -> {
Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced); Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced);
@ -128,20 +127,18 @@ public abstract class Movement implements Helper, MovementHelper {
private boolean prepared(MovementState state) { private boolean prepared(MovementState state) {
if (state.getStatus() == MovementStatus.WAITING) { if (state.getStatus() == MovementStatus.WAITING)
return true; return true;
}
for (BlockPos blockPos : positionsToBreak) { for (BlockPos blockPos : positionsToBreak) {
if(!MovementHelper.canWalkThrough(blockPos, BlockStateInterface.get(blockPos))) { if(!MovementHelper.canWalkThrough(blockPos, BlockStateInterface.get(blockPos))) {
Optional<Rotation> reachable = LookBehaviorUtils.reachable(blockPos); Optional<Rotation> reachable = LookBehaviorUtils.reachable(blockPos);
reachable.ifPresent(rotation -> { if (reachable.isPresent()) {
state.setTarget(new MovementState.MovementTarget(Optional.empty(), reachable)) state.setTarget(new MovementState.MovementTarget(reachable.get())).setInput(Input.CLICK_LEFT, true);
.setInput(Input.CLICK_LEFT, true);
});
if (reachable.isPresent())
return false; return false;
} }
} }
}
return true; return true;
} }

View File

@ -11,8 +11,8 @@ import java.util.Optional;
public class MovementState { public class MovementState {
private MovementStatus status; private MovementStatus status;
private MovementTarget goal = new MovementTarget(Optional.empty(), Optional.empty()); private MovementTarget goal = new MovementTarget();
private MovementTarget target = new MovementTarget(Optional.empty(), Optional.empty()); private MovementTarget target = new MovementTarget();
protected final Map<Input, Boolean> inputState = new HashMap<>(); protected final Map<Input, Boolean> inputState = new HashMap<>();
public MovementState setStatus(MovementStatus status) { public MovementState setStatus(MovementStatus status) {
@ -30,19 +30,39 @@ public class MovementState {
* <p> * <p>
* TODO: Decide desiredMovement type * TODO: Decide desiredMovement type
*/ */
public Optional<Vec3d> position; public Vec3d position;
/** /**
* Yaw and pitch angles that must be matched * Yaw and pitch angles that must be matched
* <p> * <p>
* getFirst() -> YAW * getFirst() -> YAW
* getSecond() -> PITCH * getSecond() -> PITCH
*/ */
public Optional<Rotation> rotation; public Rotation rotation;
public MovementTarget(Optional<Vec3d> position, Optional<Rotation> rotation) { public MovementTarget() {
this(null, null);
}
public MovementTarget(Vec3d position) {
this(position, null);
}
public MovementTarget(Rotation rotation) {
this(null, rotation);
}
public MovementTarget(Vec3d position, Rotation rotation) {
this.position = position; this.position = position;
this.rotation = rotation; this.rotation = rotation;
} }
public final Optional<Vec3d> getPosition() {
return Optional.of(this.position);
}
public final Optional<Rotation> getRotation() {
return Optional.of(this.rotation);
}
} }
public MovementTarget getGoal() { public MovementTarget getGoal() {

View File

@ -6,13 +6,11 @@ import baritone.bot.pathing.movement.MovementHelper;
import baritone.bot.pathing.movement.MovementState; import baritone.bot.pathing.movement.MovementState;
import baritone.bot.pathing.movement.MovementState.MovementStatus; import baritone.bot.pathing.movement.MovementState.MovementStatus;
import baritone.bot.utils.BlockStateInterface; import baritone.bot.utils.BlockStateInterface;
import baritone.bot.utils.Rotation;
import baritone.bot.utils.ToolSet; import baritone.bot.utils.ToolSet;
import baritone.bot.utils.Utils; import baritone.bot.utils.Utils;
import net.minecraft.block.BlockFalling; import net.minecraft.block.BlockFalling;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import java.util.Optional;
public class MovementAscend extends Movement { public class MovementAscend extends Movement {
BlockPos[] against = new BlockPos[3]; BlockPos[] against = new BlockPos[3];
@ -67,7 +65,6 @@ public class MovementAscend extends Movement {
@Override @Override
public MovementState updateState(MovementState state) { public MovementState updateState(MovementState state) {
super.updateState(state); super.updateState(state);
System.out.println("Ticking with state " + state.getStatus()); System.out.println("Ticking with state " + state.getStatus());
switch (state.getStatus()) { switch (state.getStatus()) {
@ -81,10 +78,9 @@ public class MovementAscend extends Movement {
state.setStatus(MovementStatus.SUCCESS); state.setStatus(MovementStatus.SUCCESS);
return state; return state;
} }
Rotation rotationToBlock = Utils.calcRotationFromVec3d(playerHead(), Utils.calcCenterFromCoords(positionsToBreak[0], world()));
state.setTarget(new MovementState.MovementTarget(Optional.empty(), Optional.of(Utils.calcRotationFromVec3d(new Vec3d(player().posX, player().posY + 1.62, player().posZ), Utils.calcCenterFromCoords(positionsToBreak[0], world()))))); return state.setTarget(new MovementState.MovementTarget(rotationToBlock))
state.setInput(InputOverrideHandler.Input.JUMP, true).setInput(InputOverrideHandler.Input.MOVE_FORWARD, true); .setInput(InputOverrideHandler.Input.JUMP, true).setInput(InputOverrideHandler.Input.MOVE_FORWARD, true);
return state;
default: default:
return state; return state;
} }

View File

@ -5,6 +5,7 @@ import baritone.bot.pathing.movement.Movement;
import baritone.bot.pathing.movement.MovementHelper; import baritone.bot.pathing.movement.MovementHelper;
import baritone.bot.pathing.movement.MovementState; import baritone.bot.pathing.movement.MovementState;
import baritone.bot.utils.BlockStateInterface; import baritone.bot.utils.BlockStateInterface;
import baritone.bot.utils.Rotation;
import baritone.bot.utils.ToolSet; import baritone.bot.utils.ToolSet;
import baritone.bot.utils.Utils; import baritone.bot.utils.Utils;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@ -14,9 +15,6 @@ import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks; import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import java.util.Optional;
public class MovementTraverse extends Movement { public class MovementTraverse extends Movement {
@ -98,8 +96,9 @@ public class MovementTraverse extends Movement {
state.setStatus(MovementState.MovementStatus.SUCCESS); state.setStatus(MovementState.MovementStatus.SUCCESS);
return state; return state;
} }
state.setTarget(new MovementState.MovementTarget(Optional.empty(), Optional.of(Utils.calcRotationFromVec3d(new Vec3d(player().posX, player().posY + 1.62, player().posZ), Utils.calcCenterFromCoords(positionsToBreak[0], world()))))).setInput(InputOverrideHandler.Input.MOVE_FORWARD, true); Rotation rotationToBlock = Utils.calcRotationFromVec3d(playerHead(), Utils.calcCenterFromCoords(positionsToBreak[0], world()));
return state; return state.setTarget(new MovementState.MovementTarget(rotationToBlock))
.setInput(InputOverrideHandler.Input.MOVE_FORWARD, true);
default: default:
return state; return state;

View File

@ -4,6 +4,7 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentString;
/** /**
@ -26,6 +27,10 @@ public interface Helper {
return new BlockPos(mc.player.posX, mc.player.posY, mc.player.posZ); return new BlockPos(mc.player.posX, mc.player.posY, mc.player.posZ);
} }
default Vec3d playerHead() {
return new Vec3d(mc.player.posX, mc.player.posY + mc.player.getEyeHeight(), mc.player.posZ);
}
default void displayChatMessageRaw(String message) { default void displayChatMessageRaw(String message) {
mc.ingameGUI.getChatGUI().printChatMessage(new TextComponentString(message)); mc.ingameGUI.getChatGUI().printChatMessage(new TextComponentString(message));
} }

View File

@ -152,11 +152,11 @@ public class LookManager extends Manager {
double faceX = (pos.getX() + side.getX() + 1.0D) * 0.5D; double faceX = (pos.getX() + side.getX() + 1.0D) * 0.5D;
double faceY = (pos.getY() + side.getY()) * 0.5D; double faceY = (pos.getY() + side.getY()) * 0.5D;
double faceZ = (pos.getZ() + side.getZ() + 1.0D) * 0.5D; double faceZ = (pos.getZ() + side.getZ() + 1.0D) * 0.5D;
RayTraceResult blah = raytraceTowards(faceX, faceY, faceZ); RayTraceResult blah = rayTraceTowards(faceX, faceY, faceZ);
return blah != null && blah.typeOfHit == RayTraceResult.Type.BLOCK && blah.getBlockPos().equals(pos) && blah.sideHit == dir; return blah != null && blah.typeOfHit == RayTraceResult.Type.BLOCK && blah.getBlockPos().equals(pos) && blah.sideHit == dir;
} }
public static RayTraceResult raytraceTowards(double x, double y, double z) { public static RayTraceResult rayTraceTowards(double x, double y, double z) {
return raytraceTowards(pitchAndYaw(x, y, z)); return raytraceTowards(pitchAndYaw(x, y, z));
} }