Advertisement
Guest User

BearGoals

a guest
Jul 16th, 2022
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.64 KB | None | 0 0
  1. package com.zanckor.entities.bear;
  2.  
  3. import net.minecraft.core.BlockPos;
  4. import net.minecraft.nbt.CompoundTag;
  5. import net.minecraft.network.syncher.EntityDataAccessor;
  6. import net.minecraft.network.syncher.EntityDataSerializers;
  7. import net.minecraft.network.syncher.SynchedEntityData;
  8. import net.minecraft.world.entity.EntityType;
  9. import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
  10. import net.minecraft.world.entity.ai.attributes.Attributes;
  11. import net.minecraft.world.entity.ai.goal.*;
  12. import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
  13. import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
  14. import net.minecraft.world.entity.animal.Animal;
  15. import net.minecraft.world.entity.monster.Monster;
  16. import net.minecraft.world.entity.player.Player;
  17. import net.minecraft.world.level.Level;
  18. import software.bernie.geckolib3.core.IAnimatable;
  19. import software.bernie.geckolib3.core.PlayState;
  20. import software.bernie.geckolib3.core.builder.AnimationBuilder;
  21. import software.bernie.geckolib3.core.controller.AnimationController;
  22. import software.bernie.geckolib3.core.event.predicate.AnimationEvent;
  23. import software.bernie.geckolib3.core.manager.AnimationData;
  24. import software.bernie.geckolib3.core.manager.AnimationFactory;
  25.  
  26. import javax.annotation.ParametersAreNonnullByDefault;
  27. import java.util.EnumSet;
  28. import java.util.Optional;
  29.  
  30. public class BearEntity extends Monster implements IAnimatable {
  31.  
  32.     private static final EntityDataAccessor<Boolean> DATA_HAS_TARGET = SynchedEntityData.defineId(BearEntity.class, EntityDataSerializers.BOOLEAN);
  33.     private static final EntityDataAccessor<Boolean> IS_SLEEPING = SynchedEntityData.defineId(BearEntity.class, EntityDataSerializers.BOOLEAN);
  34.     private static final EntityDataAccessor<Boolean> IS_RETURNING_HOME = SynchedEntityData.defineId(BearEntity.class, EntityDataSerializers.BOOLEAN);
  35.     private final AnimationFactory factory = new AnimationFactory(this);
  36.  
  37.  
  38.     public BearEntity(EntityType<? extends Monster> entityType, Level level) {
  39.         super(entityType, level);
  40.     }
  41.  
  42.  
  43.     protected void registerGoals() {
  44.         this.goalSelector.addGoal(1, new FloatGoal(this));
  45.         this.goalSelector.addGoal(2, new MeleeAttackGoal(this, 1.0D, false));
  46.         this.goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 8.0F));
  47.         this.goalSelector.addGoal(4, new RandomLookAroundGoal(this));
  48.         this.goalSelector.addGoal(5, new JumpGoal() {
  49.             @Override
  50.             public boolean canUse() {
  51.                 return true;
  52.             }
  53.         });
  54.         this.goalSelector.addGoal(6, new RandomSwimmingGoal(this, 10, 7));
  55.         this.goalSelector.addGoal(7, new returnHome(this, this.getOnPos()));
  56.         this.goalSelector.addGoal(8, new SleepGoal(this));
  57.  
  58.         this.targetSelector.addGoal(1, (new HurtByTargetGoal(this, BearEntity.class)));
  59.         this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Animal.class, true));
  60.     }
  61.  
  62.     public static AttributeSupplier setAttributes() {
  63.         return Animal.createMobAttributes()
  64.                 .add(Attributes.MAX_HEALTH, 100.0D)
  65.                 .add(Attributes.ATTACK_DAMAGE, 25.0f)
  66.                 .add(Attributes.ATTACK_KNOCKBACK, 2.0f)
  67.                 .add(Attributes.ATTACK_SPEED, 2.0f)
  68.                 .add(Attributes.MOVEMENT_SPEED, 0.3f)
  69.                 .add(Attributes.FOLLOW_RANGE, 24.0f).build();
  70.     }
  71.  
  72.     @ParametersAreNonnullByDefault
  73.     @Override
  74.     public void readAdditionalSaveData(CompoundTag nbt) {
  75.         super.readAdditionalSaveData(nbt);
  76.         entityCheckTarget(nbt.getBoolean("hasTarget"));
  77.         entityCheckTarget(nbt.getBoolean("isSleeping"));
  78.         entityCheckTarget(nbt.getBoolean("isReturningHome"));
  79.     }
  80.  
  81.     @ParametersAreNonnullByDefault
  82.     @Override
  83.     public void addAdditionalSaveData(CompoundTag nbt) {
  84.         super.addAdditionalSaveData(nbt);
  85.         nbt.putBoolean("hasTarget", this.entityHasTarget());
  86.         nbt.putBoolean("isSleeping", this.entityIsSleeping());
  87.         nbt.putBoolean("isReturningHome", this.entityIsReturningHome());
  88.     }
  89.  
  90.  
  91.     @Override
  92.     protected void defineSynchedData() {
  93.         super.defineSynchedData();
  94.         this.entityData.define(DATA_HAS_TARGET, false);
  95.         this.entityData.define(IS_SLEEPING, false);
  96.         this.entityData.define(IS_RETURNING_HOME, false);
  97.     }
  98.  
  99.  
  100.     @Override
  101.     public void updateControlFlags() {
  102.         if (this.getTarget() != null) {
  103.             this.entityCheckTarget(true);
  104.             this.setSprinting(true);
  105.         } else {
  106.             this.entityCheckTarget(false);
  107.             this.setSprinting(false);
  108.         }
  109.     }
  110.  
  111.  
  112.     public void entityCheckTarget(boolean hasTarget) {
  113.         this.entityData.set(DATA_HAS_TARGET, hasTarget);
  114.     }
  115.  
  116.     public void entitySetSleeping(boolean isSleeping) {
  117.         this.entityData.set(IS_SLEEPING, isSleeping);
  118.     }
  119.  
  120.     public void entitySetReturningHome(boolean isReturningHome) {
  121.         this.entityData.set(IS_SLEEPING, isReturningHome);
  122.     }
  123.  
  124.  
  125.     public boolean entityHasTarget() {
  126.         return this.entityData.get(DATA_HAS_TARGET);
  127.     }
  128.  
  129.     public boolean entityIsSleeping() {
  130.         return this.entityData.get(IS_SLEEPING);
  131.     }
  132.  
  133.     public boolean entityIsReturningHome() {
  134.         return this.entityData.get(IS_RETURNING_HOME);
  135.     }
  136.  
  137.     private <E extends IAnimatable> PlayState predicate(AnimationEvent<E> e) {
  138.  
  139.  
  140.         /*
  141.         if (this.entityHasTarget() && e.isMoving()) {
  142.  
  143.             e.getController().setAnimation(new AnimationBuilder().addAnimation("bear.run", true));
  144.  
  145.             e.getController().setAnimationSpeed(5D);
  146.  
  147.  
  148.             return PlayState.CONTINUE;
  149.         } else {
  150.             e.getController().setAnimationSpeed(1D);
  151.         }
  152.         */
  153.  
  154.         if (this.entityIsSleeping()) {
  155.             System.out.println("Is Sleeping");
  156.         }
  157.  
  158.         if (this.entityIsReturningHome()) {
  159.             System.out.println("Is Returning Home");
  160.         }
  161.  
  162.         if (e.isMoving()) {
  163.             e.getController().setAnimation(new AnimationBuilder().addAnimation("bear.walk", true));
  164.             return PlayState.CONTINUE;
  165.         }
  166.  
  167.  
  168.         e.getController().setAnimation(new AnimationBuilder().addAnimation("bear.idle", true));
  169.         return PlayState.CONTINUE;
  170.     }
  171.  
  172.  
  173.     @Override
  174.     public void registerControllers(AnimationData data) {
  175.         data.addAnimationController(new AnimationController(this, "controller", 0, this::predicate));
  176.     }
  177.  
  178.  
  179.     @Override
  180.     public AnimationFactory getFactory() {
  181.         return this.factory;
  182.     }
  183.  
  184.  
  185. }
  186.  
  187.  
  188. class returnHome extends Goal {
  189.  
  190.     private final BearEntity bear;
  191.     private Optional<BlockPos> home;
  192.     private int xHome, yHome, zHome;
  193.  
  194.     returnHome(BearEntity bear, BlockPos home) {
  195.         this.bear = bear;
  196.         this.home = bear.getSleepingPos();
  197.  
  198.         xHome = home.getX();
  199.         zHome = home.getZ();
  200.         yHome = home.getY();
  201.     }
  202.  
  203.     @Override
  204.     public boolean canUse() {
  205.         if (!bear.level.isDay() && bear.entityIsReturningHome()) {
  206.             start();
  207.             return home != null;
  208.         }
  209.         stop();
  210.         return false;
  211.     }
  212.  
  213.     @Override
  214.     public void start() {
  215.         bear.getMoveControl().setWantedPosition(xHome, zHome, yHome, 1);
  216.         bear.entitySetReturningHome(true);
  217.     }
  218.  
  219.     @Override
  220.     public void stop() {
  221.         bear.entitySetReturningHome(false);
  222.     }
  223.  
  224.  
  225. }
  226.  
  227.  
  228. class SleepGoal extends Goal {
  229.     private BearEntity bear;
  230.  
  231.     public SleepGoal(BearEntity bear) {
  232.         this.bear = bear;
  233.         this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK, Goal.Flag.JUMP));
  234.     }
  235.  
  236.     @Override
  237.     public boolean canUse() {
  238.         if (!bear.level.isDay() /*&& !bear.entityIsReturningHome()*/) {
  239.             System.out.println("STARTED");
  240.             start();
  241.             return true;
  242.         }
  243.         System.out.println("STOPPED");
  244.         stop();
  245.         return false;
  246.     }
  247.  
  248.     @Override
  249.     public boolean canContinueToUse() {
  250.         return this.canSleep();
  251.     }
  252.  
  253.  
  254.     private boolean canSleep() {
  255.         if (!bear.level.isDay()) {
  256.             return true;
  257.         }
  258.         return false;
  259.     }
  260.  
  261.     @Override
  262.     public void stop() {
  263.         if (bear.level.isDay()) {
  264.             bear.entitySetSleeping(false);
  265.         }
  266.     }
  267.  
  268.     @Override
  269.     public void start() {
  270.         System.out.println("HAD EVEN STARTED?");
  271.         bear.entitySetSleeping(true);
  272.         bear.setJumping(false);
  273.         bear.getNavigation().stop();
  274.         bear.getMoveControl().setWantedPosition(bear.getX(), bear.getY(), bear.getZ(), 0.0D);
  275.     }
  276. }
  277.  
  278.  
  279.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement