Skip to content

Commit

Permalink
Merge pull request #4320 from rfresh2/instabreak-fix
Browse files Browse the repository at this point in the history
don't apply block break delay to insta-breaks
  • Loading branch information
leijurv committed Apr 24, 2024
2 parents b31e0a8 + 15fdbb2 commit 69d3bc0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/api/java/baritone/api/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public final class Settings {

/**
* How many ticks between breaking a block and starting to break the next block. Default in game is 6 ticks.
* Values under 2 will be clamped.
* Values under 1 will be clamped. The delay only applies to non-instant (1-tick) breaks.
*/
public final Setting<Integer> blockBreakSpeed = new Setting<>(6);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ public abstract class MixinPlayerController implements IPlayerControllerMP {
@Override
public abstract void setIsHittingBlock(boolean isHittingBlock);

@Accessor("isDestroying")
@Override
public abstract boolean isHittingBlock();

@Accessor("destroyBlockPos")
@Override
public abstract BlockPos getCurrentBlock();

@Invoker("ensureHasSentCarriedItem")
@Override
public abstract void callSyncCurrentPlayItem();

@Accessor("destroyDelay")
@Override
public abstract void setDestroyDelay(int destroyDelay);
}
46 changes: 25 additions & 21 deletions src/main/java/baritone/utils/BlockBreakHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import baritone.api.BaritoneAPI;
import baritone.api.utils.IPlayerContext;
import baritone.utils.accessor.IPlayerControllerMP;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
Expand All @@ -29,10 +30,10 @@
*/
public final class BlockBreakHelper {
// base ticks between block breaks caused by tick logic
private static final int BASE_BREAK_DELAY = 2;
private static final int BASE_BREAK_DELAY = 1;

private final IPlayerContext ctx;
private boolean didBreakLastTick;
private boolean wasHitting;
private int breakDelayTimer = 0;

BlockBreakHelper(IPlayerContext ctx) {
Expand All @@ -41,13 +42,10 @@ public final class BlockBreakHelper {

public void stopBreakingBlock() {
// The player controller will never be null, but the player can be
if (ctx.player() != null && didBreakLastTick) {
if (!ctx.playerController().hasBrokenBlock()) {
// insane bypass to check breaking succeeded
ctx.playerController().setHittingBlock(true);
}
if (ctx.player() != null && wasHitting) {
ctx.playerController().setHittingBlock(false);
ctx.playerController().resetBlockRemoving();
didBreakLastTick = false;
wasHitting = false;
}
}

Expand All @@ -60,24 +58,30 @@ public void tick(boolean isLeftClick) {
boolean isBlockTrace = trace != null && trace.getType() == HitResult.Type.BLOCK;

if (isLeftClick && isBlockTrace) {
if (!didBreakLastTick) {
ctx.playerController().setHittingBlock(wasHitting);
if (ctx.playerController().hasBrokenBlock()) {
ctx.playerController().syncHeldItem();
ctx.playerController().clickBlock(((BlockHitResult) trace).getBlockPos(), ((BlockHitResult) trace).getDirection());
ctx.player().swing(InteractionHand.MAIN_HAND);
} else {
if (ctx.playerController().onPlayerDamageBlock(((BlockHitResult) trace).getBlockPos(), ((BlockHitResult) trace).getDirection())) {
ctx.player().swing(InteractionHand.MAIN_HAND);
}
if (ctx.playerController().hasBrokenBlock()) { // block broken this tick
// break delay timer only applies for multi-tick block breaks like vanilla
breakDelayTimer = BaritoneAPI.getSettings().blockBreakSpeed.value - BASE_BREAK_DELAY;
// must reset controller's destroy delay to prevent the client from delaying itself unnecessarily
((IPlayerControllerMP) ctx.minecraft().gameMode).setDestroyDelay(0);
}
}

// Attempt to break the block
if (ctx.playerController().onPlayerDamageBlock(((BlockHitResult) trace).getBlockPos(), ((BlockHitResult) trace).getDirection())) {
ctx.player().swing(InteractionHand.MAIN_HAND);
}

// if true, we're breaking a block. if false, we broke the block this tick
wasHitting = !ctx.playerController().hasBrokenBlock();
// this value will be reset by the MC client handling mouse keys
// since we're not spoofing the click keybind to the client, the client will stop the break if isDestroyingBlock is true
// we store and restore this value on the next tick to determine if we're breaking a block
ctx.playerController().setHittingBlock(false);

didBreakLastTick = true;
} else if (didBreakLastTick) {
stopBreakingBlock();
breakDelayTimer = BaritoneAPI.getSettings().blockBreakSpeed.value - BASE_BREAK_DELAY;
didBreakLastTick = false;
} else {
wasHitting = false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public interface IPlayerControllerMP {

void setIsHittingBlock(boolean isHittingBlock);

boolean isHittingBlock();

BlockPos getCurrentBlock();

void callSyncCurrentPlayItem();

void setDestroyDelay(int destroyDelay);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import baritone.api.utils.IPlayerController;
import baritone.utils.accessor.IPlayerControllerMP;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -54,7 +53,7 @@ public void syncHeldItem() {

@Override
public boolean hasBrokenBlock() {
return ((IPlayerControllerMP) mc.gameMode).getCurrentBlock().getY() == -1;
return !((IPlayerControllerMP) mc.gameMode).isHittingBlock();
}

@Override
Expand Down

0 comments on commit 69d3bc0

Please sign in to comment.