| 1 | package net.minecraft.world.entity.animal.frog;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.core.component.DataComponents;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.nbt.CompoundTag;
|
| 8 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 9 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 10 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.sounds.SoundEvent;
|
| 13 | import net.minecraft.sounds.SoundEvents;
|
| 14 | import net.minecraft.tags.ItemTags;
|
| 15 | import net.minecraft.util.profiling.Profiler;
|
| 16 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 17 | import net.minecraft.world.InteractionHand;
|
| 18 | import net.minecraft.world.InteractionResult;
|
| 19 | import net.minecraft.world.damagesource.DamageSource;
|
| 20 | import net.minecraft.world.entity.AgeableMob;
|
| 21 | import net.minecraft.world.entity.Bucketable;
|
| 22 | import net.minecraft.world.entity.ConversionParams;
|
| 23 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 24 | import net.minecraft.world.entity.EntityType;
|
| 25 | import net.minecraft.world.entity.EntityTypes;
|
| 26 | import net.minecraft.world.entity.ai.Brain;
|
| 27 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 28 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 29 | import net.minecraft.world.entity.ai.control.SmoothSwimmingLookControl;
|
| 30 | import net.minecraft.world.entity.ai.control.SmoothSwimmingMoveControl;
|
| 31 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 32 | import net.minecraft.world.entity.ai.navigation.WaterBoundPathNavigation;
|
| 33 | import net.minecraft.world.entity.ai.sensing.SensorType;
|
| 34 | import net.minecraft.world.entity.animal.Animal;
|
| 35 | import net.minecraft.world.entity.animal.fish.AbstractFish;
|
| 36 | import net.minecraft.world.entity.player.Player;
|
| 37 | import net.minecraft.world.item.ItemStack;
|
| 38 | import net.minecraft.world.item.Items;
|
| 39 | import net.minecraft.world.item.component.CustomData;
|
| 40 | import net.minecraft.world.level.Level;
|
| 41 | import net.minecraft.world.level.storage.ValueInput;
|
| 42 | import net.minecraft.world.level.storage.ValueOutput;
|
| 43 | import org.jspecify.annotations.Nullable;
|
| 44 |
|
| 45 | public class Tadpole extends AbstractFish {
|
| 46 | private static final int DEFAULT_AGE = 0;
|
| 47 | private static final EntityDataAccessor<Boolean> AGE_LOCKED = SynchedEntityData.defineId(Tadpole.class, EntityDataSerializers.BOOLEAN);
|
| 48 | @VisibleForTesting
|
| 49 | public static int ticksToBeFrog = Math.abs(-24000);
|
| 50 | public static final float HITBOX_WIDTH = 0.4F;
|
| 51 | public static final float HITBOX_HEIGHT = 0.3F;
|
| 52 | private int age = 0;
|
| 53 | protected int ageLockParticleTimer = 0;
|
| 54 | private static final Brain.Provider<Tadpole> BRAIN_PROVIDER = Brain.provider(
|
| 55 | List.of(SensorType.NEAREST_LIVING_ENTITIES, SensorType.NEAREST_PLAYERS, SensorType.HURT_BY, SensorType.FROG_TEMPTATIONS), var0 -> TadpoleAi.getActivities()
|
| 56 | );
|
| 57 |
|
| 58 | public Tadpole(final EntityType<? extends AbstractFish> type, final Level level) {
|
| 59 | super(type, level);
|
| 60 | this.moveControl = new SmoothSwimmingMoveControl<>(this, 85, 10, 0.02F, 0.1F, true);
|
| 61 | this.lookControl = new SmoothSwimmingLookControl(this, 10);
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected PathNavigation createNavigation(final Level level) {
|
| 66 | return new WaterBoundPathNavigation(this, level);
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected Brain<Tadpole> makeBrain(final Brain.Packed packedBrain) {
|
| 71 | return BRAIN_PROVIDER.makeBrain(this, packedBrain);
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public Brain<Tadpole> getBrain() {
|
| 76 | return super.getBrain();
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | protected SoundEvent getFlopSound() {
|
| 81 | return SoundEvents.TADPOLE_FLOP;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | protected void customServerAiStep(final ServerLevel level) {
|
| 86 | ProfilerFiller profiler = Profiler.get();
|
| 87 | profiler.push("tadpoleBrain");
|
| 88 | this.getBrain().tick(level, this);
|
| 89 | profiler.pop();
|
| 90 | profiler.push("tadpoleActivityUpdate");
|
| 91 | TadpoleAi.updateActivity(this);
|
| 92 | profiler.pop();
|
| 93 | super.customServerAiStep(level);
|
| 94 | }
|
| 95 |
|
| 96 | public static AttributeSupplier.Builder createAttributes() {
|
| 97 | return Animal.createAnimalAttributes().add(Attributes.MOVEMENT_SPEED, 1.0).add(Attributes.MAX_HEALTH, 6.0);
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public void aiStep() {
|
| 102 | super.aiStep();
|
| 103 | if (!this.level().isClientSide() && !this.isAgeLocked()) {
|
| 104 | this.setAge(this.age + 1);
|
| 105 | }
|
| 106 |
|
| 107 | this.ageLockParticleTimer = AgeableMob.makeAgeLockedParticle(this.level(), this, this.ageLockParticleTimer, this.isAgeLocked());
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 112 | super.addAdditionalSaveData(output);
|
| 113 | output.putInt("Age", this.age);
|
| 114 | output.putBoolean("AgeLocked", this.isAgeLocked());
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 119 | super.readAdditionalSaveData(input);
|
| 120 | this.setAge(input.getIntOr("Age", 0));
|
| 121 | this.setAgeLocked(input.getBooleanOr("AgeLocked", false));
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 126 | super.defineSynchedData(entityData);
|
| 127 | entityData.define(AGE_LOCKED, false);
|
| 128 | }
|
| 129 |
|
| 130 | protected void setAgeLocked(final boolean locked) {
|
| 131 | this.entityData.set(AGE_LOCKED, locked);
|
| 132 | }
|
| 133 |
|
| 134 | public boolean isAgeLocked() {
|
| 135 | return this.entityData.get(AGE_LOCKED);
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | protected @Nullable SoundEvent getAmbientSound() {
|
| 140 | return null;
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | protected @Nullable SoundEvent getHurtSound(final DamageSource source) {
|
| 145 | return SoundEvents.TADPOLE_HURT;
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | protected @Nullable SoundEvent getDeathSound() {
|
| 150 | return SoundEvents.TADPOLE_DEATH;
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 155 | ItemStack itemStack = player.getItemInHand(hand);
|
| 156 | if (this.isFood(itemStack) && !this.isAgeLocked()) {
|
| 157 | this.feed(player, itemStack);
|
| 158 | return InteractionResult.SUCCESS;
|
| 159 | } else if (AgeableMob.canUseGoldenDandelion(itemStack, true, this.ageLockParticleTimer, this)) {
|
| 160 | AgeableMob.setAgeLocked(this, this::isAgeLocked, player, itemStack, mob -> this.setAgeLockedData());
|
| 161 | return InteractionResult.SUCCESS;
|
| 162 | } else {
|
| 163 | return Bucketable.bucketMobPickup(player, hand, this).orElse(super.mobInteract(player, hand));
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | private void setAgeLockedData() {
|
| 168 | this.setAgeLocked(!this.isAgeLocked());
|
| 169 | this.setAge(0);
|
| 170 | this.ageLockParticleTimer = 40;
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | public boolean fromBucket() {
|
| 175 | return true;
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public void setFromBucket(final boolean fromBucket) {
|
| 180 | }
|
| 181 |
|
| 182 | @Override
|
| 183 | public void saveToBucketTag(final ItemStack bucket) {
|
| 184 | Bucketable.saveDefaultDataToBucketTag(this, bucket);
|
| 185 | CustomData.update(DataComponents.BUCKET_ENTITY_DATA, bucket, tag -> {
|
| 186 | tag.putInt("Age", this.getAge());
|
| 187 | tag.putBoolean("AgeLocked", this.isAgeLocked());
|
| 188 | });
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | public void loadFromBucketTag(final CompoundTag tag) {
|
| 193 | Bucketable.loadDefaultDataFromBucketTag(this, tag);
|
| 194 | tag.getInt("Age").ifPresent(this::setAge);
|
| 195 | this.setAgeLocked(tag.getBooleanOr("AgeLocked", false));
|
| 196 | }
|
| 197 |
|
| 198 | @Override
|
| 199 | public ItemStack getBucketItemStack() {
|
| 200 | return new ItemStack(Items.TADPOLE_BUCKET);
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | public SoundEvent getPickupSound() {
|
| 205 | return SoundEvents.BUCKET_FILL_TADPOLE;
|
| 206 | }
|
| 207 |
|
| 208 | private boolean isFood(final ItemStack itemStack) {
|
| 209 | return itemStack.is(ItemTags.FROG_FOOD);
|
| 210 | }
|
| 211 |
|
| 212 | private void feed(final Player player, final ItemStack itemStack) {
|
| 213 | this.usePlayerItem(player, itemStack);
|
| 214 | this.ageUp(AgeableMob.getSpeedUpSecondsWhenFeeding(this.getTicksLeftUntilAdult()));
|
| 215 | this.level().addParticle(ParticleTypes.HAPPY_VILLAGER, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), 0.0, 0.0, 0.0);
|
| 216 | }
|
| 217 |
|
| 218 | private void usePlayerItem(final Player player, final ItemStack itemStack) {
|
| 219 | itemStack.consume(1, player);
|
| 220 | }
|
| 221 |
|
| 222 | private int getAge() {
|
| 223 | return this.age;
|
| 224 | }
|
| 225 |
|
| 226 | private void ageUp(final int ticksToAgeUp) {
|
| 227 | this.setAge(this.age + ticksToAgeUp * 20);
|
| 228 | }
|
| 229 |
|
| 230 | private void setAge(final int newAge) {
|
| 231 | this.age = newAge;
|
| 232 | if (this.age >= ticksToBeFrog) {
|
| 233 | this.ageUp();
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | private void ageUp() {
|
| 238 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 239 | this.convertTo(EntityTypes.FROG, ConversionParams.single(this, false, false), frog -> {
|
| 240 | frog.finalizeSpawn(serverLevel, serverLevel.getCurrentDifficultyAt(frog.blockPosition()), EntitySpawnReason.CONVERSION, null);
|
| 241 | frog.setPersistenceRequired();
|
| 242 | frog.fudgePositionAfterSizeChange(this.getDimensions(this.getPose()));
|
| 243 | this.playSound(SoundEvents.TADPOLE_GROW_UP, 0.15F, 1.0F);
|
| 244 | });
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | private int getTicksLeftUntilAdult() {
|
| 249 | return Math.max(0, ticksToBeFrog - this.age);
|
| 250 | }
|
| 251 |
|
| 252 | @Override
|
| 253 | public boolean shouldDropExperience() {
|
| 254 | return false;
|
| 255 | }
|
| 256 | }
|
| 257 |
|