From a416f476d3517fc002ab4769e1f6d89e33e81aa4 Mon Sep 17 00:00:00 2001 From: Lumine1909 <133463833+Lumine1909@users.noreply.github.com> Date: Wed, 23 Jul 2025 02:59:31 -0700 Subject: [PATCH] Fix bot use actions (#606) (#605) * Stupid me * Fix bot use actions * clean up * Update leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAutoAction.java AND operator always has higher priority than OR operator, this is not "unclear" Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAutoAction.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Reuse getEntityHitResult * No bukkit, yes nms * Better rayTrace for inside aabb collision --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../org/leavesmc/leaves/bot/ServerBot.java | 35 ++++++++++++++----- .../bot/agent/actions/ServerAttackAction.java | 8 ++--- .../actions/ServerUseItemAutoAction.java | 13 ++++--- .../agent/actions/ServerUseItemOnAction.java | 35 +++++++++---------- .../actions/ServerUseItemOnOffhandAction.java | 35 +++++++++---------- .../agent/actions/ServerUseItemToAction.java | 25 ++++++++----- .../actions/ServerUseItemToOffhandAction.java | 25 ++++++++----- 7 files changed, 104 insertions(+), 72 deletions(-) diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java index 569196ca..516779c9 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java @@ -28,10 +28,12 @@ import net.minecraft.world.InteractionResult; import net.minecraft.world.SimpleMenuProvider; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntitySelector; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.PositionMoveRotation; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.entity.projectile.ProjectileUtil; import net.minecraft.world.inventory.ChestMenu; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.GameRules; @@ -40,7 +42,10 @@ import net.minecraft.world.level.gameevent.GameEvent; 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.EntityHitResult; +import net.minecraft.world.phys.HitResult; +import net.minecraft.world.phys.Vec3; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.command.CommandSender; @@ -496,19 +501,31 @@ public class ServerBot extends ServerPlayer { return this.getBukkitEntity().getLocation(); } - public Entity getTargetEntity(int maxDistance, Predicate predicate) { - List entities = this.level().getEntities((Entity) null, this.getBoundingBox(), (e -> e != this && (predicate == null || predicate.test(e)))); - if (!entities.isEmpty()) { - return entities.getFirst(); - } else { - EntityHitResult result = this.getBukkitEntity().rayTraceEntity(maxDistance, false); - if (result != null && (predicate == null || predicate.test(result.getEntity()))) { - return result.getEntity(); - } + public EntityHitResult getEntityHitResult(int maxDistance, Predicate predicate) { + EntityHitResult result = this.pick(this, maxDistance); + if (result != null && (predicate == null || predicate.test(result.getEntity()))) { + return result; } return null; } + private EntityHitResult pick(Entity entity, double maxDistance) { + double d = maxDistance; + double d1 = Mth.square(maxDistance); + Vec3 vec3 = entity.getEyePosition(1.0f); + HitResult hitResult = entity.pick(maxDistance, 1.0f, false); + double d2 = hitResult.getLocation().distanceToSqr(vec3); + if (hitResult.getType() != HitResult.Type.MISS) { + d1 = d2; + d = Math.sqrt(d2); + } + + Vec3 viewStart = entity.getViewVector(1.0f); + Vec3 viewEnd = vec3.add(viewStart.x * d, viewStart.y * d, viewStart.z * d); + AABB aABB = entity.getBoundingBox().expandTowards(viewStart.scale(d)).inflate(1.0, 1.0, 1.0); + return ProjectileUtil.getEntityHitResult(entity, vec3, viewEnd, aABB, EntitySelector.CAN_BE_PICKED, d1); + } + public void dropAll(boolean death) { NonNullList items = this.getInventory().getNonEquipmentItems(); for (int i = 0; i < items.size(); i++) { diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerAttackAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerAttackAction.java index b9008520..52361755 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerAttackAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerAttackAction.java @@ -1,6 +1,6 @@ package org.leavesmc.leaves.bot.agent.actions; -import net.minecraft.world.entity.Entity; +import net.minecraft.world.phys.EntityHitResult; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; import org.leavesmc.leaves.entity.bot.actions.CraftAttackAction; @@ -13,11 +13,11 @@ public class ServerAttackAction extends ServerTimerBotAction @Override public boolean doTick(@NotNull ServerBot bot) { - Entity entity = bot.getTargetEntity(3, target -> target.isAttackable() && !target.skipAttackInteraction(bot)); - if (entity == null) { + EntityHitResult hitResult = bot.getEntityHitResult(3, target -> target.isAttackable() && !target.skipAttackInteraction(bot)); + if (hitResult == null) { return false; } else { - bot.attack(entity); + bot.attack(hitResult.getEntity()); return true; } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAutoAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAutoAction.java index a65e6f29..93d180d9 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAutoAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemAutoAction.java @@ -1,9 +1,10 @@ package org.leavesmc.leaves.bot.agent.actions; import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.entity.Entity; +import net.minecraft.world.InteractionResult; import net.minecraft.world.level.ClipContext; import net.minecraft.world.phys.BlockHitResult; +import net.minecraft.world.phys.EntityHitResult; import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; @@ -74,11 +75,12 @@ public class ServerUseItemAutoAction extends ServerTimerBotAction chestBlockEntity.stopOpen(bot), 1); - return true; - } else { - return false; - } - - } else { - bot.updateItemInHand(InteractionHand.MAIN_HAND); - return bot.gameMode.useItemOn(bot, bot.level(), bot.getItemInHand(InteractionHand.MAIN_HAND), InteractionHand.MAIN_HAND, blockHitResult).consumesAction(); - } } + + boolean success; + if (state.getBlock() == Blocks.TRAPPED_CHEST && + bot.level().getBlockEntity(blockHitResult.getBlockPos()) instanceof TrappedChestBlockEntity chestBlockEntity + ) { + chestBlockEntity.startOpen(bot); + Bukkit.getScheduler().runTaskLater(MinecraftInternalPlugin.INSTANCE, () -> chestBlockEntity.stopOpen(bot), 1); + success = true; + } else { + bot.updateItemInHand(InteractionHand.MAIN_HAND); + success = bot.gameMode.useItemOn(bot, bot.level(), bot.getItemInHand(InteractionHand.MAIN_HAND), InteractionHand.MAIN_HAND, blockHitResult).consumesAction(); + } + if (success) { + bot.swing(InteractionHand.MAIN_HAND); + } + return success; } @Override @@ -130,4 +129,4 @@ public class ServerUseItemOnAction extends ServerTimerBotAction chestBlockEntity.stopOpen(bot), 1); - return true; - } else { - return false; - } - } else { - bot.updateItemInHand(InteractionHand.OFF_HAND); - return bot.gameMode.useItemOn(bot, bot.level(), bot.getItemInHand(InteractionHand.OFF_HAND), InteractionHand.OFF_HAND, blockHitResult).consumesAction(); - } } + + boolean success; + if (state.getBlock() == Blocks.TRAPPED_CHEST && + bot.level().getBlockEntity(blockHitResult.getBlockPos()) instanceof TrappedChestBlockEntity chestBlockEntity + ) { + chestBlockEntity.startOpen(bot); + Bukkit.getScheduler().runTaskLater(MinecraftInternalPlugin.INSTANCE, () -> chestBlockEntity.stopOpen(bot), 1); + success = true; + } else { + bot.updateItemInHand(InteractionHand.OFF_HAND); + success = bot.gameMode.useItemOn(bot, bot.level(), bot.getItemInHand(InteractionHand.OFF_HAND), InteractionHand.OFF_HAND, blockHitResult).consumesAction(); + } + if (success) { + bot.swing(InteractionHand.OFF_HAND); + } + return success; } @Override diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemToAction.java b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemToAction.java index 62cf461c..6e37ce4f 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemToAction.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/ServerUseItemToAction.java @@ -2,7 +2,9 @@ package org.leavesmc.leaves.bot.agent.actions; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; -import net.minecraft.world.entity.Entity; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.decoration.ArmorStand; +import net.minecraft.world.phys.EntityHitResult; import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.NotNull; import org.leavesmc.leaves.bot.ServerBot; @@ -39,8 +41,8 @@ public class ServerUseItemToAction extends ServerTimerBotAction= 0) { - Entity entity = bot.getTargetEntity(3, null); - boolean result = execute(bot, entity); + EntityHitResult hitResult = bot.getEntityHitResult(3, null); + boolean result = execute(bot, hitResult).consumesAction(); if (useTick >= 0) { return false; } else { @@ -69,17 +71,22 @@ public class ServerUseItemToAction extends ServerTimerBotAction= 0) { - Entity entity = bot.getTargetEntity(3, null); - boolean result = execute(bot, entity); + EntityHitResult hitResult = bot.getEntityHitResult(3, null); + boolean result = execute(bot, hitResult).consumesAction(); if (useTick >= 0) { return false; } else { @@ -69,17 +71,22 @@ public class ServerUseItemToOffhandAction extends ServerTimerBotAction