Advertisement
jayhillx

Sea Otter

Apr 22nd, 2024
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. package com.mysticsbiomes.common.entity.animal;
  2.  
  3. import com.mysticsbiomes.init.MysticEntities;
  4. import com.mysticsbiomes.init.MysticItems;
  5. import net.minecraft.nbt.CompoundTag;
  6. import net.minecraft.network.syncher.EntityDataAccessor;
  7. import net.minecraft.network.syncher.EntityDataSerializers;
  8. import net.minecraft.network.syncher.SynchedEntityData;
  9. import net.minecraft.server.level.ServerLevel;
  10. import net.minecraft.world.entity.*;
  11. import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
  12. import net.minecraft.world.entity.ai.attributes.Attributes;
  13. import net.minecraft.world.entity.ai.control.SmoothSwimmingLookControl;
  14. import net.minecraft.world.entity.ai.control.SmoothSwimmingMoveControl;
  15. import net.minecraft.world.entity.ai.goal.*;
  16. import net.minecraft.world.entity.ai.navigation.PathNavigation;
  17. import net.minecraft.world.entity.ai.navigation.WaterBoundPathNavigation;
  18. import net.minecraft.world.entity.animal.Animal;
  19. import net.minecraft.world.item.crafting.Ingredient;
  20. import net.minecraft.world.level.Level;
  21. import net.minecraft.world.level.pathfinder.BlockPathTypes;
  22. import net.minecraft.world.level.pathfinder.PathFinder;
  23. import net.minecraft.world.level.pathfinder.SwimNodeEvaluator;
  24. import net.minecraft.world.phys.Vec3;
  25.  
  26. /**
  27.  * swims on the surface.
  28.  * only swims underwater fully when playing or searching for food.
  29.  */
  30. public class SeaOtter extends Animal {
  31.     private static final EntityDataAccessor<Boolean> DATA_FLOATING_ID = SynchedEntityData.defineId(SeaOtter.class, EntityDataSerializers.BOOLEAN);
  32.     private int ticksSinceLastSwim;
  33.  
  34.     public SeaOtter(EntityType<? extends SeaOtter> type, Level level) {
  35.         super(type, level);
  36.         this.setPathfindingMalus(BlockPathTypes.WATER, 0.0F);
  37.         this.moveControl = new SmoothSwimmingMoveControl(this, 85, 10, 0.02F, 0.1F, true);
  38.         this.lookControl = new SmoothSwimmingLookControl(this, 10);
  39.     }
  40.  
  41.     protected void defineSynchedData() {
  42.         super.defineSynchedData();
  43.         this.entityData.define(DATA_FLOATING_ID, false);
  44.     }
  45.  
  46.     protected void registerGoals() {
  47.         this.goalSelector.addGoal(0, new PanicGoal(this, 2.0F));
  48.         this.goalSelector.addGoal(1, new TemptGoal(this, 1.0F, Ingredient.of(MysticItems.MILKWEED.get()), false));
  49.         this.goalSelector.addGoal(2, new SeaOtter.SwimAroundGoal(this, 1.0D, 10));
  50.         this.goalSelector.addGoal(3, new SeaOtter.LookAroundGoal(this));
  51.     }
  52.  
  53.     public static AttributeSupplier.Builder createAttributes() {
  54.         return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 16.0F).add(Attributes.MOVEMENT_SPEED, 1.0D);
  55.     }
  56.  
  57.     public void addAdditionalSaveData(CompoundTag tag) {
  58.         super.addAdditionalSaveData(tag);
  59.         tag.putBoolean("Floating", this.isFloating());
  60.         tag.putInt("TicksSinceSwam", this.ticksSinceLastSwim);
  61.     }
  62.  
  63.     public void readAdditionalSaveData(CompoundTag tag) {
  64.         super.readAdditionalSaveData(tag);
  65.         this.setFloating(tag.getBoolean("Floating"));
  66.         this.ticksSinceLastSwim = tag.getInt("TicksSinceSwam");
  67.     }
  68.  
  69.     public AgeableMob getBreedOffspring(ServerLevel level, AgeableMob mob) {
  70.         return MysticEntities.SEA_OTTER.get().create(level);
  71.     }
  72.  
  73.     public MobType getMobType() {
  74.         return MobType.WATER;
  75.     }
  76.  
  77.     // TICKS & A.I.
  78.  
  79.     public void tick() {
  80.         super.tick();
  81.     }
  82.  
  83.     public void aiStep() {
  84.         super.aiStep();
  85.     }
  86.  
  87.     // NAVIGATION & MOVEMENT
  88.  
  89.     protected PathNavigation createNavigation(Level level) {
  90.         return new SeaOtter.SeaOtterPathNavigation(this, level);
  91.     }
  92.  
  93.     public void travel(Vec3 vec3) {
  94.         if (this.isEffectiveAi() && this.isInWater()) {
  95.             this.moveRelative(this.getSpeed(), vec3);
  96.             this.move(MoverType.SELF, this.getDeltaMovement());
  97.             this.setDeltaMovement(this.getDeltaMovement().scale(0.9D));
  98.         } else {
  99.             super.travel(vec3);
  100.         }
  101.     }
  102.  
  103.     public int getMaxAirSupply() {
  104.         return 6000;
  105.     }
  106.  
  107.     /**
  108.      * @return typically true unless the sea otter is searching for food or needs to swim a long distance.
  109.      */
  110.     public boolean isFloating() {
  111.         return this.entityData.get(DATA_FLOATING_ID);
  112.     }
  113.  
  114.     public void setFloating(boolean value) {
  115.         this.entityData.set(DATA_FLOATING_ID, value);
  116.     }
  117.  
  118.     // TODO: GOALS
  119.  
  120.     class SwimAroundGoal extends RandomSwimmingGoal {
  121.  
  122.         public SwimAroundGoal(PathfinderMob mob, double speed, int interval) {
  123.             super(mob, speed, interval);
  124.         }
  125.  
  126.         public boolean canUse() {
  127.             if (!SeaOtter.this.isFloating()) {
  128.                 return super.canUse();
  129.             } else {
  130.                 return false;
  131.             }
  132.         }
  133.  
  134.         public boolean canContinueToUse() {
  135.             if (!SeaOtter.this.isFloating()) {
  136.                 return super.canContinueToUse();
  137.             } else {
  138.                 return false;
  139.             }
  140.         }
  141.     }
  142.  
  143.     class LookAroundGoal extends RandomLookAroundGoal {
  144.  
  145.         public LookAroundGoal(Mob mob) {
  146.             super(mob);
  147.         }
  148.  
  149.         public boolean canUse() {
  150.             return super.canUse() && !SeaOtter.this.isFloating();
  151.         }
  152.  
  153.         public boolean canContinueToUse() {
  154.             return super.canContinueToUse() && !SeaOtter.this.isFloating();
  155.         }
  156.     }
  157.  
  158.     // TODO: NAVIGATION & CONTROLS
  159.  
  160.     static class SeaOtterPathNavigation extends WaterBoundPathNavigation {
  161.  
  162.         public SeaOtterPathNavigation(Mob mob, Level level) {
  163.             super(mob, level);
  164.         }
  165.  
  166.         protected PathFinder createPathFinder(int nodes) {
  167.             this.nodeEvaluator = new SwimNodeEvaluator(true);
  168.             return new PathFinder(this.nodeEvaluator, nodes);
  169.         }
  170.  
  171.         protected boolean canUpdatePath() {
  172.             return true;
  173.         }
  174.     }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement