| 1 | package net.minecraft.world.entity.animal.cow;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.sounds.SoundEvent;
|
| 5 | import net.minecraft.sounds.SoundEvents;
|
| 6 | import net.minecraft.tags.ItemTags;
|
| 7 | import net.minecraft.world.InteractionHand;
|
| 8 | import net.minecraft.world.InteractionResult;
|
| 9 | import net.minecraft.world.damagesource.DamageSource;
|
| 10 | import net.minecraft.world.entity.EntityDimensions;
|
| 11 | import net.minecraft.world.entity.EntityType;
|
| 12 | import net.minecraft.world.entity.EntityTypes;
|
| 13 | import net.minecraft.world.entity.Pose;
|
| 14 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 15 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 16 | import net.minecraft.world.entity.ai.goal.BreedGoal;
|
| 17 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 18 | import net.minecraft.world.entity.ai.goal.FollowParentGoal;
|
| 19 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 20 | import net.minecraft.world.entity.ai.goal.PanicGoal;
|
| 21 | import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
|
| 22 | import net.minecraft.world.entity.ai.goal.TemptGoal;
|
| 23 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 24 | import net.minecraft.world.entity.animal.Animal;
|
| 25 | import net.minecraft.world.entity.player.Player;
|
| 26 | import net.minecraft.world.item.ItemStack;
|
| 27 | import net.minecraft.world.item.ItemUtils;
|
| 28 | import net.minecraft.world.item.Items;
|
| 29 | import net.minecraft.world.level.Level;
|
| 30 | import net.minecraft.world.level.block.state.BlockState;
|
| 31 |
|
| 32 | public abstract class AbstractCow extends Animal {
|
| 33 | private static final EntityDimensions BABY_DIMENSIONS = EntityTypes.COW.getDimensions().scale(0.5F).withEyeHeight(0.665F);
|
| 34 |
|
| 35 | public AbstractCow(final EntityType<? extends AbstractCow> type, final Level level) {
|
| 36 | super(type, level);
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected void registerGoals() {
|
| 41 | this.goalSelector.addGoal(0, new FloatGoal(this));
|
| 42 | this.goalSelector.addGoal(1, new PanicGoal(this, 2.0));
|
| 43 | this.goalSelector.addGoal(2, new BreedGoal(this, 1.0));
|
| 44 | this.goalSelector.addGoal(3, new TemptGoal(this, 1.25, i -> i.is(ItemTags.COW_FOOD), false));
|
| 45 | this.goalSelector.addGoal(4, new FollowParentGoal(this, 1.25));
|
| 46 | this.goalSelector.addGoal(5, new WaterAvoidingRandomStrollGoal(this, 1.0));
|
| 47 | this.goalSelector.addGoal(6, new LookAtPlayerGoal(this, Player.class, 6.0F));
|
| 48 | this.goalSelector.addGoal(7, new RandomLookAroundGoal(this));
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public boolean isFood(final ItemStack itemStack) {
|
| 53 | return itemStack.is(ItemTags.COW_FOOD);
|
| 54 | }
|
| 55 |
|
| 56 | public static AttributeSupplier.Builder createAttributes() {
|
| 57 | return Animal.createAnimalAttributes().add(Attributes.MAX_HEALTH, 10.0).add(Attributes.MOVEMENT_SPEED, 0.2F);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected SoundEvent getAmbientSound() {
|
| 62 | return this.getSoundSet().ambientSound().value();
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 67 | return this.getSoundSet().hurtSound().value();
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected SoundEvent getDeathSound() {
|
| 72 | return this.getSoundSet().deathSound().value();
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 77 | this.playSound(this.getSoundSet().stepSound().value(), 0.15F, 1.0F);
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | protected float getSoundVolume() {
|
| 82 | return 0.4F;
|
| 83 | }
|
| 84 |
|
| 85 | protected CowSoundVariant getSoundSet() {
|
| 86 | return SoundEvents.COW_SOUNDS.get(CowSoundVariants.SoundSet.CLASSIC);
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 91 | ItemStack itemStack = player.getItemInHand(hand);
|
| 92 | if (itemStack.is(Items.BUCKET) && !this.isBaby()) {
|
| 93 | player.playSound(SoundEvents.COW_MILK, 1.0F, 1.0F);
|
| 94 | ItemStack bucketOrMilkBucket = ItemUtils.createFilledResult(itemStack, player, Items.MILK_BUCKET.getDefaultInstance());
|
| 95 | player.setItemInHand(hand, bucketOrMilkBucket);
|
| 96 | return InteractionResult.SUCCESS;
|
| 97 | } else {
|
| 98 | return super.mobInteract(player, hand);
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 104 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 105 | }
|
| 106 | }
|
| 107 |
|