| 1 | package net.minecraft.world.entity.animal.camel;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.core.particles.ParticleTypes;
|
| 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.BlockTags;
|
| 16 | import net.minecraft.tags.ItemTags;
|
| 17 | import net.minecraft.util.Mth;
|
| 18 | import net.minecraft.util.RandomSource;
|
| 19 | import net.minecraft.util.profiling.Profiler;
|
| 20 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 21 | import net.minecraft.world.DifficultyInstance;
|
| 22 | import net.minecraft.world.InteractionHand;
|
| 23 | import net.minecraft.world.InteractionResult;
|
| 24 | import net.minecraft.world.damagesource.DamageSource;
|
| 25 | import net.minecraft.world.entity.AgeableMob;
|
| 26 | import net.minecraft.world.entity.AnimationState;
|
| 27 | import net.minecraft.world.entity.Entity;
|
| 28 | import net.minecraft.world.entity.EntityDimensions;
|
| 29 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 30 | import net.minecraft.world.entity.EntityType;
|
| 31 | import net.minecraft.world.entity.EntityTypes;
|
| 32 | import net.minecraft.world.entity.EquipmentSlot;
|
| 33 | import net.minecraft.world.entity.Leashable;
|
| 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.ai.Brain;
|
| 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.control.BodyRotationControl;
|
| 41 | import net.minecraft.world.entity.ai.control.LookControl;
|
| 42 | import net.minecraft.world.entity.ai.control.MoveControl;
|
| 43 | import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
|
| 44 | import net.minecraft.world.entity.ai.sensing.SensorType;
|
| 45 | import net.minecraft.world.entity.animal.Animal;
|
| 46 | import net.minecraft.world.entity.animal.equine.AbstractHorse;
|
| 47 | import net.minecraft.world.entity.player.Player;
|
| 48 | import net.minecraft.world.item.ItemStack;
|
| 49 | import net.minecraft.world.item.Items;
|
| 50 | import net.minecraft.world.item.equipment.Equippable;
|
| 51 | import net.minecraft.world.level.Level;
|
| 52 | import net.minecraft.world.level.LevelAccessor;
|
| 53 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 54 | import net.minecraft.world.level.block.state.BlockState;
|
| 55 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 56 | import net.minecraft.world.level.storage.ValueInput;
|
| 57 | import net.minecraft.world.level.storage.ValueOutput;
|
| 58 | import net.minecraft.world.phys.Vec2;
|
| 59 | import net.minecraft.world.phys.Vec3;
|
| 60 | import org.jspecify.annotations.Nullable;
|
| 61 |
|
| 62 | public class Camel extends AbstractHorse {
|
| 63 | public static final float BABY_SCALE = 0.6F;
|
| 64 | public static final int DASH_COOLDOWN_TICKS = 55;
|
| 65 | public static final int MAX_HEAD_Y_ROT = 30;
|
| 66 | private static final float RUNNING_SPEED_BONUS = 0.1F;
|
| 67 | private static final float DASH_VERTICAL_MOMENTUM = 1.4285F;
|
| 68 | private static final float DASH_HORIZONTAL_MOMENTUM = 22.2222F;
|
| 69 | private static final int DASH_MINIMUM_DURATION_TICKS = 5;
|
| 70 | private static final int SITDOWN_DURATION_TICKS = 40;
|
| 71 | private static final int STANDUP_DURATION_TICKS = 52;
|
| 72 | private static final int IDLE_MINIMAL_DURATION_TICKS = 80;
|
| 73 | private static final float SITTING_HEIGHT_DIFFERENCE = 1.43F;
|
| 74 | private static final long DEFAULT_LAST_POSE_CHANGE_TICK = 0L;
|
| 75 | private static final Brain.Provider<Camel> BRAIN_PROVIDER = Brain.provider(
|
| 76 | List.of(SensorType.NEAREST_LIVING_ENTITIES, SensorType.HURT_BY, SensorType.FOOD_TEMPTATIONS, SensorType.NEAREST_ADULT), var0 -> CamelAi.getActivities()
|
| 77 | );
|
| 78 | public static final EntityDataAccessor<Boolean> DASH = SynchedEntityData.defineId(Camel.class, EntityDataSerializers.BOOLEAN);
|
| 79 | public static final EntityDataAccessor<Long> LAST_POSE_CHANGE_TICK = SynchedEntityData.defineId(Camel.class, EntityDataSerializers.LONG);
|
| 80 | public final AnimationState sitAnimationState = new AnimationState();
|
| 81 | public final AnimationState sitPoseAnimationState = new AnimationState();
|
| 82 | public final AnimationState sitUpAnimationState = new AnimationState();
|
| 83 | public final AnimationState idleAnimationState = new AnimationState();
|
| 84 | public final AnimationState dashAnimationState = new AnimationState();
|
| 85 | private static final EntityDimensions BABY_STANDING_DIMENSIONS = EntityDimensions.scalable(0.95F, 1.4F).withEyeHeight(1.38F);
|
| 86 | private static final EntityDimensions BABY_SITTING_DIMENSIONS = EntityDimensions.scalable(0.95F, 0.425F).withEyeHeight(0.41F);
|
| 87 | private static final EntityDimensions ADULT_SITTING_DIMENSIONS = EntityDimensions.scalable(EntityTypes.CAMEL.getWidth(), EntityTypes.CAMEL.getHeight() - 1.43F)
|
| 88 | .withEyeHeight(0.845F);
|
| 89 | private int dashCooldown = 0;
|
| 90 | private int idleAnimationTimeout = 0;
|
| 91 |
|
| 92 | public Camel(final EntityType<? extends Camel> type, final Level level) {
|
| 93 | super(type, level);
|
| 94 | this.moveControl = new Camel.CamelMoveControl<>(this);
|
| 95 | this.lookControl = new Camel.CamelLookControl();
|
| 96 | GroundPathNavigation navigation = (GroundPathNavigation)this.getNavigation();
|
| 97 | navigation.setCanFloat(true);
|
| 98 | navigation.setCanWalkOverFences(true);
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 103 | super.addAdditionalSaveData(output);
|
| 104 | output.putLong("LastPoseTick", this.entityData.get(LAST_POSE_CHANGE_TICK));
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 109 | super.readAdditionalSaveData(input);
|
| 110 | long poseTick = input.getLongOr("LastPoseTick", 0L);
|
| 111 | if (poseTick < 0L) {
|
| 112 | this.setPose(Pose.SITTING);
|
| 113 | }
|
| 114 |
|
| 115 | this.resetLastPoseChangeTick(poseTick);
|
| 116 | }
|
| 117 |
|
| 118 | public static AttributeSupplier.Builder createAttributes() {
|
| 119 | return createBaseHorseAttributes()
|
| 120 | .add(Attributes.MAX_HEALTH, 32.0)
|
| 121 | .add(Attributes.MOVEMENT_SPEED, 0.09F)
|
| 122 | .add(Attributes.JUMP_STRENGTH, 0.42F)
|
| 123 | .add(Attributes.STEP_HEIGHT, 1.5);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 128 | super.defineSynchedData(entityData);
|
| 129 | entityData.define(DASH, false);
|
| 130 | entityData.define(LAST_POSE_CHANGE_TICK, 0L);
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | public SpawnGroupData finalizeSpawn(
|
| 135 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 136 | ) {
|
| 137 | CamelAi.initMemories(this, level.getRandom());
|
| 138 | this.resetLastPoseChangeTickToFullStand(level.getLevel().getGameTime());
|
| 139 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 140 | }
|
| 141 |
|
| 142 | public static boolean checkCamelSpawnRules(
|
| 143 | final EntityType<Camel> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 144 | ) {
|
| 145 | return level.getBlockState(pos.below()).is(BlockTags.CAMELS_SPAWNABLE_ON) && isBrightEnoughToSpawn(level, pos);
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | protected Brain<Camel> makeBrain(final Brain.Packed packedBrain) {
|
| 150 | return BRAIN_PROVIDER.makeBrain(this, packedBrain);
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public Brain<Camel> getBrain() {
|
| 155 | return super.getBrain();
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | protected void registerGoals() {
|
| 160 | }
|
| 161 |
|
| 162 | @Override
|
| 163 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 164 | return pose == Pose.SITTING
|
| 165 | ? (this.isBaby() ? BABY_SITTING_DIMENSIONS : ADULT_SITTING_DIMENSIONS)
|
| 166 | : (this.isBaby() ? BABY_STANDING_DIMENSIONS : super.getDefaultDimensions(pose));
|
| 167 | }
|
| 168 |
|
| 169 | @Override
|
| 170 | protected void customServerAiStep(final ServerLevel level) {
|
| 171 | ProfilerFiller profiler = Profiler.get();
|
| 172 | profiler.push("camelBrain");
|
| 173 | Brain<Camel> brain = this.getBrain();
|
| 174 | brain.tick(level, this);
|
| 175 | profiler.pop();
|
| 176 | profiler.push("camelActivityUpdate");
|
| 177 | CamelAi.updateActivity(this);
|
| 178 | profiler.pop();
|
| 179 | super.customServerAiStep(level);
|
| 180 | }
|
| 181 |
|
| 182 | @Override
|
| 183 | public void tick() {
|
| 184 | super.tick();
|
| 185 | if (this.isDashing() && this.dashCooldown < 50 && (this.onGround() || this.isInLiquid() || this.isPassenger())) {
|
| 186 | this.setDashing(false);
|
| 187 | }
|
| 188 |
|
| 189 | if (this.dashCooldown > 0) {
|
| 190 | this.dashCooldown--;
|
| 191 | if (this.dashCooldown == 0) {
|
| 192 | this.level().playSound(null, this.blockPosition(), this.getDashReadySound(), SoundSource.NEUTRAL, 1.0F, 1.0F);
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | if (this.level().isClientSide()) {
|
| 197 | this.setupAnimationStates();
|
| 198 | }
|
| 199 |
|
| 200 | if (this.refuseToMove()) {
|
| 201 | this.clampHeadRotationToBody();
|
| 202 | }
|
| 203 |
|
| 204 | if (this.isCamelSitting() && this.isInWater()) {
|
| 205 | this.standUpInstantly();
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | private void setupAnimationStates() {
|
| 210 | if (this.idleAnimationTimeout <= 0) {
|
| 211 | this.idleAnimationTimeout = this.random.nextInt(40) + 80;
|
| 212 | this.idleAnimationState.start(this.tickCount);
|
| 213 | } else {
|
| 214 | this.idleAnimationTimeout--;
|
| 215 | }
|
| 216 |
|
| 217 | if (this.isCamelVisuallySitting()) {
|
| 218 | this.sitUpAnimationState.stop();
|
| 219 | this.dashAnimationState.stop();
|
| 220 | if (this.isVisuallySittingDown()) {
|
| 221 | this.sitAnimationState.startIfStopped(this.tickCount);
|
| 222 | this.sitPoseAnimationState.stop();
|
| 223 | } else {
|
| 224 | this.sitAnimationState.stop();
|
| 225 | this.sitPoseAnimationState.startIfStopped(this.tickCount);
|
| 226 | }
|
| 227 | } else {
|
| 228 | this.sitAnimationState.stop();
|
| 229 | this.sitPoseAnimationState.stop();
|
| 230 | this.dashAnimationState.animateWhen(this.isDashing(), this.tickCount);
|
| 231 | this.sitUpAnimationState.animateWhen(this.isInPoseTransition() && this.getPoseTime() >= 0L, this.tickCount);
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | protected void updateWalkAnimation(final float distance) {
|
| 237 | float targetSpeed;
|
| 238 | if (this.getPose() == Pose.STANDING && !this.dashAnimationState.isStarted()) {
|
| 239 | targetSpeed = Math.min(distance * 6.0F, 1.0F);
|
| 240 | } else {
|
| 241 | targetSpeed = 0.0F;
|
| 242 | }
|
| 243 |
|
| 244 | this.walkAnimation.update(targetSpeed, 0.2F, this.isBaby() ? 3.0F : 1.0F);
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | public void travel(Vec3 input) {
|
| 249 | if (this.refuseToMove() && this.onGround()) {
|
| 250 | this.setDeltaMovement(this.getDeltaMovement().multiply(0.0, 1.0, 0.0));
|
| 251 | input = input.multiply(0.0, 1.0, 0.0);
|
| 252 | }
|
| 253 |
|
| 254 | super.travel(input);
|
| 255 | }
|
| 256 |
|
| 257 | @Override
|
| 258 | protected void tickRidden(final Player controller, final Vec3 riddenInput) {
|
| 259 | super.tickRidden(controller, riddenInput);
|
| 260 | if (controller.zza > 0.0F && this.isCamelSitting() && !this.isInPoseTransition()) {
|
| 261 | this.standUp();
|
| 262 | }
|
| 263 | }
|
| 264 |
|
| 265 | public boolean refuseToMove() {
|
| 266 | return this.isCamelSitting() || this.isInPoseTransition();
|
| 267 | }
|
| 268 |
|
| 269 | @Override
|
| 270 | protected float getRiddenSpeed(final Player controller) {
|
| 271 | float movementBonus = controller.isSprinting() && this.getJumpCooldown() == 0 ? 0.1F : 0.0F;
|
| 272 | return (float)this.getAttributeValue(Attributes.MOVEMENT_SPEED) + movementBonus;
|
| 273 | }
|
| 274 |
|
| 275 | @Override
|
| 276 | protected Vec2 getRiddenRotation(final LivingEntity controller) {
|
| 277 | return this.refuseToMove() ? new Vec2(this.getXRot(), this.getYRot()) : super.getRiddenRotation(controller);
|
| 278 | }
|
| 279 |
|
| 280 | @Override
|
| 281 | protected Vec3 getRiddenInput(final Player controller, final Vec3 selfInput) {
|
| 282 | return this.refuseToMove() ? Vec3.ZERO : super.getRiddenInput(controller, selfInput);
|
| 283 | }
|
| 284 |
|
| 285 | @Override
|
| 286 | public boolean canJump() {
|
| 287 | return !this.refuseToMove() && super.canJump();
|
| 288 | }
|
| 289 |
|
| 290 | @Override
|
| 291 | public void onPlayerJump(final int jumpAmount) {
|
| 292 | if (this.isSaddled() && this.dashCooldown <= 0 && this.onGround()) {
|
| 293 | super.onPlayerJump(jumpAmount);
|
| 294 | }
|
| 295 | }
|
| 296 |
|
| 297 | @Override
|
| 298 | public boolean canSprint() {
|
| 299 | return true;
|
| 300 | }
|
| 301 |
|
| 302 | @Override
|
| 303 | protected void executeRidersJump(final float amount, final Vec3 input) {
|
| 304 | double jumpMomentum = this.getJumpPower();
|
| 305 | this.addDeltaMovement(
|
| 306 | this.getLookAngle()
|
| 307 | .multiply(1.0, 0.0, 1.0)
|
| 308 | .normalize()
|
| 309 | .scale(22.2222F * amount * this.getAttributeValue(Attributes.MOVEMENT_SPEED) * this.getBlockSpeedFactor())
|
| 310 | .add(0.0, 1.4285F * amount * jumpMomentum, 0.0)
|
| 311 | );
|
| 312 | this.dashCooldown = 55;
|
| 313 | this.setDashing(true);
|
| 314 | this.needsSync = true;
|
| 315 | }
|
| 316 |
|
| 317 | public boolean isDashing() {
|
| 318 | return this.entityData.get(DASH);
|
| 319 | }
|
| 320 |
|
| 321 | public void setDashing(final boolean isDashing) {
|
| 322 | this.entityData.set(DASH, isDashing);
|
| 323 | }
|
| 324 |
|
| 325 | @Override
|
| 326 | public void handleStartJump(final int jumpScale) {
|
| 327 | this.makeSound(this.getDashingSound());
|
| 328 | this.gameEvent(GameEvent.ENTITY_ACTION);
|
| 329 | this.setDashing(true);
|
| 330 | }
|
| 331 |
|
| 332 | protected SoundEvent getDashingSound() {
|
| 333 | return SoundEvents.CAMEL_DASH;
|
| 334 | }
|
| 335 |
|
| 336 | protected SoundEvent getDashReadySound() {
|
| 337 | return SoundEvents.CAMEL_DASH_READY;
|
| 338 | }
|
| 339 |
|
| 340 | @Override
|
| 341 | public void handleStopJump() {
|
| 342 | }
|
| 343 |
|
| 344 | @Override
|
| 345 | public int getJumpCooldown() {
|
| 346 | return this.dashCooldown;
|
| 347 | }
|
| 348 |
|
| 349 | @Override
|
| 350 | protected SoundEvent getAmbientSound() {
|
| 351 | return SoundEvents.CAMEL_AMBIENT;
|
| 352 | }
|
| 353 |
|
| 354 | @Override
|
| 355 | protected SoundEvent getDeathSound() {
|
| 356 | return SoundEvents.CAMEL_DEATH;
|
| 357 | }
|
| 358 |
|
| 359 | @Override
|
| 360 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 361 | return SoundEvents.CAMEL_HURT;
|
| 362 | }
|
| 363 |
|
| 364 | @Override
|
| 365 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 366 | if (blockState.is(BlockTags.CAMEL_SAND_STEP_SOUND_BLOCKS)) {
|
| 367 | this.playSound(SoundEvents.CAMEL_STEP_SAND, 1.0F, 1.0F);
|
| 368 | } else {
|
| 369 | this.playSound(SoundEvents.CAMEL_STEP, 1.0F, 1.0F);
|
| 370 | }
|
| 371 | }
|
| 372 |
|
| 373 | @Override
|
| 374 | public boolean isFood(final ItemStack itemStack) {
|
| 375 | return itemStack.is(ItemTags.CAMEL_FOOD);
|
| 376 | }
|
| 377 |
|
| 378 | @Override
|
| 379 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 380 | ItemStack itemStack = player.getItemInHand(hand);
|
| 381 | if (player.isSecondaryUseActive() && !this.isBaby()) {
|
| 382 | this.openCustomInventoryScreen(player);
|
| 383 | return InteractionResult.SUCCESS;
|
| 384 | } else {
|
| 385 | InteractionResult interactionResult = itemStack.interactLivingEntity(player, this, hand);
|
| 386 | if (interactionResult.consumesAction()) {
|
| 387 | return interactionResult;
|
| 388 | } else if (this.isFood(itemStack)) {
|
| 389 | return this.fedFood(player, itemStack);
|
| 390 | } else if (this.getPassengers().size() < 2 && !this.isBaby()) {
|
| 391 | this.doPlayerRide(player);
|
| 392 | return InteractionResult.CONSUME;
|
| 393 | } else {
|
| 394 | return this.isBaby() && player.isHolding(Items.GOLDEN_DANDELION) ? super.mobInteract(player, hand) : InteractionResult.FAIL;
|
| 395 | }
|
| 396 | }
|
| 397 | }
|
| 398 |
|
| 399 | @Override
|
| 400 | public void onElasticLeashPull() {
|
| 401 | super.onElasticLeashPull();
|
| 402 | if (this.isCamelSitting() && !this.isInPoseTransition() && this.canCamelChangePose()) {
|
| 403 | this.standUp();
|
| 404 | }
|
| 405 | }
|
| 406 |
|
| 407 | @Override
|
| 408 | public Vec3[] getQuadLeashOffsets() {
|
| 409 | return Leashable.createQuadLeashOffsets(this, 0.02, 0.48, 0.25, 0.82);
|
| 410 | }
|
| 411 |
|
| 412 | public boolean canCamelChangePose() {
|
| 413 | return this.wouldNotSuffocateAtTargetPose(this.isCamelSitting() ? Pose.STANDING : Pose.SITTING);
|
| 414 | }
|
| 415 |
|
| 416 | @Override
|
| 417 | protected boolean handleEating(final Player player, final ItemStack itemStack) {
|
| 418 | if (!this.isFood(itemStack)) {
|
| 419 | return false;
|
| 420 | }
|
| 421 |
|
| 422 | boolean couldHeal = this.getHealth() < this.getMaxHealth();
|
| 423 | if (couldHeal) {
|
| 424 | this.heal(2.0F);
|
| 425 | }
|
| 426 |
|
| 427 | boolean couldSetInLove = this.isTamed() && this.getAge() == 0 && this.canFallInLove();
|
| 428 | if (couldSetInLove) {
|
| 429 | this.setInLove(player);
|
| 430 | }
|
| 431 |
|
| 432 | boolean couldAgeUp = this.canAgeUp();
|
| 433 | if (couldAgeUp) {
|
| 434 | this.level().addParticle(ParticleTypes.HAPPY_VILLAGER, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), 0.0, 0.0, 0.0);
|
| 435 | if (!this.level().isClientSide()) {
|
| 436 | this.ageUp(10);
|
| 437 | }
|
| 438 | }
|
| 439 |
|
| 440 | if (!couldHeal && !couldSetInLove && !couldAgeUp) {
|
| 441 | return false;
|
| 442 | }
|
| 443 |
|
| 444 | if (!this.isSilent()) {
|
| 445 | SoundEvent eatingSound = this.getEatingSound();
|
| 446 | if (eatingSound != null) {
|
| 447 | this.level()
|
| 448 | .playSound(
|
| 449 | null, this.getX(), this.getY(), this.getZ(), eatingSound, this.getSoundSource(), 1.0F, 1.0F + (this.random.nextFloat() - this.random.nextFloat()) * 0.2F
|
| 450 | );
|
| 451 | }
|
| 452 | }
|
| 453 |
|
| 454 | this.gameEvent(GameEvent.EAT);
|
| 455 | return true;
|
| 456 | }
|
| 457 |
|
| 458 | @Override
|
| 459 | protected boolean canPerformRearing() {
|
| 460 | return false;
|
| 461 | }
|
| 462 |
|
| 463 | @Override
|
| 464 | public boolean canMate(final Animal partner) {
|
| 465 | return partner != this && partner instanceof Camel camel && this.canParent() && camel.canParent();
|
| 466 | }
|
| 467 |
|
| 468 | public @Nullable Camel getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 469 | return EntityTypes.CAMEL.create(level, EntitySpawnReason.BREEDING);
|
| 470 | }
|
| 471 |
|
| 472 | @Override
|
| 473 | protected SoundEvent getEatingSound() {
|
| 474 | return SoundEvents.CAMEL_EAT;
|
| 475 | }
|
| 476 |
|
| 477 | @Override
|
| 478 | protected void actuallyHurt(final ServerLevel level, final DamageSource source, final float dmg) {
|
| 479 | this.standUpInstantly();
|
| 480 | super.actuallyHurt(level, source, dmg);
|
| 481 | }
|
| 482 |
|
| 483 | @Override
|
| 484 | protected Vec3 getPassengerAttachmentPoint(final Entity passenger, final EntityDimensions dimensions, final float scale) {
|
| 485 | int index = Math.max(this.getPassengers().indexOf(passenger), 0);
|
| 486 | boolean driver = index == 0;
|
| 487 | float offset = 0.5F;
|
| 488 | float height = (float)(this.isRemoved() ? 0.01F : this.getBodyAnchorAnimationYOffset(driver, 0.0F, dimensions, scale));
|
| 489 | if (this.getPassengers().size() > 1) {
|
| 490 | if (!driver) {
|
| 491 | offset = -0.7F;
|
| 492 | }
|
| 493 |
|
| 494 | if (passenger instanceof Animal) {
|
| 495 | offset += 0.2F;
|
| 496 | }
|
| 497 | }
|
| 498 |
|
| 499 | return new Vec3(0.0, height, offset * scale).yRot(-this.getYRot() * (float) (Math.PI / 180.0));
|
| 500 | }
|
| 501 |
|
| 502 | @Override
|
| 503 | public float getAgeScale() {
|
| 504 | return this.isBaby() ? 0.6F : 1.0F;
|
| 505 | }
|
| 506 |
|
| 507 | private double getBodyAnchorAnimationYOffset(final boolean isFront, final float partialTicks, final EntityDimensions dimensions, final float scale) {
|
| 508 | double ageSitYOffset = this.isBaby() ? 0.09375 : 0.375;
|
| 509 | double baseSitOffset = dimensions.height() - ageSitYOffset;
|
| 510 | float sittingHeightDifference = scale * 1.43F;
|
| 511 | float verticalDrop = sittingHeightDifference - scale * 0.2F;
|
| 512 | float bottomPoint = sittingHeightDifference - verticalDrop;
|
| 513 | boolean isInTransition = this.isInPoseTransition();
|
| 514 | boolean isSitting = this.isCamelSitting();
|
| 515 | if (isInTransition) {
|
| 516 | int animationDuration = isSitting ? 40 : 52;
|
| 517 | int halfPoint;
|
| 518 | float flexPointOffset;
|
| 519 | if (isSitting) {
|
| 520 | halfPoint = 28;
|
| 521 | flexPointOffset = isFront ? 0.5F : 0.1F;
|
| 522 | } else {
|
| 523 | halfPoint = isFront ? 24 : 32;
|
| 524 | flexPointOffset = isFront ? 0.6F : 0.35F;
|
| 525 | }
|
| 526 |
|
| 527 | float poseTime = Mth.clamp((float)this.getPoseTime() + partialTicks, 0.0F, animationDuration);
|
| 528 | boolean isFirstPart = poseTime < halfPoint;
|
| 529 | float part = isFirstPart ? poseTime / halfPoint : (poseTime - halfPoint) / (animationDuration - halfPoint);
|
| 530 | float flexPoint = sittingHeightDifference - flexPointOffset * verticalDrop;
|
| 531 | baseSitOffset += isSitting
|
| 532 | ? Mth.lerp(part, isFirstPart ? sittingHeightDifference : flexPoint, isFirstPart ? flexPoint : bottomPoint)
|
| 533 | : Mth.lerp(part, isFirstPart ? bottomPoint - sittingHeightDifference : bottomPoint - flexPoint, isFirstPart ? bottomPoint - flexPoint : 0.0F);
|
| 534 | }
|
| 535 |
|
| 536 | if (isSitting && !isInTransition) {
|
| 537 | baseSitOffset += bottomPoint;
|
| 538 | }
|
| 539 |
|
| 540 | return baseSitOffset;
|
| 541 | }
|
| 542 |
|
| 543 | @Override
|
| 544 | public Vec3 getLeashOffset(final float partialTicks) {
|
| 545 | EntityDimensions dimensions = this.getDimensions(this.getPose());
|
| 546 | float scale = this.getAgeScale();
|
| 547 | return new Vec3(0.0, this.getBodyAnchorAnimationYOffset(true, partialTicks, dimensions, scale) - 0.2F * scale, dimensions.width() * 0.56F);
|
| 548 | }
|
| 549 |
|
| 550 | @Override
|
| 551 | public int getMaxHeadYRot() {
|
| 552 | return 30;
|
| 553 | }
|
| 554 |
|
| 555 | @Override
|
| 556 | protected boolean canAddPassenger(final Entity passenger) {
|
| 557 | return this.getPassengers().size() <= 2;
|
| 558 | }
|
| 559 |
|
| 560 | public boolean isCamelSitting() {
|
| 561 | return this.entityData.get(LAST_POSE_CHANGE_TICK) < 0L;
|
| 562 | }
|
| 563 |
|
| 564 | public boolean isCamelVisuallySitting() {
|
| 565 | return this.getPoseTime() < 0L != this.isCamelSitting();
|
| 566 | }
|
| 567 |
|
| 568 | public boolean isInPoseTransition() {
|
| 569 | long poseTime = this.getPoseTime();
|
| 570 | return poseTime < (this.isCamelSitting() ? 40 : 52);
|
| 571 | }
|
| 572 |
|
| 573 | private boolean isVisuallySittingDown() {
|
| 574 | return this.isCamelSitting() && this.getPoseTime() < 40L && this.getPoseTime() >= 0L;
|
| 575 | }
|
| 576 |
|
| 577 | public void sitDown() {
|
| 578 | if (!this.isCamelSitting()) {
|
| 579 | this.makeSound(this.getSitDownSound());
|
| 580 | this.setPose(Pose.SITTING);
|
| 581 | this.gameEvent(GameEvent.ENTITY_ACTION);
|
| 582 | this.resetLastPoseChangeTick(-this.level().getGameTime());
|
| 583 | }
|
| 584 | }
|
| 585 |
|
| 586 | public void standUp() {
|
| 587 | if (this.isCamelSitting()) {
|
| 588 | this.makeSound(this.getStandUpSound());
|
| 589 | this.setPose(Pose.STANDING);
|
| 590 | this.gameEvent(GameEvent.ENTITY_ACTION);
|
| 591 | this.resetLastPoseChangeTick(this.level().getGameTime());
|
| 592 | }
|
| 593 | }
|
| 594 |
|
| 595 | protected SoundEvent getStandUpSound() {
|
| 596 | return SoundEvents.CAMEL_STAND;
|
| 597 | }
|
| 598 |
|
| 599 | protected SoundEvent getSitDownSound() {
|
| 600 | return SoundEvents.CAMEL_SIT;
|
| 601 | }
|
| 602 |
|
| 603 | public void standUpInstantly() {
|
| 604 | this.setPose(Pose.STANDING);
|
| 605 | this.gameEvent(GameEvent.ENTITY_ACTION);
|
| 606 | this.resetLastPoseChangeTickToFullStand(this.level().getGameTime());
|
| 607 | }
|
| 608 |
|
| 609 | @VisibleForTesting
|
| 610 | public void resetLastPoseChangeTick(final long syncedPoseTickTime) {
|
| 611 | this.entityData.set(LAST_POSE_CHANGE_TICK, syncedPoseTickTime);
|
| 612 | }
|
| 613 |
|
| 614 | private void resetLastPoseChangeTickToFullStand(final long currentTime) {
|
| 615 | this.resetLastPoseChangeTick(Math.max(0L, currentTime - 52L - 1L));
|
| 616 | }
|
| 617 |
|
| 618 | public long getPoseTime() {
|
| 619 | return this.level().getGameTime() - Math.abs(this.entityData.get(LAST_POSE_CHANGE_TICK));
|
| 620 | }
|
| 621 |
|
| 622 | @Override
|
| 623 | protected Holder<SoundEvent> getEquipSound(final EquipmentSlot slot, final ItemStack stack, final Equippable equippable) {
|
| 624 | return slot == EquipmentSlot.SADDLE ? this.getSaddleSound() : super.getEquipSound(slot, stack, equippable);
|
| 625 | }
|
| 626 |
|
| 627 | protected Holder.Reference<SoundEvent> getSaddleSound() {
|
| 628 | return SoundEvents.CAMEL_SADDLE;
|
| 629 | }
|
| 630 |
|
| 631 | @Override
|
| 632 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 633 | if (!this.firstTick && DASH.equals(accessor)) {
|
| 634 | this.dashCooldown = this.dashCooldown == 0 ? 55 : this.dashCooldown;
|
| 635 | }
|
| 636 |
|
| 637 | super.onSyncedDataUpdated(accessor);
|
| 638 | }
|
| 639 |
|
| 640 | @Override
|
| 641 | public boolean isTamed() {
|
| 642 | return true;
|
| 643 | }
|
| 644 |
|
| 645 | @Override
|
| 646 | public void openCustomInventoryScreen(final Player player) {
|
| 647 | if (!this.level().isClientSide()) {
|
| 648 | player.openHorseInventory(this, this.inventory);
|
| 649 | }
|
| 650 | }
|
| 651 |
|
| 652 | @Override
|
| 653 | protected BodyRotationControl createBodyControl() {
|
| 654 | return new Camel.CamelBodyRotationControl(this);
|
| 655 | }
|
| 656 |
|
| 657 | private class CamelBodyRotationControl extends BodyRotationControl {
|
| 658 | public CamelBodyRotationControl(final Camel camel) {
|
| 659 | super(camel);
|
| 660 | }
|
| 661 |
|
| 662 | @Override
|
| 663 | public void clientTick() {
|
| 664 | if (!Camel.this.refuseToMove()) {
|
| 665 | super.clientTick();
|
| 666 | }
|
| 667 | }
|
| 668 | }
|
| 669 |
|
| 670 | private class CamelLookControl extends LookControl {
|
| 671 | private CamelLookControl() {
|
| 672 | super(Camel.this);
|
| 673 | }
|
| 674 |
|
| 675 | @Override
|
| 676 | public void tick() {
|
| 677 | if (!Camel.this.hasControllingPassenger()) {
|
| 678 | super.tick();
|
| 679 | }
|
| 680 | }
|
| 681 | }
|
| 682 |
|
| 683 | private static class CamelMoveControl<T extends Camel> extends MoveControl<T> {
|
| 684 | public CamelMoveControl(final T camel) {
|
| 685 | super(camel);
|
| 686 | }
|
| 687 |
|
| 688 | @Override
|
| 689 | public void tick() {
|
| 690 | if (this.operation == MoveControl.Operation.MOVE_TO
|
| 691 | && !this.mob.isLeashed()
|
| 692 | && this.mob.isCamelSitting()
|
| 693 | && !this.mob.isInPoseTransition()
|
| 694 | && this.mob.canCamelChangePose()) {
|
| 695 | this.mob.standUp();
|
| 696 | }
|
| 697 |
|
| 698 | super.tick();
|
| 699 | }
|
| 700 | }
|
| 701 | }
|
| 702 |
|