-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityUtils.java
184 lines (146 loc) · 8.6 KB
/
EntityUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package org.auioc.mcmod.arnicalib.utils.game;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import net.minecraft.core.Vec3i;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.MobType;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
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;
public class EntityUtils {
/*================================================================================================================*/
// #region Teleport
public static void teleportTo(Entity entity, Vec3i pos) {
entity.teleportTo(pos.getX() + 0.5D, pos.getY(), pos.getZ() + 0.5D);
}
public static void teleportTo(Entity entity, Vec3 pos) {
entity.teleportTo(pos.x, pos.y, pos.z);
}
// #endregion Teleport
/*================================================================================================================*/
// #region RayTrace
public static Vec3[] getEntityViewRay(Entity entity, double rayLength) {
Vec3 entityViewVector = entity.getViewVector(1.0F);
Vec3 rayPath = entityViewVector.scale(rayLength);
Vec3 from = entity.getEyePosition(1.0F);
Vec3 to = from.add(rayPath);
return new Vec3[] {from, to};
}
@Nullable
public static EntityHitResult getEntityHitResult(Level level, Entity entity, Vec3 from, Vec3 to, AABB aabb, Predicate<Entity> predicate, float pickRadiusAddition) {
double d0 = Double.MAX_VALUE;
Entity targetEntity = null;
for (Entity entity1 : level.getEntities(targetEntity, aabb, predicate)) {
AABB entity1aabb = entity1.getBoundingBox().inflate((double) (entity1.getPickRadius() + pickRadiusAddition));
Optional<Vec3> optional = entity1aabb.clip(from, to);
if (optional.isPresent()) {
double d1 = from.distanceToSqr(optional.get());
if (d1 < d0) {
targetEntity = entity1;
d0 = d1;
}
}
}
return targetEntity == null ? null : new EntityHitResult(targetEntity);
}
public static BlockHitResult getBlockHitResult(Entity entity, double rayLength, ClipContext.Block blockMode, ClipContext.Fluid fluidMode) {
Vec3[] viewRay = getEntityViewRay(entity, rayLength);
ClipContext rayCtx = new ClipContext(viewRay[0], viewRay[1], blockMode, fluidMode, entity);
return entity.level.clip(rayCtx);
}
@Nullable
public static EntityHitResult getEntityHitResult(Entity entity, double rayLength, float pickRadiusAddition, boolean blockMode) {
Vec3[] viewRay = getEntityViewRay(entity, rayLength);
Vec3 to = viewRay[1];
if (blockMode) {
BlockHitResult rayHitBlock = getBlockHitResult(entity, rayLength, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE);
if (rayHitBlock.getType() != HitResult.Type.MISS) {
to = rayHitBlock.getLocation();
}
}
return getEntityHitResult(entity, viewRay[0], to, pickRadiusAddition);
}
@Nullable
public static EntityHitResult getEntityHitResult(Entity entity, Vec3 from, Vec3 to, float pickRadiusAddition) {
return getEntityHitResult(entity.level, entity, from, to, entity.getBoundingBox().expandTowards(to).inflate(1.0D), EntitySelector.NO_SPECTATORS, pickRadiusAddition);
}
@Nullable
public static EntityHitResult getEntityHitResult(Entity entity, Vec3 from, Vec3 to) {
return getEntityHitResult(entity, from, to, 0.0F);
}
public static BlockHitResult getBlockHitResult(Entity entity, double rayLength) {
return getBlockHitResult(entity, rayLength, ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY);
}
public static EntityHitResult getEntityHitResult(Entity entity, double rayLength) {
return getEntityHitResult(entity, rayLength, 0.0F, false);
}
public static int rayHitEntityOrBlockOrMiss(
Entity entity, double rayLength,
float pickRadiusAddition, ClipContext.Block blockMode, ClipContext.Fluid fluidMode,
Function<EntityHitResult, Integer> e, Function<BlockHitResult, Integer> b, Function<BlockHitResult, Integer> m
) {
EntityHitResult rayHitEntity = getEntityHitResult(entity, rayLength, pickRadiusAddition, false);
if (rayHitEntity != null) {
return e.apply(rayHitEntity);
}
BlockHitResult rayHitBlock = getBlockHitResult(entity, rayLength, blockMode, fluidMode);
if (rayHitBlock.getType() != HitResult.Type.MISS) {
return b.apply(rayHitBlock);
} else {
return m.apply(rayHitBlock);
}
}
public static int rayHitEntityOrBlock(Entity entity, double rayLength, Function<EntityHitResult, Integer> e, Function<BlockHitResult, Integer> b) {
return rayHitEntityOrBlockOrMiss(entity, rayLength, 0.0F, ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, e, b, m -> 0);
}
public static int rayHitLivingEntityOrBlock(Entity entity, double rayLength, Function<EntityHitResult, Integer> e, Function<BlockHitResult, Integer> b) {
return rayHitEntityOrBlock(
entity, rayLength,
(r) -> {
if (r.getEntity() instanceof LivingEntity) {
return e.apply(r);
} else {
return 0;
}
}, b
);
}
// #endregion RayTrace
/*================================================================================================================*/
// #region Predicates
public static final Predicate<Entity> IS_LIVING = (entity) -> entity instanceof LivingEntity;
public static final Predicate<Entity> IS_PLAYER = (entity) -> entity instanceof net.minecraft.world.entity.player.Player;
public static final Predicate<Entity> IS_LOCAL_PLAYER = (entity) -> entity instanceof net.minecraft.client.player.LocalPlayer;
public static final Predicate<Entity> IS_SERVER_PLAYER = (entity) -> entity instanceof net.minecraft.server.level.ServerPlayer;
public static final Predicate<Entity> IS_FAKE_PLAYER = (entity) -> entity instanceof net.minecraftforge.common.util.FakePlayer;
public static final Predicate<Entity> IS_PROJECTILE = (entity) -> entity instanceof net.minecraft.world.entity.projectile.Projectile;
public static MobCategory getCategory(Entity entity) {
return entity.getType().getCategory();
}
public static final Predicate<Entity> IS_FRIENDLY = (entity) -> getCategory(entity).isFriendly();
public static final Predicate<Entity> IS_PERSISTENT = (entity) -> getCategory(entity).isPersistent();
public static final Predicate<Entity> IS_MISC = (entity) -> getCategory(entity) == MobCategory.MISC;
public static final Predicate<Entity> IS_MONSTER = (entity) -> getCategory(entity) == MobCategory.MONSTER;
public static final Predicate<Entity> IS_CREATURE = (entity) -> getCategory(entity) == MobCategory.CREATURE;
public static final Predicate<Entity> IS_AMBIENT = (entity) -> getCategory(entity) == MobCategory.AMBIENT;
public static final Predicate<Entity> IS_AXOLOTLS = (entity) -> getCategory(entity) == MobCategory.AXOLOTLS;
public static final Predicate<Entity> IS_UNDERGROUND_WATER_CREATURE = (entity) -> getCategory(entity) == MobCategory.UNDERGROUND_WATER_CREATURE;
public static final Predicate<Entity> IS_WATER_CREATURE = (entity) -> getCategory(entity) == MobCategory.WATER_CREATURE;
public static final Predicate<Entity> IS_WATER_AMBIENT = (entity) -> getCategory(entity) == MobCategory.WATER_AMBIENT;
public static final Predicate<LivingEntity> IS_UNDEFINED = (living) -> living.getMobType() == MobType.UNDEFINED;
public static final Predicate<LivingEntity> IS_DEFINED = (living) -> living.getMobType() != MobType.UNDEFINED;
public static final Predicate<LivingEntity> IS_UNDEAD = (living) -> living.getMobType() == MobType.UNDEAD;
public static final Predicate<LivingEntity> IS_ARTHROPOD = (living) -> living.getMobType() == MobType.ARTHROPOD;
public static final Predicate<LivingEntity> IS_ILLAGER = (living) -> living.getMobType() == MobType.ILLAGER;
public static final Predicate<LivingEntity> IS_WATER = (living) -> living.getMobType() == MobType.WATER;
// #endregion Predicates
}