修改SeekTool方法

This commit is contained in:
17146 2024-08-30 00:59:57 +08:00
parent 5c0d14d38f
commit 24c215f074
2 changed files with 31 additions and 28 deletions

View file

@ -25,7 +25,6 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items; import net.minecraft.world.item.Items;
import net.minecraft.world.level.ClipContext; import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
import net.minecraftforge.event.TickEvent; import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.event.entity.player.PlayerEvent;
@ -96,7 +95,7 @@ public class PlayerEventHandler {
handleChangeFireRate(player); handleChangeFireRate(player);
handleBocekPulling(player); handleBocekPulling(player);
handleGunRecoil(player); handleGunRecoil(player);
handleWeaponSeek(player, event.player.level()); handleWeaponSeek(player);
} }
handleGround(player); handleGround(player);
@ -108,13 +107,12 @@ public class PlayerEventHandler {
} }
} }
//测试用 // 测试用
private static void handleWeaponSeek(Player player, LevelAccessor level) { private static void handleWeaponSeek(Player player) {
if (player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).zooming) { if (player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).zooming) {
Entity seekingEntity = SeekTool.seekEntity(player, level, 256,30); Entity seekingEntity = SeekTool.seekEntity(player, player.level(), 256, 30);
if (seekingEntity instanceof LivingEntity _entity && !_entity.level().isClientSide()) if (seekingEntity instanceof LivingEntity _entity && !_entity.level().isClientSide())
_entity.addEffect(new MobEffectInstance(MobEffects.GLOWING, 2, 0)); _entity.addEffect(new MobEffectInstance(MobEffects.GLOWING, 2, 0));
} }
} }

View file

@ -2,36 +2,41 @@ package net.mcreator.superbwarfare.tools;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
import java.util.Comparator; import java.util.Comparator;
import java.util.List;
public class SeekTool { public class SeekTool {
// TODO 固体方块阻挡搜寻以及目标锁定为夹角最小的实体 public static Entity seekEntity(Entity entity, Level level, double seekRange, double seekAngle) {
public static Entity seekEntity(Entity entity, LevelAccessor world, double seekRange, double seekAngle) { Vec3 pos = new Vec3(entity.getX(), entity.getY(), entity.getZ());
final Vec3 _center = new Vec3(entity.getX(), entity.getY(), entity.getZ()); return level.getEntitiesOfClass(Entity.class, new AABB(pos, pos).inflate(seekRange), e -> true).stream()
List<Entity> _entfound = world.getEntitiesOfClass(Entity.class, new AABB(_center, _center).inflate(seekRange), e -> true).stream().sorted(Comparator.comparingDouble(_entcnd -> _entcnd.distanceToSqr(_center))).toList(); .filter(e -> {
for (Entity entityiterator : _entfound) { if (calculateAngle(e, entity) < seekAngle && e instanceof LivingEntity && e != entity) {
if ((new Object() { return level.clip(new ClipContext(entity.getEyePosition(), e.getEyePosition(),
public double angle(Vec3 _start, Vec3 _end) { ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, entity)).getType() != HitResult.Type.BLOCK;
double _d0 = _start.length();
double _d1 = _end.length();
if (_d0 > 0.0D && _d1 > 0.0D) {
return Math.toDegrees(Math.acos(_start.dot(_end) / (_d0 * _d1)));
} else {
return 0.0D;
} }
} return false;
}).angle((new Vec3((entityiterator.getX() - entity.getX()), (entityiterator.getY() - entity.getY()), (entityiterator.getZ() - entity.getZ()))), (entity.getLookAngle())) < seekAngle && entityiterator instanceof LivingEntity) { }).min(Comparator.comparingDouble(e -> calculateAngle(e, entity))).orElse(null);
if (entityiterator != entity) { }
return entityiterator;
} private static double calculateAngle(Entity entityA, Entity entityB) {
} Vec3 start = new Vec3(entityA.getX() - entityB.getX(), entityA.getY() - entityB.getY(), entityA.getZ() - entityB.getZ());
Vec3 end = entityB.getLookAngle();
return calculateAngle(start, end);
}
private static double calculateAngle(Vec3 start, Vec3 end) {
double startLength = start.length();
double endLength = end.length();
if (startLength > 0.0D && endLength > 0.0D) {
return Math.toDegrees(Math.acos(start.dot(end) / (startLength * endLength)));
} else {
return 0.0D;
} }
return null;
} }
} }