| 1 | package net.minecraft.world.entity.animal.frog;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Objects;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.OptionalInt;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.core.Registry;
|
| 10 | import net.minecraft.core.component.DataComponentGetter;
|
| 11 | import net.minecraft.core.component.DataComponentType;
|
| 12 | import net.minecraft.core.component.DataComponents;
|
| 13 | import net.minecraft.core.registries.Registries;
|
| 14 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 15 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 16 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 17 | import net.minecraft.resources.ResourceKey;
|
| 18 | import net.minecraft.server.level.ServerLevel;
|
| 19 | import net.minecraft.sounds.SoundEvent;
|
| 20 | import net.minecraft.sounds.SoundEvents;
|
| 21 | import net.minecraft.sounds.SoundSource;
|
| 22 | import net.minecraft.tags.BlockTags;
|
| 23 | import net.minecraft.tags.EntityTypeTags;
|
| 24 | import net.minecraft.tags.ItemTags;
|
| 25 | import net.minecraft.util.Mth;
|
| 26 | import net.minecraft.util.RandomSource;
|
| 27 | import net.minecraft.util.Unit;
|
| 28 | import net.minecraft.util.profiling.Profiler;
|
| 29 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 30 | import net.minecraft.world.DifficultyInstance;
|
| 31 | import net.minecraft.world.damagesource.DamageSource;
|
| 32 | import net.minecraft.world.entity.AgeableMob;
|
| 33 | import net.minecraft.world.entity.AnimationState;
|
| 34 | import net.minecraft.world.entity.Entity;
|
| 35 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 36 | import net.minecraft.world.entity.EntityType;
|
| 37 | import net.minecraft.world.entity.EntityTypes;
|
| 38 | import net.minecraft.world.entity.LivingEntity;
|
| 39 | import net.minecraft.world.entity.Mob;
|
| 40 | import net.minecraft.world.entity.MoverType;
|
| 41 | import net.minecraft.world.entity.Pose;
|
| 42 | import net.minecraft.world.entity.SpawnGroupData;
|
| 43 | import net.minecraft.world.entity.ai.Brain;
|
| 44 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 45 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 46 | import net.minecraft.world.entity.ai.control.LookControl;
|
| 47 | import net.minecraft.world.entity.ai.control.SmoothSwimmingMoveControl;
|
| 48 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 49 | import net.minecraft.world.entity.ai.navigation.AmphibiousPathNavigation;
|
| 50 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 51 | import net.minecraft.world.entity.ai.sensing.SensorType;
|
| 52 | import net.minecraft.world.entity.animal.Animal;
|
| 53 | import net.minecraft.world.entity.monster.cubemob.AbstractCubeMob;
|
| 54 | import net.minecraft.world.entity.variant.SpawnContext;
|
| 55 | import net.minecraft.world.entity.variant.VariantUtils;
|
| 56 | import net.minecraft.world.item.ItemStack;
|
| 57 | import net.minecraft.world.level.Level;
|
| 58 | import net.minecraft.world.level.LevelAccessor;
|
| 59 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 60 | import net.minecraft.world.level.block.state.BlockState;
|
| 61 | import net.minecraft.world.level.pathfinder.AmphibiousNodeEvaluator;
|
| 62 | import net.minecraft.world.level.pathfinder.Node;
|
| 63 | import net.minecraft.world.level.pathfinder.PathFinder;
|
| 64 | import net.minecraft.world.level.pathfinder.PathType;
|
| 65 | import net.minecraft.world.level.pathfinder.PathfindingContext;
|
| 66 | import net.minecraft.world.level.storage.ValueInput;
|
| 67 | import net.minecraft.world.level.storage.ValueOutput;
|
| 68 | import net.minecraft.world.phys.Vec3;
|
| 69 | import org.jspecify.annotations.Nullable;
|
| 70 |
|
| 71 | public class Frog extends Animal {
|
| 72 | private static final Brain.Provider<Frog> BRAIN_PROVIDER = Brain.provider(
|
| 73 | List.of(SensorType.NEAREST_LIVING_ENTITIES, SensorType.HURT_BY, SensorType.FROG_ATTACKABLES, SensorType.FROG_TEMPTATIONS, SensorType.IS_IN_WATER),
|
| 74 | var0 -> FrogAi.getActivities()
|
| 75 | );
|
| 76 | private static final EntityDataAccessor<Holder<FrogVariant>> DATA_VARIANT_ID = SynchedEntityData.defineId(Frog.class, EntityDataSerializers.FROG_VARIANT);
|
| 77 | private static final EntityDataAccessor<OptionalInt> DATA_TONGUE_TARGET_ID = SynchedEntityData.defineId(
|
| 78 | Frog.class, EntityDataSerializers.OPTIONAL_UNSIGNED_INT
|
| 79 | );
|
| 80 | private static final int FROG_FALL_DAMAGE_REDUCTION = 5;
|
| 81 | private static final ResourceKey<FrogVariant> DEFAULT_VARIANT = FrogVariants.TEMPERATE;
|
| 82 | public final AnimationState jumpAnimationState = new AnimationState();
|
| 83 | public final AnimationState croakAnimationState = new AnimationState();
|
| 84 | public final AnimationState tongueAnimationState = new AnimationState();
|
| 85 | public final AnimationState swimIdleAnimationState = new AnimationState();
|
| 86 |
|
| 87 | public Frog(final EntityType<? extends Animal> type, final Level level) {
|
| 88 | super(type, level);
|
| 89 | this.lookControl = new Frog.FrogLookControl(this);
|
| 90 | this.setPathfindingMalus(PathType.WATER, 4.0F);
|
| 91 | this.setPathfindingMalus(PathType.TRAPDOOR, -1.0F);
|
| 92 | this.moveControl = new SmoothSwimmingMoveControl<>(this, 85, 10, 0.02F, 0.1F, true);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | protected Brain<Frog> makeBrain(final Brain.Packed packedBrain) {
|
| 97 | return BRAIN_PROVIDER.makeBrain(this, packedBrain);
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public Brain<Frog> getBrain() {
|
| 102 | return super.getBrain();
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 107 | super.defineSynchedData(entityData);
|
| 108 | Registry<FrogVariant> variants = this.registryAccess().lookupOrThrow(Registries.FROG_VARIANT);
|
| 109 | entityData.define(DATA_VARIANT_ID, VariantUtils.getDefaultOrAny(this.registryAccess(), DEFAULT_VARIANT));
|
| 110 | entityData.define(DATA_TONGUE_TARGET_ID, OptionalInt.empty());
|
| 111 | }
|
| 112 |
|
| 113 | public void eraseTongueTarget() {
|
| 114 | this.entityData.set(DATA_TONGUE_TARGET_ID, OptionalInt.empty());
|
| 115 | }
|
| 116 |
|
| 117 | public Optional<Entity> getTongueTarget() {
|
| 118 | return this.entityData.get(DATA_TONGUE_TARGET_ID).stream().mapToObj(this.level()::getEntity).filter(Objects::nonNull).findFirst();
|
| 119 | }
|
| 120 |
|
| 121 | public void setTongueTarget(final Entity target) {
|
| 122 | this.entityData.set(DATA_TONGUE_TARGET_ID, OptionalInt.of(target.getId()));
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public int getHeadRotSpeed() {
|
| 127 | return 35;
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public int getMaxHeadYRot() {
|
| 132 | return 5;
|
| 133 | }
|
| 134 |
|
| 135 | public Holder<FrogVariant> getVariant() {
|
| 136 | return this.entityData.get(DATA_VARIANT_ID);
|
| 137 | }
|
| 138 |
|
| 139 | private void setVariant(final Holder<FrogVariant> variant) {
|
| 140 | this.entityData.set(DATA_VARIANT_ID, variant);
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public <T> @Nullable T get(final DataComponentType<? extends T> type) {
|
| 145 | return type == DataComponents.FROG_VARIANT ? castComponentValue((DataComponentType<T>)type, this.getVariant()) : super.get(type);
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 150 | this.applyImplicitComponentIfPresent(components, DataComponents.FROG_VARIANT);
|
| 151 | super.applyImplicitComponents(components);
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
|
| 156 | if (type == DataComponents.FROG_VARIANT) {
|
| 157 | this.setVariant(castComponentValue(DataComponents.FROG_VARIANT, value));
|
| 158 | return true;
|
| 159 | } else {
|
| 160 | return super.applyImplicitComponent(type, value);
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 166 | super.addAdditionalSaveData(output);
|
| 167 | VariantUtils.writeVariant(output, this.getVariant());
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 172 | super.readAdditionalSaveData(input);
|
| 173 | VariantUtils.readVariant(input, Registries.FROG_VARIANT).ifPresent(this::setVariant);
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | protected void customServerAiStep(final ServerLevel level) {
|
| 178 | ProfilerFiller profiler = Profiler.get();
|
| 179 | profiler.push("frogBrain");
|
| 180 | this.getBrain().tick(level, this);
|
| 181 | profiler.pop();
|
| 182 | profiler.push("frogActivityUpdate");
|
| 183 | FrogAi.updateActivity(this);
|
| 184 | profiler.pop();
|
| 185 | super.customServerAiStep(level);
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | public void tick() {
|
| 190 | if (this.level().isClientSide()) {
|
| 191 | this.swimIdleAnimationState.animateWhen(this.isInWater() && !this.walkAnimation.isMoving(), this.tickCount);
|
| 192 | }
|
| 193 |
|
| 194 | super.tick();
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 199 | if (DATA_POSE.equals(accessor)) {
|
| 200 | Pose pose = this.getPose();
|
| 201 | if (pose == Pose.LONG_JUMPING) {
|
| 202 | this.jumpAnimationState.start(this.tickCount);
|
| 203 | } else {
|
| 204 | this.jumpAnimationState.stop();
|
| 205 | }
|
| 206 |
|
| 207 | if (pose == Pose.CROAKING) {
|
| 208 | this.croakAnimationState.start(this.tickCount);
|
| 209 | } else {
|
| 210 | this.croakAnimationState.stop();
|
| 211 | }
|
| 212 |
|
| 213 | if (pose == Pose.USING_TONGUE) {
|
| 214 | this.tongueAnimationState.start(this.tickCount);
|
| 215 | } else {
|
| 216 | this.tongueAnimationState.stop();
|
| 217 | }
|
| 218 | }
|
| 219 |
|
| 220 | super.onSyncedDataUpdated(accessor);
|
| 221 | }
|
| 222 |
|
| 223 | @Override
|
| 224 | protected void updateWalkAnimation(final float distance) {
|
| 225 | float targetSpeed;
|
| 226 | if (this.jumpAnimationState.isStarted()) {
|
| 227 | targetSpeed = 0.0F;
|
| 228 | } else {
|
| 229 | targetSpeed = Math.min(distance * 25.0F, 1.0F);
|
| 230 | }
|
| 231 |
|
| 232 | this.walkAnimation.update(targetSpeed, 0.4F, this.isBaby() ? 3.0F : 1.0F);
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | public void playEatingSound() {
|
| 237 | this.level().playSound(null, this, SoundEvents.FROG_EAT, SoundSource.NEUTRAL, 2.0F, 1.0F);
|
| 238 | }
|
| 239 |
|
| 240 | @Override
|
| 241 | public @Nullable AgeableMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 242 | Frog frog = EntityTypes.FROG.create(level, EntitySpawnReason.BREEDING);
|
| 243 | if (frog != null) {
|
| 244 | FrogAi.initMemories(frog, level.getRandom());
|
| 245 | }
|
| 246 |
|
| 247 | return frog;
|
| 248 | }
|
| 249 |
|
| 250 | @Override
|
| 251 | protected boolean canBeABaby() {
|
| 252 | return false;
|
| 253 | }
|
| 254 |
|
| 255 | @Override
|
| 256 | public void spawnChildFromBreeding(final ServerLevel level, final Animal partner) {
|
| 257 | this.finalizeSpawnChildFromBreeding(level, partner, null);
|
| 258 | this.getBrain().setMemory(MemoryModuleType.IS_PREGNANT, Unit.INSTANCE);
|
| 259 | }
|
| 260 |
|
| 261 | @Override
|
| 262 | public SpawnGroupData finalizeSpawn(
|
| 263 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 264 | ) {
|
| 265 | VariantUtils.selectVariantToSpawn(SpawnContext.create(level, this.blockPosition()), Registries.FROG_VARIANT).ifPresent(this::setVariant);
|
| 266 | FrogAi.initMemories(this, level.getRandom());
|
| 267 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 268 | }
|
| 269 |
|
| 270 | public static AttributeSupplier.Builder createAttributes() {
|
| 271 | return Animal.createAnimalAttributes()
|
| 272 | .add(Attributes.MOVEMENT_SPEED, 1.0)
|
| 273 | .add(Attributes.MAX_HEALTH, 10.0)
|
| 274 | .add(Attributes.ATTACK_DAMAGE, 10.0)
|
| 275 | .add(Attributes.STEP_HEIGHT, 1.0);
|
| 276 | }
|
| 277 |
|
| 278 | @Override
|
| 279 | protected @Nullable SoundEvent getAmbientSound() {
|
| 280 | return SoundEvents.FROG_AMBIENT;
|
| 281 | }
|
| 282 |
|
| 283 | @Override
|
| 284 | protected @Nullable SoundEvent getHurtSound(final DamageSource source) {
|
| 285 | return SoundEvents.FROG_HURT;
|
| 286 | }
|
| 287 |
|
| 288 | @Override
|
| 289 | protected @Nullable SoundEvent getDeathSound() {
|
| 290 | return SoundEvents.FROG_DEATH;
|
| 291 | }
|
| 292 |
|
| 293 | @Override
|
| 294 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 295 | this.playSound(SoundEvents.FROG_STEP, 0.15F, 1.0F);
|
| 296 | }
|
| 297 |
|
| 298 | @Override
|
| 299 | public boolean isPushedByFluid() {
|
| 300 | return false;
|
| 301 | }
|
| 302 |
|
| 303 | @Override
|
| 304 | protected int calculateFallDamage(final double fallDistance, final float damageModifier) {
|
| 305 | return super.calculateFallDamage(fallDistance, damageModifier) - 5;
|
| 306 | }
|
| 307 |
|
| 308 | @Override
|
| 309 | protected void travelInWater(final Vec3 input, final double baseGravity, final boolean isFalling, final double oldY) {
|
| 310 | this.moveRelative(this.getSpeed(), input);
|
| 311 | this.move(MoverType.SELF, this.getDeltaMovement());
|
| 312 | this.setDeltaMovement(this.getDeltaMovement().scale(0.9));
|
| 313 | }
|
| 314 |
|
| 315 | public static boolean canEat(final LivingEntity entity) {
|
| 316 | return entity instanceof AbstractCubeMob cubeMob && cubeMob.getSize() != 1 ? false : entity.is(EntityTypeTags.FROG_FOOD);
|
| 317 | }
|
| 318 |
|
| 319 | @Override
|
| 320 | protected PathNavigation createNavigation(final Level level) {
|
| 321 | return new Frog.FrogPathNavigation(this, level);
|
| 322 | }
|
| 323 |
|
| 324 | @Override
|
| 325 | public @Nullable LivingEntity getTarget() {
|
| 326 | return this.getTargetFromBrain();
|
| 327 | }
|
| 328 |
|
| 329 | @Override
|
| 330 | public boolean isFood(final ItemStack itemStack) {
|
| 331 | return itemStack.is(ItemTags.FROG_FOOD);
|
| 332 | }
|
| 333 |
|
| 334 | public static boolean checkFrogSpawnRules(
|
| 335 | final EntityType<? extends Animal> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 336 | ) {
|
| 337 | return level.getBlockState(pos.below()).is(BlockTags.FROGS_SPAWNABLE_ON) && isBrightEnoughToSpawn(level, pos);
|
| 338 | }
|
| 339 |
|
| 340 | private class FrogLookControl extends LookControl {
|
| 341 | public FrogLookControl(final Mob mob) {
|
| 342 | super(mob);
|
| 343 | }
|
| 344 |
|
| 345 | @Override
|
| 346 | protected boolean resetXRotOnTick() {
|
| 347 | return Frog.this.getTongueTarget().isEmpty();
|
| 348 | }
|
| 349 | }
|
| 350 |
|
| 351 | private static class FrogNodeEvaluator extends AmphibiousNodeEvaluator {
|
| 352 | private final BlockPos.MutableBlockPos belowPos = new BlockPos.MutableBlockPos();
|
| 353 |
|
| 354 | public FrogNodeEvaluator(final boolean prefersShallowSwimming) {
|
| 355 | super(prefersShallowSwimming);
|
| 356 | }
|
| 357 |
|
| 358 | @Override
|
| 359 | public Node getStart() {
|
| 360 | return !this.mob.isInWater()
|
| 361 | ? super.getStart()
|
| 362 | : this.getStartNode(
|
| 363 | new BlockPos(Mth.floor(this.mob.getBoundingBox().minX), Mth.floor(this.mob.getBoundingBox().minY), Mth.floor(this.mob.getBoundingBox().minZ))
|
| 364 | );
|
| 365 | }
|
| 366 |
|
| 367 | @Override
|
| 368 | public PathType getPathType(final PathfindingContext context, final int x, final int y, final int z) {
|
| 369 | this.belowPos.set(x, y - 1, z);
|
| 370 | BlockState belowState = context.getBlockState(this.belowPos);
|
| 371 | return belowState.is(BlockTags.FROG_PREFER_JUMP_TO) ? PathType.OPEN : super.getPathType(context, x, y, z);
|
| 372 | }
|
| 373 | }
|
| 374 |
|
| 375 | private static class FrogPathNavigation extends AmphibiousPathNavigation {
|
| 376 | public FrogPathNavigation(final Frog mob, final Level level) {
|
| 377 | super(mob, level);
|
| 378 | }
|
| 379 |
|
| 380 | @Override
|
| 381 | public boolean canCutCorner(final PathType pathType) {
|
| 382 | return pathType != PathType.WATER_BORDER && super.canCutCorner(pathType);
|
| 383 | }
|
| 384 |
|
| 385 | @Override
|
| 386 | protected PathFinder createPathFinder(final int maxVisitedNodes) {
|
| 387 | this.nodeEvaluator = new Frog.FrogNodeEvaluator(true);
|
| 388 | return new PathFinder(this.nodeEvaluator, maxVisitedNodes);
|
| 389 | }
|
| 390 | }
|
| 391 | }
|
| 392 |
|