优化seekEntity实现方式
This commit is contained in:
parent
18427e26b0
commit
c5cddcc814
2 changed files with 18 additions and 4 deletions
|
@ -6,10 +6,25 @@ import net.minecraft.server.level.ServerLevel;
|
||||||
import net.minecraft.world.entity.Entity;
|
import net.minecraft.world.entity.Entity;
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraft.world.level.Level;
|
import net.minecraft.world.level.Level;
|
||||||
|
import net.minecraft.world.level.entity.LevelEntityGetter;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class EntityFindUtil {
|
public class EntityFindUtil {
|
||||||
|
/**
|
||||||
|
* 获取世界里的所有实体,对ClientLevel和ServerLevel均有效
|
||||||
|
*
|
||||||
|
* @param level 目标世界
|
||||||
|
* @return 所有实体
|
||||||
|
*/
|
||||||
|
public static LevelEntityGetter<Entity> getEntities(Level level) {
|
||||||
|
if (level instanceof ServerLevel serverLevel) {
|
||||||
|
return serverLevel.getEntities();
|
||||||
|
}
|
||||||
|
var clientLevel = (ClientLevel) level;
|
||||||
|
return clientLevel.getEntities();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查找当前已知实体,对ClientLevel和ServerLevel均有效
|
* 查找当前已知实体,对ClientLevel和ServerLevel均有效
|
||||||
*
|
*
|
||||||
|
|
|
@ -3,19 +3,18 @@ package net.mcreator.superbwarfare.tools;
|
||||||
import net.minecraft.world.entity.Entity;
|
import net.minecraft.world.entity.Entity;
|
||||||
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.phys.AABB;
|
|
||||||
import net.minecraft.world.phys.HitResult;
|
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.stream.StreamSupport;
|
||||||
|
|
||||||
public class SeekTool {
|
public class SeekTool {
|
||||||
|
|
||||||
public static Entity seekEntity(Entity entity, Level level, double seekRange, double seekAngle) {
|
public static Entity seekEntity(Entity entity, Level level, double seekRange, double seekAngle) {
|
||||||
Vec3 pos = new Vec3(entity.getX(), entity.getY(), entity.getZ());
|
return StreamSupport.stream(EntityFindUtil.getEntities(level).getAll().spliterator(), false)
|
||||||
return level.getEntitiesOfClass(Entity.class, new AABB(pos, pos).inflate(seekRange), e -> true).stream()
|
|
||||||
.filter(e -> {
|
.filter(e -> {
|
||||||
if (calculateAngle(e, entity) < seekAngle && e != entity) {
|
if (e.distanceTo(entity) <= seekRange && calculateAngle(e, entity) < seekAngle && e != entity) {
|
||||||
return level.clip(new ClipContext(entity.getEyePosition(), e.getEyePosition(),
|
return level.clip(new ClipContext(entity.getEyePosition(), e.getEyePosition(),
|
||||||
ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, entity)).getType() != HitResult.Type.BLOCK;
|
ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, entity)).getType() != HitResult.Type.BLOCK;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue