Untitled

mail@pastecode.io avatarunknown
plain_text
16 days ago
6.5 kB
9
Indexable
Never
package net.tiagom.archeologyanddinos.entity.custom;

import net.minecraft.server.level.ServerLevel;
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.ai.goal.target.NearestAttackableTargetGoal;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.animal.Chicken;
import net.minecraft.world.entity.animal.Cow;
import net.minecraft.world.entity.animal.horse.Llama;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.tiagom.archeologyanddinos.entity.DinoEntities;
import net.tiagom.archeologyanddinos.entity.ai.goal.UtahSleepAtNightGoal;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import software.bernie.geckolib.animatable.GeoEntity;
import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.core.animation.*;
import software.bernie.geckolib.core.animation.AnimationState;
import software.bernie.geckolib.core.object.PlayState;
import software.bernie.geckolib.util.GeckoLibUtil;


public class UtahEntity extends Animal implements GeoEntity  {

    public static final RawAnimation UTAH_WALK = RawAnimation.begin().thenLoop("animation.utah.walk");
    public static final RawAnimation UTAH_IDLE = RawAnimation.begin().thenLoop("animation.utah.idle");
    public static final RawAnimation UTAH_ATTACK = RawAnimation.begin().thenPlay("animation.utah.attack");
    public static final RawAnimation UTAH_REST = RawAnimation.begin().thenPlayAndHold("animation.utah.rest");
    public static final RawAnimation UTAH_SCRATCH = RawAnimation.begin().thenPlay("animation.utah.stratch");

    private final AnimatableInstanceCache geoCache = GeckoLibUtil.createInstanceCache(this);
    private static final long TIME_UNTIL_REST = 20000;
    private long lastMove = System.currentTimeMillis();



    public UtahEntity(EntityType<? extends Animal> pEntityType, Level pLevel) {
        super(pEntityType, pLevel);
    }


    public static AttributeSupplier setAttributes() {
        return Animal.createMobAttributes()
                .add(Attributes.MAX_HEALTH, 50D)
                .add(Attributes.ATTACK_DAMAGE, 4.0f)
                .add(Attributes.ATTACK_SPEED, 1.0f)
                .add(Attributes.MOVEMENT_SPEED, 0.5f).build();
    }



    @Override
    protected void registerGoals() {
        this.goalSelector.addGoal(1, new FloatGoal(this));
        this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 1.2D, false));
        this.goalSelector.addGoal(3, new UtahSleepAtNightGoal(this));
        this.goalSelector.addGoal(4, new WaterAvoidingRandomStrollGoal(this, 1.0D));
        this.goalSelector.addGoal(4, new RandomLookAroundGoal(this));


        this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Player.class, true));
        this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Cow.class, true));
        this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Llama.class, true));
        this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Chicken.class, true));
    }



    @Nullable
    @Override
    public AgeableMob getBreedOffspring(@NotNull ServerLevel pLevel, @NotNull AgeableMob pOtherParent) {
        return DinoEntities.UTAHRAPTOR.get().create(pLevel);
    }



    @Override
    public void registerControllers(final AnimatableManager.ControllerRegistrar controllers) {
        controllers.add(new AnimationController<>(this, "Utah Walk", 5, this::walkAnimController));
        controllers.add(new AnimationController<>(this, "Utah Idle", 5, this::idleAnimController));
        controllers.add(new AnimationController<>(this, "Utah Attack",5, this::attackAnimController));
        controllers.add(new AnimationController<>(this, "Utah Rest",5, this::restAnimController));
        controllers.add(new AnimationController<>(this, "Utah Scratch",5, this::scratchAnimController));

    }


    private PlayState idleAnimController(AnimationState<UtahEntity> utahEntityAnimationState) {
        return utahEntityAnimationState.setAndContinue(UTAH_IDLE);
    }


    private PlayState walkAnimController(AnimationState<UtahEntity> utahEntityAnimationState) {
        if (utahEntityAnimationState.isMoving()) {
            return utahEntityAnimationState.setAndContinue(UTAH_WALK);
        }
        return PlayState.STOP;


    }

    private PlayState attackAnimController(AnimationState<UtahEntity> utahEntityAnimationState) {
        if (utahEntityAnimationState.getAnimatable().swinging
                && utahEntityAnimationState.getController().getAnimationState().equals(AnimationController.State.STOPPED)){

            utahEntityAnimationState.getController().forceAnimationReset();
            utahEntityAnimationState.setAnimation(UTAH_ATTACK);
            utahEntityAnimationState.getAnimatable().swinging = false;
        }
        return PlayState.CONTINUE;
    }


    private PlayState restAnimController(AnimationState<UtahEntity> utahEntityAnimationState) {

        long actualTime = System.currentTimeMillis();

            if (getLevel().isNight())
            {
            return utahEntityAnimationState.setAndContinue(UTAH_REST);
            }
            else {
                    if (!utahEntityAnimationState.isMoving()) {

                        long lasMoveTime = actualTime - lastMove;
                            if (lasMoveTime >= TIME_UNTIL_REST) {
                                return utahEntityAnimationState.setAndContinue(UTAH_REST);
                            }
                        }
                        else {
                            lastMove = actualTime;
                            return PlayState.STOP;
                                }
            }
        return PlayState.STOP;

    }

    private PlayState scratchAnimController(AnimationState<UtahEntity> utahEntityAnimationState) {
        return null;
    }



    @Override
    public AnimatableInstanceCache getAnimatableInstanceCache() {
        return this.geoCache;
    }


    public Level getLevel() {
        return level();
    }


}