mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-28 11:29:12 +00:00
fix: fix bot use raytrace (#669)
* fix: fix bot use raytrace * chore: refactor
This commit is contained in:
@@ -45,6 +45,7 @@ import net.minecraft.world.level.portal.TeleportTransition;
|
||||
import net.minecraft.world.level.storage.ValueInput;
|
||||
import net.minecraft.world.level.storage.ValueOutput;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.EntityHitResult;
|
||||
import net.minecraft.world.phys.HitResult;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
@@ -540,14 +541,22 @@ public class ServerBot extends ServerPlayer {
|
||||
return this.getBukkitEntity().getLocation();
|
||||
}
|
||||
|
||||
public EntityHitResult getEntityHitResult(int maxDistance, Predicate<? super Entity> predicate) {
|
||||
EntityHitResult result = this.pick(this, maxDistance);
|
||||
public EntityHitResult getEntityHitResult() {
|
||||
return this.getEntityHitResult(null);
|
||||
}
|
||||
|
||||
public EntityHitResult getEntityHitResult(Predicate<? super Entity> predicate) {
|
||||
EntityHitResult result = this.pick(this, this.entityInteractionRange());
|
||||
if (result != null && (predicate == null || predicate.test(result.getEntity()))) {
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public BlockHitResult getBlockHitResult() {
|
||||
return (BlockHitResult) this.pick(this.blockInteractionRange(), 1.0f, false);
|
||||
}
|
||||
|
||||
private EntityHitResult pick(Entity entity, double maxDistance) {
|
||||
double d = maxDistance;
|
||||
double d1 = Mth.square(maxDistance);
|
||||
|
||||
@@ -13,7 +13,7 @@ public class ServerAttackAction extends ServerTimerBotAction<ServerAttackAction>
|
||||
|
||||
@Override
|
||||
public boolean doTick(@NotNull ServerBot bot) {
|
||||
EntityHitResult hitResult = bot.getEntityHitResult(3, target -> target.isAttackable() && !target.skipAttackInteraction(bot));
|
||||
EntityHitResult hitResult = bot.getEntityHitResult(target -> target.isAttackable() && !target.skipAttackInteraction(bot));
|
||||
if (hitResult == null) {
|
||||
return false;
|
||||
} else {
|
||||
|
||||
@@ -14,9 +14,7 @@ import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.entity.bot.actions.CraftUseItemAutoAction;
|
||||
|
||||
import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemAction.useItem;
|
||||
import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemOnAction.getBlockHitResult;
|
||||
import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemOnAction.useItemOn;
|
||||
import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemToAction.getEntityHitResult;
|
||||
import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemToAction.useItemTo;
|
||||
|
||||
public class ServerUseItemAutoAction extends ServerUseBotAction<ServerUseItemAutoAction> {
|
||||
@@ -66,10 +64,10 @@ public class ServerUseItemAutoAction extends ServerUseBotAction<ServerUseItemAut
|
||||
private static @Nullable HitResult getHitResult(@NotNull ServerBot bot) {
|
||||
Vec3 eyePos = bot.getEyePosition();
|
||||
|
||||
EntityHitResult entityHitResult = getEntityHitResult(bot);
|
||||
EntityHitResult entityHitResult = bot.getEntityHitResult();
|
||||
double entityDistance = entityHitResult != null ? entityHitResult.getLocation().distanceToSqr(eyePos) : Double.MAX_VALUE;
|
||||
|
||||
BlockHitResult blockHitResult = getBlockHitResult(bot);
|
||||
BlockHitResult blockHitResult = bot.getBlockHitResult();
|
||||
double blockDistance = blockHitResult != null ? blockHitResult.getLocation().distanceToSqr(eyePos) : Double.MAX_VALUE;
|
||||
|
||||
if (entityDistance == Double.MAX_VALUE && blockDistance == Double.MAX_VALUE) {
|
||||
|
||||
@@ -3,11 +3,8 @@ package org.leavesmc.leaves.bot.agent.actions;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.level.ClipContext;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.HitResult;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.entity.bot.actions.CraftUseItemOnAction;
|
||||
|
||||
@@ -19,19 +16,10 @@ public class ServerUseItemOnAction extends ServerUseBotAction<ServerUseItemOnAct
|
||||
|
||||
@Override
|
||||
protected boolean interact(@NotNull ServerBot bot) {
|
||||
BlockHitResult hitResult = getBlockHitResult(bot);
|
||||
BlockHitResult hitResult = bot.getBlockHitResult();
|
||||
return useItemOn(bot, hitResult, InteractionHand.MAIN_HAND).consumesAction();
|
||||
}
|
||||
|
||||
public static @Nullable BlockHitResult getBlockHitResult(@NotNull ServerBot bot) {
|
||||
HitResult result = bot.getRayTrace((int) bot.blockInteractionRange(), ClipContext.Fluid.NONE);
|
||||
if (result instanceof BlockHitResult blockHitResult) {
|
||||
return blockHitResult;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static InteractionResult useItemOn(ServerBot bot, BlockHitResult hitResult, InteractionHand hand) {
|
||||
if (hitResult == null) {
|
||||
return InteractionResult.FAIL;
|
||||
|
||||
@@ -6,7 +6,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.entity.bot.actions.CraftUseItemOnOffhandAction;
|
||||
|
||||
import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemOnAction.getBlockHitResult;
|
||||
import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemOnAction.useItemOn;
|
||||
|
||||
public class ServerUseItemOnOffhandAction extends ServerUseBotAction<ServerUseItemOnOffhandAction> {
|
||||
@@ -17,7 +16,7 @@ public class ServerUseItemOnOffhandAction extends ServerUseBotAction<ServerUseIt
|
||||
|
||||
@Override
|
||||
protected boolean interact(@NotNull ServerBot bot) {
|
||||
BlockHitResult hitResult = getBlockHitResult(bot);
|
||||
BlockHitResult hitResult = bot.getBlockHitResult();
|
||||
return useItemOn(bot, hitResult, InteractionHand.OFF_HAND).consumesAction();
|
||||
}
|
||||
|
||||
|
||||
@@ -17,14 +17,10 @@ public class ServerUseItemToAction extends ServerUseBotAction<ServerUseItemToAct
|
||||
|
||||
@Override
|
||||
protected boolean interact(@NotNull ServerBot bot) {
|
||||
EntityHitResult hitResult = getEntityHitResult(bot);
|
||||
EntityHitResult hitResult = bot.getEntityHitResult();
|
||||
return useItemTo(bot, hitResult, InteractionHand.MAIN_HAND).consumesAction();
|
||||
}
|
||||
|
||||
public static EntityHitResult getEntityHitResult(@NotNull ServerBot bot) {
|
||||
return bot.getEntityHitResult((int) bot.entityInteractionRange(), null);
|
||||
}
|
||||
|
||||
public static InteractionResult useItemTo(ServerBot bot, EntityHitResult hitResult, InteractionHand hand) {
|
||||
if (hitResult == null) {
|
||||
return InteractionResult.FAIL;
|
||||
|
||||
@@ -6,7 +6,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.entity.bot.actions.CraftUseItemToOffhandAction;
|
||||
|
||||
import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemToAction.getEntityHitResult;
|
||||
import static org.leavesmc.leaves.bot.agent.actions.ServerUseItemToAction.useItemTo;
|
||||
|
||||
public class ServerUseItemToOffhandAction extends ServerUseBotAction<ServerUseItemToOffhandAction> {
|
||||
@@ -17,7 +16,7 @@ public class ServerUseItemToOffhandAction extends ServerUseBotAction<ServerUseIt
|
||||
|
||||
@Override
|
||||
protected boolean interact(@NotNull ServerBot bot) {
|
||||
EntityHitResult hitResult = getEntityHitResult(bot);
|
||||
EntityHitResult hitResult = bot.getEntityHitResult();
|
||||
return useItemTo(bot, hitResult, InteractionHand.OFF_HAND).consumesAction();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user