ProjectileUtil .java

net.minecraft.world.entity.projectile.ProjectileUtil (class, 284 lines): members: DEFAULT_ENTITY_HIT_RESULT_MARGIN, getHitResultOnMoveVector, entityMargin.

net.minecraft.world.entity.projectileclass284 linescommon10.6 KB

API Surface (10)

HitResult getHitResultOnMoveVector(final Entity source, final Predicate<Entity> matching)
Either<BlockHitResult, Collection<EntityHitResult>> getHitEntitiesAlong(final Entity attacker, final AttackRange attackRange, final Predicate<Entity> matching, final ClipContext.Block blockClipType)
HitResult getHitResultOnMoveVector(final Entity source, final Predicate<Entity> matching, final ClipContext.Block clipType)
HitResult getHitResultOnViewVector(final Entity source, final Predicate<Entity> matching, final double distance)
float computeMargin(final Entity source)
Collection<EntityHitResult> getManyEntityHitResult(final Level level, final Entity source, final Vec3 from, final Vec3 to, final AABB targetSearchArea, final Predicate<Entity> matching, final boolean includeFromEntity, final boolean projectSurfaceHitLocation)
Collection<EntityHitResult> getManyEntityHitResult(final Level level, final Entity source, final Vec3 from, final Vec3 to, final AABB targetSearchArea, final Predicate<Entity> matching, final float entityMargin, final ClipContext.Block clipType, final boolean includeFromEntity, final boolean projectSurfaceHitLocation)
void rotateTowardsMovement(final Entity projectile, final float rotationSpeed)
InteractionHand getWeaponHoldingHand(final LivingEntity mob, final Item weaponItem)
AbstractArrow getMobArrow(final LivingEntity mob, final ItemStack projectile, final float power, final @Nullable ItemStack firedFromWeapon)

Fields & Constants (12)

DEFAULT_ENTITY_HIT_RESULT_MARGIN
matching
from
source
delta
level
entityMargin
origin
to
targetSearchArea
includeFromEntity
clipType

Full Source

ProjectileUtil.java284 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.projectile;
2
3
import com.mojang.datafixers.util.Either;
4
import java.util.ArrayList;
5
import java.util.Collection;
6
import java.util.List;
7
import java.util.Optional;
8
import java.util.function.Predicate;
9
import net.minecraft.util.Mth;
10
import net.minecraft.world.InteractionHand;
11
import net.minecraft.world.entity.Entity;
12
import net.minecraft.world.entity.LivingEntity;
13
import net.minecraft.world.entity.projectile.arrow.AbstractArrow;
14
import net.minecraft.world.item.ArrowItem;
15
import net.minecraft.world.item.Item;
16
import net.minecraft.world.item.ItemStack;
17
import net.minecraft.world.item.Items;
18
import net.minecraft.world.item.component.AttackRange;
19
import net.minecraft.world.level.ClipContext;
20
import net.minecraft.world.level.Level;
21
import net.minecraft.world.phys.AABB;
22
import net.minecraft.world.phys.BlockHitResult;
23
import net.minecraft.world.phys.EntityHitResult;
24
import net.minecraft.world.phys.HitResult;
25
import net.minecraft.world.phys.Vec3;
26
import org.jspecify.annotations.Nullable;
27
28
public final class ProjectileUtil {
29
	public static final float DEFAULT_ENTITY_HIT_RESULT_MARGIN = 0.3F;
30
31
	public static HitResult getHitResultOnMoveVector(final Entity source, final Predicate<Entity> matching) {
32
		Vec3 movement = source.getDeltaMovement();
33
		Level level = source.level();
34
		Vec3 from = source.position();
35
		return getHitResult(from, source, matching, movement, level, computeMargin(source), ClipContext.Block.COLLIDER);
36
	}
37
38
	public static Either<BlockHitResult, Collection<EntityHitResult>> getHitEntitiesAlong(
39
		final Entity attacker, final AttackRange attackRange, final Predicate<Entity> matching, final ClipContext.Block blockClipType
40
	) {
41
		Vec3 look = attacker.getHeadLookAngle();
42
		Vec3 eyePosition = attacker.getEyePosition();
43
		Vec3 from = eyePosition.add(look.scale(attackRange.effectiveMinRange(attacker)));
44
		double movementComponent = attacker.getKnownMovement().dot(look);
45
		Vec3 to = eyePosition.add(look.scale(attackRange.effectiveMaxRange(attacker) + Math.max(0.0, movementComponent)));
46
		return getHitEntitiesAlong(attacker, eyePosition, from, matching, to, attackRange.hitboxMargin(), blockClipType);
47
	}
48
49
	public static HitResult getHitResultOnMoveVector(final Entity source, final Predicate<Entity> matching, final ClipContext.Block clipType) {
50
		Vec3 movement = source.getDeltaMovement();
51
		Level level = source.level();
52
		Vec3 from = source.position();
53
		return getHitResult(from, source, matching, movement, level, computeMargin(source), clipType);
54
	}
55
56
	public static HitResult getHitResultOnViewVector(final Entity source, final Predicate<Entity> matching, final double distance) {
57
		Vec3 viewVector = source.getViewVector(0.0F).scale(distance);
58
		Level level = source.level();
59
		Vec3 from = source.getEyePosition();
60
		return getHitResult(from, source, matching, viewVector, level, 0.0F, ClipContext.Block.COLLIDER);
61
	}
62
63
	private static HitResult getHitResult(
64
		final Vec3 from,
65
		final Entity source,
66
		final Predicate<Entity> matching,
67
		final Vec3 delta,
68
		final Level level,
69
		final float entityMargin,
70
		final ClipContext.Block clipType
71
	) {
72
		Vec3 to = from.add(delta);
73
		HitResult hitResult = level.clipIncludingBorder(new ClipContext(from, to, clipType, ClipContext.Fluid.NONE, source));
74
		if (hitResult.getType() != HitResult.Type.MISS) {
75
			to = hitResult.getLocation();
76
		}
77
78
		HitResult entityHit = getEntityHitResult(level, source, from, to, source.getBoundingBox().expandTowards(delta).inflate(1.0), matching, entityMargin);
79
		if (entityHit != null) {
80
			hitResult = entityHit;
81
		}
82
83
		return hitResult;
84
	}
85
86
	private static Either<BlockHitResult, Collection<EntityHitResult>> getHitEntitiesAlong(
87
		final Entity source,
88
		final Vec3 origin,
89
		final Vec3 from,
90
		final Predicate<Entity> matching,
91
		Vec3 to,
92
		final float entityMargin,
93
		final ClipContext.Block clipType
94
	) {
95
		Level level = source.level();
96
		BlockHitResult hitResult = level.clipIncludingBorder(new ClipContext(origin, to, clipType, ClipContext.Fluid.NONE, source));
97
		if (hitResult.getType() != HitResult.Type.MISS) {
98
			to = hitResult.getLocation();
99
			if (origin.distanceToSqr(to) < origin.distanceToSqr(from)) {
100
				return Either.left(hitResult);
101
			}
102
		}
103
104
		AABB searchArea = AABB.ofSize(from, entityMargin, entityMargin, entityMargin).expandTowards(to.subtract(from)).inflate(1.0);
105
		Collection<EntityHitResult> entityHit = getManyEntityHitResult(level, source, from, to, searchArea, matching, entityMargin, clipType, true, true);
106
		return !entityHit.isEmpty() ? Either.right(entityHit) : Either.left(hitResult);
107
	}
108
109
	public static @Nullable EntityHitResult getEntityHitResult(
110
		final Entity except, final Vec3 from, final Vec3 to, final AABB box, final Predicate<Entity> matching, final double maxValue
111
	) {
112
		Level level = except.level();
113
		double nearest = maxValue;
114
		Entity hovered = null;
115
		Vec3 hoveredPos = null;
116
117
		for (Entity entity : level.getEntities(except, box, matching)) {
118
			AABB bb = entity.getBoundingBox().inflate(entity.getPickRadius());
119
			Optional<Vec3> clipPoint = bb.clip(from, to);
120
			if (bb.contains(from)) {
121
				if (nearest >= 0.0 && entity.canBePickedFromInside()) {
122
					hovered = entity;
123
					hoveredPos = clipPoint.orElse(from);
124
					nearest = 0.0;
125
				}
126
			} else if (clipPoint.isPresent()) {
127
				Vec3 location = clipPoint.get();
128
				double dd = from.distanceToSqr(location);
129
				if (dd < nearest || nearest == 0.0) {
130
					if (entity.getRootVehicle() == except.getRootVehicle()) {
131
						if (nearest == 0.0) {
132
							hovered = entity;
133
							hoveredPos = location;
134
						}
135
					} else {
136
						hovered = entity;
137
						hoveredPos = location;
138
						nearest = dd;
139
					}
140
				}
141
			}
142
		}
143
144
		return hovered == null ? null : new EntityHitResult(hovered, hoveredPos);
145
	}
146
147
	public static @Nullable EntityHitResult getEntityHitResult(
148
		final Level level, final Projectile source, final Vec3 from, final Vec3 to, final AABB targetSearchArea, final Predicate<Entity> matching
149
	) {
150
		return getEntityHitResult(level, source, from, to, targetSearchArea, matching, computeMargin(source));
151
	}
152
153
	public static float computeMargin(final Entity source) {
154
		return Math.max(0.0F, Math.min(0.3F, (source.tickCount - 2) / 20.0F));
155
	}
156
157
	public static @Nullable EntityHitResult getEntityHitResult(
158
		final Level level,
159
		final Entity source,
160
		final Vec3 from,
161
		final Vec3 to,
162
		final AABB targetSearchArea,
163
		final Predicate<Entity> matching,
164
		final float entityMargin
165
	) {
166
		double nearest = Double.MAX_VALUE;
167
		Optional<Vec3> nearestLocation = Optional.empty();
168
		Entity hitEntity = null;
169
170
		for (Entity entity : level.getEntities(source, targetSearchArea, matching)) {
171
			AABB bb = entity.getBoundingBox().inflate(entityMargin);
172
			Optional<Vec3> location = bb.clip(from, to);
173
			if (location.isPresent()) {
174
				double dd = from.distanceToSqr(location.get());
175
				if (dd < nearest) {
176
					hitEntity = entity;
177
					nearest = dd;
178
					nearestLocation = location;
179
				}
180
			}
181
		}
182
183
		return hitEntity == null ? null : new EntityHitResult(hitEntity, nearestLocation.get());
184
	}
185
186
	public static Collection<EntityHitResult> getManyEntityHitResult(
187
		final Level level,
188
		final Entity source,
189
		final Vec3 from,
190
		final Vec3 to,
191
		final AABB targetSearchArea,
192
		final Predicate<Entity> matching,
193
		final boolean includeFromEntity,
194
		final boolean projectSurfaceHitLocation
195
	) {
196
		return getManyEntityHitResult(
197
			level, source, from, to, targetSearchArea, matching, computeMargin(source), ClipContext.Block.COLLIDER, includeFromEntity, projectSurfaceHitLocation
198
		);
199
	}
200
201
	public static Collection<EntityHitResult> getManyEntityHitResult(
202
		final Level level,
203
		final Entity source,
204
		final Vec3 from,
205
		final Vec3 to,
206
		final AABB targetSearchArea,
207
		final Predicate<Entity> matching,
208
		final float entityMargin,
209
		final ClipContext.Block clipType,
210
		final boolean includeFromEntity,
211
		final boolean projectSurfaceHitLocation
212
	) {
213
		List<EntityHitResult> collector = new ArrayList<>();
214
215
		for (Entity entity : level.getEntities(source, targetSearchArea, matching)) {
216
			AABB entityBB = entity.getBoundingBox();
217
			if (includeFromEntity && entityBB.contains(from)) {
218
				collector.add(new EntityHitResult(entity, from));
219
			} else {
220
				Optional<Vec3> exactHit = entityBB.clip(from, to);
221
				if (exactHit.isPresent()) {
222
					collector.add(new EntityHitResult(entity, exactHit.get()));
223
				} else if (!(entityMargin <= 0.0)) {
224
					Optional<Vec3> outsideHit = entityBB.inflate(entityMargin).clip(from, to);
225
					if (!outsideHit.isEmpty()) {
226
						Vec3 outsideHitPosition = outsideHit.get();
227
						Vec3 towardsTarget = entityBB.getCenter();
228
						BlockHitResult hitResult = level.clipIncludingBorder(new ClipContext(outsideHitPosition, towardsTarget, clipType, ClipContext.Fluid.NONE, source));
229
						if (hitResult.getType() != HitResult.Type.MISS) {
230
							towardsTarget = hitResult.getLocation();
231
						}
232
233
						Optional<Vec3> surfaceHit = entity.getBoundingBox().clip(outsideHitPosition, towardsTarget);
234
						if (surfaceHit.isPresent()) {
235
							collector.add(new EntityHitResult(entity, projectSurfaceHitLocation ? surfaceHit.get() : outsideHitPosition));
236
						}
237
					}
238
				}
239
			}
240
		}
241
242
		return collector;
243
	}
244
245
	public static void rotateTowardsMovement(final Entity projectile, final float rotationSpeed) {
246
		Vec3 movement = projectile.getDeltaMovement();
247
		if (movement.lengthSqr() != 0.0) {
248
			double sd = movement.horizontalDistance();
249
			projectile.setYRot((float)(Mth.atan2(movement.z, movement.x) * 180.0F / (float)Math.PI) + 90.0F);
250
			projectile.setXRot((float)(Mth.atan2(sd, movement.y) * 180.0F / (float)Math.PI) - 90.0F);
251
252
			while (projectile.getXRot() - projectile.xRotO < -180.0F) {
253
				projectile.xRotO -= 360.0F;
254
			}
255
256
			while (projectile.getXRot() - projectile.xRotO >= 180.0F) {
257
				projectile.xRotO += 360.0F;
258
			}
259
260
			while (projectile.getYRot() - projectile.yRotO < -180.0F) {
261
				projectile.yRotO -= 360.0F;
262
			}
263
264
			while (projectile.getYRot() - projectile.yRotO >= 180.0F) {
265
				projectile.yRotO += 360.0F;
266
			}
267
268
			projectile.setXRot(Mth.lerp(rotationSpeed, projectile.xRotO, projectile.getXRot()));
269
			projectile.setYRot(Mth.lerp(rotationSpeed, projectile.yRotO, projectile.getYRot()));
270
		}
271
	}
272
273
	public static InteractionHand getWeaponHoldingHand(final LivingEntity mob, final Item weaponItem) {
274
		return mob.getMainHandItem().is(weaponItem) ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND;
275
	}
276
277
	public static AbstractArrow getMobArrow(final LivingEntity mob, final ItemStack projectile, final float power, final @Nullable ItemStack firedFromWeapon) {
278
		ArrowItem arrowItem = (ArrowItem)(projectile.getItem() instanceof ArrowItem ? projectile.getItem() : Items.ARROW);
279
		AbstractArrow arrow = arrowItem.createArrow(mob.level(), projectile, mob, firedFromWeapon);
280
		arrow.setBaseDamageFromMob(power);
281
		return arrow;
282
	}
283
}
284

Decompiled Minecraft 26.3 source of ProjectileUtil (net/minecraft/world/entity/projectile/ProjectileUtil.java) with Mojang official mappings, stored verbatim on the site.