| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.function.Function;
|
| 7 | import java.util.function.ToDoubleFunction;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.sounds.SoundEvent;
|
| 11 | import net.minecraft.sounds.SoundSource;
|
| 12 | import net.minecraft.tags.BlockTags;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import net.minecraft.util.valueproviders.UniformInt;
|
| 15 | import net.minecraft.world.damagesource.DamageSource;
|
| 16 | import net.minecraft.world.effect.MobEffects;
|
| 17 | import net.minecraft.world.entity.LivingEntity;
|
| 18 | import net.minecraft.world.entity.ai.Brain;
|
| 19 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 20 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 21 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 22 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 23 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 24 | import net.minecraft.world.entity.animal.goat.Goat;
|
| 25 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 26 | import net.minecraft.world.phys.Vec3;
|
| 27 |
|
| 28 | public class RamTarget extends Behavior<Goat> {
|
| 29 | public static final int TIME_OUT_DURATION = 200;
|
| 30 | public static final float RAM_SPEED_FORCE_FACTOR = 1.65F;
|
| 31 | private final Function<Goat, UniformInt> getTimeBetweenRams;
|
| 32 | private final TargetingConditions ramTargeting;
|
| 33 | private final float speed;
|
| 34 | private final ToDoubleFunction<Goat> getKnockbackForce;
|
| 35 | private Vec3 ramDirection;
|
| 36 | private final Function<Goat, SoundEvent> getImpactSound;
|
| 37 | private final Function<Goat, SoundEvent> getHornBreakSound;
|
| 38 |
|
| 39 | public RamTarget(
|
| 40 | final Function<Goat, UniformInt> getTimeBetweenRams,
|
| 41 | final TargetingConditions ramTargeting,
|
| 42 | final float speed,
|
| 43 | final ToDoubleFunction<Goat> getKnockbackForce,
|
| 44 | final Function<Goat, SoundEvent> getImpactSound,
|
| 45 | final Function<Goat, SoundEvent> getHornBreakSound
|
| 46 | ) {
|
| 47 | super(ImmutableMap.of(MemoryModuleType.RAM_COOLDOWN_TICKS, MemoryStatus.VALUE_ABSENT, MemoryModuleType.RAM_TARGET, MemoryStatus.VALUE_PRESENT), 200);
|
| 48 | this.getTimeBetweenRams = getTimeBetweenRams;
|
| 49 | this.ramTargeting = ramTargeting;
|
| 50 | this.speed = speed;
|
| 51 | this.getKnockbackForce = getKnockbackForce;
|
| 52 | this.getImpactSound = getImpactSound;
|
| 53 | this.getHornBreakSound = getHornBreakSound;
|
| 54 | this.ramDirection = Vec3.ZERO;
|
| 55 | }
|
| 56 |
|
| 57 | protected boolean checkExtraStartConditions(final ServerLevel level, final Goat body) {
|
| 58 | return body.getBrain().hasMemoryValue(MemoryModuleType.RAM_TARGET);
|
| 59 | }
|
| 60 |
|
| 61 | protected boolean canStillUse(final ServerLevel level, final Goat body, final long timestamp) {
|
| 62 | return body.getBrain().hasMemoryValue(MemoryModuleType.RAM_TARGET);
|
| 63 | }
|
| 64 |
|
| 65 | protected void start(final ServerLevel level, final Goat body, final long timestamp) {
|
| 66 | BlockPos curPos = body.blockPosition();
|
| 67 | Brain<?> brain = body.getBrain();
|
| 68 | Vec3 ramTargetPos = brain.getMemory(MemoryModuleType.RAM_TARGET).get();
|
| 69 | this.ramDirection = new Vec3(curPos.getX() - ramTargetPos.x(), 0.0, curPos.getZ() - ramTargetPos.z()).normalize();
|
| 70 | brain.setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(ramTargetPos, this.speed, 0));
|
| 71 | }
|
| 72 |
|
| 73 | protected void tick(final ServerLevel level, final Goat body, final long timestamp) {
|
| 74 | List<LivingEntity> nearbyEntities = level.getNearbyEntities(LivingEntity.class, this.ramTargeting, body, body.getBoundingBox());
|
| 75 | Brain<?> brain = body.getBrain();
|
| 76 | if (!nearbyEntities.isEmpty()) {
|
| 77 | LivingEntity ramTarget = nearbyEntities.get(0);
|
| 78 | DamageSource damageSource = level.damageSources().noAggroMobAttack(body);
|
| 79 | float damage = (float)body.getAttributeValue(Attributes.ATTACK_DAMAGE);
|
| 80 | if (ramTarget.hurtServer(level, damageSource, damage)) {
|
| 81 | EnchantmentHelper.doPostAttackEffects(level, ramTarget, damageSource);
|
| 82 | }
|
| 83 |
|
| 84 | int movementSpeedLevel = body.hasEffect(MobEffects.SPEED) ? body.getEffect(MobEffects.SPEED).getAmplifier() + 1 : 0;
|
| 85 | int movementSlowdownLevel = body.hasEffect(MobEffects.SLOWNESS) ? body.getEffect(MobEffects.SLOWNESS).getAmplifier() + 1 : 0;
|
| 86 | float speedBoostPower = 0.25F * (movementSpeedLevel - movementSlowdownLevel);
|
| 87 | float speedFactor = Mth.clamp(body.getSpeed() * 1.65F, 0.2F, 3.0F) + speedBoostPower;
|
| 88 | DamageSource source = level.damageSources().mobAttack(body);
|
| 89 | float blockedDamage = ramTarget.applyItemBlocking(level, source, damage);
|
| 90 | float blockingFactor = blockedDamage > 0.0F ? 0.5F : 1.0F;
|
| 91 | ramTarget.knockback(blockingFactor * speedFactor * this.getKnockbackForce.applyAsDouble(body), this.ramDirection.x(), this.ramDirection.z(), source, damage);
|
| 92 | this.finishRam(level, body);
|
| 93 | level.playSound(null, body, this.getImpactSound.apply(body), SoundSource.NEUTRAL, 1.0F, 1.0F);
|
| 94 | } else if (this.hasRammedHornBreakingBlock(level, body)) {
|
| 95 | level.playSound(null, body, this.getImpactSound.apply(body), SoundSource.NEUTRAL, 1.0F, 1.0F);
|
| 96 | boolean dropped = body.dropHorn();
|
| 97 | if (dropped) {
|
| 98 | level.playSound(null, body, this.getHornBreakSound.apply(body), SoundSource.NEUTRAL, 1.0F, 1.0F);
|
| 99 | }
|
| 100 |
|
| 101 | this.finishRam(level, body);
|
| 102 | } else {
|
| 103 | Optional<WalkTarget> walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET);
|
| 104 | Optional<Vec3> ramTarget = brain.getMemory(MemoryModuleType.RAM_TARGET);
|
| 105 | boolean lostOrReachedTarget = walkTarget.isEmpty()
|
| 106 | || ramTarget.isEmpty()
|
| 107 | || walkTarget.get().getTarget().currentPosition().closerThan(ramTarget.get(), 0.25);
|
| 108 | if (lostOrReachedTarget) {
|
| 109 | this.finishRam(level, body);
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | private boolean hasRammedHornBreakingBlock(final ServerLevel level, final Goat body) {
|
| 115 | Vec3 horizontalMovementNormalized = body.getDeltaMovement().multiply(1.0, 0.0, 1.0).normalize();
|
| 116 | BlockPos facingBlockPosition = BlockPos.containing(body.position().add(horizontalMovementNormalized));
|
| 117 | return level.getBlockState(facingBlockPosition).is(BlockTags.SNAPS_GOAT_HORN)
|
| 118 | || level.getBlockState(facingBlockPosition.above()).is(BlockTags.SNAPS_GOAT_HORN);
|
| 119 | }
|
| 120 |
|
| 121 | protected void finishRam(final ServerLevel level, final Goat body) {
|
| 122 | level.broadcastEntityEvent(body, (byte)59);
|
| 123 | body.getBrain().setMemory(MemoryModuleType.RAM_COOLDOWN_TICKS, this.getTimeBetweenRams.apply(body).sample(level.getRandom()));
|
| 124 | body.getBrain().eraseMemory(MemoryModuleType.RAM_TARGET);
|
| 125 | }
|
| 126 | }
|
| 127 |
|