| 1 | package net.minecraft.world.entity.animal.goat;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Holder;
|
| 6 | import net.minecraft.core.registries.Registries;
|
| 7 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 8 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 9 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.sounds.SoundEvent;
|
| 12 | import net.minecraft.sounds.SoundEvents;
|
| 13 | import net.minecraft.sounds.SoundSource;
|
| 14 | import net.minecraft.tags.BlockTags;
|
| 15 | import net.minecraft.tags.InstrumentTags;
|
| 16 | import net.minecraft.tags.ItemTags;
|
| 17 | import net.minecraft.tags.TagKey;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.util.RandomSource;
|
| 20 | import net.minecraft.util.profiling.Profiler;
|
| 21 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 22 | import net.minecraft.world.DifficultyInstance;
|
| 23 | import net.minecraft.world.InteractionHand;
|
| 24 | import net.minecraft.world.InteractionResult;
|
| 25 | import net.minecraft.world.damagesource.DamageSource;
|
| 26 | import net.minecraft.world.entity.AgeableMob;
|
| 27 | import net.minecraft.world.entity.EntityAttachment;
|
| 28 | import net.minecraft.world.entity.EntityAttachments;
|
| 29 | import net.minecraft.world.entity.EntityDimensions;
|
| 30 | import net.minecraft.world.entity.EntityEvent;
|
| 31 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 32 | import net.minecraft.world.entity.EntityType;
|
| 33 | import net.minecraft.world.entity.EntityTypes;
|
| 34 | import net.minecraft.world.entity.Pose;
|
| 35 | import net.minecraft.world.entity.SpawnGroupData;
|
| 36 | import net.minecraft.world.entity.ai.Brain;
|
| 37 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 38 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 39 | import net.minecraft.world.entity.ai.sensing.SensorType;
|
| 40 | import net.minecraft.world.entity.animal.Animal;
|
| 41 | import net.minecraft.world.entity.item.ItemEntity;
|
| 42 | import net.minecraft.world.entity.player.Player;
|
| 43 | import net.minecraft.world.item.Instrument;
|
| 44 | import net.minecraft.world.item.InstrumentItem;
|
| 45 | import net.minecraft.world.item.ItemStack;
|
| 46 | import net.minecraft.world.item.ItemUtils;
|
| 47 | import net.minecraft.world.item.Items;
|
| 48 | import net.minecraft.world.level.Level;
|
| 49 | import net.minecraft.world.level.LevelAccessor;
|
| 50 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 51 | import net.minecraft.world.level.block.state.BlockState;
|
| 52 | import net.minecraft.world.level.pathfinder.PathType;
|
| 53 | import net.minecraft.world.level.storage.ValueInput;
|
| 54 | import net.minecraft.world.level.storage.ValueOutput;
|
| 55 | import net.minecraft.world.phys.Vec3;
|
| 56 | import org.jspecify.annotations.Nullable;
|
| 57 |
|
| 58 | public class Goat extends Animal {
|
| 59 | public static final float LONG_JUMPING_DIMENSION_SCALE_FACTOR = 0.7F;
|
| 60 | private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.45F, 0.65F)
|
| 61 | .withEyeHeight(0.59375F)
|
| 62 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.53125F, 0.0F));
|
| 63 | public static final float BABY_DEFAULT_X_HEAD_ROT = 22.5F;
|
| 64 | public static final float MAX_ADDED_RAMMING_X_HEAD_ROT = 30.0F;
|
| 65 | private static final float BABY_SCALE = 0.55F;
|
| 66 | private static final int ADULT_ATTACK_DAMAGE = 2;
|
| 67 | private static final int BABY_ATTACK_DAMAGE = 1;
|
| 68 | private static final Brain.Provider<Goat> BRAIN_PROVIDER = Brain.provider(
|
| 69 | List.of(
|
| 70 | SensorType.NEAREST_LIVING_ENTITIES,
|
| 71 | SensorType.NEAREST_PLAYERS,
|
| 72 | SensorType.NEAREST_ITEMS,
|
| 73 | SensorType.NEAREST_ADULT,
|
| 74 | SensorType.HURT_BY,
|
| 75 | SensorType.FOOD_TEMPTATIONS
|
| 76 | ),
|
| 77 | var0 -> GoatAi.getActivities()
|
| 78 | );
|
| 79 | public static final int GOAT_FALL_DAMAGE_REDUCTION = 10;
|
| 80 | public static final double GOAT_SCREAMING_CHANCE = 0.02;
|
| 81 | public static final double UNIHORN_CHANCE = 0.1F;
|
| 82 | private static final EntityDataAccessor<Boolean> DATA_IS_SCREAMING_GOAT = SynchedEntityData.defineId(Goat.class, EntityDataSerializers.BOOLEAN);
|
| 83 | private static final EntityDataAccessor<Boolean> DATA_HAS_LEFT_HORN = SynchedEntityData.defineId(Goat.class, EntityDataSerializers.BOOLEAN);
|
| 84 | private static final EntityDataAccessor<Boolean> DATA_HAS_RIGHT_HORN = SynchedEntityData.defineId(Goat.class, EntityDataSerializers.BOOLEAN);
|
| 85 | private static final boolean DEFAULT_IS_SCREAMING = false;
|
| 86 | private static final boolean DEFAULT_HAS_LEFT_HORN = true;
|
| 87 | private static final boolean DEFAULT_HAS_RIGHT_HORN = true;
|
| 88 | private boolean isLoweringHead;
|
| 89 | private int lowerHeadTick;
|
| 90 |
|
| 91 | public Goat(final EntityType<? extends Goat> type, final Level level) {
|
| 92 | super(type, level);
|
| 93 | this.getNavigation().setCanFloat(true);
|
| 94 | this.setPathfindingMalus(PathType.POWDER_SNOW, -1.0F);
|
| 95 | this.setPathfindingMalus(PathType.ON_TOP_OF_POWDER_SNOW, -1.0F);
|
| 96 | }
|
| 97 |
|
| 98 | public ItemStack createHorn() {
|
| 99 | RandomSource random = RandomSource.createThreadLocalInstance(this.getUUID().hashCode());
|
| 100 | TagKey<Instrument> key = this.isScreamingGoat() ? InstrumentTags.SCREAMING_GOAT_HORNS : InstrumentTags.REGULAR_GOAT_HORNS;
|
| 101 | return this.level()
|
| 102 | .registryAccess()
|
| 103 | .lookupOrThrow(Registries.INSTRUMENT)
|
| 104 | .getRandomElementOf(key, random)
|
| 105 | .map(instrument -> InstrumentItem.create(Items.GOAT_HORN, (Holder<Instrument>)instrument))
|
| 106 | .orElseGet(() -> new ItemStack(Items.GOAT_HORN));
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | protected Brain<Goat> makeBrain(final Brain.Packed packedBrain) {
|
| 111 | return BRAIN_PROVIDER.makeBrain(this, packedBrain);
|
| 112 | }
|
| 113 |
|
| 114 | public static AttributeSupplier.Builder createAttributes() {
|
| 115 | return Animal.createAnimalAttributes().add(Attributes.MAX_HEALTH, 10.0).add(Attributes.MOVEMENT_SPEED, 0.2F).add(Attributes.ATTACK_DAMAGE, 2.0);
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | protected void ageBoundaryReached() {
|
| 120 | this.getAttribute(Attributes.ATTACK_DAMAGE).setBaseValue(this.isBaby() ? 1.0 : 2.0);
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | protected int calculateFallDamage(final double fallDistance, final float damageModifier) {
|
| 125 | return super.calculateFallDamage(fallDistance, damageModifier) - 10;
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | protected SoundEvent getAmbientSound() {
|
| 130 | return this.isScreamingGoat() ? SoundEvents.GOAT_SCREAMING_AMBIENT : SoundEvents.GOAT_AMBIENT;
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 135 | return this.isScreamingGoat() ? SoundEvents.GOAT_SCREAMING_HURT : SoundEvents.GOAT_HURT;
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | protected SoundEvent getDeathSound() {
|
| 140 | return this.isScreamingGoat() ? SoundEvents.GOAT_SCREAMING_DEATH : SoundEvents.GOAT_DEATH;
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 145 | this.playSound(SoundEvents.GOAT_STEP, 0.15F, 1.0F);
|
| 146 | }
|
| 147 |
|
| 148 | protected SoundEvent getMilkingSound() {
|
| 149 | return this.isScreamingGoat() ? SoundEvents.GOAT_SCREAMING_MILK : SoundEvents.GOAT_MILK;
|
| 150 | }
|
| 151 |
|
| 152 | public @Nullable Goat getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 153 | Goat newGoat = EntityTypes.GOAT.create(level, EntitySpawnReason.BREEDING);
|
| 154 | if (newGoat != null) {
|
| 155 | GoatAi.initMemories(newGoat, level.getRandom());
|
| 156 | boolean babyIsScreaming = (level.getRandom().nextBoolean() ? this : partner) instanceof Goat goat && goat.isScreamingGoat()
|
| 157 | || level.getRandom().nextDouble() < 0.02;
|
| 158 | newGoat.setScreamingGoat(babyIsScreaming);
|
| 159 | }
|
| 160 |
|
| 161 | return newGoat;
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public float getAgeScale() {
|
| 166 | return this.isBaby() ? 0.55F : 1.0F;
|
| 167 | }
|
| 168 |
|
| 169 | @Override
|
| 170 | public Brain<Goat> getBrain() {
|
| 171 | return super.getBrain();
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | protected void customServerAiStep(final ServerLevel level) {
|
| 176 | ProfilerFiller profiler = Profiler.get();
|
| 177 | profiler.push("goatBrain");
|
| 178 | this.getBrain().tick(level, this);
|
| 179 | profiler.pop();
|
| 180 | profiler.push("goatActivityUpdate");
|
| 181 | GoatAi.updateActivity(this);
|
| 182 | profiler.pop();
|
| 183 | super.customServerAiStep(level);
|
| 184 | }
|
| 185 |
|
| 186 | @Override
|
| 187 | public int getMaxHeadYRot() {
|
| 188 | return 15;
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | public void setYHeadRot(final float yHeadRot) {
|
| 193 | int maxHeadYRot = this.getMaxHeadYRot();
|
| 194 | float deltaFromBody = Mth.degreesDifference(this.yBodyRot, yHeadRot);
|
| 195 | float deltaFromBodyClamped = Mth.clamp(deltaFromBody, -maxHeadYRot, maxHeadYRot);
|
| 196 | super.setYHeadRot(this.yBodyRot + deltaFromBodyClamped);
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | protected void playEatingSound() {
|
| 201 | this.level()
|
| 202 | .playSound(
|
| 203 | null,
|
| 204 | this,
|
| 205 | this.isScreamingGoat() ? SoundEvents.GOAT_SCREAMING_EAT : SoundEvents.GOAT_EAT,
|
| 206 | SoundSource.NEUTRAL,
|
| 207 | 1.0F,
|
| 208 | Mth.randomBetween(this.level().getRandom(), 0.8F, 1.2F)
|
| 209 | );
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public boolean isFood(final ItemStack itemStack) {
|
| 214 | return itemStack.is(ItemTags.GOAT_FOOD);
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 219 | ItemStack heldItem = player.getItemInHand(hand);
|
| 220 | if (heldItem.is(Items.BUCKET) && !this.isBaby()) {
|
| 221 | player.playSound(this.getMilkingSound(), 1.0F, 1.0F);
|
| 222 | ItemStack bucketOrMilkBucket = ItemUtils.createFilledResult(heldItem, player, Items.MILK_BUCKET.getDefaultInstance());
|
| 223 | player.setItemInHand(hand, bucketOrMilkBucket);
|
| 224 | return InteractionResult.SUCCESS;
|
| 225 | }
|
| 226 |
|
| 227 | InteractionResult interactionResult = super.mobInteract(player, hand);
|
| 228 | if (interactionResult.consumesAction() && this.isFood(heldItem)) {
|
| 229 | this.playEatingSound();
|
| 230 | }
|
| 231 |
|
| 232 | return interactionResult;
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | public SpawnGroupData finalizeSpawn(
|
| 237 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 238 | ) {
|
| 239 | RandomSource random = level.getRandom();
|
| 240 | GoatAi.initMemories(this, random);
|
| 241 | this.setScreamingGoat(random.nextDouble() < 0.02);
|
| 242 | this.ageBoundaryReached();
|
| 243 | if (!this.isBaby() && random.nextFloat() < 0.1F) {
|
| 244 | EntityDataAccessor<Boolean> hornToRemove = random.nextBoolean() ? DATA_HAS_LEFT_HORN : DATA_HAS_RIGHT_HORN;
|
| 245 | this.entityData.set(hornToRemove, false);
|
| 246 | }
|
| 247 |
|
| 248 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 249 | }
|
| 250 |
|
| 251 | @Override
|
| 252 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 253 | EntityDimensions entityDimensions = this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 254 | return pose == Pose.LONG_JUMPING ? entityDimensions.scale(0.7F) : entityDimensions;
|
| 255 | }
|
| 256 |
|
| 257 | @Override
|
| 258 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 259 | super.addAdditionalSaveData(output);
|
| 260 | output.putBoolean("IsScreamingGoat", this.isScreamingGoat());
|
| 261 | output.putBoolean("HasLeftHorn", this.hasLeftHorn());
|
| 262 | output.putBoolean("HasRightHorn", this.hasRightHorn());
|
| 263 | }
|
| 264 |
|
| 265 | @Override
|
| 266 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 267 | super.readAdditionalSaveData(input);
|
| 268 | this.setScreamingGoat(input.getBooleanOr("IsScreamingGoat", false));
|
| 269 | this.entityData.set(DATA_HAS_LEFT_HORN, input.getBooleanOr("HasLeftHorn", true));
|
| 270 | this.entityData.set(DATA_HAS_RIGHT_HORN, input.getBooleanOr("HasRightHorn", true));
|
| 271 | }
|
| 272 |
|
| 273 | @Override
|
| 274 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 275 | if (id == 58) {
|
| 276 | this.isLoweringHead = true;
|
| 277 | } else if (id == 59) {
|
| 278 | this.isLoweringHead = false;
|
| 279 | } else {
|
| 280 | super.handleEntityEvent(id);
|
| 281 | }
|
| 282 | }
|
| 283 |
|
| 284 | @Override
|
| 285 | public void aiStep() {
|
| 286 | if (this.isLoweringHead) {
|
| 287 | this.lowerHeadTick++;
|
| 288 | } else {
|
| 289 | this.lowerHeadTick -= 2;
|
| 290 | }
|
| 291 |
|
| 292 | this.lowerHeadTick = Mth.clamp(this.lowerHeadTick, 0, 20);
|
| 293 | super.aiStep();
|
| 294 | }
|
| 295 |
|
| 296 | @Override
|
| 297 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 298 | super.defineSynchedData(entityData);
|
| 299 | entityData.define(DATA_IS_SCREAMING_GOAT, false);
|
| 300 | entityData.define(DATA_HAS_LEFT_HORN, true);
|
| 301 | entityData.define(DATA_HAS_RIGHT_HORN, true);
|
| 302 | }
|
| 303 |
|
| 304 | public boolean hasLeftHorn() {
|
| 305 | return this.entityData.get(DATA_HAS_LEFT_HORN);
|
| 306 | }
|
| 307 |
|
| 308 | public boolean hasRightHorn() {
|
| 309 | return this.entityData.get(DATA_HAS_RIGHT_HORN);
|
| 310 | }
|
| 311 |
|
| 312 | public boolean dropHorn() {
|
| 313 | if (this.isBaby()) {
|
| 314 | return false;
|
| 315 | }
|
| 316 |
|
| 317 | boolean hasLeft = this.hasLeftHorn();
|
| 318 | boolean hasRight = this.hasRightHorn();
|
| 319 | if (!hasLeft && !hasRight) {
|
| 320 | return false;
|
| 321 | }
|
| 322 |
|
| 323 | EntityDataAccessor<Boolean> hornToDrop;
|
| 324 | if (!hasLeft) {
|
| 325 | hornToDrop = DATA_HAS_RIGHT_HORN;
|
| 326 | } else if (!hasRight) {
|
| 327 | hornToDrop = DATA_HAS_LEFT_HORN;
|
| 328 | } else {
|
| 329 | hornToDrop = this.random.nextBoolean() ? DATA_HAS_LEFT_HORN : DATA_HAS_RIGHT_HORN;
|
| 330 | }
|
| 331 |
|
| 332 | this.entityData.set(hornToDrop, false);
|
| 333 | Vec3 bodyPosition = this.position();
|
| 334 | ItemStack item = this.createHorn();
|
| 335 | double deltaX = Mth.randomBetween(this.random, -0.2F, 0.2F);
|
| 336 | double deltaY = Mth.randomBetween(this.random, 0.3F, 0.7F);
|
| 337 | double deltaZ = Mth.randomBetween(this.random, -0.2F, 0.2F);
|
| 338 | ItemEntity itemEntity = new ItemEntity(this.level(), bodyPosition.x(), bodyPosition.y(), bodyPosition.z(), item, deltaX, deltaY, deltaZ);
|
| 339 | this.level().addFreshEntity(itemEntity);
|
| 340 | return true;
|
| 341 | }
|
| 342 |
|
| 343 | public boolean isScreamingGoat() {
|
| 344 | return this.entityData.get(DATA_IS_SCREAMING_GOAT);
|
| 345 | }
|
| 346 |
|
| 347 | public void setScreamingGoat(final boolean isScreamingGoat) {
|
| 348 | this.entityData.set(DATA_IS_SCREAMING_GOAT, isScreamingGoat);
|
| 349 | }
|
| 350 |
|
| 351 | public float getRammingXHeadRot() {
|
| 352 | float maxRammingXHeadRot = this.isBaby() ? 52.5F : 30.0F;
|
| 353 | return this.lowerHeadTick / 20.0F * maxRammingXHeadRot * (float) (Math.PI / 180.0);
|
| 354 | }
|
| 355 |
|
| 356 | public static boolean checkGoatSpawnRules(
|
| 357 | final EntityType<? extends Animal> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 358 | ) {
|
| 359 | return level.getBlockState(pos.below()).is(BlockTags.GOATS_SPAWNABLE_ON) && isBrightEnoughToSpawn(level, pos);
|
| 360 | }
|
| 361 | }
|
| 362 |
|