| 1 | package net.minecraft.world.entity.animal.chicken;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Holder;
|
| 5 | import net.minecraft.core.Registry;
|
| 6 | import net.minecraft.core.component.DataComponentGetter;
|
| 7 | import net.minecraft.core.component.DataComponentType;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.core.registries.Registries;
|
| 10 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 11 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 12 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 13 | import net.minecraft.resources.ResourceKey;
|
| 14 | import net.minecraft.server.level.ServerLevel;
|
| 15 | import net.minecraft.sounds.SoundEvent;
|
| 16 | import net.minecraft.sounds.SoundEvents;
|
| 17 | import net.minecraft.tags.ItemTags;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.world.DifficultyInstance;
|
| 20 | import net.minecraft.world.damagesource.DamageSource;
|
| 21 | import net.minecraft.world.entity.AgeableMob;
|
| 22 | import net.minecraft.world.entity.Entity;
|
| 23 | import net.minecraft.world.entity.EntityAttachment;
|
| 24 | import net.minecraft.world.entity.EntityAttachments;
|
| 25 | import net.minecraft.world.entity.EntityDimensions;
|
| 26 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 27 | import net.minecraft.world.entity.EntityType;
|
| 28 | import net.minecraft.world.entity.EntityTypes;
|
| 29 | import net.minecraft.world.entity.LivingEntity;
|
| 30 | import net.minecraft.world.entity.Pose;
|
| 31 | import net.minecraft.world.entity.SpawnGroupData;
|
| 32 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 33 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 34 | import net.minecraft.world.entity.ai.goal.BreedGoal;
|
| 35 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 36 | import net.minecraft.world.entity.ai.goal.FollowParentGoal;
|
| 37 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 38 | import net.minecraft.world.entity.ai.goal.PanicGoal;
|
| 39 | import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
|
| 40 | import net.minecraft.world.entity.ai.goal.TemptGoal;
|
| 41 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 42 | import net.minecraft.world.entity.animal.Animal;
|
| 43 | import net.minecraft.world.entity.player.Player;
|
| 44 | import net.minecraft.world.entity.variant.SpawnContext;
|
| 45 | import net.minecraft.world.entity.variant.VariantUtils;
|
| 46 | import net.minecraft.world.item.ItemStack;
|
| 47 | import net.minecraft.world.level.Level;
|
| 48 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 49 | import net.minecraft.world.level.block.state.BlockState;
|
| 50 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 51 | import net.minecraft.world.level.pathfinder.PathType;
|
| 52 | import net.minecraft.world.level.storage.ValueInput;
|
| 53 | import net.minecraft.world.level.storage.ValueOutput;
|
| 54 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 55 | import net.minecraft.world.phys.Vec3;
|
| 56 | import org.jspecify.annotations.Nullable;
|
| 57 |
|
| 58 | public class Chicken extends Animal {
|
| 59 | private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.3F, 0.4F)
|
| 60 | .withEyeHeight(0.28125F)
|
| 61 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.375F, 0.0F));
|
| 62 | private static final EntityDataAccessor<Holder<ChickenVariant>> DATA_VARIANT_ID = SynchedEntityData.defineId(
|
| 63 | Chicken.class, EntityDataSerializers.CHICKEN_VARIANT
|
| 64 | );
|
| 65 | private static final EntityDataAccessor<Holder<ChickenSoundVariant>> DATA_SOUND_VARIANT_ID = SynchedEntityData.defineId(
|
| 66 | Chicken.class, EntityDataSerializers.CHICKEN_SOUND_VARIANT
|
| 67 | );
|
| 68 | private static final boolean DEFAULT_CHICKEN_JOCKEY = false;
|
| 69 | public float flap;
|
| 70 | public float flapSpeed;
|
| 71 | public float oFlapSpeed;
|
| 72 | public float oFlap;
|
| 73 | public float flapping = 1.0F;
|
| 74 | private float nextFlap = 1.0F;
|
| 75 | public int eggTime;
|
| 76 | public boolean isChickenJockey = false;
|
| 77 |
|
| 78 | public Chicken(final EntityType<? extends Chicken> type, final Level level) {
|
| 79 | super(type, level);
|
| 80 | this.eggTime = this.random.nextInt(6000) + 6000;
|
| 81 | this.setPathfindingMalus(PathType.WATER, 0.0F);
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | protected void registerGoals() {
|
| 86 | this.goalSelector.addGoal(0, new FloatGoal(this));
|
| 87 | this.goalSelector.addGoal(1, new PanicGoal(this, 1.4));
|
| 88 | this.goalSelector.addGoal(2, new BreedGoal(this, 1.0));
|
| 89 | this.goalSelector.addGoal(3, new TemptGoal(this, 1.0, i -> i.is(ItemTags.CHICKEN_FOOD), false));
|
| 90 | this.goalSelector.addGoal(4, new FollowParentGoal(this, 1.1));
|
| 91 | this.goalSelector.addGoal(5, new WaterAvoidingRandomStrollGoal(this, 1.0));
|
| 92 | this.goalSelector.addGoal(6, new LookAtPlayerGoal(this, Player.class, 6.0F));
|
| 93 | this.goalSelector.addGoal(7, new RandomLookAroundGoal(this));
|
| 94 | }
|
| 95 |
|
| 96 | private Holder<ChickenSoundVariant> getSoundVariant() {
|
| 97 | return this.entityData.get(DATA_SOUND_VARIANT_ID);
|
| 98 | }
|
| 99 |
|
| 100 | private void setSoundVariant(final Holder<ChickenSoundVariant> soundVariant) {
|
| 101 | this.entityData.set(DATA_SOUND_VARIANT_ID, soundVariant);
|
| 102 | }
|
| 103 |
|
| 104 | private ChickenSoundVariant.ChickenSoundSet getSoundSet() {
|
| 105 | return this.isBaby() ? this.getSoundVariant().value().babySounds() : this.getSoundVariant().value().adultSounds();
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 110 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 111 | }
|
| 112 |
|
| 113 | public static AttributeSupplier.Builder createAttributes() {
|
| 114 | return Animal.createAnimalAttributes().add(Attributes.MAX_HEALTH, 4.0).add(Attributes.MOVEMENT_SPEED, 0.25);
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public void aiStep() {
|
| 119 | super.aiStep();
|
| 120 | this.oFlap = this.flap;
|
| 121 | this.oFlapSpeed = this.flapSpeed;
|
| 122 | this.flapSpeed = this.flapSpeed + (this.onGround() ? -1.0F : 4.0F) * 0.3F;
|
| 123 | this.flapSpeed = Mth.clamp(this.flapSpeed, 0.0F, 1.0F);
|
| 124 | if (!this.onGround() && this.flapping < 1.0F) {
|
| 125 | this.flapping = 1.0F;
|
| 126 | }
|
| 127 |
|
| 128 | this.flapping *= 0.9F;
|
| 129 | Vec3 movement = this.getDeltaMovement();
|
| 130 | if (!this.onGround() && movement.y < 0.0) {
|
| 131 | this.setDeltaMovement(movement.multiply(1.0, 0.6, 1.0));
|
| 132 | }
|
| 133 |
|
| 134 | this.flap = this.flap + this.flapping * 2.0F;
|
| 135 | if (this.level() instanceof ServerLevel level && this.isAlive() && !this.isBaby() && !this.isChickenJockey() && --this.eggTime <= 0) {
|
| 136 | if (this.dropFromGiftLootTable(level, BuiltInLootTables.CHICKEN_LAY, this::spawnAtLocation)) {
|
| 137 | this.playSound(SoundEvents.CHICKEN_EGG, 1.0F, (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F);
|
| 138 | this.gameEvent(GameEvent.ENTITY_PLACE);
|
| 139 | }
|
| 140 |
|
| 141 | this.eggTime = this.random.nextInt(6000) + 6000;
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | protected boolean isFlapping() {
|
| 147 | return this.flyDist > this.nextFlap;
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | protected void onFlap() {
|
| 152 | this.nextFlap = this.flyDist + this.flapSpeed / 2.0F;
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | protected SoundEvent getAmbientSound() {
|
| 157 | return this.getSoundSet().ambientSound().value();
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 162 | return this.getSoundSet().hurtSound().value();
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | protected SoundEvent getDeathSound() {
|
| 167 | return this.getSoundSet().deathSound().value();
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 172 | this.playSound(this.getSoundSet().stepSound().value(), 0.15F, 1.0F);
|
| 173 | }
|
| 174 |
|
| 175 | public @Nullable Chicken getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 176 | Chicken baby = EntityTypes.CHICKEN.create(level, EntitySpawnReason.BREEDING);
|
| 177 | if (baby != null && partner instanceof Chicken partnerChicken) {
|
| 178 | baby.setVariant(this.random.nextBoolean() ? this.getVariant() : partnerChicken.getVariant());
|
| 179 | }
|
| 180 |
|
| 181 | return baby;
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | public SpawnGroupData finalizeSpawn(
|
| 186 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 187 | ) {
|
| 188 | VariantUtils.selectVariantToSpawn(SpawnContext.create(level, this.blockPosition()), Registries.CHICKEN_VARIANT).ifPresent(this::setVariant);
|
| 189 | this.setSoundVariant(ChickenSoundVariants.pickRandomSoundVariant(this.registryAccess(), level.getRandom()));
|
| 190 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 191 | }
|
| 192 |
|
| 193 | @Override
|
| 194 | public boolean isFood(final ItemStack itemStack) {
|
| 195 | return itemStack.is(ItemTags.CHICKEN_FOOD);
|
| 196 | }
|
| 197 |
|
| 198 | @Override
|
| 199 | protected int getBaseExperienceReward(final ServerLevel level) {
|
| 200 | return this.isChickenJockey() ? 10 : super.getBaseExperienceReward(level);
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 205 | super.defineSynchedData(entityData);
|
| 206 | Registry<ChickenSoundVariant> chickenSoundVariants = this.registryAccess().lookupOrThrow(Registries.CHICKEN_SOUND_VARIANT);
|
| 207 | entityData.define(DATA_VARIANT_ID, VariantUtils.getDefaultOrAny(this.registryAccess(), ChickenVariants.TEMPERATE));
|
| 208 | entityData.define(DATA_SOUND_VARIANT_ID, chickenSoundVariants.get(ChickenSoundVariants.CLASSIC).or(chickenSoundVariants::getAny).orElseThrow());
|
| 209 | }
|
| 210 |
|
| 211 | @Override
|
| 212 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 213 | super.readAdditionalSaveData(input);
|
| 214 | this.isChickenJockey = input.getBooleanOr("IsChickenJockey", false);
|
| 215 | input.getInt("EggLayTime").ifPresent(time -> this.eggTime = time);
|
| 216 | VariantUtils.readVariant(input, Registries.CHICKEN_VARIANT).ifPresent(this::setVariant);
|
| 217 | input.read("sound_variant", ResourceKey.codec(Registries.CHICKEN_SOUND_VARIANT))
|
| 218 | .flatMap(soundVariant -> this.registryAccess().lookupOrThrow(Registries.CHICKEN_SOUND_VARIANT).get((ResourceKey<ChickenSoundVariant>)soundVariant))
|
| 219 | .ifPresent(this::setSoundVariant);
|
| 220 | }
|
| 221 |
|
| 222 | @Override
|
| 223 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 224 | super.addAdditionalSaveData(output);
|
| 225 | output.putBoolean("IsChickenJockey", this.isChickenJockey);
|
| 226 | output.putInt("EggLayTime", this.eggTime);
|
| 227 | VariantUtils.writeVariant(output, this.getVariant());
|
| 228 | this.getSoundVariant()
|
| 229 | .unwrapKey()
|
| 230 | .ifPresent(
|
| 231 | soundVariant -> output.store("sound_variant", ResourceKey.codec(Registries.CHICKEN_SOUND_VARIANT), (ResourceKey<ChickenSoundVariant>)soundVariant)
|
| 232 | );
|
| 233 | }
|
| 234 |
|
| 235 | public void setVariant(final Holder<ChickenVariant> variant) {
|
| 236 | this.entityData.set(DATA_VARIANT_ID, variant);
|
| 237 | }
|
| 238 |
|
| 239 | public Holder<ChickenVariant> getVariant() {
|
| 240 | return this.entityData.get(DATA_VARIANT_ID);
|
| 241 | }
|
| 242 |
|
| 243 | @Override
|
| 244 | public <T> @Nullable T get(final DataComponentType<? extends T> type) {
|
| 245 | if (type == DataComponents.CHICKEN_VARIANT) {
|
| 246 | return castComponentValue((DataComponentType<T>)type, this.getVariant());
|
| 247 | } else {
|
| 248 | return type == DataComponents.CHICKEN_SOUND_VARIANT ? castComponentValue((DataComponentType<T>)type, this.getSoundVariant()) : super.get(type);
|
| 249 | }
|
| 250 | }
|
| 251 |
|
| 252 | @Override
|
| 253 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 254 | this.applyImplicitComponentIfPresent(components, DataComponents.CHICKEN_VARIANT);
|
| 255 | this.applyImplicitComponentIfPresent(components, DataComponents.CHICKEN_SOUND_VARIANT);
|
| 256 | super.applyImplicitComponents(components);
|
| 257 | }
|
| 258 |
|
| 259 | @Override
|
| 260 | protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
|
| 261 | if (type == DataComponents.CHICKEN_VARIANT) {
|
| 262 | this.setVariant(castComponentValue(DataComponents.CHICKEN_VARIANT, value));
|
| 263 | return true;
|
| 264 | } else if (type == DataComponents.CHICKEN_SOUND_VARIANT) {
|
| 265 | this.setSoundVariant(castComponentValue(DataComponents.CHICKEN_SOUND_VARIANT, value));
|
| 266 | return true;
|
| 267 | } else {
|
| 268 | return super.applyImplicitComponent(type, value);
|
| 269 | }
|
| 270 | }
|
| 271 |
|
| 272 | @Override
|
| 273 | public boolean removeWhenFarAway(final double distSqr) {
|
| 274 | return this.isChickenJockey();
|
| 275 | }
|
| 276 |
|
| 277 | @Override
|
| 278 | protected void positionRider(final Entity passenger, final Entity.MoveFunction moveFunction) {
|
| 279 | super.positionRider(passenger, moveFunction);
|
| 280 | if (passenger instanceof LivingEntity livingEntity) {
|
| 281 | livingEntity.yBodyRot = this.yBodyRot;
|
| 282 | }
|
| 283 | }
|
| 284 |
|
| 285 | public boolean isChickenJockey() {
|
| 286 | return this.isChickenJockey;
|
| 287 | }
|
| 288 |
|
| 289 | public void setChickenJockey(final boolean isChickenJockey) {
|
| 290 | this.isChickenJockey = isChickenJockey;
|
| 291 | }
|
| 292 | }
|
| 293 |
|