Search in sources :

Example 36 with BetterBlockPos

use of baritone.api.utils.BetterBlockPos in project Spark-Client by Spark-Client-Development.

the class PathingBehavior method tickPath.

private void tickPath() {
    pausedThisTick = false;
    if (pauseRequestedLastTick && safeToCancel) {
        pauseRequestedLastTick = false;
        if (unpausedLastTick) {
            baritone.getInputOverrideHandler().clearAllKeys();
            baritone.getInputOverrideHandler().getBlockBreakHelper().stopBreakingBlock();
        }
        unpausedLastTick = false;
        pausedThisTick = true;
        return;
    }
    unpausedLastTick = true;
    if (cancelRequested) {
        cancelRequested = false;
        baritone.getInputOverrideHandler().clearAllKeys();
    }
    synchronized (pathPlanLock) {
        synchronized (pathCalcLock) {
            if (inProgress != null) {
                // we are calculating
                // are we calculating the right thing though? 🤔
                BetterBlockPos calcFrom = inProgress.getStart();
                Optional<IPath> currentBest = inProgress.bestPathSoFar();
                if (// if current ends in inProgress's start, then we're ok
                (current == null || !current.getPath().getDest().equals(calcFrom)) && !calcFrom.equals(ctx.playerFeet()) && // if current starts in our playerFeet or pathStart, then we're ok
                !calcFrom.equals(expectedSegmentStart) && // if
                (!currentBest.isPresent() || (!currentBest.get().positions().contains(ctx.playerFeet()) && !currentBest.get().positions().contains(expectedSegmentStart)))) {
                    // when it was *just* started, currentBest will be empty so we need to also check calcFrom since that's always present
                    // cancellation doesn't dispatch any events
                    inProgress.cancel();
                }
            }
        }
        if (current == null) {
            return;
        }
        safeToCancel = current.onTick();
        if (current.failed() || current.finished()) {
            current = null;
            if (goal == null || goal.isInGoal(ctx.playerFeet())) {
                logDebug("All done. At " + goal);
                queuePathEvent(PathEvent.AT_GOAL);
                next = null;
                if (Baritone.settings().disconnectOnArrival.getValue()) {
                    ctx.world().sendQuittingDisconnectingPacket();
                }
                return;
            }
            if (next != null && !next.getPath().positions().contains(ctx.playerFeet()) && !next.getPath().positions().contains(expectedSegmentStart)) {
                // can contain either one
                // if the current path failed, we may not actually be on the next one, so make sure
                logDebug("Discarding next path as it does not contain current position");
                // for example if we had a nicely planned ahead path that starts where current ends
                // that's all fine and good
                // but if we fail in the middle of current
                // we're nowhere close to our planned ahead path
                // so need to discard it sadly.
                queuePathEvent(PathEvent.DISCARD_NEXT);
                next = null;
            }
            if (next != null) {
                logDebug("Continuing on to planned next path");
                queuePathEvent(PathEvent.CONTINUING_ONTO_PLANNED_NEXT);
                current = next;
                next = null;
                // don't waste a tick doing nothing, get started right away
                current.onTick();
                return;
            }
            // at this point, current just ended, but we aren't in the goal and have no plan for the future
            synchronized (pathCalcLock) {
                if (inProgress != null) {
                    queuePathEvent(PathEvent.PATH_FINISHED_NEXT_STILL_CALCULATING);
                    return;
                }
                // we aren't calculating
                queuePathEvent(PathEvent.CALC_STARTED);
                findPathInNewThread(expectedSegmentStart, true, context);
            }
            return;
        }
        // at this point, we know current is in progress
        if (safeToCancel && next != null && next.snipsnapifpossible()) {
            // a movement just ended; jump directly onto the next path
            logDebug("Splicing into planned next path early...");
            queuePathEvent(PathEvent.SPLICING_ONTO_NEXT_EARLY);
            current = next;
            next = null;
            current.onTick();
            return;
        }
        if (Baritone.settings().splicePath.getValue()) {
            current = current.trySplice(next);
        }
        if (next != null && current.getPath().getDest().equals(next.getPath().getDest())) {
            next = null;
        }
        synchronized (pathCalcLock) {
            if (inProgress != null) {
                // if we aren't calculating right now
                return;
            }
            if (next != null) {
                // and we have no plan for what to do next
                return;
            }
            if (goal == null || goal.isInGoal(current.getPath().getDest())) {
                // and this path doesn't get us all the way there
                return;
            }
            if (ticksRemainingInSegment(false).get() < Baritone.settings().planningTickLookahead.getValue()) {
                // and this path has 7.5 seconds or less left
                // don't include the current movement so a very long last movement (e.g. descend) doesn't trip it up
                // if we actually included current, it wouldn't start planning ahead until the last movement was done, if the last movement took more than 7.5 seconds on its own
                logDebug("Path almost over. Planning ahead...");
                queuePathEvent(PathEvent.NEXT_SEGMENT_CALC_STARTED);
                findPathInNewThread(current.getPath().getDest(), false, context);
            }
        }
    }
}
Also used : IPath(baritone.api.pathing.calc.IPath) BetterBlockPos(baritone.api.utils.BetterBlockPos)

Example 37 with BetterBlockPos

use of baritone.api.utils.BetterBlockPos in project Spark-Client by Spark-Client-Development.

the class RenderCommand method execute.

@Override
public void execute(String label, IArgConsumer args) throws CommandException {
    args.requireMax(0);
    BetterBlockPos origin = ctx.playerFeet();
    int renderDistance = (mc.gameSettings.renderDistanceChunks + 1) * 16;
    mc.renderGlobal.markBlockRangeForRenderUpdate(origin.x - renderDistance, 0, origin.z - renderDistance, origin.x + renderDistance, 255, origin.z + renderDistance);
    logDirect("Done");
}
Also used : BetterBlockPos(baritone.api.utils.BetterBlockPos)

Example 38 with BetterBlockPos

use of baritone.api.utils.BetterBlockPos in project Spark-Client by Spark-Client-Development.

the class SurfaceCommand method execute.

@Override
public void execute(String label, IArgConsumer args) throws CommandException {
    final BetterBlockPos playerPos = baritone.getPlayerContext().playerFeet();
    final int surfaceLevel = baritone.getPlayerContext().world().getSeaLevel();
    final int worldHeight = baritone.getPlayerContext().world().getActualHeight();
    // As this would imply that your are already on the open surface
    if (playerPos.getY() > surfaceLevel && mc.world.getBlockState(playerPos.up()).getBlock() instanceof BlockAir) {
        logDirect("Already at surface");
        return;
    }
    final int startingYPos = Math.max(playerPos.getY(), surfaceLevel);
    for (int currentIteratedY = startingYPos; currentIteratedY < worldHeight; currentIteratedY++) {
        final BetterBlockPos newPos = new BetterBlockPos(playerPos.getX(), currentIteratedY, playerPos.getZ());
        if (!(mc.world.getBlockState(newPos).getBlock() instanceof BlockAir) && newPos.getY() > playerPos.getY()) {
            Goal goal = new GoalBlock(newPos.up());
            logDirect(String.format("Going to: %s", goal.toString()));
            baritone.getCustomGoalProcess().setGoalAndPath(goal);
            return;
        }
    }
    logDirect("No higher location found");
}
Also used : BlockAir(net.minecraft.block.BlockAir) Goal(baritone.api.pathing.goals.Goal) GoalBlock(baritone.api.pathing.goals.GoalBlock) BetterBlockPos(baritone.api.utils.BetterBlockPos)

Example 39 with BetterBlockPos

use of baritone.api.utils.BetterBlockPos in project Spark-Client by Spark-Client-Development.

the class WaypointsCommand method execute.

@Override
public void execute(String label, IArgConsumer args) throws CommandException {
    Action action = args.hasAny() ? Action.getByName(args.getString()) : Action.LIST;
    if (action == null) {
        throw new CommandInvalidTypeException(args.consumed(), "an action");
    }
    BiFunction<IWaypoint, Action, ITextComponent> toComponent = (waypoint, _action) -> {
        ITextComponent component = new TextComponentString("");
        ITextComponent tagComponent = new TextComponentString(waypoint.getTag().name() + " ");
        tagComponent.getStyle().setColor(TextFormatting.GRAY);
        String name = waypoint.getName();
        ITextComponent nameComponent = new TextComponentString(!name.isEmpty() ? name : "<empty>");
        nameComponent.getStyle().setColor(!name.isEmpty() ? TextFormatting.GRAY : TextFormatting.DARK_GRAY);
        ITextComponent timestamp = new TextComponentString(" @ " + new Date(waypoint.getCreationTimestamp()));
        timestamp.getStyle().setColor(TextFormatting.DARK_GRAY);
        component.appendSibling(tagComponent);
        component.appendSibling(nameComponent);
        component.appendSibling(timestamp);
        component.getStyle().setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString("Click to select"))).setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("%s%s %s %s @ %d", FORCE_COMMAND_PREFIX, label, _action.names[0], waypoint.getTag().getName(), waypoint.getCreationTimestamp())));
        return component;
    };
    Function<IWaypoint, ITextComponent> transform = waypoint -> toComponent.apply(waypoint, action == Action.LIST ? Action.INFO : action);
    if (action == Action.LIST) {
        IWaypoint.Tag tag = args.hasAny() ? IWaypoint.Tag.getByName(args.peekString()) : null;
        if (tag != null) {
            args.get();
        }
        IWaypoint[] waypoints = tag != null ? ForWaypoints.getWaypointsByTag(this.baritone, tag) : ForWaypoints.getWaypoints(this.baritone);
        if (waypoints.length > 0) {
            args.requireMax(1);
            Paginator.paginate(args, waypoints, () -> logDirect(tag != null ? String.format("All waypoints by tag %s:", tag.name()) : "All waypoints:"), transform, String.format("%s%s %s%s", FORCE_COMMAND_PREFIX, label, action.names[0], tag != null ? " " + tag.getName() : ""));
        } else {
            args.requireMax(0);
            throw new CommandInvalidStateException(tag != null ? "No waypoints found by that tag" : "No waypoints found");
        }
    } else if (action == Action.SAVE) {
        IWaypoint.Tag tag = IWaypoint.Tag.getByName(args.getString());
        if (tag == null) {
            throw new CommandInvalidStateException(String.format("'%s' is not a tag ", args.consumedString()));
        }
        String name = args.hasAny() ? args.getString() : "";
        BetterBlockPos pos = args.hasAny() ? args.getDatatypePost(RelativeBlockPos.INSTANCE, ctx.playerFeet()) : ctx.playerFeet();
        args.requireMax(0);
        IWaypoint waypoint = new Waypoint(name, tag, pos);
        ForWaypoints.waypoints(this.baritone).addWaypoint(waypoint);
        ITextComponent component = new TextComponentString("Waypoint added: ");
        component.getStyle().setColor(TextFormatting.GRAY);
        component.appendSibling(toComponent.apply(waypoint, Action.INFO));
        logDirect(component);
    } else if (action == Action.CLEAR) {
        args.requireMax(1);
        IWaypoint.Tag tag = IWaypoint.Tag.getByName(args.getString());
        IWaypoint[] waypoints = ForWaypoints.getWaypointsByTag(this.baritone, tag);
        for (IWaypoint waypoint : waypoints) {
            ForWaypoints.waypoints(this.baritone).removeWaypoint(waypoint);
        }
        logDirect(String.format("Cleared %d waypoints", waypoints.length));
    } else {
        IWaypoint[] waypoints = args.getDatatypeFor(ForWaypoints.INSTANCE);
        IWaypoint waypoint = null;
        if (args.hasAny() && args.peekString().equals("@")) {
            args.requireExactly(2);
            args.get();
            long timestamp = args.getAs(Long.class);
            for (IWaypoint iWaypoint : waypoints) {
                if (iWaypoint.getCreationTimestamp() == timestamp) {
                    waypoint = iWaypoint;
                    break;
                }
            }
            if (waypoint == null) {
                throw new CommandInvalidStateException("Timestamp was specified but no waypoint was found");
            }
        } else {
            switch(waypoints.length) {
                case 0:
                    throw new CommandInvalidStateException("No waypoints found");
                case 1:
                    waypoint = waypoints[0];
                    break;
                default:
                    break;
            }
        }
        if (waypoint == null) {
            args.requireMax(1);
            Paginator.paginate(args, waypoints, () -> logDirect("Multiple waypoints were found:"), transform, String.format("%s%s %s %s", FORCE_COMMAND_PREFIX, label, action.names[0], args.consumedString()));
        } else {
            if (action == Action.INFO) {
                logDirect(transform.apply(waypoint));
                logDirect(String.format("Position: %s", waypoint.getLocation()));
                ITextComponent deleteComponent = new TextComponentString("Click to delete this waypoint");
                deleteComponent.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("%s%s delete %s @ %d", FORCE_COMMAND_PREFIX, label, waypoint.getTag().getName(), waypoint.getCreationTimestamp())));
                ITextComponent goalComponent = new TextComponentString("Click to set goal to this waypoint");
                goalComponent.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("%s%s goal %s @ %d", FORCE_COMMAND_PREFIX, label, waypoint.getTag().getName(), waypoint.getCreationTimestamp())));
                ITextComponent backComponent = new TextComponentString("Click to return to the waypoints list");
                backComponent.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("%s%s list", FORCE_COMMAND_PREFIX, label)));
                logDirect(deleteComponent);
                logDirect(goalComponent);
                logDirect(backComponent);
            } else if (action == Action.DELETE) {
                ForWaypoints.waypoints(this.baritone).removeWaypoint(waypoint);
                logDirect("That waypoint has successfully been deleted");
            } else if (action == Action.GOAL) {
                Goal goal = new GoalBlock(waypoint.getLocation());
                baritone.getCustomGoalProcess().setGoal(goal);
                logDirect(String.format("Goal: %s", goal));
            } else if (action == Action.GOTO) {
                Goal goal = new GoalBlock(waypoint.getLocation());
                baritone.getCustomGoalProcess().setGoalAndPath(goal);
                logDirect(String.format("Going to: %s", goal));
            }
        }
    }
}
Also used : java.util(java.util) TabCompleteHelper(baritone.api.command.helpers.TabCompleteHelper) Command(baritone.api.command.Command) BiFunction(java.util.function.BiFunction) ClickEvent(net.minecraft.util.text.event.ClickEvent) Waypoint(baritone.api.cache.Waypoint) IArgConsumer(baritone.api.command.argument.IArgConsumer) Function(java.util.function.Function) ITextComponent(net.minecraft.util.text.ITextComponent) IBaritone(baritone.api.IBaritone) IWaypoint(baritone.api.cache.IWaypoint) CommandInvalidStateException(baritone.api.command.exception.CommandInvalidStateException) ForWaypoints(baritone.api.command.datatypes.ForWaypoints) CommandException(baritone.api.command.exception.CommandException) TextFormatting(net.minecraft.util.text.TextFormatting) Paginator(baritone.api.command.helpers.Paginator) GoalBlock(baritone.api.pathing.goals.GoalBlock) CommandInvalidTypeException(baritone.api.command.exception.CommandInvalidTypeException) FORCE_COMMAND_PREFIX(baritone.api.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX) Goal(baritone.api.pathing.goals.Goal) BetterBlockPos(baritone.api.utils.BetterBlockPos) TextComponentString(net.minecraft.util.text.TextComponentString) Stream(java.util.stream.Stream) HoverEvent(net.minecraft.util.text.event.HoverEvent) RelativeBlockPos(baritone.api.command.datatypes.RelativeBlockPos) HoverEvent(net.minecraft.util.text.event.HoverEvent) GoalBlock(baritone.api.pathing.goals.GoalBlock) CommandInvalidTypeException(baritone.api.command.exception.CommandInvalidTypeException) ClickEvent(net.minecraft.util.text.event.ClickEvent) ITextComponent(net.minecraft.util.text.ITextComponent) TextComponentString(net.minecraft.util.text.TextComponentString) TextComponentString(net.minecraft.util.text.TextComponentString) Goal(baritone.api.pathing.goals.Goal) IWaypoint(baritone.api.cache.IWaypoint) BetterBlockPos(baritone.api.utils.BetterBlockPos) Waypoint(baritone.api.cache.Waypoint) IWaypoint(baritone.api.cache.IWaypoint) CommandInvalidStateException(baritone.api.command.exception.CommandInvalidStateException)

Example 40 with BetterBlockPos

use of baritone.api.utils.BetterBlockPos in project Spark-Client by Spark-Client-Development.

the class MemoryBehavior method onSendPacket.

@Override
public synchronized void onSendPacket(PacketEvent event) {
    if (!Baritone.settings().containerMemory.getValue()) {
        return;
    }
    Packet p = event.getPacket();
    if (event.getState() == EventState.PRE) {
        if (p instanceof CPacketPlayerTryUseItemOnBlock) {
            CPacketPlayerTryUseItemOnBlock packet = event.cast();
            TileEntity tileEntity = ctx.world().getTileEntity(packet.getPos());
            // Ensure the TileEntity is a container of some sort
            if (tileEntity instanceof TileEntityLockable) {
                TileEntityLockable lockable = (TileEntityLockable) tileEntity;
                int size = lockable.getSizeInventory();
                BetterBlockPos position = BetterBlockPos.from(tileEntity.getPos());
                BetterBlockPos adj = BetterBlockPos.from(neighboringConnectedBlock(position));
                System.out.println(position + " " + adj);
                if (adj != null) {
                    // double chest or double trapped chest
                    size *= 2;
                    if (adj.getX() < position.getX() || adj.getZ() < position.getZ()) {
                        // standardize on the lower coordinate, regardless of which side of the large chest we right clicked
                        position = adj;
                    }
                }
                this.futureInventories.add(new FutureInventory(System.nanoTime() / 1000000L, size, lockable.getGuiID(), position));
            }
        }
        if (p instanceof CPacketCloseWindow) {
            getCurrent().save();
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) Packet(net.minecraft.network.Packet) TileEntityLockable(net.minecraft.tileentity.TileEntityLockable) BetterBlockPos(baritone.api.utils.BetterBlockPos) CPacketCloseWindow(net.minecraft.network.play.client.CPacketCloseWindow) Waypoint(baritone.api.cache.Waypoint) CPacketPlayerTryUseItemOnBlock(net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock)

Aggregations

BetterBlockPos (baritone.api.utils.BetterBlockPos)57 IBlockState (net.minecraft.block.state.IBlockState)15 Goal (baritone.api.pathing.goals.Goal)14 BlockPos (net.minecraft.util.math.BlockPos)10 CommandInvalidStateException (baritone.api.command.exception.CommandInvalidStateException)9 IWaypoint (baritone.api.cache.IWaypoint)6 GoalBlock (baritone.api.pathing.goals.GoalBlock)6 Waypoint (baritone.api.cache.Waypoint)5 Rotation (baritone.api.utils.Rotation)5 EnumFacing (net.minecraft.util.EnumFacing)5 IBaritone (baritone.api.IBaritone)4 Command (baritone.api.command.Command)4 IArgConsumer (baritone.api.command.argument.IArgConsumer)4 ForBlockOptionalMeta (baritone.api.command.datatypes.ForBlockOptionalMeta)4 RelativeBlockPos (baritone.api.command.datatypes.RelativeBlockPos)4 RelativeGoal (baritone.api.command.datatypes.RelativeGoal)4 CommandException (baritone.api.command.exception.CommandException)4 CommandInvalidTypeException (baritone.api.command.exception.CommandInvalidTypeException)4 IPath (baritone.api.pathing.calc.IPath)4 PathingCommand (baritone.api.process.PathingCommand)4