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

View File

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

View File

@ -108,11 +108,10 @@ public abstract class Movement implements Helper, MovementHelper {
* @return Status
*/
public MovementStatus update() {
MovementState latestState = updateState(currentState);
latestState.getTarget().rotation.ifPresent(LookBehavior.INSTANCE::updateTarget);
//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().getRotation().ifPresent(LookBehavior.INSTANCE::updateTarget);
// 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.inputState.forEach((input, forced) -> {
Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced);
System.out.println(input + " AND " + forced);
@ -128,18 +127,16 @@ public abstract class Movement implements Helper, MovementHelper {
private boolean prepared(MovementState state) {
if (state.getStatus() == MovementStatus.WAITING) {
if (state.getStatus() == MovementStatus.WAITING)
return true;
}
for(BlockPos blockPos : positionsToBreak) {
for (BlockPos blockPos : positionsToBreak) {
if(!MovementHelper.canWalkThrough(blockPos, BlockStateInterface.get(blockPos))) {
Optional<Rotation> reachable = LookBehaviorUtils.reachable(blockPos);
reachable.ifPresent(rotation -> {
state.setTarget(new MovementState.MovementTarget(Optional.empty(), reachable))
.setInput(Input.CLICK_LEFT, true);
});
if (reachable.isPresent())
if (reachable.isPresent()) {
state.setTarget(new MovementState.MovementTarget(reachable.get())).setInput(Input.CLICK_LEFT, true);
return false;
}
}
}
return true;

View File

@ -11,8 +11,8 @@ import java.util.Optional;
public class MovementState {
private MovementStatus status;
private MovementTarget goal = new MovementTarget(Optional.empty(), Optional.empty());
private MovementTarget target = new MovementTarget(Optional.empty(), Optional.empty());
private MovementTarget goal = new MovementTarget();
private MovementTarget target = new MovementTarget();
protected final Map<Input, Boolean> inputState = new HashMap<>();
public MovementState setStatus(MovementStatus status) {
@ -30,19 +30,39 @@ public class MovementState {
* <p>
* TODO: Decide desiredMovement type
*/
public Optional<Vec3d> position;
public Vec3d position;
/**
* Yaw and pitch angles that must be matched
* <p>
* getFirst() -> YAW
* 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.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() {

View File

@ -6,13 +6,11 @@ import baritone.bot.pathing.movement.MovementHelper;
import baritone.bot.pathing.movement.MovementState;
import baritone.bot.pathing.movement.MovementState.MovementStatus;
import baritone.bot.utils.BlockStateInterface;
import baritone.bot.utils.Rotation;
import baritone.bot.utils.ToolSet;
import baritone.bot.utils.Utils;
import net.minecraft.block.BlockFalling;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import java.util.Optional;
public class MovementAscend extends Movement {
BlockPos[] against = new BlockPos[3];
@ -67,7 +65,6 @@ public class MovementAscend extends Movement {
@Override
public MovementState updateState(MovementState state) {
super.updateState(state);
System.out.println("Ticking with state " + state.getStatus());
switch (state.getStatus()) {
@ -81,10 +78,9 @@ public class MovementAscend extends Movement {
state.setStatus(MovementStatus.SUCCESS);
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())))));
state.setInput(InputOverrideHandler.Input.JUMP, true).setInput(InputOverrideHandler.Input.MOVE_FORWARD, true);
return state;
Rotation rotationToBlock = Utils.calcRotationFromVec3d(playerHead(), Utils.calcCenterFromCoords(positionsToBreak[0], world()));
return state.setTarget(new MovementState.MovementTarget(rotationToBlock))
.setInput(InputOverrideHandler.Input.JUMP, true).setInput(InputOverrideHandler.Input.MOVE_FORWARD, true);
default:
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.MovementState;
import baritone.bot.utils.BlockStateInterface;
import baritone.bot.utils.Rotation;
import baritone.bot.utils.ToolSet;
import baritone.bot.utils.Utils;
import net.minecraft.block.Block;
@ -14,9 +15,6 @@ import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import java.util.Optional;
public class MovementTraverse extends Movement {
@ -98,8 +96,9 @@ public class MovementTraverse extends Movement {
state.setStatus(MovementState.MovementStatus.SUCCESS);
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);
return state;
Rotation rotationToBlock = Utils.calcRotationFromVec3d(playerHead(), Utils.calcCenterFromCoords(positionsToBreak[0], world()));
return state.setTarget(new MovementState.MovementTarget(rotationToBlock))
.setInput(InputOverrideHandler.Input.MOVE_FORWARD, true);
default:
return state;

View File

@ -4,6 +4,7 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
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);
}
default Vec3d playerHead() {
return new Vec3d(mc.player.posX, mc.player.posY + mc.player.getEyeHeight(), mc.player.posZ);
}
default void displayChatMessageRaw(String 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 faceY = (pos.getY() + side.getY()) * 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;
}
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));
}