| 1 | package net.minecraft.world.entity.animal.feline;
|
| 2 |
|
| 3 | import java.util.function.Predicate;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Holder;
|
| 6 | import net.minecraft.core.Registry;
|
| 7 | import net.minecraft.core.component.DataComponentGetter;
|
| 8 | import net.minecraft.core.component.DataComponentType;
|
| 9 | import net.minecraft.core.component.DataComponents;
|
| 10 | import net.minecraft.core.registries.Registries;
|
| 11 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 12 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 13 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 14 | import net.minecraft.resources.ResourceKey;
|
| 15 | import net.minecraft.server.level.ServerLevel;
|
| 16 | import net.minecraft.sounds.SoundEvent;
|
| 17 | import net.minecraft.tags.BlockTags;
|
| 18 | import net.minecraft.tags.ItemTags;
|
| 19 | import net.minecraft.util.Mth;
|
| 20 | import net.minecraft.util.RandomSource;
|
| 21 | import net.minecraft.world.DifficultyInstance;
|
| 22 | import net.minecraft.world.InteractionHand;
|
| 23 | import net.minecraft.world.InteractionResult;
|
| 24 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 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.EntitySelector;
|
| 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.LivingEntity;
|
| 35 | import net.minecraft.world.entity.Pose;
|
| 36 | import net.minecraft.world.entity.SpawnGroupData;
|
| 37 | import net.minecraft.world.entity.TamableAnimal;
|
| 38 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 39 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 40 | import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
|
| 41 | import net.minecraft.world.entity.ai.goal.BreedGoal;
|
| 42 | import net.minecraft.world.entity.ai.goal.CatLieOnBlockGoal;
|
| 43 | import net.minecraft.world.entity.ai.goal.CatSitOnBlockGoal;
|
| 44 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 45 | import net.minecraft.world.entity.ai.goal.FollowOwnerGoal;
|
| 46 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 47 | import net.minecraft.world.entity.ai.goal.LeapAtTargetGoal;
|
| 48 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 49 | import net.minecraft.world.entity.ai.goal.OcelotAttackGoal;
|
| 50 | import net.minecraft.world.entity.ai.goal.SitWhenOrderedToGoal;
|
| 51 | import net.minecraft.world.entity.ai.goal.TemptGoal;
|
| 52 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 53 | import net.minecraft.world.entity.ai.goal.target.NonTameRandomTargetGoal;
|
| 54 | import net.minecraft.world.entity.animal.Animal;
|
| 55 | import net.minecraft.world.entity.animal.rabbit.Rabbit;
|
| 56 | import net.minecraft.world.entity.animal.turtle.Turtle;
|
| 57 | import net.minecraft.world.entity.item.ItemEntity;
|
| 58 | import net.minecraft.world.entity.player.Player;
|
| 59 | import net.minecraft.world.entity.variant.SpawnContext;
|
| 60 | import net.minecraft.world.entity.variant.VariantUtils;
|
| 61 | import net.minecraft.world.item.DyeColor;
|
| 62 | import net.minecraft.world.item.ItemStack;
|
| 63 | import net.minecraft.world.level.Level;
|
| 64 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 65 | import net.minecraft.world.level.block.BedBlock;
|
| 66 | import net.minecraft.world.level.block.state.BlockState;
|
| 67 | import net.minecraft.world.level.storage.ValueInput;
|
| 68 | import net.minecraft.world.level.storage.ValueOutput;
|
| 69 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 70 | import net.minecraft.world.phys.AABB;
|
| 71 | import org.jspecify.annotations.Nullable;
|
| 72 |
|
| 73 | public class Cat extends TamableAnimal {
|
| 74 | public static final double TEMPT_SPEED_MOD = 0.6;
|
| 75 | public static final double WALK_SPEED_MOD = 0.8;
|
| 76 | public static final double SPRINT_SPEED_MOD = 1.33;
|
| 77 | private static final EntityDataAccessor<Holder<CatVariant>> DATA_VARIANT_ID = SynchedEntityData.defineId(Cat.class, EntityDataSerializers.CAT_VARIANT);
|
| 78 | private static final EntityDataAccessor<Boolean> IS_LYING = SynchedEntityData.defineId(Cat.class, EntityDataSerializers.BOOLEAN);
|
| 79 | private static final EntityDataAccessor<Boolean> RELAX_STATE_ONE = SynchedEntityData.defineId(Cat.class, EntityDataSerializers.BOOLEAN);
|
| 80 | private static final EntityDataAccessor<Integer> DATA_COLLAR_COLOR = SynchedEntityData.defineId(Cat.class, EntityDataSerializers.INT);
|
| 81 | private static final EntityDataAccessor<Holder<CatSoundVariant>> DATA_SOUND_VARIANT_ID = SynchedEntityData.defineId(
|
| 82 | Cat.class, EntityDataSerializers.CAT_SOUND_VARIANT
|
| 83 | );
|
| 84 | private static final ResourceKey<CatVariant> DEFAULT_VARIANT = CatVariants.BLACK;
|
| 85 | private static final DyeColor DEFAULT_COLLAR_COLOR = DyeColor.RED;
|
| 86 | private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.3F, 0.35F)
|
| 87 | .withEyeHeight(0.34375F)
|
| 88 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.3125F, 0.0F));
|
| 89 | private Cat.@Nullable CatAvoidEntityGoal<Player> avoidPlayersGoal;
|
| 90 | private @Nullable TemptGoal temptGoal;
|
| 91 | private float lieDownAmount;
|
| 92 | private float lieDownAmountO;
|
| 93 | private float lieDownAmountTail;
|
| 94 | private float lieDownAmountOTail;
|
| 95 | private boolean isLyingOnTopOfSleepingPlayer;
|
| 96 | private float relaxStateOneAmount;
|
| 97 | private float relaxStateOneAmountO;
|
| 98 |
|
| 99 | public Cat(final EntityType<? extends Cat> type, final Level level) {
|
| 100 | super(type, level);
|
| 101 | this.reassessTameGoals();
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | protected void registerGoals() {
|
| 106 | this.temptGoal = new Cat.CatTemptGoal(this, 0.6, i -> i.is(ItemTags.CAT_FOOD), true);
|
| 107 | this.goalSelector.addGoal(1, new FloatGoal(this));
|
| 108 | this.goalSelector.addGoal(1, new TamableAnimal.TamableAnimalPanicGoal(1.5));
|
| 109 | this.goalSelector.addGoal(2, new SitWhenOrderedToGoal(this));
|
| 110 | this.goalSelector.addGoal(3, new Cat.CatRelaxOnOwnerGoal(this));
|
| 111 | this.goalSelector.addGoal(4, this.temptGoal);
|
| 112 | this.goalSelector.addGoal(5, new CatLieOnBlockGoal(this, 1.1, 8));
|
| 113 | this.goalSelector.addGoal(6, new FollowOwnerGoal(this, 1.0, 10.0F, 5.0F));
|
| 114 | this.goalSelector.addGoal(7, new CatSitOnBlockGoal(this, 0.8));
|
| 115 | this.goalSelector.addGoal(8, new LeapAtTargetGoal(this, 0.3F));
|
| 116 | this.goalSelector.addGoal(9, new OcelotAttackGoal(this));
|
| 117 | this.goalSelector.addGoal(10, new BreedGoal(this, 0.8));
|
| 118 | this.goalSelector.addGoal(11, new WaterAvoidingRandomStrollGoal(this, 0.8, 1.0000001E-5F));
|
| 119 | this.goalSelector.addGoal(12, new LookAtPlayerGoal(this, Player.class, 10.0F));
|
| 120 | this.targetSelector.addGoal(1, new NonTameRandomTargetGoal<>(this, Rabbit.class, false, null));
|
| 121 | this.targetSelector.addGoal(1, new NonTameRandomTargetGoal<>(this, Turtle.class, false, Turtle.BABY_ON_LAND_SELECTOR));
|
| 122 | }
|
| 123 |
|
| 124 | public Holder<CatVariant> getVariant() {
|
| 125 | return this.entityData.get(DATA_VARIANT_ID);
|
| 126 | }
|
| 127 |
|
| 128 | private void setVariant(final Holder<CatVariant> variant) {
|
| 129 | this.entityData.set(DATA_VARIANT_ID, variant);
|
| 130 | }
|
| 131 |
|
| 132 | private Holder<CatSoundVariant> getSoundVariant() {
|
| 133 | return this.entityData.get(DATA_SOUND_VARIANT_ID);
|
| 134 | }
|
| 135 |
|
| 136 | private void setSoundVariant(final Holder<CatSoundVariant> soundVariant) {
|
| 137 | this.entityData.set(DATA_SOUND_VARIANT_ID, soundVariant);
|
| 138 | }
|
| 139 |
|
| 140 | private CatSoundVariant.CatSoundSet getSoundSet() {
|
| 141 | return this.isBaby() ? this.getSoundVariant().value().babySounds() : this.getSoundVariant().value().adultSounds();
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public <T> @Nullable T get(final DataComponentType<? extends T> type) {
|
| 146 | if (type == DataComponents.CAT_VARIANT) {
|
| 147 | return castComponentValue((DataComponentType<T>)type, this.getVariant());
|
| 148 | } else if (type == DataComponents.CAT_SOUND_VARIANT) {
|
| 149 | return castComponentValue((DataComponentType<T>)type, this.getSoundVariant());
|
| 150 | } else {
|
| 151 | return type == DataComponents.CAT_COLLAR ? castComponentValue((DataComponentType<T>)type, this.getCollarColor()) : super.get(type);
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 157 | this.applyImplicitComponentIfPresent(components, DataComponents.CAT_VARIANT);
|
| 158 | this.applyImplicitComponentIfPresent(components, DataComponents.CAT_SOUND_VARIANT);
|
| 159 | this.applyImplicitComponentIfPresent(components, DataComponents.CAT_COLLAR);
|
| 160 | super.applyImplicitComponents(components);
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
|
| 165 | if (type == DataComponents.CAT_VARIANT) {
|
| 166 | this.setVariant(castComponentValue(DataComponents.CAT_VARIANT, value));
|
| 167 | return true;
|
| 168 | } else if (type == DataComponents.CAT_SOUND_VARIANT) {
|
| 169 | this.setSoundVariant(castComponentValue(DataComponents.CAT_SOUND_VARIANT, value));
|
| 170 | return true;
|
| 171 | } else if (type == DataComponents.CAT_COLLAR) {
|
| 172 | this.setCollarColor(castComponentValue(DataComponents.CAT_COLLAR, value));
|
| 173 | return true;
|
| 174 | } else {
|
| 175 | return super.applyImplicitComponent(type, value);
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | public void setLying(final boolean value) {
|
| 180 | this.entityData.set(IS_LYING, value);
|
| 181 | }
|
| 182 |
|
| 183 | public boolean isLying() {
|
| 184 | return this.entityData.get(IS_LYING);
|
| 185 | }
|
| 186 |
|
| 187 | private void setRelaxStateOne(final boolean value) {
|
| 188 | this.entityData.set(RELAX_STATE_ONE, value);
|
| 189 | }
|
| 190 |
|
| 191 | private boolean isRelaxStateOne() {
|
| 192 | return this.entityData.get(RELAX_STATE_ONE);
|
| 193 | }
|
| 194 |
|
| 195 | public DyeColor getCollarColor() {
|
| 196 | return DyeColor.byId(this.entityData.get(DATA_COLLAR_COLOR));
|
| 197 | }
|
| 198 |
|
| 199 | private void setCollarColor(final DyeColor color) {
|
| 200 | this.entityData.set(DATA_COLLAR_COLOR, color.getId());
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 205 | super.defineSynchedData(entityData);
|
| 206 | Registry<CatSoundVariant> catSoundVariants = this.registryAccess().lookupOrThrow(Registries.CAT_SOUND_VARIANT);
|
| 207 | entityData.define(DATA_VARIANT_ID, VariantUtils.getDefaultOrAny(this.registryAccess(), DEFAULT_VARIANT));
|
| 208 | entityData.define(DATA_SOUND_VARIANT_ID, catSoundVariants.get(CatSoundVariants.CLASSIC).or(catSoundVariants::getAny).orElseThrow());
|
| 209 | entityData.define(IS_LYING, false);
|
| 210 | entityData.define(RELAX_STATE_ONE, false);
|
| 211 | entityData.define(DATA_COLLAR_COLOR, DEFAULT_COLLAR_COLOR.getId());
|
| 212 | }
|
| 213 |
|
| 214 | @Override
|
| 215 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 216 | super.addAdditionalSaveData(output);
|
| 217 | VariantUtils.writeVariant(output, this.getVariant());
|
| 218 | this.getSoundVariant()
|
| 219 | .unwrapKey()
|
| 220 | .ifPresent(soundVariant -> output.store("sound_variant", ResourceKey.codec(Registries.CAT_SOUND_VARIANT), (ResourceKey<CatSoundVariant>)soundVariant));
|
| 221 | output.store("CollarColor", DyeColor.LEGACY_ID_CODEC, this.getCollarColor());
|
| 222 | }
|
| 223 |
|
| 224 | @Override
|
| 225 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 226 | super.readAdditionalSaveData(input);
|
| 227 | VariantUtils.readVariant(input, Registries.CAT_VARIANT).ifPresent(this::setVariant);
|
| 228 | input.read("sound_variant", ResourceKey.codec(Registries.CAT_SOUND_VARIANT))
|
| 229 | .flatMap(soundVariant -> this.registryAccess().lookupOrThrow(Registries.CAT_SOUND_VARIANT).get((ResourceKey<CatSoundVariant>)soundVariant))
|
| 230 | .ifPresent(this::setSoundVariant);
|
| 231 | this.setCollarColor(input.read("CollarColor", DyeColor.LEGACY_ID_CODEC).orElse(DEFAULT_COLLAR_COLOR));
|
| 232 | }
|
| 233 |
|
| 234 | @Override
|
| 235 | public void customServerAiStep(final ServerLevel level) {
|
| 236 | if (this.getMoveControl().hasWanted()) {
|
| 237 | double speed = this.getMoveControl().getSpeedModifier();
|
| 238 | if (speed == 0.6) {
|
| 239 | this.setPose(Pose.CROUCHING);
|
| 240 | this.setSprinting(false);
|
| 241 | } else if (speed == 1.33) {
|
| 242 | this.setPose(Pose.STANDING);
|
| 243 | this.setSprinting(true);
|
| 244 | } else {
|
| 245 | this.setPose(Pose.STANDING);
|
| 246 | this.setSprinting(false);
|
| 247 | }
|
| 248 | } else {
|
| 249 | this.setPose(Pose.STANDING);
|
| 250 | this.setSprinting(false);
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | @Override
|
| 255 | protected @Nullable SoundEvent getAmbientSound() {
|
| 256 | if (this.isTame()) {
|
| 257 | if (this.isInLove()) {
|
| 258 | return this.getSoundSet().purrSound().value();
|
| 259 | } else {
|
| 260 | return this.random.nextInt(4) == 0 ? this.getSoundSet().purreowSound().value() : this.getSoundSet().ambientSound().value();
|
| 261 | }
|
| 262 | } else {
|
| 263 | return this.getSoundSet().strayAmbientSound().value();
|
| 264 | }
|
| 265 | }
|
| 266 |
|
| 267 | @Override
|
| 268 | public int getAmbientSoundInterval() {
|
| 269 | return 120;
|
| 270 | }
|
| 271 |
|
| 272 | public void hiss() {
|
| 273 | this.makeSound(this.getSoundSet().hissSound().value());
|
| 274 | }
|
| 275 |
|
| 276 | @Override
|
| 277 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 278 | return this.getSoundSet().hurtSound().value();
|
| 279 | }
|
| 280 |
|
| 281 | @Override
|
| 282 | protected SoundEvent getDeathSound() {
|
| 283 | return this.getSoundSet().deathSound().value();
|
| 284 | }
|
| 285 |
|
| 286 | public static AttributeSupplier.Builder createAttributes() {
|
| 287 | return Animal.createAnimalAttributes().add(Attributes.MAX_HEALTH, 10.0).add(Attributes.MOVEMENT_SPEED, 0.3F).add(Attributes.ATTACK_DAMAGE, 3.0);
|
| 288 | }
|
| 289 |
|
| 290 | @Override
|
| 291 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 292 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 293 | }
|
| 294 |
|
| 295 | @Override
|
| 296 | protected void playEatingSound() {
|
| 297 | this.playSound(this.getSoundSet().eatSound().value(), 1.0F, 1.0F);
|
| 298 | }
|
| 299 |
|
| 300 | @Override
|
| 301 | public void tick() {
|
| 302 | super.tick();
|
| 303 | if (this.temptGoal != null && this.temptGoal.isRunning() && !this.isTame() && this.tickCount % 100 == 0) {
|
| 304 | this.playSound(this.getSoundSet().begForFoodSound().value(), 1.0F, 1.0F);
|
| 305 | }
|
| 306 |
|
| 307 | this.handleLieDown();
|
| 308 | }
|
| 309 |
|
| 310 | private void handleLieDown() {
|
| 311 | if ((this.isLying() || this.isRelaxStateOne()) && this.tickCount % 5 == 0) {
|
| 312 | this.playSound(this.getSoundSet().purrSound().value(), 0.6F + 0.4F * (this.random.nextFloat() - this.random.nextFloat()), 1.0F);
|
| 313 | }
|
| 314 |
|
| 315 | this.updateLieDownAmount();
|
| 316 | this.updateRelaxStateOneAmount();
|
| 317 | this.isLyingOnTopOfSleepingPlayer = false;
|
| 318 | if (this.isLying()) {
|
| 319 | BlockPos catPos = this.blockPosition();
|
| 320 |
|
| 321 | for (Player player : this.level().getEntitiesOfClass(Player.class, new AABB(catPos).inflate(2.0, 2.0, 2.0))) {
|
| 322 | if (player.isSleeping()) {
|
| 323 | this.isLyingOnTopOfSleepingPlayer = true;
|
| 324 | break;
|
| 325 | }
|
| 326 | }
|
| 327 | }
|
| 328 | }
|
| 329 |
|
| 330 | public boolean isLyingOnTopOfSleepingPlayer() {
|
| 331 | return this.isLyingOnTopOfSleepingPlayer;
|
| 332 | }
|
| 333 |
|
| 334 | private void updateLieDownAmount() {
|
| 335 | this.lieDownAmountO = this.lieDownAmount;
|
| 336 | this.lieDownAmountOTail = this.lieDownAmountTail;
|
| 337 | if (this.isLying()) {
|
| 338 | this.lieDownAmount = Math.min(1.0F, this.lieDownAmount + 0.15F);
|
| 339 | this.lieDownAmountTail = Math.min(1.0F, this.lieDownAmountTail + 0.08F);
|
| 340 | } else {
|
| 341 | this.lieDownAmount = Math.max(0.0F, this.lieDownAmount - 0.22F);
|
| 342 | this.lieDownAmountTail = Math.max(0.0F, this.lieDownAmountTail - 0.13F);
|
| 343 | }
|
| 344 | }
|
| 345 |
|
| 346 | private void updateRelaxStateOneAmount() {
|
| 347 | this.relaxStateOneAmountO = this.relaxStateOneAmount;
|
| 348 | if (this.isRelaxStateOne()) {
|
| 349 | this.relaxStateOneAmount = Math.min(1.0F, this.relaxStateOneAmount + 0.1F);
|
| 350 | } else {
|
| 351 | this.relaxStateOneAmount = Math.max(0.0F, this.relaxStateOneAmount - 0.13F);
|
| 352 | }
|
| 353 | }
|
| 354 |
|
| 355 | public float getLieDownAmount(final float a) {
|
| 356 | return Mth.lerp(a, this.lieDownAmountO, this.lieDownAmount);
|
| 357 | }
|
| 358 |
|
| 359 | public float getLieDownAmountTail(final float a) {
|
| 360 | return Mth.lerp(a, this.lieDownAmountOTail, this.lieDownAmountTail);
|
| 361 | }
|
| 362 |
|
| 363 | public float getRelaxStateOneAmount(final float a) {
|
| 364 | return Mth.lerp(a, this.relaxStateOneAmountO, this.relaxStateOneAmount);
|
| 365 | }
|
| 366 |
|
| 367 | public @Nullable Cat getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 368 | Cat baby = EntityTypes.CAT.create(level, EntitySpawnReason.BREEDING);
|
| 369 | if (baby != null && partner instanceof Cat partnerCat) {
|
| 370 | if (this.random.nextBoolean()) {
|
| 371 | baby.setVariant(this.getVariant());
|
| 372 | } else {
|
| 373 | baby.setVariant(partnerCat.getVariant());
|
| 374 | }
|
| 375 |
|
| 376 | if (this.isTame()) {
|
| 377 | baby.setOwnerReference(this.getOwnerReference());
|
| 378 | baby.setTame(true, true);
|
| 379 | DyeColor parent1CollarColor = this.getCollarColor();
|
| 380 | DyeColor parent2CollarColor = partnerCat.getCollarColor();
|
| 381 | baby.setCollarColor(DyeColor.getMixedColor(level, parent1CollarColor, parent2CollarColor));
|
| 382 | }
|
| 383 | }
|
| 384 |
|
| 385 | return baby;
|
| 386 | }
|
| 387 |
|
| 388 | @Override
|
| 389 | public boolean canMate(final Animal partner) {
|
| 390 | if (!this.isTame()) {
|
| 391 | return false;
|
| 392 | } else {
|
| 393 | return !(partner instanceof Cat cat) ? false : cat.isTame() && super.canMate(partner);
|
| 394 | }
|
| 395 | }
|
| 396 |
|
| 397 | @Override
|
| 398 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 399 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
|
| 400 | ) {
|
| 401 | groupData = super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 402 | VariantUtils.selectVariantToSpawn(SpawnContext.create(level, this.blockPosition()), Registries.CAT_VARIANT).ifPresent(this::setVariant);
|
| 403 | this.setSoundVariant(CatSoundVariants.pickRandomSoundVariant(this.registryAccess(), level.getRandom()));
|
| 404 | return groupData;
|
| 405 | }
|
| 406 |
|
| 407 | @Override
|
| 408 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 409 | ItemStack itemStack = player.getItemInHand(hand);
|
| 410 | if (this.isTame()) {
|
| 411 | if (this.isOwnedBy(player)) {
|
| 412 | if (itemStack.is(ItemTags.CAT_COLLAR_DYES)) {
|
| 413 | DyeColor color = itemStack.get(DataComponents.DYE);
|
| 414 | if (color != null && color != this.getCollarColor()) {
|
| 415 | if (!this.level().isClientSide()) {
|
| 416 | this.setCollarColor(color);
|
| 417 | itemStack.consume(1, player);
|
| 418 | this.setPersistenceRequired();
|
| 419 | }
|
| 420 |
|
| 421 | return InteractionResult.SUCCESS;
|
| 422 | }
|
| 423 | } else if (this.isFood(itemStack) && this.getHealth() < this.getMaxHealth()) {
|
| 424 | if (!this.level().isClientSide()) {
|
| 425 | this.feed(player, hand, itemStack, 1.0F, 1.0F);
|
| 426 | }
|
| 427 |
|
| 428 | return InteractionResult.SUCCESS;
|
| 429 | }
|
| 430 |
|
| 431 | InteractionResult parentInteraction = super.mobInteract(player, hand);
|
| 432 | if (!parentInteraction.consumesAction()) {
|
| 433 | this.setOrderedToSit(!this.isOrderedToSit());
|
| 434 | return InteractionResult.SUCCESS;
|
| 435 | }
|
| 436 |
|
| 437 | return parentInteraction;
|
| 438 | }
|
| 439 | } else if (this.isFood(itemStack)) {
|
| 440 | if (!this.level().isClientSide()) {
|
| 441 | this.usePlayerItem(player, hand, itemStack);
|
| 442 | this.tryToTame(player);
|
| 443 | this.setPersistenceRequired();
|
| 444 | this.playEatingSound();
|
| 445 | }
|
| 446 |
|
| 447 | return InteractionResult.SUCCESS;
|
| 448 | }
|
| 449 |
|
| 450 | InteractionResult interact = super.mobInteract(player, hand);
|
| 451 | if (interact.consumesAction()) {
|
| 452 | this.setPersistenceRequired();
|
| 453 | }
|
| 454 |
|
| 455 | return interact;
|
| 456 | }
|
| 457 |
|
| 458 | @Override
|
| 459 | public boolean isFood(final ItemStack itemStack) {
|
| 460 | return itemStack.is(ItemTags.CAT_FOOD);
|
| 461 | }
|
| 462 |
|
| 463 | @Override
|
| 464 | public boolean removeWhenFarAway(final double distSqr) {
|
| 465 | return !this.isTame() && this.tickCount > 2400;
|
| 466 | }
|
| 467 |
|
| 468 | @Override
|
| 469 | public void setTame(final boolean isTame, final boolean includeSideEffects) {
|
| 470 | super.setTame(isTame, includeSideEffects);
|
| 471 | this.reassessTameGoals();
|
| 472 | }
|
| 473 |
|
| 474 | protected void reassessTameGoals() {
|
| 475 | if (this.avoidPlayersGoal == null) {
|
| 476 | this.avoidPlayersGoal = new Cat.CatAvoidEntityGoal<>(this, Player.class, 16.0F, 0.8, 1.33);
|
| 477 | }
|
| 478 |
|
| 479 | this.goalSelector.removeGoal(this.avoidPlayersGoal);
|
| 480 | if (!this.isTame()) {
|
| 481 | this.goalSelector.addGoal(4, this.avoidPlayersGoal);
|
| 482 | }
|
| 483 | }
|
| 484 |
|
| 485 | private void tryToTame(final Player player) {
|
| 486 | if (this.random.nextInt(3) == 0) {
|
| 487 | this.tame(player);
|
| 488 | this.setOrderedToSit(true);
|
| 489 | this.level().broadcastEntityEvent(this, (byte)7);
|
| 490 | } else {
|
| 491 | this.level().broadcastEntityEvent(this, (byte)6);
|
| 492 | }
|
| 493 | }
|
| 494 |
|
| 495 | @Override
|
| 496 | public boolean isSteppingCarefully() {
|
| 497 | return this.isCrouching() || super.isSteppingCarefully();
|
| 498 | }
|
| 499 |
|
| 500 | private static class CatAvoidEntityGoal<T extends LivingEntity> extends AvoidEntityGoal<T> {
|
| 501 | private final Cat cat;
|
| 502 |
|
| 503 | public CatAvoidEntityGoal(final Cat cat, final Class<T> avoidClass, final float maxDist, final double walkSpeedModifier, final double sprintSpeedModifier) {
|
| 504 | super(cat, avoidClass, maxDist, walkSpeedModifier, sprintSpeedModifier, EntitySelector.NO_CREATIVE_OR_SPECTATOR);
|
| 505 | this.cat = cat;
|
| 506 | }
|
| 507 |
|
| 508 | @Override
|
| 509 | public boolean canUse() {
|
| 510 | return !this.cat.isTame() && super.canUse();
|
| 511 | }
|
| 512 |
|
| 513 | @Override
|
| 514 | public boolean canContinueToUse() {
|
| 515 | return !this.cat.isTame() && super.canContinueToUse();
|
| 516 | }
|
| 517 | }
|
| 518 |
|
| 519 | private static class CatRelaxOnOwnerGoal extends Goal {
|
| 520 | private final Cat cat;
|
| 521 | private @Nullable Player ownerPlayer;
|
| 522 | private @Nullable BlockPos goalPos;
|
| 523 | private int onBedTicks;
|
| 524 |
|
| 525 | public CatRelaxOnOwnerGoal(final Cat cat) {
|
| 526 | this.cat = cat;
|
| 527 | }
|
| 528 |
|
| 529 | @Override
|
| 530 | public boolean canUse() {
|
| 531 | if (!this.cat.isTame()) {
|
| 532 | return false;
|
| 533 | }
|
| 534 |
|
| 535 | if (this.cat.isOrderedToSit()) {
|
| 536 | return false;
|
| 537 | }
|
| 538 |
|
| 539 | LivingEntity owner = this.cat.getOwner();
|
| 540 | if (owner instanceof Player playerOwner) {
|
| 541 | this.ownerPlayer = playerOwner;
|
| 542 | if (!owner.isSleeping()) {
|
| 543 | return false;
|
| 544 | }
|
| 545 |
|
| 546 | if (this.cat.distanceToSqr(this.ownerPlayer) > 100.0) {
|
| 547 | return false;
|
| 548 | }
|
| 549 |
|
| 550 | BlockPos ownerPos = this.ownerPlayer.blockPosition();
|
| 551 | BlockState ownerPosState = this.cat.level().getBlockState(ownerPos);
|
| 552 | if (ownerPosState.is(BlockTags.CATS_CAN_LIE_ON)) {
|
| 553 | this.goalPos = ownerPosState.getOptionalValue(BedBlock.FACING).map(bedDir -> ownerPos.relative(bedDir.getOpposite())).orElseGet(ownerPos::immutable);
|
| 554 | return !this.spaceIsOccupied();
|
| 555 | }
|
| 556 | }
|
| 557 |
|
| 558 | return false;
|
| 559 | }
|
| 560 |
|
| 561 | private boolean spaceIsOccupied() {
|
| 562 | for (Cat otherCat : this.cat.level().getEntitiesOfClass(Cat.class, new AABB(this.goalPos).inflate(2.0))) {
|
| 563 | if (otherCat != this.cat && (otherCat.isLying() || otherCat.isRelaxStateOne())) {
|
| 564 | return true;
|
| 565 | }
|
| 566 | }
|
| 567 |
|
| 568 | return false;
|
| 569 | }
|
| 570 |
|
| 571 | @Override
|
| 572 | public boolean canContinueToUse() {
|
| 573 | return this.cat.isTame()
|
| 574 | && !this.cat.isOrderedToSit()
|
| 575 | && this.ownerPlayer != null
|
| 576 | && this.ownerPlayer.isSleeping()
|
| 577 | && this.goalPos != null
|
| 578 | && !this.spaceIsOccupied();
|
| 579 | }
|
| 580 |
|
| 581 | @Override
|
| 582 | public void start() {
|
| 583 | if (this.goalPos != null) {
|
| 584 | this.cat.setInSittingPose(false);
|
| 585 | this.cat.getNavigation().moveTo(this.goalPos.getX(), this.goalPos.getY(), this.goalPos.getZ(), 1.1F);
|
| 586 | }
|
| 587 | }
|
| 588 |
|
| 589 | @Override
|
| 590 | public void stop() {
|
| 591 | this.cat.setLying(false);
|
| 592 | if (this.ownerPlayer.getSleepTimer() >= 100
|
| 593 | && this.cat.level().getRandom().nextFloat()
|
| 594 | < this.cat.level().environmentAttributes().getValue(EnvironmentAttributes.CAT_WAKING_UP_GIFT_CHANCE, this.cat.position())) {
|
| 595 | this.giveMorningGift();
|
| 596 | }
|
| 597 |
|
| 598 | this.onBedTicks = 0;
|
| 599 | this.cat.setRelaxStateOne(false);
|
| 600 | this.cat.getNavigation().stop();
|
| 601 | }
|
| 602 |
|
| 603 | private void giveMorningGift() {
|
| 604 | RandomSource random = this.cat.getRandom();
|
| 605 | BlockPos.MutableBlockPos catPos = new BlockPos.MutableBlockPos();
|
| 606 | catPos.set(this.cat.isLeashed() ? this.cat.getLeashHolder().blockPosition() : this.cat.blockPosition());
|
| 607 | this.cat
|
| 608 | .randomTeleport(
|
| 609 | catPos.getX() + random.nextInt(11) - 5,
|
| 610 | catPos.getY() + random.nextInt(5) - 2,
|
| 611 | catPos.getZ() + random.nextInt(11) - 5,
|
| 612 | false,
|
| 613 | BlockTags.CAT_DOES_NOT_TELEPORT_TO
|
| 614 | );
|
| 615 | catPos.set(this.cat.blockPosition());
|
| 616 | this.cat
|
| 617 | .dropFromGiftLootTable(
|
| 618 | getServerLevel(this.cat),
|
| 619 | BuiltInLootTables.CAT_MORNING_GIFT,
|
| 620 | (level, itemStack) -> level.addFreshEntity(
|
| 621 | new ItemEntity(
|
| 622 | level,
|
| 623 | (double)catPos.getX() - Mth.sin(this.cat.yBodyRot * (float) (Math.PI / 180.0)),
|
| 624 | catPos.getY(),
|
| 625 | (double)catPos.getZ() + Mth.cos(this.cat.yBodyRot * (float) (Math.PI / 180.0)),
|
| 626 | itemStack
|
| 627 | )
|
| 628 | )
|
| 629 | );
|
| 630 | }
|
| 631 |
|
| 632 | @Override
|
| 633 | public void tick() {
|
| 634 | if (this.ownerPlayer != null && this.goalPos != null) {
|
| 635 | this.cat.setInSittingPose(false);
|
| 636 | this.cat.getNavigation().moveTo(this.goalPos.getX(), this.goalPos.getY(), this.goalPos.getZ(), 1.1F);
|
| 637 | if (this.cat.distanceToSqr(this.ownerPlayer) < 2.5) {
|
| 638 | this.onBedTicks++;
|
| 639 | if (this.onBedTicks > this.adjustedTickDelay(16)) {
|
| 640 | this.cat.setLying(true);
|
| 641 | this.cat.setRelaxStateOne(false);
|
| 642 | } else {
|
| 643 | this.cat.lookAt(this.ownerPlayer, 45.0F, 45.0F);
|
| 644 | this.cat.setRelaxStateOne(true);
|
| 645 | }
|
| 646 | } else {
|
| 647 | this.cat.setLying(false);
|
| 648 | }
|
| 649 | }
|
| 650 | }
|
| 651 | }
|
| 652 |
|
| 653 | private static class CatTemptGoal extends TemptGoal {
|
| 654 | private @Nullable Player selectedPlayer;
|
| 655 | private final Cat cat;
|
| 656 |
|
| 657 | public CatTemptGoal(final Cat mob, final double speedModifier, final Predicate<ItemStack> items, final boolean canScare) {
|
| 658 | super(mob, speedModifier, items, canScare);
|
| 659 | this.cat = mob;
|
| 660 | }
|
| 661 |
|
| 662 | @Override
|
| 663 | public void tick() {
|
| 664 | super.tick();
|
| 665 | if (this.selectedPlayer == null && this.mob.getRandom().nextInt(this.adjustedTickDelay(600)) == 0) {
|
| 666 | this.selectedPlayer = this.player;
|
| 667 | } else if (this.mob.getRandom().nextInt(this.adjustedTickDelay(500)) == 0) {
|
| 668 | this.selectedPlayer = null;
|
| 669 | }
|
| 670 | }
|
| 671 |
|
| 672 | @Override
|
| 673 | protected boolean canScare() {
|
| 674 | return this.selectedPlayer != null && this.selectedPlayer.equals(this.player) ? false : super.canScare();
|
| 675 | }
|
| 676 |
|
| 677 | @Override
|
| 678 | public boolean canUse() {
|
| 679 | return super.canUse() && !this.cat.isTame();
|
| 680 | }
|
| 681 | }
|
| 682 | }
|
| 683 |
|