| 1 | package net.minecraft.world.entity.animal.rabbit;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import java.util.function.IntFunction;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.component.DataComponentGetter;
|
| 9 | import net.minecraft.core.component.DataComponentType;
|
| 10 | import net.minecraft.core.component.DataComponents;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 13 | import net.minecraft.network.codec.StreamCodec;
|
| 14 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 15 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 16 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 17 | import net.minecraft.resources.Identifier;
|
| 18 | import net.minecraft.server.level.ServerLevel;
|
| 19 | import net.minecraft.sounds.SoundEvent;
|
| 20 | import net.minecraft.sounds.SoundEvents;
|
| 21 | import net.minecraft.sounds.SoundSource;
|
| 22 | import net.minecraft.tags.BiomeTags;
|
| 23 | import net.minecraft.tags.BlockTags;
|
| 24 | import net.minecraft.tags.ItemTags;
|
| 25 | import net.minecraft.util.ByIdMap;
|
| 26 | import net.minecraft.util.Mth;
|
| 27 | import net.minecraft.util.RandomSource;
|
| 28 | import net.minecraft.util.StringRepresentable;
|
| 29 | import net.minecraft.util.Util;
|
| 30 | import net.minecraft.world.DifficultyInstance;
|
| 31 | import net.minecraft.world.damagesource.DamageSource;
|
| 32 | import net.minecraft.world.entity.AgeableMob;
|
| 33 | import net.minecraft.world.entity.AnimationState;
|
| 34 | import net.minecraft.world.entity.EntityDimensions;
|
| 35 | import net.minecraft.world.entity.EntityEvent;
|
| 36 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 37 | import net.minecraft.world.entity.EntityType;
|
| 38 | import net.minecraft.world.entity.EntityTypes;
|
| 39 | import net.minecraft.world.entity.Leashable;
|
| 40 | import net.minecraft.world.entity.LivingEntity;
|
| 41 | import net.minecraft.world.entity.Pose;
|
| 42 | import net.minecraft.world.entity.SpawnGroupData;
|
| 43 | import net.minecraft.world.entity.ai.attributes.AttributeModifier;
|
| 44 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 45 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 46 | import net.minecraft.world.entity.ai.control.JumpControl;
|
| 47 | import net.minecraft.world.entity.ai.control.MoveControl;
|
| 48 | import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
|
| 49 | import net.minecraft.world.entity.ai.goal.BreedGoal;
|
| 50 | import net.minecraft.world.entity.ai.goal.ClimbOnTopOfPowderSnowGoal;
|
| 51 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 52 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 53 | import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
|
| 54 | import net.minecraft.world.entity.ai.goal.MoveToBlockGoal;
|
| 55 | import net.minecraft.world.entity.ai.goal.PanicGoal;
|
| 56 | import net.minecraft.world.entity.ai.goal.TemptGoal;
|
| 57 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 58 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 59 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 60 | import net.minecraft.world.entity.animal.Animal;
|
| 61 | import net.minecraft.world.entity.animal.wolf.Wolf;
|
| 62 | import net.minecraft.world.entity.monster.Monster;
|
| 63 | import net.minecraft.world.entity.player.Player;
|
| 64 | import net.minecraft.world.item.ItemStack;
|
| 65 | import net.minecraft.world.level.Level;
|
| 66 | import net.minecraft.world.level.LevelAccessor;
|
| 67 | import net.minecraft.world.level.LevelReader;
|
| 68 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 69 | import net.minecraft.world.level.biome.Biome;
|
| 70 | import net.minecraft.world.level.block.Block;
|
| 71 | import net.minecraft.world.level.block.Blocks;
|
| 72 | import net.minecraft.world.level.block.CarrotBlock;
|
| 73 | import net.minecraft.world.level.block.state.BlockState;
|
| 74 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 75 | import net.minecraft.world.level.gamerules.GameRules;
|
| 76 | import net.minecraft.world.level.pathfinder.Path;
|
| 77 | import net.minecraft.world.level.storage.ValueInput;
|
| 78 | import net.minecraft.world.level.storage.ValueOutput;
|
| 79 | import net.minecraft.world.phys.Vec3;
|
| 80 | import org.jspecify.annotations.Nullable;
|
| 81 |
|
| 82 | public class Rabbit extends Animal {
|
| 83 | private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.24F, 0.4F).withEyeHeight(0.39F);
|
| 84 | public static final double STROLL_SPEED_MOD = 0.6;
|
| 85 | public static final double BREED_SPEED_MOD = 0.8;
|
| 86 | public static final double FOLLOW_SPEED_MOD = 1.0;
|
| 87 | public static final double FLEE_SPEED_MOD = 2.2;
|
| 88 | public static final double ATTACK_SPEED_MOD = 1.4;
|
| 89 | private static final double BABY_JUMP_HEIGHT = 0.5;
|
| 90 | private static final double ADULT_JUMP_HEIGHT = 1.5;
|
| 91 | private static final int JUMP_DELAY_TICKS = 10;
|
| 92 | private static final int PANIC_JUMP_DELAY_TICKS = 3;
|
| 93 | private static final int JUMP_DURATION_IN_TICKS = 15;
|
| 94 | private static final EntityDataAccessor<Integer> DATA_TYPE_ID = SynchedEntityData.defineId(Rabbit.class, EntityDataSerializers.INT);
|
| 95 | private static final int DEFAULT_MORE_CARROT_TICKS = 0;
|
| 96 | public final AnimationState hopAnimationState = new AnimationState();
|
| 97 | public final AnimationState idleHeadTiltAnimationState = new AnimationState();
|
| 98 | private static final int IDLE_MINIMAL_DURATION_TICKS = 180;
|
| 99 | private int idleAnimationTimeout = this.random.nextInt(40) + 180;
|
| 100 | private static final Identifier KILLER_BUNNY = Identifier.withDefaultNamespace("killer_bunny");
|
| 101 | private static final int DEFAULT_ATTACK_POWER = 3;
|
| 102 | private static final int EVIL_ATTACK_POWER_INCREMENT = 5;
|
| 103 | private static final Identifier EVIL_ATTACK_POWER_MODIFIER = Identifier.withDefaultNamespace("evil");
|
| 104 | private static final int EVIL_ARMOR_VALUE = 8;
|
| 105 | private static final int MORE_CARROTS_DELAY = 40;
|
| 106 | private int jumpTicks;
|
| 107 | private int jumpDuration;
|
| 108 | private boolean wasOnGround;
|
| 109 | private int jumpDelayTicks;
|
| 110 | private int moreCarrotTicks = 0;
|
| 111 |
|
| 112 | public Rabbit(final EntityType<? extends Rabbit> type, final Level level) {
|
| 113 | super(type, level);
|
| 114 | this.jumpControl = new Rabbit.RabbitJumpControl(this);
|
| 115 | this.moveControl = new Rabbit.RabbitMoveControl<>(this);
|
| 116 | this.setSpeedModifier(0.0);
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | protected void registerGoals() {
|
| 121 | this.goalSelector.addGoal(1, new FloatGoal(this));
|
| 122 | this.goalSelector.addGoal(1, new ClimbOnTopOfPowderSnowGoal(this, this.level()));
|
| 123 | this.goalSelector.addGoal(1, new Rabbit.RabbitPanicGoal(this, 2.2));
|
| 124 | this.goalSelector.addGoal(2, new BreedGoal(this, 0.8));
|
| 125 | this.goalSelector.addGoal(3, new TemptGoal(this, 1.0, i -> i.is(ItemTags.RABBIT_FOOD), false));
|
| 126 | this.goalSelector.addGoal(4, new Rabbit.RabbitAvoidEntityGoal<>(this, Player.class, 8.0F, 2.2, 2.2));
|
| 127 | this.goalSelector.addGoal(4, new Rabbit.RabbitAvoidEntityGoal<>(this, Wolf.class, 10.0F, 2.2, 2.2));
|
| 128 | this.goalSelector.addGoal(4, new Rabbit.RabbitAvoidEntityGoal<>(this, Monster.class, 4.0F, 2.2, 2.2));
|
| 129 | this.goalSelector.addGoal(5, new Rabbit.RaidGardenGoal(this));
|
| 130 | this.goalSelector.addGoal(6, new WaterAvoidingRandomStrollGoal(this, 0.6));
|
| 131 | this.goalSelector.addGoal(11, new LookAtPlayerGoal(this, Player.class, 10.0F));
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | protected EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 136 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | protected float getJumpPower() {
|
| 141 | float baseJumpPower = 0.3F;
|
| 142 | if (this.moveControl.getSpeedModifier() <= 0.6) {
|
| 143 | baseJumpPower = 0.2F;
|
| 144 | }
|
| 145 |
|
| 146 | Path path = this.navigation.getPath();
|
| 147 | if (path != null && !path.isDone()) {
|
| 148 | Vec3 currentPos = path.getNextEntityPos(this);
|
| 149 | if (currentPos.y > this.getY() + 0.5) {
|
| 150 | baseJumpPower = 0.5F;
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | if (this.horizontalCollision || this.jumping && this.moveControl.getWantedY() > this.getY() + 0.5) {
|
| 155 | baseJumpPower = 0.5F;
|
| 156 | }
|
| 157 |
|
| 158 | return super.getJumpPower(baseJumpPower / 0.42F);
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | public void jumpFromGround() {
|
| 163 | super.jumpFromGround();
|
| 164 | double speedModifier = this.moveControl.getSpeedModifier();
|
| 165 | if (speedModifier > 0.0) {
|
| 166 | double current = this.getDeltaMovement().horizontalDistanceSqr();
|
| 167 | if (current < 0.01) {
|
| 168 | this.moveRelative(0.1F, new Vec3(0.0, this.isBaby() ? 0.5 : 1.5, 1.0));
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | if (!this.level().isClientSide()) {
|
| 173 | this.level().broadcastEntityEvent(this, (byte)1);
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | public float getJumpCompletion(final float a) {
|
| 178 | return this.jumpDuration == 0 ? 0.0F : (this.jumpTicks + a) / this.jumpDuration;
|
| 179 | }
|
| 180 |
|
| 181 | public void setSpeedModifier(final double speed) {
|
| 182 | this.getNavigation().setSpeedModifier(speed);
|
| 183 | this.moveControl.setWantedPosition(this.moveControl.getWantedX(), this.moveControl.getWantedY(), this.moveControl.getWantedZ(), speed);
|
| 184 | }
|
| 185 |
|
| 186 | @Override
|
| 187 | public void setJumping(final boolean jump) {
|
| 188 | super.setJumping(jump);
|
| 189 | if (jump) {
|
| 190 | this.playSound(this.getJumpSound(), this.getSoundVolume(), ((this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F) * 0.8F);
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | public void startJumping() {
|
| 195 | this.setJumping(true);
|
| 196 | this.jumpDuration = 15;
|
| 197 | this.jumpTicks = 0;
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 202 | super.defineSynchedData(entityData);
|
| 203 | entityData.define(DATA_TYPE_ID, Rabbit.Variant.DEFAULT.id);
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | public void customServerAiStep(final ServerLevel level) {
|
| 208 | if (this.jumpDelayTicks > 0) {
|
| 209 | this.jumpDelayTicks--;
|
| 210 | }
|
| 211 |
|
| 212 | if (this.moreCarrotTicks > 0) {
|
| 213 | this.moreCarrotTicks = this.moreCarrotTicks - this.random.nextInt(3);
|
| 214 | if (this.moreCarrotTicks < 0) {
|
| 215 | this.moreCarrotTicks = 0;
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | if (this.onGround()) {
|
| 220 | if (!this.wasOnGround) {
|
| 221 | this.setJumping(false);
|
| 222 | this.checkLandingDelay();
|
| 223 | }
|
| 224 |
|
| 225 | if (this.getVariant() == Rabbit.Variant.EVIL && this.jumpDelayTicks == 0) {
|
| 226 | LivingEntity target = this.getTarget();
|
| 227 | if (target != null && this.distanceToSqr(target) < 16.0) {
|
| 228 | this.facePoint(target.getX(), target.getZ());
|
| 229 | this.moveControl.setWantedPosition(target.getX(), target.getY(), target.getZ(), this.moveControl.getSpeedModifier());
|
| 230 | this.startJumping();
|
| 231 | this.wasOnGround = true;
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | Rabbit.RabbitJumpControl jumpControl = (Rabbit.RabbitJumpControl)this.jumpControl;
|
| 236 | if (!jumpControl.wantJump()) {
|
| 237 | if (this.moveControl.hasWanted() && this.jumpDelayTicks == 0) {
|
| 238 | Path path = this.navigation.getPath();
|
| 239 | Vec3 pos = new Vec3(this.moveControl.getWantedX(), this.moveControl.getWantedY(), this.moveControl.getWantedZ());
|
| 240 | if (path != null && !path.isDone()) {
|
| 241 | pos = path.getNextEntityPos(this);
|
| 242 | }
|
| 243 |
|
| 244 | this.facePoint(pos.x, pos.z);
|
| 245 | this.startJumping();
|
| 246 | }
|
| 247 | } else if (!jumpControl.canJump()) {
|
| 248 | this.enableJumpControl();
|
| 249 | }
|
| 250 | }
|
| 251 |
|
| 252 | this.wasOnGround = this.onGround();
|
| 253 | }
|
| 254 |
|
| 255 | @Override
|
| 256 | public boolean canSpawnSprintParticle() {
|
| 257 | return false;
|
| 258 | }
|
| 259 |
|
| 260 | private void facePoint(final double faceX, final double faceZ) {
|
| 261 | this.setYRot((float)(Mth.atan2(faceZ - this.getZ(), faceX - this.getX()) * 180.0F / (float)Math.PI) - 90.0F);
|
| 262 | }
|
| 263 |
|
| 264 | private void enableJumpControl() {
|
| 265 | ((Rabbit.RabbitJumpControl)this.jumpControl).setCanJump(true);
|
| 266 | }
|
| 267 |
|
| 268 | private void disableJumpControl() {
|
| 269 | ((Rabbit.RabbitJumpControl)this.jumpControl).setCanJump(false);
|
| 270 | }
|
| 271 |
|
| 272 | private void setLandingDelay() {
|
| 273 | this.jumpDelayTicks = this.moveControl.getSpeedModifier() < 2.2 ? 10 : 3;
|
| 274 | }
|
| 275 |
|
| 276 | private void checkLandingDelay() {
|
| 277 | this.setLandingDelay();
|
| 278 | this.disableJumpControl();
|
| 279 | }
|
| 280 |
|
| 281 | @Override
|
| 282 | public void aiStep() {
|
| 283 | super.aiStep();
|
| 284 | if (this.jumpTicks != this.jumpDuration) {
|
| 285 | this.jumpTicks++;
|
| 286 | } else if (this.jumpDuration != 0) {
|
| 287 | this.jumpTicks = 0;
|
| 288 | this.jumpDuration = 0;
|
| 289 | this.setJumping(false);
|
| 290 | }
|
| 291 | }
|
| 292 |
|
| 293 | public static AttributeSupplier.Builder createAttributes() {
|
| 294 | return Animal.createAnimalAttributes().add(Attributes.MAX_HEALTH, 3.0).add(Attributes.MOVEMENT_SPEED, 0.3F).add(Attributes.ATTACK_DAMAGE, 3.0);
|
| 295 | }
|
| 296 |
|
| 297 | @Override
|
| 298 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 299 | super.addAdditionalSaveData(output);
|
| 300 | output.store("RabbitType", Rabbit.Variant.LEGACY_CODEC, this.getVariant());
|
| 301 | output.putInt("MoreCarrotTicks", this.moreCarrotTicks);
|
| 302 | }
|
| 303 |
|
| 304 | @Override
|
| 305 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 306 | super.readAdditionalSaveData(input);
|
| 307 | this.setVariant(input.read("RabbitType", Rabbit.Variant.LEGACY_CODEC).orElse(Rabbit.Variant.DEFAULT));
|
| 308 | this.moreCarrotTicks = input.getIntOr("MoreCarrotTicks", 0);
|
| 309 | }
|
| 310 |
|
| 311 | protected SoundEvent getJumpSound() {
|
| 312 | return SoundEvents.RABBIT_JUMP;
|
| 313 | }
|
| 314 |
|
| 315 | @Override
|
| 316 | protected SoundEvent getAmbientSound() {
|
| 317 | return SoundEvents.RABBIT_AMBIENT;
|
| 318 | }
|
| 319 |
|
| 320 | @Override
|
| 321 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 322 | return SoundEvents.RABBIT_HURT;
|
| 323 | }
|
| 324 |
|
| 325 | @Override
|
| 326 | protected SoundEvent getDeathSound() {
|
| 327 | return SoundEvents.RABBIT_DEATH;
|
| 328 | }
|
| 329 |
|
| 330 | @Override
|
| 331 | public void playAttackSound() {
|
| 332 | if (this.getVariant() == Rabbit.Variant.EVIL) {
|
| 333 | this.playSound(SoundEvents.RABBIT_ATTACK, 1.0F, (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F);
|
| 334 | }
|
| 335 | }
|
| 336 |
|
| 337 | @Override
|
| 338 | public SoundSource getSoundSource() {
|
| 339 | return this.getVariant() == Rabbit.Variant.EVIL ? SoundSource.HOSTILE : SoundSource.NEUTRAL;
|
| 340 | }
|
| 341 |
|
| 342 | public @Nullable Rabbit getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 343 | Rabbit offspring = EntityTypes.RABBIT.create(level, EntitySpawnReason.BREEDING);
|
| 344 | if (offspring != null) {
|
| 345 | Rabbit.Variant variant = getRandomRabbitVariant(level, this.blockPosition());
|
| 346 | if (this.random.nextInt(20) != 0) {
|
| 347 | if (partner instanceof Rabbit rabbitPartner && this.random.nextBoolean()) {
|
| 348 | variant = rabbitPartner.getVariant();
|
| 349 | } else {
|
| 350 | variant = this.getVariant();
|
| 351 | }
|
| 352 | }
|
| 353 |
|
| 354 | offspring.setVariant(variant);
|
| 355 | }
|
| 356 |
|
| 357 | return offspring;
|
| 358 | }
|
| 359 |
|
| 360 | @Override
|
| 361 | public boolean isFood(final ItemStack itemStack) {
|
| 362 | return itemStack.is(ItemTags.RABBIT_FOOD);
|
| 363 | }
|
| 364 |
|
| 365 | public Rabbit.Variant getVariant() {
|
| 366 | return Rabbit.Variant.byId(this.entityData.get(DATA_TYPE_ID));
|
| 367 | }
|
| 368 |
|
| 369 | private void setVariant(final Rabbit.Variant variant) {
|
| 370 | if (variant == Rabbit.Variant.EVIL) {
|
| 371 | this.getAttribute(Attributes.ARMOR).setBaseValue(8.0);
|
| 372 | this.goalSelector.addGoal(4, new MeleeAttackGoal(this, 1.4, true));
|
| 373 | this.targetSelector.addGoal(1, new HurtByTargetGoal(this).setAlertOthers());
|
| 374 | this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Player.class, true));
|
| 375 | this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Wolf.class, true));
|
| 376 | this.getAttribute(Attributes.ATTACK_DAMAGE)
|
| 377 | .addOrUpdateTransientModifier(new AttributeModifier(EVIL_ATTACK_POWER_MODIFIER, 5.0, AttributeModifier.Operation.ADD_VALUE));
|
| 378 | if (!this.hasCustomName()) {
|
| 379 | this.setCustomName(Component.translatable(Util.makeDescriptionId("entity", KILLER_BUNNY)));
|
| 380 | }
|
| 381 | } else {
|
| 382 | this.getAttribute(Attributes.ATTACK_DAMAGE).removeModifier(EVIL_ATTACK_POWER_MODIFIER);
|
| 383 | }
|
| 384 |
|
| 385 | this.entityData.set(DATA_TYPE_ID, variant.id);
|
| 386 | }
|
| 387 |
|
| 388 | @Override
|
| 389 | public <T> @Nullable T get(final DataComponentType<? extends T> type) {
|
| 390 | return type == DataComponents.RABBIT_VARIANT ? castComponentValue((DataComponentType<T>)type, this.getVariant()) : super.get(type);
|
| 391 | }
|
| 392 |
|
| 393 | @Override
|
| 394 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 395 | this.applyImplicitComponentIfPresent(components, DataComponents.RABBIT_VARIANT);
|
| 396 | super.applyImplicitComponents(components);
|
| 397 | }
|
| 398 |
|
| 399 | @Override
|
| 400 | protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
|
| 401 | if (type == DataComponents.RABBIT_VARIANT) {
|
| 402 | this.setVariant(castComponentValue(DataComponents.RABBIT_VARIANT, value));
|
| 403 | return true;
|
| 404 | } else {
|
| 405 | return super.applyImplicitComponent(type, value);
|
| 406 | }
|
| 407 | }
|
| 408 |
|
| 409 | @Override
|
| 410 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 411 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
|
| 412 | ) {
|
| 413 | Rabbit.Variant variant = getRandomRabbitVariant(level, this.blockPosition());
|
| 414 | if (groupData instanceof Rabbit.RabbitGroupData) {
|
| 415 | variant = ((Rabbit.RabbitGroupData)groupData).variant;
|
| 416 | } else {
|
| 417 | groupData = new Rabbit.RabbitGroupData(variant);
|
| 418 | }
|
| 419 |
|
| 420 | this.setVariant(variant);
|
| 421 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 422 | }
|
| 423 |
|
| 424 | private static Rabbit.Variant getRandomRabbitVariant(final LevelAccessor level, final BlockPos pos) {
|
| 425 | Holder<Biome> biome = level.getBiome(pos);
|
| 426 | int randomVal = level.getRandom().nextInt(100);
|
| 427 | if (biome.is(BiomeTags.SPAWNS_WHITE_RABBITS)) {
|
| 428 | return randomVal < 80 ? Rabbit.Variant.WHITE : Rabbit.Variant.WHITE_SPLOTCHED;
|
| 429 | } else if (biome.is(BiomeTags.SPAWNS_GOLD_RABBITS)) {
|
| 430 | return Rabbit.Variant.GOLD;
|
| 431 | } else {
|
| 432 | return randomVal < 50 ? Rabbit.Variant.BROWN : (randomVal < 90 ? Rabbit.Variant.SALT : Rabbit.Variant.BLACK);
|
| 433 | }
|
| 434 | }
|
| 435 |
|
| 436 | public static boolean checkRabbitSpawnRules(
|
| 437 | final EntityType<Rabbit> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 438 | ) {
|
| 439 | return level.getBlockState(pos.below()).is(BlockTags.RABBITS_SPAWNABLE_ON) && isBrightEnoughToSpawn(level, pos);
|
| 440 | }
|
| 441 |
|
| 442 | private boolean wantsMoreFood() {
|
| 443 | return this.moreCarrotTicks <= 0;
|
| 444 | }
|
| 445 |
|
| 446 | @Override
|
| 447 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 448 | if (id == 1) {
|
| 449 | this.spawnSprintParticle();
|
| 450 | this.jumpDuration = 15;
|
| 451 | this.jumpTicks = 0;
|
| 452 | } else {
|
| 453 | super.handleEntityEvent(id);
|
| 454 | }
|
| 455 | }
|
| 456 |
|
| 457 | @Override
|
| 458 | public Vec3 getLeashOffset() {
|
| 459 | return new Vec3(0.0, 0.6F * this.getEyeHeight(), this.getBbWidth() * 0.4F);
|
| 460 | }
|
| 461 |
|
| 462 | private void setupAnimationStates() {
|
| 463 | if (this.shouldPlayIdleAnimation()) {
|
| 464 | this.idleAnimationTimeout = this.random.nextInt(40) + 180;
|
| 465 | this.idleHeadTiltAnimationState.start(this.tickCount);
|
| 466 | } else if (this.jumpTicks > 0) {
|
| 467 | this.hopAnimationState.startIfStopped(this.tickCount);
|
| 468 | this.idleHeadTiltAnimationState.stop();
|
| 469 | } else {
|
| 470 | this.idleAnimationTimeout--;
|
| 471 | this.hopAnimationState.stop();
|
| 472 | }
|
| 473 | }
|
| 474 |
|
| 475 | private boolean shouldPlayIdleAnimation() {
|
| 476 | return this.idleAnimationTimeout <= 0 && (this.getLeashData() == null || this.getLeashData().leashHolder == null) && !this.isNoAi();
|
| 477 | }
|
| 478 |
|
| 479 | @Override
|
| 480 | public void setLeashData(final Leashable.@Nullable LeashData leashData) {
|
| 481 | super.setLeashData(leashData);
|
| 482 | this.idleHeadTiltAnimationState.stop();
|
| 483 | }
|
| 484 |
|
| 485 | @Override
|
| 486 | public void baseTick() {
|
| 487 | super.baseTick();
|
| 488 | if (this.level().isClientSide()) {
|
| 489 | this.setupAnimationStates();
|
| 490 | }
|
| 491 | }
|
| 492 |
|
| 493 | private static class RabbitAvoidEntityGoal<T extends LivingEntity> extends AvoidEntityGoal<T> {
|
| 494 | private final Rabbit rabbit;
|
| 495 |
|
| 496 | public RabbitAvoidEntityGoal(
|
| 497 | final Rabbit rabbit, final Class<T> avoidClass, final float maxDist, final double walkSpeedModifier, final double sprintSpeedModifier
|
| 498 | ) {
|
| 499 | super(rabbit, avoidClass, maxDist, walkSpeedModifier, sprintSpeedModifier);
|
| 500 | this.rabbit = rabbit;
|
| 501 | }
|
| 502 |
|
| 503 | @Override
|
| 504 | public boolean canUse() {
|
| 505 | return this.rabbit.getVariant() != Rabbit.Variant.EVIL && super.canUse();
|
| 506 | }
|
| 507 | }
|
| 508 |
|
| 509 | public static class RabbitGroupData extends AgeableMob.AgeableMobGroupData {
|
| 510 | public final Rabbit.Variant variant;
|
| 511 |
|
| 512 | public RabbitGroupData(final Rabbit.Variant variant) {
|
| 513 | super(1.0F);
|
| 514 | this.variant = variant;
|
| 515 | }
|
| 516 | }
|
| 517 |
|
| 518 | public static class RabbitJumpControl extends JumpControl {
|
| 519 | private final Rabbit rabbit;
|
| 520 | private boolean canJump;
|
| 521 |
|
| 522 | public RabbitJumpControl(final Rabbit rabbit) {
|
| 523 | super(rabbit);
|
| 524 | this.rabbit = rabbit;
|
| 525 | }
|
| 526 |
|
| 527 | public boolean wantJump() {
|
| 528 | return this.jump;
|
| 529 | }
|
| 530 |
|
| 531 | public boolean canJump() {
|
| 532 | return this.canJump;
|
| 533 | }
|
| 534 |
|
| 535 | public void setCanJump(final boolean canJump) {
|
| 536 | this.canJump = canJump;
|
| 537 | }
|
| 538 |
|
| 539 | @Override
|
| 540 | public void tick() {
|
| 541 | if (this.jump) {
|
| 542 | this.rabbit.startJumping();
|
| 543 | this.jump = false;
|
| 544 | }
|
| 545 | }
|
| 546 | }
|
| 547 |
|
| 548 | private static class RabbitMoveControl<T extends Rabbit> extends MoveControl<T> {
|
| 549 | private double nextJumpSpeed;
|
| 550 |
|
| 551 | public RabbitMoveControl(final T rabbit) {
|
| 552 | super(rabbit);
|
| 553 | }
|
| 554 |
|
| 555 | @Override
|
| 556 | public void tick() {
|
| 557 | if (this.mob.onGround() && !this.mob.jumping && !((Rabbit.RabbitJumpControl)this.mob.jumpControl).wantJump()) {
|
| 558 | this.mob.setSpeedModifier(0.0);
|
| 559 | } else if (this.hasWanted() || this.operation == MoveControl.Operation.JUMPING) {
|
| 560 | this.mob.setSpeedModifier(this.nextJumpSpeed);
|
| 561 | }
|
| 562 |
|
| 563 | super.tick();
|
| 564 | }
|
| 565 |
|
| 566 | @Override
|
| 567 | public void setWantedPosition(final double x, final double y, final double z, double speedModifier) {
|
| 568 | if (this.mob.isInWater()) {
|
| 569 | speedModifier = 1.5;
|
| 570 | }
|
| 571 |
|
| 572 | super.setWantedPosition(x, y, z, speedModifier);
|
| 573 | if (speedModifier > 0.0) {
|
| 574 | this.nextJumpSpeed = speedModifier;
|
| 575 | }
|
| 576 | }
|
| 577 | }
|
| 578 |
|
| 579 | private static class RabbitPanicGoal extends PanicGoal {
|
| 580 | private final Rabbit rabbit;
|
| 581 |
|
| 582 | public RabbitPanicGoal(final Rabbit rabbit, final double speedModifier) {
|
| 583 | super(rabbit, speedModifier);
|
| 584 | this.rabbit = rabbit;
|
| 585 | }
|
| 586 |
|
| 587 | @Override
|
| 588 | public void tick() {
|
| 589 | super.tick();
|
| 590 | this.rabbit.setSpeedModifier(this.speedModifier);
|
| 591 | }
|
| 592 | }
|
| 593 |
|
| 594 | private static class RaidGardenGoal extends MoveToBlockGoal {
|
| 595 | private final Rabbit rabbit;
|
| 596 | private boolean wantsToRaid;
|
| 597 | private boolean canRaid;
|
| 598 |
|
| 599 | public RaidGardenGoal(final Rabbit rabbit) {
|
| 600 | super(rabbit, 0.7F, 16);
|
| 601 | this.rabbit = rabbit;
|
| 602 | }
|
| 603 |
|
| 604 | @Override
|
| 605 | public boolean canUse() {
|
| 606 | if (this.nextStartTick <= 0) {
|
| 607 | if (!getServerLevel(this.rabbit).getGameRules().get(GameRules.MOB_GRIEFING)) {
|
| 608 | return false;
|
| 609 | }
|
| 610 |
|
| 611 | this.canRaid = false;
|
| 612 | this.wantsToRaid = this.rabbit.wantsMoreFood();
|
| 613 | }
|
| 614 |
|
| 615 | return super.canUse();
|
| 616 | }
|
| 617 |
|
| 618 | @Override
|
| 619 | public boolean canContinueToUse() {
|
| 620 | return this.canRaid && super.canContinueToUse();
|
| 621 | }
|
| 622 |
|
| 623 | @Override
|
| 624 | public void tick() {
|
| 625 | super.tick();
|
| 626 | this.rabbit
|
| 627 | .getLookControl()
|
| 628 | .setLookAt(this.blockPos.getX() + 0.5, this.blockPos.getY() + 1, this.blockPos.getZ() + 0.5, 10.0F, this.rabbit.getMaxHeadXRot());
|
| 629 | if (this.isReachedTarget()) {
|
| 630 | Level level = this.rabbit.level();
|
| 631 | BlockPos cropsPos = this.blockPos.above();
|
| 632 | BlockState blockState = level.getBlockState(cropsPos);
|
| 633 | Block block = blockState.getBlock();
|
| 634 | if (this.canRaid && block instanceof CarrotBlock) {
|
| 635 | int carrotAge = blockState.getValue(CarrotBlock.AGE);
|
| 636 | if (carrotAge == 0) {
|
| 637 | level.setBlock(cropsPos, Blocks.AIR.defaultBlockState(), 2);
|
| 638 | level.destroyBlock(cropsPos, true, this.rabbit);
|
| 639 | } else {
|
| 640 | level.setBlock(cropsPos, blockState.setValue(CarrotBlock.AGE, carrotAge - 1), 2);
|
| 641 | level.gameEvent(GameEvent.BLOCK_CHANGE, cropsPos, GameEvent.Context.of(this.rabbit));
|
| 642 | level.levelEvent(2001, cropsPos, Block.getId(blockState));
|
| 643 | }
|
| 644 |
|
| 645 | this.rabbit.moreCarrotTicks = 40;
|
| 646 | }
|
| 647 |
|
| 648 | this.canRaid = false;
|
| 649 | this.nextStartTick = 10;
|
| 650 | }
|
| 651 | }
|
| 652 |
|
| 653 | @Override
|
| 654 | protected boolean isValidTarget(final LevelReader level, final BlockPos pos) {
|
| 655 | BlockState state = level.getBlockState(pos);
|
| 656 | if (state.is(BlockTags.SUPPORTS_CROPS) && this.wantsToRaid && !this.canRaid) {
|
| 657 | state = level.getBlockState(pos.above());
|
| 658 | if (state.getBlock() instanceof CarrotBlock carrotBlock && carrotBlock.isMaxAge(state)) {
|
| 659 | this.canRaid = true;
|
| 660 | return true;
|
| 661 | }
|
| 662 | }
|
| 663 |
|
| 664 | return false;
|
| 665 | }
|
| 666 | }
|
| 667 |
|
| 668 | public enum Variant implements StringRepresentable {
|
| 669 | BROWN(0, "brown"),
|
| 670 | WHITE(1, "white"),
|
| 671 | BLACK(2, "black"),
|
| 672 | WHITE_SPLOTCHED(3, "white_splotched"),
|
| 673 | GOLD(4, "gold"),
|
| 674 | SALT(5, "salt"),
|
| 675 | EVIL(99, "evil");
|
| 676 |
|
| 677 | public static final Rabbit.Variant DEFAULT = BROWN;
|
| 678 | private static final IntFunction<Rabbit.Variant> BY_ID = ByIdMap.sparse(Rabbit.Variant::id, values(), DEFAULT);
|
| 679 | public static final Codec<Rabbit.Variant> CODEC = StringRepresentable.fromEnum(Rabbit.Variant::values);
|
| 680 | @Deprecated
|
| 681 | public static final Codec<Rabbit.Variant> LEGACY_CODEC = Codec.INT.xmap(BY_ID::apply, Rabbit.Variant::id);
|
| 682 | public static final StreamCodec<ByteBuf, Rabbit.Variant> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, Rabbit.Variant::id);
|
| 683 | private final int id;
|
| 684 | private final String name;
|
| 685 |
|
| 686 | Variant(final int id, final String name) {
|
| 687 | this.id = id;
|
| 688 | this.name = name;
|
| 689 | }
|
| 690 |
|
| 691 | @Override
|
| 692 | public String getSerializedName() {
|
| 693 | return this.name;
|
| 694 | }
|
| 695 |
|
| 696 | public int id() {
|
| 697 | return this.id;
|
| 698 | }
|
| 699 |
|
| 700 | public static Rabbit.Variant byId(final int id) {
|
| 701 | return BY_ID.apply(id);
|
| 702 | }
|
| 703 | }
|
| 704 | }
|
| 705 |
|