| 1 | package net.minecraft.world.entity.animal.panda;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import java.util.EnumSet;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.IntFunction;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.particles.ItemParticleOption;
|
| 9 | import net.minecraft.core.particles.ParticleTypes;
|
| 10 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 11 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 12 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 13 | import net.minecraft.server.level.ServerLevel;
|
| 14 | import net.minecraft.sounds.SoundEvent;
|
| 15 | import net.minecraft.sounds.SoundEvents;
|
| 16 | import net.minecraft.tags.DamageTypeTags;
|
| 17 | import net.minecraft.tags.ItemTags;
|
| 18 | import net.minecraft.util.ByIdMap;
|
| 19 | import net.minecraft.util.Mth;
|
| 20 | import net.minecraft.util.RandomSource;
|
| 21 | import net.minecraft.util.StringRepresentable;
|
| 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.Entity;
|
| 28 | import net.minecraft.world.entity.EntityAttachment;
|
| 29 | import net.minecraft.world.entity.EntityAttachments;
|
| 30 | import net.minecraft.world.entity.EntityDimensions;
|
| 31 | import net.minecraft.world.entity.EntitySelector;
|
| 32 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 33 | import net.minecraft.world.entity.EntityType;
|
| 34 | import net.minecraft.world.entity.EntityTypes;
|
| 35 | import net.minecraft.world.entity.EquipmentSlot;
|
| 36 | import net.minecraft.world.entity.LivingEntity;
|
| 37 | import net.minecraft.world.entity.Mob;
|
| 38 | import net.minecraft.world.entity.Pose;
|
| 39 | import net.minecraft.world.entity.SpawnGroupData;
|
| 40 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 41 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 42 | import net.minecraft.world.entity.ai.control.MoveControl;
|
| 43 | import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
|
| 44 | import net.minecraft.world.entity.ai.goal.BreedGoal;
|
| 45 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 46 | import net.minecraft.world.entity.ai.goal.FollowParentGoal;
|
| 47 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 48 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 49 | import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
|
| 50 | import net.minecraft.world.entity.ai.goal.PanicGoal;
|
| 51 | import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
|
| 52 | import net.minecraft.world.entity.ai.goal.TemptGoal;
|
| 53 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 54 | import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
|
| 55 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 56 | import net.minecraft.world.entity.animal.Animal;
|
| 57 | import net.minecraft.world.entity.item.ItemEntity;
|
| 58 | import net.minecraft.world.entity.monster.Monster;
|
| 59 | import net.minecraft.world.entity.player.Player;
|
| 60 | import net.minecraft.world.item.ItemStack;
|
| 61 | import net.minecraft.world.item.ItemStackTemplate;
|
| 62 | import net.minecraft.world.item.Items;
|
| 63 | import net.minecraft.world.level.Level;
|
| 64 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 65 | import net.minecraft.world.level.block.Blocks;
|
| 66 | import net.minecraft.world.level.block.state.BlockState;
|
| 67 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 68 | import net.minecraft.world.level.gamerules.GameRules;
|
| 69 | import net.minecraft.world.level.storage.ValueInput;
|
| 70 | import net.minecraft.world.level.storage.ValueOutput;
|
| 71 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 72 | import net.minecraft.world.phys.Vec3;
|
| 73 | import org.jspecify.annotations.Nullable;
|
| 74 |
|
| 75 | public class Panda extends Animal {
|
| 76 | private static final EntityDataAccessor<Integer> UNHAPPY_COUNTER = SynchedEntityData.defineId(Panda.class, EntityDataSerializers.INT);
|
| 77 | private static final EntityDataAccessor<Integer> SNEEZE_COUNTER = SynchedEntityData.defineId(Panda.class, EntityDataSerializers.INT);
|
| 78 | private static final EntityDataAccessor<Integer> EAT_COUNTER = SynchedEntityData.defineId(Panda.class, EntityDataSerializers.INT);
|
| 79 | private static final EntityDataAccessor<Byte> MAIN_GENE_ID = SynchedEntityData.defineId(Panda.class, EntityDataSerializers.BYTE);
|
| 80 | private static final EntityDataAccessor<Byte> HIDDEN_GENE_ID = SynchedEntityData.defineId(Panda.class, EntityDataSerializers.BYTE);
|
| 81 | private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(Panda.class, EntityDataSerializers.BYTE);
|
| 82 | private static final TargetingConditions BREED_TARGETING = TargetingConditions.forNonCombat().range(8.0);
|
| 83 | private static final EntityDimensions BABY_DIMENSIONS = EntityTypes.PANDA
|
| 84 | .getDimensions()
|
| 85 | .scale(0.5F)
|
| 86 | .withEyeHeight(0.28125F)
|
| 87 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.5625F, 0.0F));
|
| 88 | private static final int FLAG_SNEEZE = 2;
|
| 89 | private static final int FLAG_ROLL = 4;
|
| 90 | private static final int FLAG_SIT = 8;
|
| 91 | private static final int FLAG_ON_BACK = 16;
|
| 92 | private static final int EAT_TICK_INTERVAL = 5;
|
| 93 | public static final int TOTAL_ROLL_STEPS = 32;
|
| 94 | private static final int TOTAL_UNHAPPY_TIME = 32;
|
| 95 | private boolean gotBamboo;
|
| 96 | private boolean didBite;
|
| 97 | public int rollCounter;
|
| 98 | private Vec3 rollDelta;
|
| 99 | private float sitAmount;
|
| 100 | private float sitAmountO;
|
| 101 | private float onBackAmount;
|
| 102 | private float onBackAmountO;
|
| 103 | private float rollAmount;
|
| 104 | private float rollAmountO;
|
| 105 | private Panda.PandaLookAtPlayerGoal lookAtPlayerGoal;
|
| 106 |
|
| 107 | public Panda(final EntityType<? extends Panda> type, final Level level) {
|
| 108 | super(type, level);
|
| 109 | this.moveControl = new Panda.PandaMoveControl<>(this);
|
| 110 | if (!this.isBaby()) {
|
| 111 | this.setCanPickUpLoot(true);
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | protected boolean canDispenserEquipIntoSlot(final EquipmentSlot slot) {
|
| 117 | return slot == EquipmentSlot.MAINHAND && this.canPickUpLoot();
|
| 118 | }
|
| 119 |
|
| 120 | public int getUnhappyCounter() {
|
| 121 | return this.entityData.get(UNHAPPY_COUNTER);
|
| 122 | }
|
| 123 |
|
| 124 | public void setUnhappyCounter(final int value) {
|
| 125 | this.entityData.set(UNHAPPY_COUNTER, value);
|
| 126 | }
|
| 127 |
|
| 128 | public boolean isSneezing() {
|
| 129 | return this.getFlag(2);
|
| 130 | }
|
| 131 |
|
| 132 | public boolean isSitting() {
|
| 133 | return this.getFlag(8);
|
| 134 | }
|
| 135 |
|
| 136 | public void sit(final boolean value) {
|
| 137 | this.setFlag(8, value);
|
| 138 | }
|
| 139 |
|
| 140 | public boolean isOnBack() {
|
| 141 | return this.getFlag(16);
|
| 142 | }
|
| 143 |
|
| 144 | public void setOnBack(final boolean value) {
|
| 145 | this.setFlag(16, value);
|
| 146 | }
|
| 147 |
|
| 148 | public boolean isEating() {
|
| 149 | return this.entityData.get(EAT_COUNTER) > 0;
|
| 150 | }
|
| 151 |
|
| 152 | public void eat(final boolean value) {
|
| 153 | this.entityData.set(EAT_COUNTER, value ? 1 : 0);
|
| 154 | }
|
| 155 |
|
| 156 | private int getEatCounter() {
|
| 157 | return this.entityData.get(EAT_COUNTER);
|
| 158 | }
|
| 159 |
|
| 160 | private void setEatCounter(final int value) {
|
| 161 | this.entityData.set(EAT_COUNTER, value);
|
| 162 | }
|
| 163 |
|
| 164 | public void sneeze(final boolean value) {
|
| 165 | this.setFlag(2, value);
|
| 166 | if (!value) {
|
| 167 | this.setSneezeCounter(0);
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | public int getSneezeCounter() {
|
| 172 | return this.entityData.get(SNEEZE_COUNTER);
|
| 173 | }
|
| 174 |
|
| 175 | public void setSneezeCounter(final int value) {
|
| 176 | this.entityData.set(SNEEZE_COUNTER, value);
|
| 177 | }
|
| 178 |
|
| 179 | public Panda.Gene getMainGene() {
|
| 180 | return Panda.Gene.byId(this.entityData.get(MAIN_GENE_ID));
|
| 181 | }
|
| 182 |
|
| 183 | public void setMainGene(Panda.Gene gene) {
|
| 184 | if (gene.getId() > 6) {
|
| 185 | gene = Panda.Gene.getRandom(this.random);
|
| 186 | }
|
| 187 |
|
| 188 | this.entityData.set(MAIN_GENE_ID, (byte)gene.getId());
|
| 189 | }
|
| 190 |
|
| 191 | public Panda.Gene getHiddenGene() {
|
| 192 | return Panda.Gene.byId(this.entityData.get(HIDDEN_GENE_ID));
|
| 193 | }
|
| 194 |
|
| 195 | public void setHiddenGene(Panda.Gene gene) {
|
| 196 | if (gene.getId() > 6) {
|
| 197 | gene = Panda.Gene.getRandom(this.random);
|
| 198 | }
|
| 199 |
|
| 200 | this.entityData.set(HIDDEN_GENE_ID, (byte)gene.getId());
|
| 201 | }
|
| 202 |
|
| 203 | public boolean isRolling() {
|
| 204 | return this.getFlag(4);
|
| 205 | }
|
| 206 |
|
| 207 | public void roll(final boolean value) {
|
| 208 | this.setFlag(4, value);
|
| 209 | }
|
| 210 |
|
| 211 | @Override
|
| 212 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 213 | super.defineSynchedData(entityData);
|
| 214 | entityData.define(UNHAPPY_COUNTER, 0);
|
| 215 | entityData.define(SNEEZE_COUNTER, 0);
|
| 216 | entityData.define(MAIN_GENE_ID, (byte)0);
|
| 217 | entityData.define(HIDDEN_GENE_ID, (byte)0);
|
| 218 | entityData.define(DATA_ID_FLAGS, (byte)0);
|
| 219 | entityData.define(EAT_COUNTER, 0);
|
| 220 | }
|
| 221 |
|
| 222 | private boolean getFlag(final int flag) {
|
| 223 | return (this.entityData.get(DATA_ID_FLAGS) & flag) != 0;
|
| 224 | }
|
| 225 |
|
| 226 | private void setFlag(final int flag, final boolean value) {
|
| 227 | byte current = this.entityData.get(DATA_ID_FLAGS);
|
| 228 | if (value) {
|
| 229 | this.entityData.set(DATA_ID_FLAGS, (byte)(current | flag));
|
| 230 | } else {
|
| 231 | this.entityData.set(DATA_ID_FLAGS, (byte)(current & ~flag));
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 237 | super.addAdditionalSaveData(output);
|
| 238 | output.store("MainGene", Panda.Gene.CODEC, this.getMainGene());
|
| 239 | output.store("HiddenGene", Panda.Gene.CODEC, this.getHiddenGene());
|
| 240 | }
|
| 241 |
|
| 242 | @Override
|
| 243 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 244 | super.readAdditionalSaveData(input);
|
| 245 | this.setMainGene(input.read("MainGene", Panda.Gene.CODEC).orElse(Panda.Gene.NORMAL));
|
| 246 | this.setHiddenGene(input.read("HiddenGene", Panda.Gene.CODEC).orElse(Panda.Gene.NORMAL));
|
| 247 | }
|
| 248 |
|
| 249 | @Override
|
| 250 | public @Nullable AgeableMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 251 | Panda baby = EntityTypes.PANDA.create(level, EntitySpawnReason.BREEDING);
|
| 252 | if (baby != null) {
|
| 253 | if (partner instanceof Panda partnerPanda) {
|
| 254 | baby.setGeneFromParents(this, partnerPanda);
|
| 255 | }
|
| 256 |
|
| 257 | baby.setAttributes();
|
| 258 | }
|
| 259 |
|
| 260 | return baby;
|
| 261 | }
|
| 262 |
|
| 263 | @Override
|
| 264 | protected void registerGoals() {
|
| 265 | this.goalSelector.addGoal(0, new FloatGoal(this));
|
| 266 | this.goalSelector.addGoal(2, new Panda.PandaPanicGoal(this, 2.0));
|
| 267 | this.goalSelector.addGoal(2, new Panda.PandaBreedGoal(this, 1.0));
|
| 268 | this.goalSelector.addGoal(3, new Panda.PandaAttackGoal(this, 1.2F, true));
|
| 269 | this.goalSelector.addGoal(4, new TemptGoal(this, 1.0, i -> i.is(ItemTags.PANDA_FOOD), false));
|
| 270 | this.goalSelector.addGoal(6, new Panda.PandaAvoidGoal<>(this, Player.class, 8.0F, 2.0, 2.0));
|
| 271 | this.goalSelector.addGoal(6, new Panda.PandaAvoidGoal<>(this, Monster.class, 4.0F, 2.0, 2.0));
|
| 272 | this.goalSelector.addGoal(7, new Panda.PandaSitGoal());
|
| 273 | this.goalSelector.addGoal(8, new Panda.PandaLieOnBackGoal(this));
|
| 274 | this.goalSelector.addGoal(8, new Panda.PandaSneezeGoal(this));
|
| 275 | this.lookAtPlayerGoal = new Panda.PandaLookAtPlayerGoal(this, Player.class, 6.0F);
|
| 276 | this.goalSelector.addGoal(9, this.lookAtPlayerGoal);
|
| 277 | this.goalSelector.addGoal(10, new RandomLookAroundGoal(this));
|
| 278 | this.goalSelector.addGoal(12, new Panda.PandaRollGoal(this));
|
| 279 | this.goalSelector.addGoal(13, new FollowParentGoal(this, 1.25));
|
| 280 | this.goalSelector.addGoal(14, new WaterAvoidingRandomStrollGoal(this, 1.0));
|
| 281 | this.targetSelector.addGoal(1, new Panda.PandaHurtByTargetGoal(this).setAlertOthers());
|
| 282 | }
|
| 283 |
|
| 284 | public static AttributeSupplier.Builder createAttributes() {
|
| 285 | return Animal.createAnimalAttributes().add(Attributes.MOVEMENT_SPEED, 0.15F).add(Attributes.ATTACK_DAMAGE, 6.0);
|
| 286 | }
|
| 287 |
|
| 288 | public Panda.Gene getVariant() {
|
| 289 | return Panda.Gene.getVariantFromGenes(this.getMainGene(), this.getHiddenGene());
|
| 290 | }
|
| 291 |
|
| 292 | public boolean isLazy() {
|
| 293 | return this.getVariant() == Panda.Gene.LAZY;
|
| 294 | }
|
| 295 |
|
| 296 | public boolean isWorried() {
|
| 297 | return this.getVariant() == Panda.Gene.WORRIED;
|
| 298 | }
|
| 299 |
|
| 300 | public boolean isPlayful() {
|
| 301 | return this.getVariant() == Panda.Gene.PLAYFUL;
|
| 302 | }
|
| 303 |
|
| 304 | public boolean isBrown() {
|
| 305 | return this.getVariant() == Panda.Gene.BROWN;
|
| 306 | }
|
| 307 |
|
| 308 | public boolean isWeak() {
|
| 309 | return this.getVariant() == Panda.Gene.WEAK;
|
| 310 | }
|
| 311 |
|
| 312 | @Override
|
| 313 | public boolean isAggressive() {
|
| 314 | return this.getVariant() == Panda.Gene.AGGRESSIVE;
|
| 315 | }
|
| 316 |
|
| 317 | @Override
|
| 318 | public boolean canBeLeashed() {
|
| 319 | return false;
|
| 320 | }
|
| 321 |
|
| 322 | @Override
|
| 323 | public boolean doHurtTarget(final ServerLevel level, final Entity target) {
|
| 324 | if (!this.isAggressive()) {
|
| 325 | this.didBite = true;
|
| 326 | }
|
| 327 |
|
| 328 | return super.doHurtTarget(level, target);
|
| 329 | }
|
| 330 |
|
| 331 | @Override
|
| 332 | public void playAttackSound() {
|
| 333 | this.playSound(SoundEvents.PANDA_BITE, 1.0F, 1.0F);
|
| 334 | }
|
| 335 |
|
| 336 | @Override
|
| 337 | public void tick() {
|
| 338 | super.tick();
|
| 339 | if (this.isWorried()) {
|
| 340 | if (this.level().isThundering() && !this.isInWater()) {
|
| 341 | this.sit(true);
|
| 342 | this.eat(false);
|
| 343 | } else if (!this.isEating()) {
|
| 344 | this.sit(false);
|
| 345 | }
|
| 346 | }
|
| 347 |
|
| 348 | LivingEntity target = this.getTarget();
|
| 349 | if (target == null) {
|
| 350 | this.gotBamboo = false;
|
| 351 | this.didBite = false;
|
| 352 | }
|
| 353 |
|
| 354 | if (this.getUnhappyCounter() > 0) {
|
| 355 | if (target != null) {
|
| 356 | this.lookAt(target, 90.0F, 90.0F);
|
| 357 | }
|
| 358 |
|
| 359 | if (this.getUnhappyCounter() == 29 || this.getUnhappyCounter() == 14) {
|
| 360 | this.playSound(SoundEvents.PANDA_CANT_BREED, 1.0F, 1.0F);
|
| 361 | }
|
| 362 |
|
| 363 | this.setUnhappyCounter(this.getUnhappyCounter() - 1);
|
| 364 | }
|
| 365 |
|
| 366 | if (this.isSneezing()) {
|
| 367 | this.setSneezeCounter(this.getSneezeCounter() + 1);
|
| 368 | if (this.getSneezeCounter() > 20) {
|
| 369 | this.sneeze(false);
|
| 370 | this.afterSneeze();
|
| 371 | } else if (this.getSneezeCounter() == 1) {
|
| 372 | this.playSound(SoundEvents.PANDA_PRE_SNEEZE, 1.0F, 1.0F);
|
| 373 | }
|
| 374 | }
|
| 375 |
|
| 376 | if (this.isRolling()) {
|
| 377 | this.handleRoll();
|
| 378 | } else {
|
| 379 | this.rollCounter = 0;
|
| 380 | }
|
| 381 |
|
| 382 | if (this.isSitting()) {
|
| 383 | this.setXRot(0.0F);
|
| 384 | }
|
| 385 |
|
| 386 | this.updateSitAmount();
|
| 387 | this.handleEating();
|
| 388 | this.updateOnBackAnimation();
|
| 389 | this.updateRollAmount();
|
| 390 | }
|
| 391 |
|
| 392 | public boolean isScared() {
|
| 393 | return this.isWorried() && this.level().isThundering();
|
| 394 | }
|
| 395 |
|
| 396 | private void handleEating() {
|
| 397 | if (!this.isEating() && this.isSitting() && !this.isScared() && !this.getItemBySlot(EquipmentSlot.MAINHAND).isEmpty() && this.random.nextInt(80) == 1) {
|
| 398 | this.eat(true);
|
| 399 | } else if (this.getItemBySlot(EquipmentSlot.MAINHAND).isEmpty() || !this.isSitting()) {
|
| 400 | this.eat(false);
|
| 401 | }
|
| 402 |
|
| 403 | if (this.isEating()) {
|
| 404 | this.addEatingParticles();
|
| 405 | if (!this.level().isClientSide() && this.getEatCounter() > 80 && this.random.nextInt(20) == 1) {
|
| 406 | if (this.getEatCounter() > 100 && this.getItemBySlot(EquipmentSlot.MAINHAND).is(ItemTags.PANDA_EATS_FROM_GROUND)) {
|
| 407 | if (!this.level().isClientSide()) {
|
| 408 | this.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
|
| 409 | this.gameEvent(GameEvent.EAT);
|
| 410 | }
|
| 411 |
|
| 412 | this.sit(false);
|
| 413 | }
|
| 414 |
|
| 415 | this.eat(false);
|
| 416 | return;
|
| 417 | }
|
| 418 |
|
| 419 | this.setEatCounter(this.getEatCounter() + 1);
|
| 420 | }
|
| 421 | }
|
| 422 |
|
| 423 | private void addEatingParticles() {
|
| 424 | if (this.getEatCounter() % 5 == 0) {
|
| 425 | this.playSound(SoundEvents.PANDA_EAT, 0.5F + 0.5F * this.random.nextInt(2), (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F);
|
| 426 | ItemStack heldItem = this.getItemBySlot(EquipmentSlot.MAINHAND);
|
| 427 | if (!heldItem.isEmpty()) {
|
| 428 | ItemParticleOption breakParticle = new ItemParticleOption(ParticleTypes.ITEM, ItemStackTemplate.fromNonEmptyStack(heldItem));
|
| 429 |
|
| 430 | for (int i = 0; i < 6; i++) {
|
| 431 | Vec3 velocity = new Vec3((this.random.nextFloat() - 0.5) * 0.1, this.random.nextFloat() * 0.1 + 0.1, (this.random.nextFloat() - 0.5) * 0.1)
|
| 432 | .xRot(-this.getXRot() * (float) (Math.PI / 180.0))
|
| 433 | .yRot(-this.getYRot() * (float) (Math.PI / 180.0));
|
| 434 | Vec3 position = new Vec3((this.random.nextFloat() - 0.5) * 0.8, -this.random.nextFloat() * 0.6 - 0.3, 1.0 + (this.random.nextFloat() - 0.5) * 0.4)
|
| 435 | .yRot(-this.yBodyRot * (float) (Math.PI / 180.0))
|
| 436 | .add(this.getX(), this.getEyeY() + 1.0, this.getZ());
|
| 437 | this.level().addParticle(breakParticle, position.x, position.y, position.z, velocity.x, velocity.y + 0.05, velocity.z);
|
| 438 | }
|
| 439 | }
|
| 440 | }
|
| 441 | }
|
| 442 |
|
| 443 | private void updateSitAmount() {
|
| 444 | this.sitAmountO = this.sitAmount;
|
| 445 | if (this.isSitting()) {
|
| 446 | this.sitAmount = Math.min(1.0F, this.sitAmount + 0.15F);
|
| 447 | } else {
|
| 448 | this.sitAmount = Math.max(0.0F, this.sitAmount - 0.19F);
|
| 449 | }
|
| 450 | }
|
| 451 |
|
| 452 | private void updateOnBackAnimation() {
|
| 453 | this.onBackAmountO = this.onBackAmount;
|
| 454 | if (this.isOnBack()) {
|
| 455 | this.onBackAmount = Math.min(1.0F, this.onBackAmount + 0.15F);
|
| 456 | } else {
|
| 457 | this.onBackAmount = Math.max(0.0F, this.onBackAmount - 0.19F);
|
| 458 | }
|
| 459 | }
|
| 460 |
|
| 461 | private void updateRollAmount() {
|
| 462 | this.rollAmountO = this.rollAmount;
|
| 463 | if (this.isRolling()) {
|
| 464 | this.rollAmount = Math.min(1.0F, this.rollAmount + 0.15F);
|
| 465 | } else {
|
| 466 | this.rollAmount = Math.max(0.0F, this.rollAmount - 0.19F);
|
| 467 | }
|
| 468 | }
|
| 469 |
|
| 470 | public float getSitAmount(final float a) {
|
| 471 | return Mth.lerp(a, this.sitAmountO, this.sitAmount);
|
| 472 | }
|
| 473 |
|
| 474 | public float getLieOnBackAmount(final float a) {
|
| 475 | return Mth.lerp(a, this.onBackAmountO, this.onBackAmount);
|
| 476 | }
|
| 477 |
|
| 478 | public float getRollAmount(final float a) {
|
| 479 | return Mth.lerp(a, this.rollAmountO, this.rollAmount);
|
| 480 | }
|
| 481 |
|
| 482 | private void handleRoll() {
|
| 483 | this.rollCounter++;
|
| 484 | if (this.rollCounter > 32) {
|
| 485 | this.roll(false);
|
| 486 | } else {
|
| 487 | if (!this.level().isClientSide()) {
|
| 488 | Vec3 movement = this.getDeltaMovement();
|
| 489 | if (this.rollCounter == 1) {
|
| 490 | float angle = this.getYRot() * (float) (Math.PI / 180.0);
|
| 491 | float multiplier = this.isBaby() ? 0.1F : 0.2F;
|
| 492 | this.rollDelta = new Vec3(movement.x + -Mth.sin(angle) * multiplier, 0.0, movement.z + Mth.cos(angle) * multiplier);
|
| 493 | this.setDeltaMovement(this.rollDelta.add(0.0, 0.27, 0.0));
|
| 494 | } else if (this.rollCounter != 7.0F && this.rollCounter != 15.0F && this.rollCounter != 23.0F) {
|
| 495 | this.setDeltaMovement(this.rollDelta.x, movement.y, this.rollDelta.z);
|
| 496 | } else {
|
| 497 | this.setDeltaMovement(0.0, this.onGround() ? 0.27 : movement.y, 0.0);
|
| 498 | }
|
| 499 | }
|
| 500 | }
|
| 501 | }
|
| 502 |
|
| 503 | private void afterSneeze() {
|
| 504 | Vec3 movement = this.getDeltaMovement();
|
| 505 | Level level = this.level();
|
| 506 | level.addParticle(
|
| 507 | ParticleTypes.SNEEZE,
|
| 508 | this.getX() - (this.getBbWidth() + 1.0F) * 0.5 * Mth.sin(this.yBodyRot * (float) (Math.PI / 180.0)),
|
| 509 | this.getEyeY() - 0.1F,
|
| 510 | this.getZ() + (this.getBbWidth() + 1.0F) * 0.5 * Mth.cos(this.yBodyRot * (float) (Math.PI / 180.0)),
|
| 511 | movement.x,
|
| 512 | 0.0,
|
| 513 | movement.z
|
| 514 | );
|
| 515 | this.playSound(SoundEvents.PANDA_SNEEZE, 1.0F, 1.0F);
|
| 516 |
|
| 517 | for (Panda panda : level.getEntitiesOfClass(Panda.class, this.getBoundingBox().inflate(10.0))) {
|
| 518 | if (!panda.isBaby() && panda.onGround() && !panda.isInWater() && panda.canPerformAction()) {
|
| 519 | panda.jumpFromGround();
|
| 520 | }
|
| 521 | }
|
| 522 |
|
| 523 | if (this.level() instanceof ServerLevel serverLevel && serverLevel.getGameRules().get(GameRules.MOB_DROPS)) {
|
| 524 | this.dropFromGiftLootTable(serverLevel, BuiltInLootTables.PANDA_SNEEZE, this::spawnAtLocation);
|
| 525 | }
|
| 526 | }
|
| 527 |
|
| 528 | @Override
|
| 529 | protected void pickUpItem(final ServerLevel level, final ItemEntity entity) {
|
| 530 | if (this.getItemBySlot(EquipmentSlot.MAINHAND).isEmpty() && canPickUpAndEat(entity)) {
|
| 531 | this.onItemPickup(entity);
|
| 532 | ItemStack itemStack = entity.getItem();
|
| 533 | this.setItemSlot(EquipmentSlot.MAINHAND, itemStack);
|
| 534 | this.setGuaranteedDrop(EquipmentSlot.MAINHAND);
|
| 535 | this.take(entity, itemStack.getCount());
|
| 536 | entity.discard();
|
| 537 | }
|
| 538 | }
|
| 539 |
|
| 540 | @Override
|
| 541 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 542 | this.sit(false);
|
| 543 | return super.hurtServer(level, source, damage);
|
| 544 | }
|
| 545 |
|
| 546 | @Override
|
| 547 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 548 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
|
| 549 | ) {
|
| 550 | RandomSource random = level.getRandom();
|
| 551 | this.setMainGene(Panda.Gene.getRandom(random));
|
| 552 | this.setHiddenGene(Panda.Gene.getRandom(random));
|
| 553 | this.setAttributes();
|
| 554 | if (groupData == null) {
|
| 555 | groupData = new AgeableMob.AgeableMobGroupData(0.2F);
|
| 556 | }
|
| 557 |
|
| 558 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 559 | }
|
| 560 |
|
| 561 | public void setGeneFromParents(final Panda parent1, final @Nullable Panda parent2) {
|
| 562 | if (parent2 == null) {
|
| 563 | if (this.random.nextBoolean()) {
|
| 564 | this.setMainGene(parent1.getOneOfGenesRandomly());
|
| 565 | this.setHiddenGene(Panda.Gene.getRandom(this.random));
|
| 566 | } else {
|
| 567 | this.setMainGene(Panda.Gene.getRandom(this.random));
|
| 568 | this.setHiddenGene(parent1.getOneOfGenesRandomly());
|
| 569 | }
|
| 570 | } else if (this.random.nextBoolean()) {
|
| 571 | this.setMainGene(parent1.getOneOfGenesRandomly());
|
| 572 | this.setHiddenGene(parent2.getOneOfGenesRandomly());
|
| 573 | } else {
|
| 574 | this.setMainGene(parent2.getOneOfGenesRandomly());
|
| 575 | this.setHiddenGene(parent1.getOneOfGenesRandomly());
|
| 576 | }
|
| 577 |
|
| 578 | if (this.random.nextInt(32) == 0) {
|
| 579 | this.setMainGene(Panda.Gene.getRandom(this.random));
|
| 580 | }
|
| 581 |
|
| 582 | if (this.random.nextInt(32) == 0) {
|
| 583 | this.setHiddenGene(Panda.Gene.getRandom(this.random));
|
| 584 | }
|
| 585 | }
|
| 586 |
|
| 587 | private Panda.Gene getOneOfGenesRandomly() {
|
| 588 | return this.random.nextBoolean() ? this.getMainGene() : this.getHiddenGene();
|
| 589 | }
|
| 590 |
|
| 591 | public void setAttributes() {
|
| 592 | if (this.isWeak()) {
|
| 593 | this.getAttribute(Attributes.MAX_HEALTH).setBaseValue(10.0);
|
| 594 | }
|
| 595 |
|
| 596 | if (this.isLazy()) {
|
| 597 | this.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.07F);
|
| 598 | }
|
| 599 | }
|
| 600 |
|
| 601 | private void tryToSit() {
|
| 602 | if (!this.isInWater()) {
|
| 603 | this.setZza(0.0F);
|
| 604 | this.getNavigation().stop();
|
| 605 | this.sit(true);
|
| 606 | }
|
| 607 | }
|
| 608 |
|
| 609 | @Override
|
| 610 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 611 | ItemStack interactionItemStack = player.getItemInHand(hand);
|
| 612 | if (this.isScared()) {
|
| 613 | return InteractionResult.PASS;
|
| 614 | }
|
| 615 |
|
| 616 | if (this.isOnBack()) {
|
| 617 | this.setOnBack(false);
|
| 618 | return InteractionResult.SUCCESS;
|
| 619 | }
|
| 620 |
|
| 621 | if (this.isFood(interactionItemStack)) {
|
| 622 | if (this.getTarget() != null) {
|
| 623 | this.gotBamboo = true;
|
| 624 | }
|
| 625 |
|
| 626 | if (this.canAgeUp()) {
|
| 627 | this.usePlayerItem(player, hand, interactionItemStack);
|
| 628 | this.ageUp((int)(-this.getAge() / 20 * 0.1F), true);
|
| 629 | } else {
|
| 630 | if (this.isBaby()) {
|
| 631 | return InteractionResult.PASS;
|
| 632 | }
|
| 633 |
|
| 634 | if (!this.level().isClientSide() && this.getAge() == 0 && this.canFallInLove()) {
|
| 635 | this.usePlayerItem(player, hand, interactionItemStack);
|
| 636 | this.setInLove(player);
|
| 637 | } else {
|
| 638 | if (!(this.level() instanceof ServerLevel level) || this.isSitting() || this.isInWater()) {
|
| 639 | return InteractionResult.PASS;
|
| 640 | }
|
| 641 |
|
| 642 | this.tryToSit();
|
| 643 | this.eat(true);
|
| 644 | ItemStack pandasCurrentItem = this.getItemBySlot(EquipmentSlot.MAINHAND);
|
| 645 | if (!pandasCurrentItem.isEmpty() && !player.hasInfiniteMaterials()) {
|
| 646 | this.spawnAtLocation(level, pandasCurrentItem);
|
| 647 | }
|
| 648 |
|
| 649 | this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(interactionItemStack.getItem(), 1));
|
| 650 | this.usePlayerItem(player, hand, interactionItemStack);
|
| 651 | }
|
| 652 | }
|
| 653 |
|
| 654 | return InteractionResult.SUCCESS_SERVER;
|
| 655 | } else {
|
| 656 | return this.isBaby() && player.isHolding(Items.GOLDEN_DANDELION) ? super.mobInteract(player, hand) : InteractionResult.PASS;
|
| 657 | }
|
| 658 | }
|
| 659 |
|
| 660 | @Override
|
| 661 | protected @Nullable SoundEvent getAmbientSound() {
|
| 662 | if (this.isAggressive()) {
|
| 663 | return SoundEvents.PANDA_AGGRESSIVE_AMBIENT;
|
| 664 | } else {
|
| 665 | return this.isWorried() ? SoundEvents.PANDA_WORRIED_AMBIENT : SoundEvents.PANDA_AMBIENT;
|
| 666 | }
|
| 667 | }
|
| 668 |
|
| 669 | @Override
|
| 670 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 671 | this.playSound(SoundEvents.PANDA_STEP, 0.15F, 1.0F);
|
| 672 | }
|
| 673 |
|
| 674 | @Override
|
| 675 | public boolean isFood(final ItemStack itemStack) {
|
| 676 | return itemStack.is(ItemTags.PANDA_FOOD);
|
| 677 | }
|
| 678 |
|
| 679 | @Override
|
| 680 | protected @Nullable SoundEvent getDeathSound() {
|
| 681 | return SoundEvents.PANDA_DEATH;
|
| 682 | }
|
| 683 |
|
| 684 | @Override
|
| 685 | protected @Nullable SoundEvent getHurtSound(final DamageSource source) {
|
| 686 | return SoundEvents.PANDA_HURT;
|
| 687 | }
|
| 688 |
|
| 689 | public boolean canPerformAction() {
|
| 690 | return !this.isOnBack() && !this.isScared() && !this.isEating() && !this.isRolling() && !this.isSitting();
|
| 691 | }
|
| 692 |
|
| 693 | @Override
|
| 694 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 695 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 696 | }
|
| 697 |
|
| 698 | private static boolean canPickUpAndEat(final ItemEntity entity) {
|
| 699 | return entity.getItem().is(ItemTags.PANDA_EATS_FROM_GROUND) && entity.isAlive() && !entity.hasPickUpDelay();
|
| 700 | }
|
| 701 |
|
| 702 | public enum Gene implements StringRepresentable {
|
| 703 | NORMAL(0, "normal", false),
|
| 704 | LAZY(1, "lazy", false),
|
| 705 | WORRIED(2, "worried", false),
|
| 706 | PLAYFUL(3, "playful", false),
|
| 707 | BROWN(4, "brown", true),
|
| 708 | WEAK(5, "weak", true),
|
| 709 | AGGRESSIVE(6, "aggressive", false);
|
| 710 |
|
| 711 | public static final Codec<Panda.Gene> CODEC = StringRepresentable.fromEnum(Panda.Gene::values);
|
| 712 | private static final IntFunction<Panda.Gene> BY_ID = ByIdMap.continuous(Panda.Gene::getId, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
|
| 713 | private static final int MAX_GENE = 6;
|
| 714 | private final int id;
|
| 715 | private final String name;
|
| 716 | private final boolean isRecessive;
|
| 717 |
|
| 718 | Gene(final int id, final String name, final boolean isRecessive) {
|
| 719 | this.id = id;
|
| 720 | this.name = name;
|
| 721 | this.isRecessive = isRecessive;
|
| 722 | }
|
| 723 |
|
| 724 | public int getId() {
|
| 725 | return this.id;
|
| 726 | }
|
| 727 |
|
| 728 | @Override
|
| 729 | public String getSerializedName() {
|
| 730 | return this.name;
|
| 731 | }
|
| 732 |
|
| 733 | public boolean isRecessive() {
|
| 734 | return this.isRecessive;
|
| 735 | }
|
| 736 |
|
| 737 | private static Panda.Gene getVariantFromGenes(final Panda.Gene mainGene, final Panda.Gene hiddenGene) {
|
| 738 | if (mainGene.isRecessive()) {
|
| 739 | return mainGene == hiddenGene ? mainGene : NORMAL;
|
| 740 | } else {
|
| 741 | return mainGene;
|
| 742 | }
|
| 743 | }
|
| 744 |
|
| 745 | public static Panda.Gene byId(final int id) {
|
| 746 | return BY_ID.apply(id);
|
| 747 | }
|
| 748 |
|
| 749 | public static Panda.Gene getRandom(final RandomSource random) {
|
| 750 | int nextInt = random.nextInt(16);
|
| 751 | if (nextInt == 0) {
|
| 752 | return LAZY;
|
| 753 | } else if (nextInt == 1) {
|
| 754 | return WORRIED;
|
| 755 | } else if (nextInt == 2) {
|
| 756 | return PLAYFUL;
|
| 757 | } else if (nextInt == 4) {
|
| 758 | return AGGRESSIVE;
|
| 759 | } else if (nextInt < 9) {
|
| 760 | return WEAK;
|
| 761 | } else {
|
| 762 | return nextInt < 11 ? BROWN : NORMAL;
|
| 763 | }
|
| 764 | }
|
| 765 | }
|
| 766 |
|
| 767 | private static class PandaAttackGoal extends MeleeAttackGoal {
|
| 768 | private final Panda panda;
|
| 769 |
|
| 770 | public PandaAttackGoal(final Panda mob, final double speedModifier, final boolean trackTarget) {
|
| 771 | super(mob, speedModifier, trackTarget);
|
| 772 | this.panda = mob;
|
| 773 | }
|
| 774 |
|
| 775 | @Override
|
| 776 | public boolean canUse() {
|
| 777 | return this.panda.canPerformAction() && super.canUse();
|
| 778 | }
|
| 779 | }
|
| 780 |
|
| 781 | private static class PandaAvoidGoal<T extends LivingEntity> extends AvoidEntityGoal<T> {
|
| 782 | private final Panda panda;
|
| 783 |
|
| 784 | public PandaAvoidGoal(final Panda panda, final Class<T> avoidClass, final float maxDist, final double walkSpeedModifier, final double sprintSpeedModifier) {
|
| 785 | super(panda, avoidClass, maxDist, walkSpeedModifier, sprintSpeedModifier, EntitySelector.NO_SPECTATORS);
|
| 786 | this.panda = panda;
|
| 787 | }
|
| 788 |
|
| 789 | @Override
|
| 790 | public boolean canUse() {
|
| 791 | return this.panda.isWorried() && this.panda.canPerformAction() && super.canUse();
|
| 792 | }
|
| 793 | }
|
| 794 |
|
| 795 | private static class PandaBreedGoal extends BreedGoal {
|
| 796 | private final Panda panda;
|
| 797 | private int unhappyCooldown;
|
| 798 |
|
| 799 | public PandaBreedGoal(final Panda panda, final double speedModifier) {
|
| 800 | super(panda, speedModifier);
|
| 801 | this.panda = panda;
|
| 802 | }
|
| 803 |
|
| 804 | @Override
|
| 805 | public boolean canUse() {
|
| 806 | if (!super.canUse() || this.panda.getUnhappyCounter() != 0) {
|
| 807 | return false;
|
| 808 | }
|
| 809 |
|
| 810 | if (!this.canFindBamboo()) {
|
| 811 | if (this.unhappyCooldown <= this.panda.tickCount) {
|
| 812 | this.panda.setUnhappyCounter(32);
|
| 813 | this.unhappyCooldown = this.panda.tickCount + 600;
|
| 814 | if (this.panda.isEffectiveAi()) {
|
| 815 | Player player = this.level.getNearestPlayer(Panda.BREED_TARGETING, this.panda);
|
| 816 | this.panda.lookAtPlayerGoal.setTarget(player);
|
| 817 | }
|
| 818 | }
|
| 819 |
|
| 820 | return false;
|
| 821 | } else {
|
| 822 | return true;
|
| 823 | }
|
| 824 | }
|
| 825 |
|
| 826 | private boolean canFindBamboo() {
|
| 827 | BlockPos pandaPos = this.panda.blockPosition();
|
| 828 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 829 |
|
| 830 | for (int yOff = 0; yOff < 3; yOff++) {
|
| 831 | for (int r = 0; r < 8; r++) {
|
| 832 | for (int x = 0; x <= r; x = x > 0 ? -x : 1 - x) {
|
| 833 | for (int z = x < r && x > -r ? r : 0; z <= r; z = z > 0 ? -z : 1 - z) {
|
| 834 | pos.setWithOffset(pandaPos, x, yOff, z);
|
| 835 | if (this.level.getBlockState(pos).is(Blocks.BAMBOO)) {
|
| 836 | return true;
|
| 837 | }
|
| 838 | }
|
| 839 | }
|
| 840 | }
|
| 841 | }
|
| 842 |
|
| 843 | return false;
|
| 844 | }
|
| 845 | }
|
| 846 |
|
| 847 | private static class PandaHurtByTargetGoal extends HurtByTargetGoal {
|
| 848 | private final Panda panda;
|
| 849 |
|
| 850 | public PandaHurtByTargetGoal(final Panda mob, final Class<?>... ignoreDamageFromTheseTypes) {
|
| 851 | super(mob, ignoreDamageFromTheseTypes);
|
| 852 | this.panda = mob;
|
| 853 | }
|
| 854 |
|
| 855 | @Override
|
| 856 | public boolean canContinueToUse() {
|
| 857 | if (!this.panda.gotBamboo && !this.panda.didBite) {
|
| 858 | return super.canContinueToUse();
|
| 859 | }
|
| 860 |
|
| 861 | this.panda.setTarget(null);
|
| 862 | return false;
|
| 863 | }
|
| 864 |
|
| 865 | @Override
|
| 866 | protected void alertOther(final Mob other, final LivingEntity hurtByMob) {
|
| 867 | if (other instanceof Panda && other.isAggressive()) {
|
| 868 | other.setTarget(hurtByMob);
|
| 869 | }
|
| 870 | }
|
| 871 | }
|
| 872 |
|
| 873 | private static class PandaLieOnBackGoal extends Goal {
|
| 874 | private final Panda panda;
|
| 875 | private int cooldown;
|
| 876 |
|
| 877 | public PandaLieOnBackGoal(final Panda panda) {
|
| 878 | this.panda = panda;
|
| 879 | }
|
| 880 |
|
| 881 | @Override
|
| 882 | public boolean canUse() {
|
| 883 | return this.cooldown < this.panda.tickCount && this.panda.isLazy() && this.panda.canPerformAction() && this.panda.random.nextInt(reducedTickDelay(400)) == 1;
|
| 884 | }
|
| 885 |
|
| 886 | @Override
|
| 887 | public boolean canContinueToUse() {
|
| 888 | return !this.panda.isInWater() && (this.panda.isLazy() || this.panda.random.nextInt(reducedTickDelay(600)) != 1)
|
| 889 | ? this.panda.random.nextInt(reducedTickDelay(2000)) != 1
|
| 890 | : false;
|
| 891 | }
|
| 892 |
|
| 893 | @Override
|
| 894 | public void start() {
|
| 895 | this.panda.setOnBack(true);
|
| 896 | this.cooldown = 0;
|
| 897 | }
|
| 898 |
|
| 899 | @Override
|
| 900 | public void stop() {
|
| 901 | this.panda.setOnBack(false);
|
| 902 | this.cooldown = this.panda.tickCount + 200;
|
| 903 | }
|
| 904 | }
|
| 905 |
|
| 906 | private static class PandaLookAtPlayerGoal extends LookAtPlayerGoal {
|
| 907 | private final Panda panda;
|
| 908 |
|
| 909 | public PandaLookAtPlayerGoal(final Panda mob, final Class<? extends LivingEntity> lookAtType, final float lookDistance) {
|
| 910 | super(mob, lookAtType, lookDistance);
|
| 911 | this.panda = mob;
|
| 912 | }
|
| 913 |
|
| 914 | public void setTarget(final LivingEntity entity) {
|
| 915 | this.lookAt = entity;
|
| 916 | }
|
| 917 |
|
| 918 | @Override
|
| 919 | public boolean canContinueToUse() {
|
| 920 | return this.lookAt != null && super.canContinueToUse();
|
| 921 | }
|
| 922 |
|
| 923 | @Override
|
| 924 | public boolean canUse() {
|
| 925 | if (this.mob.getRandom().nextFloat() >= this.probability) {
|
| 926 | return false;
|
| 927 | }
|
| 928 |
|
| 929 | if (this.lookAt == null) {
|
| 930 | ServerLevel level = getServerLevel(this.mob);
|
| 931 | if (this.lookAtType == Player.class) {
|
| 932 | this.lookAt = level.getNearestPlayer(this.lookAtContext, this.mob, this.mob.getX(), this.mob.getEyeY(), this.mob.getZ());
|
| 933 | } else {
|
| 934 | this.lookAt = level.getNearestEntity(
|
| 935 | this.mob.level().getEntitiesOfClass(this.lookAtType, this.mob.getBoundingBox().inflate(this.lookDistance, 3.0, this.lookDistance), entity -> true),
|
| 936 | this.lookAtContext,
|
| 937 | this.mob,
|
| 938 | this.mob.getX(),
|
| 939 | this.mob.getEyeY(),
|
| 940 | this.mob.getZ()
|
| 941 | );
|
| 942 | }
|
| 943 | }
|
| 944 |
|
| 945 | return this.panda.canPerformAction() && this.lookAt != null;
|
| 946 | }
|
| 947 |
|
| 948 | @Override
|
| 949 | public void tick() {
|
| 950 | if (this.lookAt != null) {
|
| 951 | super.tick();
|
| 952 | }
|
| 953 | }
|
| 954 | }
|
| 955 |
|
| 956 | private static class PandaMoveControl<T extends Panda> extends MoveControl<T> {
|
| 957 | public PandaMoveControl(final T mob) {
|
| 958 | super(mob);
|
| 959 | }
|
| 960 |
|
| 961 | @Override
|
| 962 | public void tick() {
|
| 963 | if (this.mob.canPerformAction()) {
|
| 964 | super.tick();
|
| 965 | }
|
| 966 | }
|
| 967 | }
|
| 968 |
|
| 969 | private static class PandaPanicGoal extends PanicGoal {
|
| 970 | private final Panda panda;
|
| 971 |
|
| 972 | public PandaPanicGoal(final Panda mob, final double speedModifier) {
|
| 973 | super(mob, speedModifier, DamageTypeTags.PANIC_ENVIRONMENTAL_CAUSES);
|
| 974 | this.panda = mob;
|
| 975 | }
|
| 976 |
|
| 977 | @Override
|
| 978 | public boolean canContinueToUse() {
|
| 979 | if (this.panda.isSitting()) {
|
| 980 | this.panda.getNavigation().stop();
|
| 981 | return false;
|
| 982 | } else {
|
| 983 | return super.canContinueToUse();
|
| 984 | }
|
| 985 | }
|
| 986 | }
|
| 987 |
|
| 988 | private static class PandaRollGoal extends Goal {
|
| 989 | private final Panda panda;
|
| 990 |
|
| 991 | public PandaRollGoal(final Panda panda) {
|
| 992 | this.panda = panda;
|
| 993 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK, Goal.Flag.JUMP));
|
| 994 | }
|
| 995 |
|
| 996 | @Override
|
| 997 | public boolean canUse() {
|
| 998 | if ((this.panda.isBaby() || this.panda.isPlayful()) && this.panda.onGround()) {
|
| 999 | if (!this.panda.canPerformAction()) {
|
| 1000 | return false;
|
| 1001 | } else {
|
| 1002 | float angle = this.panda.getYRot() * (float) (Math.PI / 180.0);
|
| 1003 | float xDir = -Mth.sin(angle);
|
| 1004 | float zDir = Mth.cos(angle);
|
| 1005 | int xStep = Math.abs(xDir) > 0.5 ? Mth.sign(xDir) : 0;
|
| 1006 | int zStep = Math.abs(zDir) > 0.5 ? Mth.sign(zDir) : 0;
|
| 1007 | if (this.panda.level().getBlockState(this.panda.blockPosition().offset(xStep, -1, zStep)).isAir()) {
|
| 1008 | return true;
|
| 1009 | } else {
|
| 1010 | return this.panda.isPlayful() && this.panda.random.nextInt(reducedTickDelay(60)) == 1 ? true : this.panda.random.nextInt(reducedTickDelay(500)) == 1;
|
| 1011 | }
|
| 1012 | }
|
| 1013 | } else {
|
| 1014 | return false;
|
| 1015 | }
|
| 1016 | }
|
| 1017 |
|
| 1018 | @Override
|
| 1019 | public boolean canContinueToUse() {
|
| 1020 | return false;
|
| 1021 | }
|
| 1022 |
|
| 1023 | @Override
|
| 1024 | public void start() {
|
| 1025 | this.panda.roll(true);
|
| 1026 | }
|
| 1027 |
|
| 1028 | @Override
|
| 1029 | public boolean isInterruptable() {
|
| 1030 | return false;
|
| 1031 | }
|
| 1032 | }
|
| 1033 |
|
| 1034 | private class PandaSitGoal extends Goal {
|
| 1035 | private int cooldown;
|
| 1036 |
|
| 1037 | public PandaSitGoal() {
|
| 1038 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 1039 | }
|
| 1040 |
|
| 1041 | @Override
|
| 1042 | public boolean canUse() {
|
| 1043 | if (this.cooldown > Panda.this.tickCount
|
| 1044 | || Panda.this.isBaby()
|
| 1045 | || Panda.this.isInWater()
|
| 1046 | || !Panda.this.canPerformAction()
|
| 1047 | || Panda.this.getUnhappyCounter() > 0) {
|
| 1048 | return false;
|
| 1049 | } else {
|
| 1050 | return !Panda.this.getItemBySlot(EquipmentSlot.MAINHAND).isEmpty()
|
| 1051 | ? true
|
| 1052 | : !Panda.this.level().getEntitiesOfClass(ItemEntity.class, Panda.this.getBoundingBox().inflate(6.0, 6.0, 6.0), Panda::canPickUpAndEat).isEmpty();
|
| 1053 | }
|
| 1054 | }
|
| 1055 |
|
| 1056 | @Override
|
| 1057 | public boolean canContinueToUse() {
|
| 1058 | return !Panda.this.isInWater() && (Panda.this.isLazy() || Panda.this.random.nextInt(reducedTickDelay(600)) != 1)
|
| 1059 | ? Panda.this.random.nextInt(reducedTickDelay(2000)) != 1
|
| 1060 | : false;
|
| 1061 | }
|
| 1062 |
|
| 1063 | @Override
|
| 1064 | public void tick() {
|
| 1065 | if (!Panda.this.isSitting() && !Panda.this.getItemBySlot(EquipmentSlot.MAINHAND).isEmpty()) {
|
| 1066 | Panda.this.tryToSit();
|
| 1067 | }
|
| 1068 | }
|
| 1069 |
|
| 1070 | @Override
|
| 1071 | public void start() {
|
| 1072 | if (Panda.this.getItemBySlot(EquipmentSlot.MAINHAND).isEmpty()) {
|
| 1073 | List<ItemEntity> items = Panda.this.level()
|
| 1074 | .getEntitiesOfClass(ItemEntity.class, Panda.this.getBoundingBox().inflate(8.0, 8.0, 8.0), Panda::canPickUpAndEat);
|
| 1075 | if (!items.isEmpty()) {
|
| 1076 | Panda.this.getNavigation().moveTo(items.getFirst(), 1.2F);
|
| 1077 | }
|
| 1078 | } else {
|
| 1079 | Panda.this.tryToSit();
|
| 1080 | }
|
| 1081 |
|
| 1082 | this.cooldown = 0;
|
| 1083 | }
|
| 1084 |
|
| 1085 | @Override
|
| 1086 | public void stop() {
|
| 1087 | ItemStack itemStack = Panda.this.getItemBySlot(EquipmentSlot.MAINHAND);
|
| 1088 | if (!itemStack.isEmpty()) {
|
| 1089 | Panda.this.spawnAtLocation(getServerLevel(Panda.this.level()), itemStack);
|
| 1090 | Panda.this.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
|
| 1091 | int waitSeconds = Panda.this.isLazy() ? Panda.this.random.nextInt(50) + 10 : Panda.this.random.nextInt(150) + 10;
|
| 1092 | this.cooldown = Panda.this.tickCount + waitSeconds * 20;
|
| 1093 | }
|
| 1094 |
|
| 1095 | Panda.this.sit(false);
|
| 1096 | }
|
| 1097 | }
|
| 1098 |
|
| 1099 | private static class PandaSneezeGoal extends Goal {
|
| 1100 | private final Panda panda;
|
| 1101 |
|
| 1102 | public PandaSneezeGoal(final Panda panda) {
|
| 1103 | this.panda = panda;
|
| 1104 | }
|
| 1105 |
|
| 1106 | @Override
|
| 1107 | public boolean canUse() {
|
| 1108 | if (this.panda.isBaby() && this.panda.canPerformAction()) {
|
| 1109 | return this.panda.isWeak() && this.panda.random.nextInt(reducedTickDelay(500)) == 1 ? true : this.panda.random.nextInt(reducedTickDelay(6000)) == 1;
|
| 1110 | } else {
|
| 1111 | return false;
|
| 1112 | }
|
| 1113 | }
|
| 1114 |
|
| 1115 | @Override
|
| 1116 | public boolean canContinueToUse() {
|
| 1117 | return false;
|
| 1118 | }
|
| 1119 |
|
| 1120 | @Override
|
| 1121 | public void start() {
|
| 1122 | this.panda.sneeze(true);
|
| 1123 | }
|
| 1124 | }
|
| 1125 | }
|
| 1126 |
|