Future-compatibility for multiple Baritone instances

This commit is contained in:
Brady 2019-09-25 18:27:47 -05:00
parent aff3103f36
commit 9f271b1f44
No known key found for this signature in database
GPG Key ID: 73A788379A197567
4 changed files with 52 additions and 38 deletions

View File

@ -35,21 +35,27 @@ import net.minecraft.world.World;
*/
public interface IPlayerController {
void syncHeldItem();
boolean hasBrokenBlock();
boolean onPlayerDamageBlock(BlockPos pos, EnumFacing side);
void resetBlockRemoving();
ItemStack windowClick(int windowId, int slotId, int mouseButton, ClickType type, EntityPlayer player);
void setGameType(GameType type);
GameType getGameType();
default double getBlockReachDistance() {
return this.getGameType().isCreative() ? 5.0F : 4.5F;
}
EnumActionResult processRightClickBlock(EntityPlayerSP player, World world, BlockPos pos, EnumFacing direction, Vec3d vec, EnumHand hand);
EnumActionResult processRightClick(EntityPlayerSP player, World world, EnumHand hand);
boolean clickBlock(BlockPos loc, EnumFacing face);
void setHittingBlock(boolean hittingBlock);
default double getBlockReachDistance() {
return this.getGameType().isCreative() ? 5.0F : 4.5F;
}
}

View File

@ -19,11 +19,7 @@ package baritone.utils;
import baritone.api.utils.Helper;
import baritone.api.utils.IPlayerContext;
import baritone.utils.accessor.IPlayerControllerMP;
import net.minecraft.client.Minecraft;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
/**
@ -32,49 +28,46 @@ import net.minecraft.util.math.RayTraceResult;
*/
public final class BlockBreakHelper implements Helper {
private final IPlayerContext ctx;
private boolean didBreakLastTick;
private final IPlayerContext playerContext;
public BlockBreakHelper(IPlayerContext playerContext) {
this.playerContext = playerContext;
}
private void tryBreakBlock(BlockPos pos, EnumFacing side) {
if (playerContext.playerController().onPlayerDamageBlock(pos, side)) {
playerContext.player().swingArm(EnumHand.MAIN_HAND);
}
BlockBreakHelper(IPlayerContext ctx) {
this.ctx = ctx;
}
public void stopBreakingBlock() {
// The player controller will never be null, but the player can be
if (playerContext.player() != null && didBreakLastTick) {
if (((IPlayerControllerMP) mc.playerController).getCurrentBlock().getY() != -1) {
if (ctx.player() != null && didBreakLastTick) {
if (!ctx.playerController().hasBrokenBlock()) {
// insane bypass to check breaking succeeded
((IPlayerControllerMP) mc.playerController).setIsHittingBlock(true);
ctx.playerController().setHittingBlock(true);
}
playerContext.playerController().resetBlockRemoving();
ctx.playerController().resetBlockRemoving();
didBreakLastTick = false;
}
}
public void tick(boolean isLeftClick) {
RayTraceResult trace = playerContext.objectMouseOver();
RayTraceResult trace = ctx.objectMouseOver();
boolean isBlockTrace = trace != null && trace.typeOfHit == RayTraceResult.Type.BLOCK;
if (isLeftClick && isBlockTrace) {
if (!didBreakLastTick) {
((IPlayerControllerMP) Minecraft.getMinecraft().playerController).callSyncCurrentPlayItem();
Minecraft.getMinecraft().playerController.clickBlock(trace.getBlockPos(), trace.sideHit);
playerContext.player().swingArm(EnumHand.MAIN_HAND);
ctx.playerController().syncHeldItem();
ctx.playerController().clickBlock(trace.getBlockPos(), trace.sideHit);
ctx.player().swingArm(EnumHand.MAIN_HAND);
}
tryBreakBlock(trace.getBlockPos(), trace.sideHit);
// Attempt to break the block
if (ctx.playerController().onPlayerDamageBlock(trace.getBlockPos(), trace.sideHit)) {
ctx.player().swingArm(EnumHand.MAIN_HAND);
}
didBreakLastTick = true;
} else if (didBreakLastTick) {
stopBreakingBlock();
didBreakLastTick = false;
}
((IPlayerControllerMP) Minecraft.getMinecraft().playerController).setIsHittingBlock(false);
ctx.playerController().setHittingBlock(false);
}
}

View File

@ -28,7 +28,7 @@ public class BlockPlaceHelper implements Helper {
private final IPlayerContext ctx;
private int rightClickTimer;
public BlockPlaceHelper(IPlayerContext playerContext) {
BlockPlaceHelper(IPlayerContext playerContext) {
this.ctx = playerContext;
}

View File

@ -19,6 +19,7 @@ package baritone.utils.player;
import baritone.api.utils.Helper;
import baritone.api.utils.IPlayerController;
import baritone.utils.accessor.IPlayerControllerMP;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.entity.player.EntityPlayer;
@ -42,6 +43,16 @@ public enum PrimaryPlayerController implements IPlayerController, Helper {
INSTANCE;
@Override
public void syncHeldItem() {
((IPlayerControllerMP) mc.playerController).callSyncCurrentPlayItem();
}
@Override
public boolean hasBrokenBlock() {
return ((IPlayerControllerMP) mc.playerController).getCurrentBlock().getY() == -1;
}
@Override
public boolean onPlayerDamageBlock(BlockPos pos, EnumFacing side) {
return mc.playerController.onPlayerDamageBlock(pos, side);
@ -57,11 +68,6 @@ public enum PrimaryPlayerController implements IPlayerController, Helper {
return mc.playerController.windowClick(windowId, slotId, mouseButton, type, player);
}
@Override
public void setGameType(GameType type) {
mc.playerController.setGameType(type);
}
@Override
public GameType getGameType() {
return mc.playerController.getCurrentGameType();
@ -69,7 +75,6 @@ public enum PrimaryPlayerController implements IPlayerController, Helper {
@Override
public EnumActionResult processRightClickBlock(EntityPlayerSP player, World world, BlockPos pos, EnumFacing direction, Vec3d vec, EnumHand hand) {
// primaryplayercontroller is always in a WorldClient so this is ok
return mc.playerController.processRightClickBlock(player, (WorldClient) world, pos, direction, vec, hand);
}
@ -77,4 +82,14 @@ public enum PrimaryPlayerController implements IPlayerController, Helper {
public EnumActionResult processRightClick(EntityPlayerSP player, World world, EnumHand hand) {
return mc.playerController.processRightClick(player, world, hand);
}
@Override
public boolean clickBlock(BlockPos loc, EnumFacing face) {
return mc.playerController.clickBlock(loc, face);
}
@Override
public void setHittingBlock(boolean hittingBlock) {
((IPlayerControllerMP) mc.playerController).setIsHittingBlock(hittingBlock);
}
}