| 1 | package net.minecraft.world.item.component;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import io.netty.buffer.ByteBuf;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.function.Predicate;
|
| 9 | import java.util.function.ToDoubleFunction;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.Direction;
|
| 12 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 13 | import net.minecraft.network.codec.StreamCodec;
|
| 14 | import net.minecraft.util.ExtraCodecs;
|
| 15 | import net.minecraft.world.entity.Entity;
|
| 16 | import net.minecraft.world.entity.LivingEntity;
|
| 17 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.entity.projectile.ProjectileUtil;
|
| 20 | import net.minecraft.world.level.ClipContext;
|
| 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 |
|
| 27 | public record AttackRange(float minReach, float maxReach, float minCreativeReach, float maxCreativeReach, float hitboxMargin, float mobFactor) {
|
| 28 | public static final Codec<AttackRange> CODEC = RecordCodecBuilder.create(
|
| 29 | i -> i.group(
|
| 30 | ExtraCodecs.floatRange(0.0F, 64.0F).optionalFieldOf("min_reach", 0.0F).forGetter(AttackRange::minReach),
|
| 31 | ExtraCodecs.floatRange(0.0F, 64.0F).optionalFieldOf("max_reach", 3.0F).forGetter(AttackRange::maxReach),
|
| 32 | ExtraCodecs.floatRange(0.0F, 64.0F).optionalFieldOf("min_creative_reach", 0.0F).forGetter(AttackRange::minCreativeReach),
|
| 33 | ExtraCodecs.floatRange(0.0F, 64.0F).optionalFieldOf("max_creative_reach", 5.0F).forGetter(AttackRange::maxCreativeReach),
|
| 34 | ExtraCodecs.floatRange(0.0F, 1.0F).optionalFieldOf("hitbox_margin", 0.3F).forGetter(AttackRange::hitboxMargin),
|
| 35 | Codec.floatRange(0.0F, 2.0F).optionalFieldOf("mob_factor", 1.0F).forGetter(AttackRange::mobFactor)
|
| 36 | )
|
| 37 | .apply(i, AttackRange::new)
|
| 38 | );
|
| 39 | public static final StreamCodec<ByteBuf, AttackRange> STREAM_CODEC = StreamCodec.composite(
|
| 40 | ByteBufCodecs.FLOAT,
|
| 41 | AttackRange::minReach,
|
| 42 | ByteBufCodecs.FLOAT,
|
| 43 | AttackRange::maxReach,
|
| 44 | ByteBufCodecs.FLOAT,
|
| 45 | AttackRange::minCreativeReach,
|
| 46 | ByteBufCodecs.FLOAT,
|
| 47 | AttackRange::maxCreativeReach,
|
| 48 | ByteBufCodecs.FLOAT,
|
| 49 | AttackRange::hitboxMargin,
|
| 50 | ByteBufCodecs.FLOAT,
|
| 51 | AttackRange::mobFactor,
|
| 52 | AttackRange::new
|
| 53 | );
|
| 54 |
|
| 55 | public static AttackRange defaultFor(final LivingEntity livingEntity) {
|
| 56 | float interactionRange = (float)livingEntity.getAttributeValue(Attributes.ENTITY_INTERACTION_RANGE);
|
| 57 | return new AttackRange(0.0F, interactionRange, 0.0F, interactionRange, 0.0F, 1.0F);
|
| 58 | }
|
| 59 |
|
| 60 | public HitResult getClosesetHit(final Entity attacker, final float partial, final Predicate<Entity> matching) {
|
| 61 | Either<BlockHitResult, Collection<EntityHitResult>> result = ProjectileUtil.getHitEntitiesAlong(attacker, this, matching, ClipContext.Block.OUTLINE);
|
| 62 | if (result.left().isPresent()) {
|
| 63 | return result.left().get();
|
| 64 | }
|
| 65 |
|
| 66 | Collection<EntityHitResult> targets = result.right().get();
|
| 67 | EntityHitResult entity = null;
|
| 68 | Vec3 attackerPos = attacker.getEyePosition(partial);
|
| 69 | double closestDistance = Double.MAX_VALUE;
|
| 70 |
|
| 71 | for (EntityHitResult target : targets) {
|
| 72 | double distance = attackerPos.distanceToSqr(target.getLocation());
|
| 73 | if (distance < closestDistance) {
|
| 74 | closestDistance = distance;
|
| 75 | entity = target;
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | if (entity != null) {
|
| 80 | return entity;
|
| 81 | }
|
| 82 |
|
| 83 | Vec3 eyeGaze = attacker.getHeadLookAngle();
|
| 84 | Vec3 missPosition = attacker.getEyePosition(partial).add(eyeGaze);
|
| 85 | return BlockHitResult.miss(missPosition, Direction.getApproximateNearest(eyeGaze), BlockPos.containing(missPosition));
|
| 86 | }
|
| 87 |
|
| 88 | public float effectiveMinRange(final Entity entity) {
|
| 89 | if (entity instanceof Player player) {
|
| 90 | return player.isCreative() ? this.minCreativeReach : this.minReach;
|
| 91 | } else {
|
| 92 | return this.minReach * this.mobFactor;
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | public float effectiveMaxRange(final Entity entity) {
|
| 97 | if (entity instanceof Player player) {
|
| 98 | return player.isCreative() ? this.maxCreativeReach : this.maxReach;
|
| 99 | } else {
|
| 100 | return this.maxReach * this.mobFactor;
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | public boolean isInRange(final LivingEntity attacker, final Vec3 location) {
|
| 105 | return this.isInRange(attacker, location::distanceToSqr, 0.0);
|
| 106 | }
|
| 107 |
|
| 108 | public boolean isInRange(final LivingEntity attacker, final AABB boundingBox, final double extraBuffer) {
|
| 109 | return this.isInRange(attacker, boundingBox::distanceToSqr, extraBuffer);
|
| 110 | }
|
| 111 |
|
| 112 | private boolean isInRange(final LivingEntity attacker, final ToDoubleFunction<Vec3> distanceFunction, final double extraBuffer) {
|
| 113 | double distance = Math.sqrt(distanceFunction.applyAsDouble(attacker.getEyePosition()));
|
| 114 | double minReach = this.effectiveMinRange(attacker) - this.hitboxMargin - extraBuffer;
|
| 115 | double maxReach = this.effectiveMaxRange(attacker) + this.hitboxMargin + extraBuffer;
|
| 116 | return distance >= minReach && distance <= maxReach;
|
| 117 | }
|
| 118 | }
|
| 119 |
|