Files
MasterWild/src/main/java/ovh/glitchlabs/masterwild/entity/custom/RoosterEntity.java
T

117 lines
4.1 KiB
Java

package ovh.glitchlabs.masterwild.entity.custom;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.AgeableMob;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.goal.*;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.pathfinder.PathType;
import org.jetbrains.annotations.Nullable;
import ovh.glitchlabs.masterwild.Sounds.ModSounds;
import ovh.glitchlabs.masterwild.entity.ModEntities;
public class RoosterEntity extends Animal {
private long lastDay = -1;
private int crowDelay = -1;
public RoosterEntity(EntityType<? extends Animal> entityType, Level level) {
super(entityType, level);
this.setPathfindingMalus(PathType.WATER, 0.0F);
}
public static AttributeSupplier.Builder createAttributes() {
return Mob.createMobAttributes()
.add(Attributes.MOVEMENT_SPEED, 0.3)
.add(Attributes.MAX_HEALTH, 5)
.add(Attributes.ARMOR, 0)
.add(Attributes.ATTACK_DAMAGE, 0)
.add(Attributes.FOLLOW_RANGE, 16)
.add(Attributes.STEP_HEIGHT, 0.6);
}
@Override
protected void registerGoals() {
this.goalSelector.addGoal(0, new FloatGoal(this));
this.goalSelector.addGoal(1, new PanicGoal(this, 1.4));
this.goalSelector.addGoal(2, new BreedGoal(this, 1.0));
this.goalSelector.addGoal(3, new TemptGoal(this, 1.0, stack -> stack.is(ItemTags.CHICKEN_FOOD), false));
this.goalSelector.addGoal(4, new FollowParentGoal(this, 1.1));
this.goalSelector.addGoal(5, new WaterAvoidingRandomStrollGoal(this, 1.0));
this.goalSelector.addGoal(6, new LookAtPlayerGoal(this, Player.class, 6.0F));
this.goalSelector.addGoal(7, new RandomLookAroundGoal(this));
}
@Override
protected SoundEvent getAmbientSound() {
return SoundEvents.CHICKEN_AMBIENT;
}
@Override
protected SoundEvent getHurtSound(DamageSource damageSource) {
return SoundEvents.CHICKEN_HURT;
}
@Override
protected SoundEvent getDeathSound() {
return SoundEvents.CHICKEN_DEATH;
}
@Override
protected void playStepSound(BlockPos blockPos, BlockState blockState) {
this.playSound(SoundEvents.CHICKEN_STEP, 0.15F, 1.0F);
}
@Override
public boolean isFood(ItemStack stack) {
return stack.is(ItemTags.CHICKEN_FOOD);
}
@Nullable
@Override
public AgeableMob getBreedOffspring(ServerLevel level, AgeableMob partner) {
return ModEntities.ROOSTER.get().create(level);
}
@Override
protected void dropCustomDeathLoot(ServerLevel serverLevel, DamageSource source, boolean recentlyHit) {
super.dropCustomDeathLoot(serverLevel, source, recentlyHit);
this.spawnAtLocation(new ItemStack(Items.CHICKEN));
}
@Override
public void tick() {
super.tick();
if (this.level().isClientSide()) {
return;
}
long day = this.level().getDayTime() / 24000L;
if (this.lastDay != -1 && day != this.lastDay && !this.isBaby()) {
this.crowDelay = this.random.nextInt(60);
}
this.lastDay = day;
if (this.crowDelay >= 0 && --this.crowDelay < 0) {
if (this.isAlive() && !this.level().isRaining() && !this.level().isThundering()) {
this.crow();
}
}
}
private void crow() {
this.playSound(ModSounds.ROOSTER_CROW.get(), 2.0f, 0.8f + this.random.nextFloat() * 0.4f);
}
}