public class PenguinEntity extends Animal {
public PenguinEntity(EntityType<? extends Animal> pEntityType, Level pLevel) {
super(pEntityType, pLevel);
}
code
public final AnimationState idleAnimationState = new AnimationState();
private int idleAnimationTimeout = 0;
@Override
public void tick() {
super.tick();
if(this.level().isClientSide()) {
setupAnimationStates();
}
}
private void setupAnimationStates() {
if(this.idleAnimationTimeout <= 0) {
this.idleAnimationTimeout = this.random.nextInt(40) + 80;
this.idleAnimationState.start(this.tickCount);
} else {
--this.idleAnimationTimeout;
}
}
@Override
protected void updateWalkAnimation(float pPartialTick) {
float f;
if(this.getPose() == Pose.STANDING) {
f = Math.min(pPartialTick * 6F, 1f);
} else {
f = 0f;
}
this.walkAnimation.update(f, 0.2f);
}
@Override
protected void registerGoals() {
this.goalSelector.addGoal(0, new FloatGoal(this)); // Ensure they float in water
this.goalSelector.addGoal(1, new PanicGoal(this, 1.5D)); // Flee when hurt (priority 1)
this.goalSelector.addGoal(2, new BreedGoal(this, 1.15D));
this.goalSelector.addGoal(3, new TemptGoal(this, 1.2D,
Ingredient.of(Items.COD, Items.SALMON, Items.COOKED_SALMON, Items.COOKED_COD), false));
this.goalSelector.addGoal(4, new FollowParentGoal(this, 1.1D));
this.goalSelector.addGoal(5, new RandomSwimmingGoal(this, 1.0D, 80)); // Swim in water
this.goalSelector.addGoal(6, new WaterAvoidingRandomStrollGoal(this, 1.0D)); // Stroll on land
this.goalSelector.addGoal(7, new LookAtPlayerGoal(this, Player.class, 3f));
this.goalSelector.addGoal(8, new RandomLookAroundGoal(this));
}
public static AttributeSupplier.Builder createAttributes() {
return Animal.createLivingAttributes()
.add(Attributes.MAX_HEALTH, 10D)
.add(Attributes.MOVEMENT_SPEED, 0.15D)
.add(Attributes.FOLLOW_RANGE, 24D);
}
@Override
public void travel(Vec3 pTravelVector) {
if (this.isEffectiveAi() && this.isInWater()) {
this.moveRelative(0.1F, pTravelVector);
this.move(MoverType.SELF, this.getDeltaMovement());
this.setDeltaMovement(this.getDeltaMovement().scale(0.9D));
} else {
super.travel(pTravelVector);
}
}
@Nullable
@Override
public AgeableMob getBreedOffspring(ServerLevel pLevel, AgeableMob pOtherParent) {
return ModEntities.PENGUIN.get().create(pLevel);
}
@Override
public boolean isFood(ItemStack pStack) {
return pStack.is(Items.COD) || pStack.is(Items.SALMON) ||
pStack.is(Items.COOKED_COD) || pStack.is(Items.COOKED_SALMON);
}
@Nullable
@Override
protected SoundEvent getAmbientSound() {
return SoundEvents.DOLPHIN_AMBIENT;
}
@Nullable
@Override
protected SoundEvent getHurtSound(DamageSource pDamageSource) {
return SoundEvents.DOLPHIN_HURT;
}
@Nullable
@Override
protected SoundEvent getDeathSound() {
return SoundEvents.DOLPHIN_DEATH;
}
}
Assistant
Of course! To integrate the swimming animation, you need to make changes in both the PenguinEntity class (to manage the animation state) and the PenguinModel class (to play the animation).
Here are the necessary changes, starting with your PenguinEntity.
1. Changes for PenguinEntity.java
You need to add a new AnimationState for swimming and update your setupAnimationStates method to correctly switch between idle and swimming based on whether the penguin is in water.
java
package net.mattias.oceancritteria.entity.custom;
import net.mattias.oceancritteria.entity.ModEntities;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.*;
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.item.crafting.Ingredient;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;
public class PenguinEntity extends Animal {
public PenguinEntity(EntityType<? extends Animal> pEntityType, Level pLevel) {
super(pEntityType, pLevel);
}
public final AnimationState idleAnimationState = new AnimationState();
// ADD a new AnimationState for swimming
public final AnimationState swimAnimationState = new AnimationState();
private int idleAnimationTimeout = 0;
@Override
public void tick() {
super.tick();
if (this.level().isClientSide()) {
setupAnimationStates();
}
}
// UPDATE this method to handle swimming
private void setupAnimationStates() {
if (this.isInWater()) {
// If in water, start the swimming animation and stop the idle one
this.swimAnimationState.startIfStopped(this.tickCount);
this.idleAnimationState.stop();
} else {
// If on land, stop the swimming animation and handle the idle one
this.swimAnimationState.stop();
if (this.idleAnimationTimeout <= 0) {
this.idleAnimationTimeout = this.random.nextInt(40) + 80;
this.idleAnimationState.start(this.tickCount);
} else {
--this.idleAnimationTimeout;
}
}
}
@Override
protected void updateWalkAnimation(float pPartialTick) {
float f;
if (this.getPose() == Pose.STANDING) {
f = Math.min(pPartialTick * 6F, 1f);
} else {
f = 0f;
}
this.walkAnimation.update(f, 0.2f);
}
@Override
protected void registerGoals() {
this.goalSelector.addGoal(0, new FloatGoal(this));
this.goalSelector.addGoal(1, new PanicGoal(this, 1.5D));
this.goalSelector.addGoal(2, new BreedGoal(this, 1.15D));
this.goalSelector.addGoal(3, new TemptGoal(this, 1.2D,
Ingredient.of(Items.COD, Items.SALMON, Items.COOKED_SALMON, Items.COOKED_COD), false));
this.goalSelector.addGoal(4, new FollowParentGoal(this, 1.1D));
this.goalSelector.addGoal(5, new RandomSwimmingGoal(this, 1.0D, 80));
this.goalSelector.addGoal(6, new WaterAvoidingRandomStrollGoal(this, 1.0D));
this.goalSelector.addGoal(7, new LookAtPlayerGoal(this, Player.class, 3f));
this.goalSelector.addGoal(8, new RandomLookAroundGoal(this));
}
public static AttributeSupplier.Builder createAttributes() {
return Animal.createLivingAttributes()
.add(Attributes.MAX_HEALTH, 10D)
.add(Attributes.MOVEMENT_SPEED, 0.15D)
.add(Attributes.FOLLOW_RANGE, 24D);
}
@Override
public void travel(Vec3 pTravelVector) {
if (this.isEffectiveAi() && this.isInWater()) {
this.moveRelative(0.1F, pTravelVector);
this.move(MoverType.SELF, this.getDeltaMovement());
this.setDeltaMovement(this.getDeltaMovement().scale(0.9D));
} else {
super.travel(pTravelVector);
}
}
@Nullable
@Override
public AgeableMob getBreedOffspring(ServerLevel pLevel, AgeableMob pOtherParent) {
return ModEntities.PENGUIN.get().create(pLevel);
}
@Override
public boolean isFood(ItemStack pStack) {
return pStack.is(Items.COD) || pStack.is(Items.SALMON) ||
pStack.is(Items.COOKED_COD) || pStack.is(Items.COOKED_SALMON);
}
// Reminder: You may want to change these sounds to something more bird-like!
@Nullable
@Override
protected SoundEvent getAmbientSound() {
return SoundEvents.DOLPHIN_AMBIENT;
}
@Nullable
@Override
protected SoundEvent getHurtSound(DamageSource pDamageSource) {
return SoundEvents.DOLPHIN_HURT;
}
@Nullable
@Override
protected SoundEvent getDeathSound() {
return SoundEvents.DOLPHIN_DEATH;
}
}
2. Changes for PenguinModel.java
Now, update your setupAnim method to use the new swimAnimationState. It should check if the penguin is in water and play the appropriate animation.
Here is the complete, updated setupAnim method for your model class.
java
// Assuming T extends PenguinEntity
@Override
public void setupAnim(T entity, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch) {
this.root().getAllParts().forEach(ModelPart::resetPose);
this.applyHeadRotation(netHeadYaw, headPitch, ageInTicks);
// Check if the entity is in water
if (entity.isInWater()) {
// If swimming, play the swim animation
this.animate(entity.swimAnimationState, ModAnimationDefinitions.PENGUIN_SWIM, ageInTicks, 1f);
} else {
// If on land, play the walk and idle animations
this.animateWalk(ModAnimationDefinitions.PENGUIN_WALK, limbSwing, limbSwingAmount, 2f, 2.5f);
this.animate(entity.idleAnimationState, ModAnimationDefinitions.PENGUIN_IDLE, ageInTicks, 1f);
}
}
Summary of Changes:
PenguinEntity.java:
Added a public final AnimationState swimAnimationState.
Modified setupAnimationStates() to use an if/else block:
If isInWater(), it starts swimAnimationState and stops idleAnimationState.
Otherwise (on land), it stops swimAnimationState and runs the logic for idleAnimationState.
PenguinModel.java (in setupAnim):
It now checks entity.isInWater().
If true, it calls this.animate() with the swimAnimationState and PENGUIN_SWIM definition.
If false, it runs the original code for the walking and idle animations.
This setup ensures a clean separation between land and water animations, making your penguin behave much more realistically.