Advertisement
jayhillx

Red Panda

Jan 6th, 2024
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. package com.mysticsbiomes.common.entity.animal;
  2.  
  3. import com.mysticsbiomes.init.MysticEntities;
  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.server.level.ServerLevel;
  9. import net.minecraft.world.InteractionHand;
  10. import net.minecraft.world.InteractionResult;
  11. import net.minecraft.world.entity.*;
  12. import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
  13. import net.minecraft.world.entity.ai.attributes.Attributes;
  14. import net.minecraft.world.entity.ai.control.LookControl;
  15. import net.minecraft.world.entity.ai.control.MoveControl;
  16. import net.minecraft.world.entity.animal.*;
  17. import net.minecraft.world.entity.player.Player;
  18. import net.minecraft.world.item.Items;
  19. import net.minecraft.world.level.Level;
  20.  
  21. /**
  22.  * Red Pandas spawn in the Bamboo Blossom Forest, scouting out bamboo, and stealing items.
  23.  */
  24. public class RedPanda extends Fox {
  25.     private static final EntityDataAccessor<Byte> DATA_FLAGS_ID = SynchedEntityData.defineId(RedPanda.class, EntityDataSerializers.BYTE);
  26.     public final AnimationState sleepingAnimationState = new AnimationState();
  27.     public final AnimationState standingPoseAnimationState = new AnimationState();
  28.  
  29.     public RedPanda(EntityType<? extends RedPanda> type, Level level) {
  30.         super(type, level);
  31.         this.lookControl = new LookControl(this);
  32.         this.moveControl = new MoveControl(this);
  33.     }
  34.  
  35.     protected void defineSynchedData() {
  36.         super.defineSynchedData();
  37.         this.entityData.define(DATA_FLAGS_ID, (byte)0);
  38.     }
  39.  
  40.     public static AttributeSupplier.Builder createAttributes() {
  41.         return Mob.createMobAttributes().add(Attributes.MOVEMENT_SPEED, 1.0F).add(Attributes.MAX_HEALTH, 10.0D).add(Attributes.FOLLOW_RANGE, 32.0D).add(Attributes.ATTACK_DAMAGE, 2.0D);
  42.     }
  43.  
  44.     public RedPanda getBreedOffspring(ServerLevel level, AgeableMob mob) {
  45.         return MysticEntities.RED_PANDA.get().create(level);
  46.     }
  47.  
  48.     @Override
  49.     public EntityDimensions getDimensions(Pose pose) {
  50.         return pose == Pose.STANDING ? EntityDimensions.scalable(0.5F, 1.125F) : super.getDimensions(pose);
  51.     }
  52.  
  53.     @Override
  54.     protected float getStandingEyeHeight(Pose pose, EntityDimensions dimensions) {
  55.         return pose == Pose.STANDING ? 1.0F : super.getStandingEyeHeight(pose, dimensions);
  56.     }
  57.  
  58.     public void addAdditionalSaveData(CompoundTag tag) {
  59.         super.addAdditionalSaveData(tag);
  60.         tag.putBoolean("IsSleeping", this.isSleeping());
  61.         tag.putBoolean("IsStanding", this.isStanding());
  62.     }
  63.  
  64.     public void readAdditionalSaveData(CompoundTag tag) {
  65.         super.readAdditionalSaveData(tag);
  66.         this.setSleeping(tag.getBoolean("IsSleeping"));
  67.         this.setStanding(tag.getBoolean("IsStanding"));
  68.     }
  69.  
  70.     public void tick() {
  71.         super.tick();
  72.  
  73.         if (this.level().isClientSide()) {
  74.             this.setupAnimationStates();
  75.         }
  76.     }
  77.  
  78.     private void setupAnimationStates() {
  79.         this.sleepingAnimationState.animateWhen(this.isSleeping(), this.tickCount);
  80.  
  81.         if (this.isStanding()) {
  82.             this.setPose(this.walkAnimation.isMoving() ? Pose.STANDING : Pose.SITTING);
  83.         }
  84.     }
  85.  
  86.     private boolean getFlag(int value) {
  87.         return (this.entityData.get(DATA_FLAGS_ID) & value) != 0;
  88.     }
  89.  
  90.     private void setFlag(int value, boolean b) {
  91.         if (b) {
  92.             this.entityData.set(DATA_FLAGS_ID, (byte)(this.entityData.get(DATA_FLAGS_ID) | value));
  93.         } else {
  94.             this.entityData.set(DATA_FLAGS_ID, (byte)(this.entityData.get(DATA_FLAGS_ID) & ~value));
  95.         }
  96.     }
  97.  
  98.     public boolean isSleeping() {
  99.         return this.getFlag(4);
  100.     }
  101.  
  102.     public void setSleeping(boolean sleeping) {
  103.         this.setPose(sleeping ? Pose.SLEEPING : Pose.SITTING);
  104.         this.setFlag(4, sleeping);
  105.     }
  106.  
  107.     public boolean isStanding() {
  108.         return this.getFlag(8);
  109.     }
  110.  
  111.     public void setStanding(boolean standing) {
  112.         this.setPose(standing ? Pose.STANDING : Pose.SITTING);
  113.         this.setFlag(8, standing);
  114.     }
  115.  
  116.     @Override
  117.     public InteractionResult mobInteract(Player player, InteractionHand hand) {
  118.         this.setSleeping(player.getMainHandItem().is(Items.STICK));
  119.         this.setStanding(player.getMainHandItem().is(Items.PINK_PETALS));
  120.         return super.mobInteract(player, hand);
  121.     }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement