minecraft-source/src/net/minecraft/world/entity/animal/Animal.java

199 lines
6.2 KiB
Java

package net.minecraft.world.entity.animal;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import javax.annotation.Nullable;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import java.util.Random;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.BlockPos;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;
import java.util.UUID;
import net.minecraft.world.entity.AgableMob;
public abstract class Animal extends AgableMob {
private int inLove;
private UUID loveCause;
protected Animal(final EntityType<? extends Animal> akr, final Level bjt) {
super(akr, bjt);
}
@Override
protected void customServerAiStep() {
if (this.getAge() != 0) {
this.inLove = 0;
}
super.customServerAiStep();
}
@Override
public void aiStep() {
super.aiStep();
if (this.getAge() != 0) {
this.inLove = 0;
}
if (this.inLove > 0) {
--this.inLove;
if (this.inLove % 10 == 0) {
final double double2 = this.random.nextGaussian() * 0.02;
final double double3 = this.random.nextGaussian() * 0.02;
final double double4 = this.random.nextGaussian() * 0.02;
this.level.addParticle(ParticleTypes.HEART, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), double2, double3, double4);
}
}
}
@Override
public boolean hurt(final DamageSource ajw, final float float2) {
if (this.isInvulnerableTo(ajw)) {
return false;
}
this.inLove = 0;
return super.hurt(ajw, float2);
}
@Override
public float getWalkTargetValue(final BlockPos fk, final LevelReader bjw) {
if (bjw.getBlockState(fk.below()).getBlock() == Blocks.GRASS_BLOCK) {
return 10.0f;
}
return bjw.getBrightness(fk) - 0.5f;
}
@Override
public void addAdditionalSaveData(final CompoundTag jt) {
super.addAdditionalSaveData(jt);
jt.putInt("InLove", this.inLove);
if (this.loveCause != null) {
jt.putUUID("LoveCause", this.loveCause);
}
}
@Override
public double getRidingHeight() {
return 0.14;
}
@Override
public void readAdditionalSaveData(final CompoundTag jt) {
super.readAdditionalSaveData(jt);
this.inLove = jt.getInt("InLove");
this.loveCause = (jt.hasUUID("LoveCause") ? jt.getUUID("LoveCause") : null);
}
public static boolean checkAnimalSpawnRules(final EntityType<? extends Animal> akr, final LevelAccessor bju, final MobSpawnType akz, final BlockPos fk, final Random random) {
return bju.getBlockState(fk.below()).getBlock() == Blocks.GRASS_BLOCK && bju.getRawBrightness(fk, 0) > 8;
}
@Override
public int getAmbientSoundInterval() {
return 120;
}
@Override
public boolean removeWhenFarAway(final double double1) {
return false;
}
@Override
protected int getExperienceReward(final Player ayg) {
return 1 + this.level.random.nextInt(3);
}
public boolean isFood(final ItemStack bek) {
return bek.getItem() == Items.WHEAT;
}
@Override
public boolean mobInteract(final Player ayg, final InteractionHand ajh) {
final ItemStack bek4 = ayg.getItemInHand(ajh);
if (this.isFood(bek4)) {
if (!this.level.isClientSide && this.getAge() == 0 && this.canFallInLove()) {
this.usePlayerItem(ayg, bek4);
this.setInLove(ayg);
ayg.swing(ajh, true);
return true;
}
if (this.isBaby()) {
this.usePlayerItem(ayg, bek4);
this.ageUp((int)(-this.getAge() / 20 * 0.1f), true);
return true;
}
}
return super.mobInteract(ayg, ajh);
}
protected void usePlayerItem(final Player ayg, final ItemStack bek) {
if (!ayg.abilities.instabuild) {
bek.shrink(1);
}
}
public boolean canFallInLove() {
return this.inLove <= 0;
}
public void setInLove(@Nullable final Player ayg) {
this.inLove = 600;
if (ayg != null) {
this.loveCause = ayg.getUUID();
}
this.level.broadcastEntityEvent(this, (byte)18);
}
public void setInLoveTime(final int integer) {
this.inLove = integer;
}
@Nullable
public ServerPlayer getLoveCause() {
if (this.loveCause == null) {
return null;
}
final Player ayg2 = this.level.getPlayerByUUID(this.loveCause);
if (ayg2 instanceof ServerPlayer) {
return (ServerPlayer)ayg2;
}
return null;
}
public boolean isInLove() {
return this.inLove > 0;
}
public void resetLove() {
this.inLove = 0;
}
public boolean canMate(final Animal asz) {
return asz != this && asz.getClass() == this.getClass() && this.isInLove() && asz.isInLove();
}
@Override
public void handleEntityEvent(final byte byte1) {
if (byte1 == 18) {
for (int integer3 = 0; integer3 < 7; ++integer3) {
final double double4 = this.random.nextGaussian() * 0.02;
final double double5 = this.random.nextGaussian() * 0.02;
final double double6 = this.random.nextGaussian() * 0.02;
this.level.addParticle(ParticleTypes.HEART, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), double4, double5, double6);
}
}
else {
super.handleEntityEvent(byte1);
}
}
}