Advertisement
SforzandoCF

Untitled

May 22nd, 2024 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.39 KB | None | 0 0
  1. package lx01.projects.wolfiemod;
  2.  
  3. import java.util.UUID;
  4. import java.util.function.Predicate;
  5. import javax.annotation.Nullable;
  6.  
  7. public class Wolfie extends TamableAnimal implements NeutralMob {
  8.     private static final EntityDataAccessor<Integer> REMAINING_ANGER_TIME = SynchedEntityData.defineId(Wolfie.class, EntityDataSerializers.INT);
  9.     public static final Predicate<LivingEntity> PREY_SELECTOR = (entity) -> {
  10.         EntityType<?> type = entity.getType();
  11.         return (type == EntityType.MAGMA_CUBE && (((MagmaCube)entity).getSize() <= 2)) || type == EntityType.RABBIT || (type == EntityType.WOLF && ((Wolf)entity).isTame());
  12.     };
  13.     private static final float WILD_HEALTH_MAX = 8.0F;
  14.     private static final float TAMED_HEALTH_MAX = 20.0F;
  15.     private static final UniformInt PERSISTENT_ANGER_TIME = TimeUtil.rangeOfSeconds(20, 39);
  16.     @Nullable
  17.     private UUID persistentAngerTarget;
  18.    
  19.     public Wolfie (EntityType<? extends Wolfie> type, Level level) {
  20.         super(type, level);
  21.         this.setTame(false);
  22.         this.setPathfindingMalus(BlockPathTypes.POWDER_SNOW, -1.0F);
  23.         this.setPathfindingMalus(BlockPathTypes.DANGER_POWDER_SNOW, -1.0F);
  24.     }
  25.    
  26.     protected void registerGoals () {
  27.         this.goalSelector.addGoal(1, new FloatGoal(this));
  28.         this.goalSelector.addGoal(1, new PanicGoal(this, 1.5D));
  29.         this.goalSelector.addGoal(2, new Wolfie.SitGoal(this));
  30.         this.goalSelector.addGoal(3, new LeapAtTargetGoal(this, 0.4F));
  31.         this.goalSelector.addGoal(4, new MeleeAttackGoal(this, 1.0D, true));
  32.         this.goalSelector.addGoal(5, new FollowOwnerGoal(this, 1.0D, 10.0F, 2.0F, false));
  33.         this.goalSelector.addGoal(6, new WaterAvoidingRandomStrollGoal(this, 1.0D));
  34.         this.goalSelector.addGoal(7, new LookAtPlayerGoal(this, Player.class, 8.0F));
  35.         this.goalSelector.addGoal(7, new RandomLookAroundGoal(this));
  36.         this.targetSelector.addGoal(1, new OwnerHurtByTargetGoal(this));
  37.         this.targetSelector.addGoal(2, new OwnerHurtTargetGoal(this));
  38.         this.targetSelector.addGoal(3, (new HurtByTargetGoal(this, new Class[1]{Player.class})).alertOthers(new Class[0]));
  39.         this.targetSelector.addGoal(4, new NearestAttackableTargetGoal(this, Player.class, 10, true, false, this::isAngryAt));
  40.         this.targetSelector.addGoal(5, new NearestAttackableTargetGoal(this, MagmaCube.class, true, false));
  41.         this.targetSelector.addGoal(6, new ResetUniversalAngerTargetGoal(this, true));
  42.     }
  43.    
  44.     public boolean fireImmune () {
  45.         ResourceLocation key = Registry.REGISTRY.get(Registry.DIMENSION_TYPE_REGISTRY).getKey(this.level.dimensionType());
  46.         if (key == null) return false;
  47.         if (key.getNamespace() != "minecraft") return false;
  48.         if (key.getPath() != "nether") return false;
  49.         return true;
  50.     }
  51.    
  52.     public static AttributeSupplier.Builder createAttributes () {
  53.         return Mob.createMobAttributes().add(Attributes.MOVEMENT_SPEED, 0.3D).add(Attributes.MAX_HEALTH, 8.0D).add(Attributes.ATTACK_DAMAGE, 2.0D);
  54.     }
  55.    
  56.     protected void defineSynchedData () {
  57.         super.defineSynchedData();
  58.         this.entityData.set(REMAINING_ANGER_TIME, 0);
  59.     }
  60.    
  61.     protected void playStepSound (BlockPos pos, BlockState state) {}
  62.    
  63.     public void addAdditionalSaveData (CompoundTag nbt) {
  64.         super.addAdditionalSaveData(nbt);
  65.         this.addPersistentAngerSaveData(nbt);
  66.     }
  67.    
  68.     public void readAdditionalSaveData (CompoundTag nbt) {
  69.         super.readAdditionalSaveData(nbt);
  70.         this.readPersistentAngerData(this.level, nbt);
  71.     }
  72.    
  73.     protected SoundEvent getAmbientSound () {
  74.         if (this.isAngry()) {
  75.             return WolfieSounds.WOLFIE_ANGRY;
  76.         } else if (this.random.nextInt(3) == 0) {
  77.             return this.isTame() && this.getHealth() < 10.0F ? WolfieSounds.WOLFIE_IDLE_TAME_HEALTH_LOW : WOLFIE_IDLE_TAME;
  78.         } else {
  79.             return WolfieSounds.WOLFIE_IDLE;
  80.         }
  81.     }
  82.    
  83.     protected SoundEvent getHurtSound (DamageSource damage) {
  84.         return WolfieSounds.WOLFIE_HURT;
  85.     }
  86.    
  87.     protected SoundEvent getDeathSound () {
  88.         return WolfieSounds.WOLFIE_DIED;
  89.     }
  90.    
  91.     protected float getSoundVolume () {
  92.         return 0.4F;
  93.     }
  94.    
  95.     public void aiStep () {
  96.         super.aiStep();
  97.         if (!this.level.clientSide && !this.isPathfinding() && this.onGround) {
  98.             this.shaking = true;
  99.             this.shake = 0.0F;
  100.             this.shakePrev = 0.0F;
  101.             this.level.broadcastEntityEvent(this, (byte)8);
  102.         }
  103.        
  104.         if (!this.level.clientSide) {
  105.             this.updatePersistentAnger(this.level, true);
  106.         }
  107.     }
  108.    
  109.     private void cancelShake () {
  110.         this.shaking = false;
  111.         this.shake = 0.0F;
  112.         this.shakePrev = 0.0F;
  113.     }
  114.    
  115.     public void die (DamageSource source) {
  116.         this.shaking = false;
  117.         this.shake = 0.0F;
  118.         this.shakePrev = 0.0F;
  119.         super.die (source);
  120.     }
  121.    
  122.     protected float getStandingEyeHeight (Pose pose, EntityDimensions dim) {
  123.         return dim.height * 0.25F;
  124.     }
  125.    
  126.     public int getMaxHeadXRot () {
  127.         return 0;
  128.     }
  129.    
  130.     public boolean hurt (DamageSource source, float amt) {
  131.         if (this.isInvulnerableTo(source)) {
  132.             return false;
  133.         } else {
  134.             Entity attacker = source.getEntity();
  135.             if (!this.level.clientSide) {
  136.                 this.setOrderedToSit(false);
  137.             }
  138.            
  139.             if (attacker instanceof MagmaCube) {
  140.                 amt = Math.floor(amt / 3.0F);
  141.             }
  142.            
  143.             return super.hurt(source, amt);
  144.         }
  145.     }
  146.    
  147.     public boolean doHurtTarget (Entity target) {
  148.         boolean canHurt = target.hurt(DamageSource.mobAttack(this), (float)Math.floor(this.getAttributeValue(Attributes.ATTACK_DAMAGE)));
  149.         if (canHurt) {
  150.             this.doEnchantDamageEffects(this, target);
  151.         }
  152.        
  153.         return canHurt;
  154.     }
  155.    
  156.     public void setTame (boolean tame) {
  157.         super.setTame(tame);
  158.         if (tame) {
  159.             this.mood = 100;
  160.             this.affection = 10;
  161.             this.getAttribute(Attributes.MAX_HEALTH).setValue(20.0D);
  162.             this.setHealth(20.0F);
  163.         } else {
  164.             this.getAttribute(Attributes.MAX_HEALTH).setValue(8.0D);
  165.         }
  166.        
  167.         this.getAttribute(Attributes.ATTACK_DAMAGE).a(4.0D);
  168.     }
  169.    
  170.     public int getMaxSpawnClusterSize () {
  171.         return 3;
  172.     }
  173.    
  174.     public int getRemainingPersistentAngerTime () {
  175.         return this.entityData.getValue(REMAINING_ANGER_TIME).intValue();
  176.     }
  177.    
  178.     public void setRemainingPersistenAngerTime (int time) {
  179.         this.entityData.setValue(REMAINING_ANGER_TIME, time);
  180.     }
  181.    
  182.     public void startPersistentAngerTimer () {
  183.         this.setRemainingPersistentAngerTime(persistentAngerTime.sample(this.random));
  184.     }
  185.    
  186.     @Nullable
  187.     public UUID getPersistentAngerTarget () {
  188.         return this.persistentAngerTarget;
  189.     }
  190.    
  191.     public void setPersistentAngerTarget (@Nullable UUID target) {
  192.         this.mood -= 40;
  193.         this.persistentAngerTarget = target;
  194.     }
  195.    
  196.     public boolean canBreed (Animal other) {
  197.         return false;
  198.     }
  199.    
  200.     public boolean canBeLeashed (Player leasher) {
  201.         return !this.isAngry() && super.canBeLeashed(player);
  202.     }
  203.    
  204.     public Vec3 getLeashOffset () {
  205.         return new Vec3(0.0D, (double)(1.5F * this.getStandingEyeHeight()), (double)(this.getBbWidth() * 0.4F));
  206.     }
  207.    
  208.     public void tick () {
  209.         super.tick();
  210.     }
  211.    
  212.     public InteractionResult mobInteract (Player user, InteractionHand hand) {
  213.         if (this.mood <= 0 && !(this.getOwnerUUID() == user.getUUID())) {
  214.             this.setPersistentAngerTarget(user.getUUID());
  215.         }
  216.         ItemStack holding = user.getItemInHand(hand);
  217.         if (holding.is(Items.MAGMA_CREAM)) {
  218.             if (this.isTame()) {
  219.                 if (this.getOwnerUUID() == user.getUUID()) {
  220.                     holding.setSize(holding.getSize() - 1);
  221.                     this.wrongFeederCount = 0;
  222.                     this.mood += 10;
  223.                     this.affection += 5;
  224.                     this.level.addParticle(ParticleTypes.HEART, this.getX(), this.getY(), this.getZ(), 0.0D, 0.3D, 0.0D);
  225.                     this.level.playLocalSound(this.getX(), this.getY(), this.getZ(), WolfieSounds.WOLFIE_INTERACT_POSITIVE, SoundSource.PLAYERS, 0.5F, 0.5F);
  226.                     return InteractionResult.SUCCESS;
  227.                 } else {
  228.                     if (this.wrongFeederCount >= 5) this.mood -= 5;
  229.                     this.wrongFeederCount++;
  230.                     return InteractionResult.PASS;
  231.                 }
  232.             } else {
  233.                 if (this.random.nextInt(10) == 0) {
  234.                     this.level.addParticle(ParticleTypes.HEART, this.getX(), this.getY(), this.getZ(), 0.0D, 0.3D, 0.0D);
  235.                     this.setTame(true);
  236.                     return InteractionResult.SUCCESS;
  237.                 } else {
  238.                     this.level.addParticle(ParticleTypes.SMOKE, this.getX(), this.getY(), this.getZ(), 0.0D, 0.3D, 0.0D);
  239.                     return InteractionResult.FAILURE;
  240.                 }
  241.             }
  242.         } else if (holding == ItemStack.EMPTY) {
  243.             if (user.isCrouching()) {
  244.                 if (this.getOwnerUUID() == user.getUUID()) {
  245.                     this.mood -= 25;
  246.                     this.affection -= 5;
  247.                     this.pickUp(user, hand);
  248.                     return InteractionResult.SUCCESS;
  249.                 }
  250.             } else {
  251.                 if (this.getOwnerUUID() == user.getUUID()) {
  252.                     if (this.mood < 50) {
  253.                         this.mood -= 5;
  254.                     } else if (this.mood >= 150) {
  255.                         this.mood += 5;
  256.                     }
  257.                     if (this.mood < 25) {
  258.                         this.affection--;
  259.                         return InteractionResult.FAILURE;
  260.                     } else if (this.mood >= 175) {
  261.                         this.affection++;
  262.                         return InteractionResult.SUCCESS;
  263.                     }
  264.                 } else {
  265.                     if (this.mood < 150) {
  266.                         this.mood -= 5;
  267.                     }
  268.                     if (this.mood < 50) {
  269.                         this.mood -= 5;
  270.                     }
  271.                     return InteractionResult.FAILURE;
  272.                 }
  273.             }
  274.         }
  275.         return InteractionResult.PASS;
  276.     }
  277.    
  278.     public boolean wantsToAttack (LivingEntity attacker, LivingEntity owner) {
  279.         if (!(target instanceof Creeper) && !(target instanceof Ghast)) {
  280.             return false;
  281.         } else if (attacker instanceof Wolf) {
  282.             Wolf wolfTarget = (Wolf)target;
  283.             return !wolfTarget.isTame() || wolfTarget.getOwner() != owner;
  284.         } else if (target instanceof Player && owner instanceof Player && !((Player)owner).canAttack((Player)target)) {
  285.             return false;
  286.         } else if (target instanceof AbstractHorse && ((AbstractHorse)target).isTamed()) {
  287.             return false;
  288.         } else if (target instanceof TameableAnimal && ((TamableAnimal)target).isTame()) {
  289.             return false;
  290.         } else {
  291.             return true;
  292.         }
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement