| 1 | package net.minecraft.world.entity.animal.sheep;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Holder;
|
| 5 | import net.minecraft.core.component.DataComponentGetter;
|
| 6 | import net.minecraft.core.component.DataComponentType;
|
| 7 | import net.minecraft.core.component.DataComponents;
|
| 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.sounds.SoundSource;
|
| 15 | import net.minecraft.tags.ItemTags;
|
| 16 | import net.minecraft.util.Mth;
|
| 17 | import net.minecraft.world.DifficultyInstance;
|
| 18 | import net.minecraft.world.InteractionHand;
|
| 19 | import net.minecraft.world.InteractionResult;
|
| 20 | import net.minecraft.world.damagesource.DamageSource;
|
| 21 | import net.minecraft.world.entity.AgeableMob;
|
| 22 | import net.minecraft.world.entity.EntityAttachment;
|
| 23 | import net.minecraft.world.entity.EntityAttachments;
|
| 24 | import net.minecraft.world.entity.EntityDimensions;
|
| 25 | import net.minecraft.world.entity.EntityEvent;
|
| 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.Pose;
|
| 30 | import net.minecraft.world.entity.Shearable;
|
| 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.EatBlockGoal;
|
| 36 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 37 | import net.minecraft.world.entity.ai.goal.FollowParentGoal;
|
| 38 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 39 | import net.minecraft.world.entity.ai.goal.PanicGoal;
|
| 40 | import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
|
| 41 | import net.minecraft.world.entity.ai.goal.TemptGoal;
|
| 42 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 43 | import net.minecraft.world.entity.animal.Animal;
|
| 44 | import net.minecraft.world.entity.item.ItemEntity;
|
| 45 | import net.minecraft.world.entity.player.Player;
|
| 46 | import net.minecraft.world.item.DyeColor;
|
| 47 | import net.minecraft.world.item.ItemStack;
|
| 48 | import net.minecraft.world.item.Items;
|
| 49 | import net.minecraft.world.level.Level;
|
| 50 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 51 | import net.minecraft.world.level.biome.Biome;
|
| 52 | import net.minecraft.world.level.block.state.BlockState;
|
| 53 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 54 | import net.minecraft.world.level.storage.ValueInput;
|
| 55 | import net.minecraft.world.level.storage.ValueOutput;
|
| 56 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 57 | import org.jspecify.annotations.Nullable;
|
| 58 |
|
| 59 | public class Sheep extends Animal implements Shearable {
|
| 60 | private static final int EAT_ANIMATION_TICKS = 40;
|
| 61 | private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.45F, 0.65F)
|
| 62 | .withEyeHeight(0.65625F)
|
| 63 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.5625F, 0.0F));
|
| 64 | private static final EntityDataAccessor<Byte> DATA_WOOL_ID = SynchedEntityData.defineId(Sheep.class, EntityDataSerializers.BYTE);
|
| 65 | private static final DyeColor DEFAULT_COLOR = DyeColor.WHITE;
|
| 66 | private static final boolean DEFAULT_SHEARED = false;
|
| 67 | private int eatAnimationTick;
|
| 68 | private EatBlockGoal eatBlockGoal;
|
| 69 |
|
| 70 | public Sheep(final EntityType<? extends Sheep> type, final Level level) {
|
| 71 | super(type, level);
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | protected void registerGoals() {
|
| 76 | this.eatBlockGoal = new EatBlockGoal(this);
|
| 77 | this.goalSelector.addGoal(0, new FloatGoal(this));
|
| 78 | this.goalSelector.addGoal(1, new PanicGoal(this, 1.25));
|
| 79 | this.goalSelector.addGoal(2, new BreedGoal(this, 1.0));
|
| 80 | this.goalSelector.addGoal(3, new TemptGoal(this, 1.1, i -> i.is(ItemTags.SHEEP_FOOD), false));
|
| 81 | this.goalSelector.addGoal(4, new FollowParentGoal(this, 1.1));
|
| 82 | this.goalSelector.addGoal(5, this.eatBlockGoal);
|
| 83 | this.goalSelector.addGoal(6, new WaterAvoidingRandomStrollGoal(this, 1.0));
|
| 84 | this.goalSelector.addGoal(7, new LookAtPlayerGoal(this, Player.class, 6.0F));
|
| 85 | this.goalSelector.addGoal(8, new RandomLookAroundGoal(this));
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public boolean isFood(final ItemStack itemStack) {
|
| 90 | return itemStack.is(ItemTags.SHEEP_FOOD);
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected void customServerAiStep(final ServerLevel level) {
|
| 95 | this.eatAnimationTick = this.eatBlockGoal.getEatAnimationTick();
|
| 96 | super.customServerAiStep(level);
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public void aiStep() {
|
| 101 | if (this.level().isClientSide()) {
|
| 102 | this.eatAnimationTick = Math.max(0, this.eatAnimationTick - 1);
|
| 103 | }
|
| 104 |
|
| 105 | super.aiStep();
|
| 106 | }
|
| 107 |
|
| 108 | public static AttributeSupplier.Builder createAttributes() {
|
| 109 | return Animal.createAnimalAttributes().add(Attributes.MAX_HEALTH, 8.0).add(Attributes.MOVEMENT_SPEED, 0.23F);
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 114 | super.defineSynchedData(entityData);
|
| 115 | entityData.define(DATA_WOOL_ID, (byte)0);
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 120 | if (id == 10) {
|
| 121 | this.eatAnimationTick = 40;
|
| 122 | } else {
|
| 123 | super.handleEntityEvent(id);
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | public float getHeadEatPositionScale(final float a) {
|
| 128 | if (this.eatAnimationTick <= 0) {
|
| 129 | return 0.0F;
|
| 130 | } else if (this.eatAnimationTick >= 4 && this.eatAnimationTick <= 36) {
|
| 131 | return 1.0F;
|
| 132 | } else {
|
| 133 | return this.eatAnimationTick < 4 ? (this.eatAnimationTick - a) / 4.0F : -(this.eatAnimationTick - 40 - a) / 4.0F;
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | public float getHeadEatAngleScale(final float a) {
|
| 138 | if (this.eatAnimationTick > 4 && this.eatAnimationTick <= 36) {
|
| 139 | float scale = (this.eatAnimationTick - 4 - a) / 32.0F;
|
| 140 | return (float) (Math.PI / 5) + 0.21991149F * Mth.sin(scale * 28.7F);
|
| 141 | } else {
|
| 142 | return this.eatAnimationTick > 0 ? (float) (Math.PI / 5) : this.getXRot(a) * (float) (Math.PI / 180.0);
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 148 | ItemStack itemStack = player.getItemInHand(hand);
|
| 149 | if (itemStack.is(Items.SHEARS)) {
|
| 150 | if (this.level() instanceof ServerLevel level && this.readyForShearing()) {
|
| 151 | this.shear(level, SoundSource.PLAYERS, itemStack);
|
| 152 | this.gameEvent(GameEvent.SHEAR, player);
|
| 153 | itemStack.hurtAndBreak(1, player, hand.asEquipmentSlot());
|
| 154 | return InteractionResult.SUCCESS_SERVER;
|
| 155 | } else {
|
| 156 | return InteractionResult.CONSUME;
|
| 157 | }
|
| 158 | } else {
|
| 159 | return super.mobInteract(player, hand);
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public void shear(final ServerLevel level, final SoundSource soundSource, final ItemStack tool) {
|
| 165 | level.playSound(null, this, SoundEvents.SHEEP_SHEAR, soundSource, 1.0F, 1.0F);
|
| 166 | this.dropFromShearingLootTable(
|
| 167 | level,
|
| 168 | BuiltInLootTables.SHEAR_SHEEP,
|
| 169 | tool,
|
| 170 | (l, drop) -> {
|
| 171 | for (int i = 0; i < drop.getCount(); i++) {
|
| 172 | ItemEntity entity = this.spawnAtLocation(l, drop.copyWithCount(1), 1.0F);
|
| 173 | if (entity != null) {
|
| 174 | entity.setDeltaMovement(
|
| 175 | entity.getDeltaMovement()
|
| 176 | .add(
|
| 177 | (this.random.nextFloat() - this.random.nextFloat()) * 0.1F,
|
| 178 | this.random.nextFloat() * 0.05F,
|
| 179 | (this.random.nextFloat() - this.random.nextFloat()) * 0.1F
|
| 180 | )
|
| 181 | );
|
| 182 | }
|
| 183 | }
|
| 184 | }
|
| 185 | );
|
| 186 | this.setSheared(true);
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | public boolean readyForShearing() {
|
| 191 | return !this.isSheared() && !this.isBaby();
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 196 | super.addAdditionalSaveData(output);
|
| 197 | output.putBoolean("Sheared", this.isSheared());
|
| 198 | output.store("Color", DyeColor.LEGACY_ID_CODEC, this.getColor());
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 203 | super.readAdditionalSaveData(input);
|
| 204 | this.setSheared(input.getBooleanOr("Sheared", false));
|
| 205 | this.setColor(input.read("Color", DyeColor.LEGACY_ID_CODEC).orElse(DEFAULT_COLOR));
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | protected SoundEvent getAmbientSound() {
|
| 210 | return SoundEvents.SHEEP_AMBIENT;
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 215 | return SoundEvents.SHEEP_HURT;
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | protected SoundEvent getDeathSound() {
|
| 220 | return SoundEvents.SHEEP_DEATH;
|
| 221 | }
|
| 222 |
|
| 223 | @Override
|
| 224 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 225 | this.playSound(SoundEvents.SHEEP_STEP, 0.15F, 1.0F);
|
| 226 | }
|
| 227 |
|
| 228 | public DyeColor getColor() {
|
| 229 | return DyeColor.byId(this.entityData.get(DATA_WOOL_ID) & 15);
|
| 230 | }
|
| 231 |
|
| 232 | public void setColor(final DyeColor color) {
|
| 233 | byte current = this.entityData.get(DATA_WOOL_ID);
|
| 234 | this.entityData.set(DATA_WOOL_ID, (byte)(current & 240 | color.getId() & 15));
|
| 235 | }
|
| 236 |
|
| 237 | @Override
|
| 238 | public <T> @Nullable T get(final DataComponentType<? extends T> type) {
|
| 239 | return type == DataComponents.SHEEP_COLOR ? castComponentValue((DataComponentType<T>)type, this.getColor()) : super.get(type);
|
| 240 | }
|
| 241 |
|
| 242 | @Override
|
| 243 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 244 | this.applyImplicitComponentIfPresent(components, DataComponents.SHEEP_COLOR);
|
| 245 | super.applyImplicitComponents(components);
|
| 246 | }
|
| 247 |
|
| 248 | @Override
|
| 249 | protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
|
| 250 | if (type == DataComponents.SHEEP_COLOR) {
|
| 251 | this.setColor(castComponentValue(DataComponents.SHEEP_COLOR, value));
|
| 252 | return true;
|
| 253 | } else {
|
| 254 | return super.applyImplicitComponent(type, value);
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | public boolean isSheared() {
|
| 259 | return (this.entityData.get(DATA_WOOL_ID) & 16) != 0;
|
| 260 | }
|
| 261 |
|
| 262 | public void setSheared(final boolean value) {
|
| 263 | byte current = this.entityData.get(DATA_WOOL_ID);
|
| 264 | if (value) {
|
| 265 | this.entityData.set(DATA_WOOL_ID, (byte)(current | 16));
|
| 266 | } else {
|
| 267 | this.entityData.set(DATA_WOOL_ID, (byte)(current & -17));
|
| 268 | }
|
| 269 | }
|
| 270 |
|
| 271 | public static DyeColor getRandomSheepColor(final ServerLevelAccessor level, final BlockPos pos) {
|
| 272 | Holder<Biome> biome = level.getBiome(pos);
|
| 273 | return SheepColorSpawnRules.getSheepColor(biome, level.getRandom());
|
| 274 | }
|
| 275 |
|
| 276 | @Override
|
| 277 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 278 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 279 | }
|
| 280 |
|
| 281 | public @Nullable Sheep getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 282 | Sheep sheep = EntityTypes.SHEEP.create(level, EntitySpawnReason.BREEDING);
|
| 283 | if (sheep != null) {
|
| 284 | DyeColor parent1DyeColor = this.getColor();
|
| 285 | DyeColor parent2DyeColor = ((Sheep)partner).getColor();
|
| 286 | sheep.setColor(DyeColor.getMixedColor(level, parent1DyeColor, parent2DyeColor));
|
| 287 | }
|
| 288 |
|
| 289 | return sheep;
|
| 290 | }
|
| 291 |
|
| 292 | @Override
|
| 293 | public void ate() {
|
| 294 | super.ate();
|
| 295 | this.setSheared(false);
|
| 296 | if (this.canAgeUp()) {
|
| 297 | this.ageUp(60);
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | @Override
|
| 302 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 303 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 304 | ) {
|
| 305 | this.setColor(getRandomSheepColor(level, this.blockPosition()));
|
| 306 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 307 | }
|
| 308 | }
|
| 309 |
|