Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.94 KB | None | 0 0
  1. package com.maideniles.beautifulbiomes.entity;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import com.google.common.base.Predicate;
  6. import com.maideniles.beautifulbiomes.entity.ai.EntityAIOcelotSit;
  7.  
  8. import net.minecraft.block.Block;
  9. import net.minecraft.block.state.IBlockState;
  10. import net.minecraft.entity.Entity;
  11. import net.minecraft.entity.EntityAgeable;
  12. import net.minecraft.entity.EntityLiving;
  13. import net.minecraft.entity.IEntityLivingData;
  14. import net.minecraft.entity.SharedMonsterAttributes;
  15. import net.minecraft.entity.ai.EntityAIAvoidEntity;
  16. import net.minecraft.entity.ai.EntityAIFollowOwner;
  17. import net.minecraft.entity.ai.EntityAILeapAtTarget;
  18. import net.minecraft.entity.ai.EntityAIMate;
  19. import net.minecraft.entity.ai.EntityAIOcelotAttack;
  20.  
  21. import net.minecraft.entity.ai.EntityAISit;
  22. import net.minecraft.entity.ai.EntityAISwimming;
  23. import net.minecraft.entity.ai.EntityAITargetNonTamed;
  24. import net.minecraft.entity.ai.EntityAITempt;
  25. import net.minecraft.entity.ai.EntityAIWanderAvoidWater;
  26. import net.minecraft.entity.ai.EntityAIWatchClosest;
  27. import net.minecraft.entity.passive.EntityAnimal;
  28. import net.minecraft.entity.passive.EntityChicken;
  29. import net.minecraft.entity.passive.EntityTameable;
  30. import net.minecraft.entity.player.EntityPlayer;
  31. import net.minecraft.init.Blocks;
  32. import net.minecraft.init.Items;
  33. import net.minecraft.init.SoundEvents;
  34. import net.minecraft.item.ItemStack;
  35. import net.minecraft.nbt.NBTTagCompound;
  36. import net.minecraft.network.datasync.DataParameter;
  37. import net.minecraft.network.datasync.DataSerializers;
  38. import net.minecraft.network.datasync.EntityDataManager;
  39. import net.minecraft.util.DamageSource;
  40. import net.minecraft.util.EnumHand;
  41. import net.minecraft.util.ResourceLocation;
  42. import net.minecraft.util.SoundEvent;
  43. import net.minecraft.util.datafix.DataFixer;
  44. import net.minecraft.util.math.BlockPos;
  45. import net.minecraft.util.text.translation.I18n;
  46. import net.minecraft.world.DifficultyInstance;
  47. import net.minecraft.world.World;
  48. import net.minecraft.world.storage.loot.LootTableList;
  49.  
  50. public class EntityMintyKitty extends EntityTameable
  51. {
  52.     private static final DataParameter<Integer> KITTY_VARIANT = EntityDataManager.<Integer>createKey(EntityMintyKitty.class, DataSerializers.VARINT);
  53.     private EntityAIAvoidEntity<EntityPlayer> avoidEntity;
  54.     /** The tempt AI task for this mob, used to prevent taming while it is fleeing. */
  55.     private EntityAITempt aiTempt;
  56.  
  57.     public EntityMintyKitty(World worldIn)
  58.     {
  59.         super(worldIn);
  60.         this.setSize(0.6F, 0.7F);
  61.     }
  62.  
  63.     protected void initEntityAI()
  64.     {
  65.         this.aiSit = new EntityAISit(this);
  66.         this.aiTempt = new EntityAITempt(this, 0.6D, Items.FISH, true);
  67.         this.tasks.addTask(1, new EntityAISwimming(this));
  68.         this.tasks.addTask(2, this.aiSit);
  69.         this.tasks.addTask(3, this.aiTempt);
  70.         this.tasks.addTask(5, new EntityAIFollowOwner(this, 1.0D, 10.0F, 5.0F));
  71.      
  72.         this.tasks.addTask(7, new EntityAILeapAtTarget(this, 0.3F));
  73.         this.tasks.addTask(8, new EntityAIOcelotAttack(this));
  74.         this.tasks.addTask(9, new EntityAIMate(this, 0.8D));
  75.         this.tasks.addTask(10, new EntityAIWanderAvoidWater(this, 0.8D, 1.0000001E-5F));
  76.         this.tasks.addTask(11, new EntityAIWatchClosest(this, EntityPlayer.class, 10.0F));
  77.         this.targetTasks.addTask(1, new EntityAITargetNonTamed(this, EntityChicken.class, false, (Predicate)null));
  78.     }
  79.  
  80.     protected void entityInit()
  81.     {
  82.         super.entityInit();
  83.         this.dataManager.register(KITTY_VARIANT, Integer.valueOf(0));
  84.     }
  85.  
  86.     public void updateAITasks()
  87.     {
  88.         if (this.getMoveHelper().isUpdating())
  89.         {
  90.             double d0 = this.getMoveHelper().getSpeed();
  91.  
  92.             if (d0 == 0.6D)
  93.             {
  94.                 this.setSneaking(true);
  95.                 this.setSprinting(false);
  96.             }
  97.             else if (d0 == 1.33D)
  98.             {
  99.                 this.setSneaking(false);
  100.                 this.setSprinting(true);
  101.             }
  102.             else
  103.             {
  104.                 this.setSneaking(false);
  105.                 this.setSprinting(false);
  106.             }
  107.         }
  108.         else
  109.         {
  110.             this.setSneaking(false);
  111.             this.setSprinting(false);
  112.         }
  113.     }
  114.  
  115.     /**
  116.      * Determines if an entity can be despawned, used on idle far away entities
  117.      */
  118.     protected boolean canDespawn()
  119.     {
  120.         return !this.isTamed() && this.ticksExisted > 2400;
  121.     }
  122.  
  123.     protected void applyEntityAttributes()
  124.     {
  125.         super.applyEntityAttributes();
  126.         this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(10.0D);
  127.         this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.30000001192092896D);
  128.     }
  129.  
  130.     public void fall(float distance, float damageMultiplier)
  131.     {
  132.     }
  133.  
  134.     public static void registerFixesOcelot(DataFixer fixer)
  135.     {
  136.         EntityLiving.registerFixesMob(fixer, EntityMintyKitty.class);
  137.     }
  138.  
  139.     /**
  140.      * (abstract) Protected helper method to write subclass entity data to NBT.
  141.      */
  142.     public void writeEntityToNBT(NBTTagCompound compound)
  143.     {
  144.         super.writeEntityToNBT(compound);
  145.         compound.setInteger("CatType", this.getTameSkin());
  146.     }
  147.  
  148.     /**
  149.      * (abstract) Protected helper method to read subclass entity data from NBT.
  150.      */
  151.     public void readEntityFromNBT(NBTTagCompound compound)
  152.     {
  153.         super.readEntityFromNBT(compound);
  154.         this.setTameSkin(compound.getInteger("CatType"));
  155.     }
  156.  
  157.     @Nullable
  158.     protected SoundEvent getAmbientSound()
  159.     {
  160.         if (this.isTamed())
  161.         {
  162.             if (this.isInLove())
  163.             {
  164.                 return SoundEvents.ENTITY_CAT_PURR;
  165.             }
  166.             else
  167.             {
  168.                 return this.rand.nextInt(4) == 0 ? SoundEvents.ENTITY_CAT_PURREOW : SoundEvents.ENTITY_CAT_AMBIENT;
  169.             }
  170.         }
  171.         else
  172.         {
  173.             return null;
  174.         }
  175.     }
  176.  
  177.     protected SoundEvent getHurtSound(DamageSource damageSourceIn)
  178.     {
  179.         return SoundEvents.ENTITY_CAT_HURT;
  180.     }
  181.  
  182.     protected SoundEvent getDeathSound()
  183.     {
  184.         return SoundEvents.ENTITY_CAT_DEATH;
  185.     }
  186.  
  187.     /**
  188.      * Returns the volume for the sounds this mob makes.
  189.      */
  190.     protected float getSoundVolume()
  191.     {
  192.         return 0.4F;
  193.     }
  194.  
  195.     public boolean attackEntityAsMob(Entity entityIn)
  196.     {
  197.         return entityIn.attackEntityFrom(DamageSource.causeMobDamage(this), 3.0F);
  198.     }
  199.  
  200.     /**
  201.      * Called when the entity is attacked.
  202.      */
  203.     public boolean attackEntityFrom(DamageSource source, float amount)
  204.     {
  205.         if (this.isEntityInvulnerable(source))
  206.         {
  207.             return false;
  208.         }
  209.         else
  210.         {
  211.             if (this.aiSit != null)
  212.             {
  213.                 this.aiSit.setSitting(false);
  214.             }
  215.  
  216.             return super.attackEntityFrom(source, amount);
  217.         }
  218.     }
  219.  
  220.     @Nullable
  221.     protected ResourceLocation getLootTable()
  222.     {
  223.         return LootTableList.ENTITIES_OCELOT;
  224.     }
  225.  
  226.     public boolean processInteract(EntityPlayer player, EnumHand hand)
  227.     {
  228.         ItemStack itemstack = player.getHeldItem(hand);
  229.  
  230.         if (this.isTamed())
  231.         {
  232.             if (this.isOwner(player) && !this.world.isRemote && !this.isBreedingItem(itemstack))
  233.             {
  234.                 this.aiSit.setSitting(!this.isSitting());
  235.             }
  236.         }
  237.         else if ((this.aiTempt == null || this.aiTempt.isRunning()) && itemstack.getItem() == Items.FISH && player.getDistanceSq(this) < 9.0D)
  238.         {
  239.             if (!player.capabilities.isCreativeMode)
  240.             {
  241.                 itemstack.shrink(1);
  242.             }
  243.  
  244.             if (!this.world.isRemote)
  245.             {
  246.                 if (this.rand.nextInt(3) == 0 && !net.minecraftforge.event.ForgeEventFactory.onAnimalTame(this, player))
  247.                 {
  248.                     this.setTamedBy(player);
  249.                     this.setTameSkin(1 + this.world.rand.nextInt(3));
  250.                     this.playTameEffect(true);
  251.                     this.aiSit.setSitting(true);
  252.                     this.world.setEntityState(this, (byte)7);
  253.                 }
  254.                 else
  255.                 {
  256.                     this.playTameEffect(false);
  257.                     this.world.setEntityState(this, (byte)6);
  258.                 }
  259.             }
  260.  
  261.             return true;
  262.         }
  263.  
  264.         return super.processInteract(player, hand);
  265.     }
  266.  
  267.     public EntityMintyKitty createChild(EntityAgeable ageable)
  268.     {
  269.         EntityMintyKitty minty_kitty = new EntityMintyKitty(this.world);
  270.  
  271.         if (this.isTamed())
  272.         {
  273.             minty_kitty.setOwnerId(this.getOwnerId());
  274.             minty_kitty.setTamed(true);
  275.             minty_kitty.setTameSkin(this.getTameSkin());
  276.         }
  277.  
  278.         return minty_kitty;
  279.     }
  280.  
  281.     /**
  282.      * Checks if the parameter is an item which this animal can be fed to breed it (wheat, carrots or seeds depending on
  283.      * the animal type)
  284.      */
  285.     public boolean isBreedingItem(ItemStack stack)
  286.     {
  287.         return stack.getItem() == Items.FISH;
  288.     }
  289.  
  290.     /**
  291.      * Returns true if the mob is currently able to mate with the specified mob.
  292.      */
  293.     public boolean canMateWith(EntityAnimal otherAnimal)
  294.     {
  295.         if (otherAnimal == this)
  296.         {
  297.             return false;
  298.         }
  299.         else if (!this.isTamed())
  300.         {
  301.             return false;
  302.         }
  303.         else if (!(otherAnimal instanceof EntityMintyKitty))
  304.         {
  305.             return false;
  306.         }
  307.         else
  308.         {
  309.             EntityMintyKitty minty_kitty = (EntityMintyKitty)otherAnimal;
  310.  
  311.             if (!minty_kitty.isTamed())
  312.             {
  313.                 return false;
  314.             }
  315.             else
  316.             {
  317.                 return this.isInLove() && minty_kitty.isInLove();
  318.             }
  319.         }
  320.     }
  321.  
  322.     public int getTameSkin()
  323.     {
  324.         return ((Integer)this.dataManager.get(KITTY_VARIANT)).intValue();
  325.     }
  326.  
  327.     public void setTameSkin(int skinId)
  328.     {
  329.         this.dataManager.set(KITTY_VARIANT, Integer.valueOf(skinId));
  330.     }
  331.  
  332.     /**
  333.      * Checks if the entity's current position is a valid location to spawn this entity.
  334.      */
  335.     public boolean getCanSpawnHere()
  336.     {
  337.         return this.world.rand.nextInt(3) != 0;
  338.     }
  339.  
  340.     /**
  341.      * Checks that the entity is not colliding with any blocks / liquids
  342.      */
  343.     public boolean isNotColliding()
  344.     {
  345.         if (this.world.checkNoEntityCollision(this.getEntityBoundingBox(), this) && this.world.getCollisionBoxes(this, this.getEntityBoundingBox()).isEmpty() && !this.world.containsAnyLiquid(this.getEntityBoundingBox()))
  346.         {
  347.             BlockPos blockpos = new BlockPos(this.posX, this.getEntityBoundingBox().minY, this.posZ);
  348.  
  349.             if (blockpos.getY() < this.world.getSeaLevel())
  350.             {
  351.                 return false;
  352.             }
  353.  
  354.             IBlockState iblockstate = this.world.getBlockState(blockpos.down());
  355.             Block block = iblockstate.getBlock();
  356.  
  357.             if (block == Blocks.GRASS || block.isLeaves(iblockstate, this.world, blockpos.down()))
  358.             {
  359.                 return true;
  360.             }
  361.         }
  362.  
  363.         return false;
  364.     }
  365.  
  366.     /**
  367.      * Get the name of this object. For players this returns their username
  368.      */
  369.     public String getName()
  370.     {
  371.         if (this.hasCustomName())
  372.         {
  373.             return this.getCustomNameTag();
  374.         }
  375.         else
  376.         {
  377.             return this.isTamed() ? I18n.translateToLocal("entity.Cat.name") : super.getName();
  378.         }
  379.     }
  380.  
  381.     protected void setupTamedAI()
  382.     {
  383.         if (this.avoidEntity == null)
  384.         {
  385.             this.avoidEntity = new EntityAIAvoidEntity<EntityPlayer>(this, EntityPlayer.class, 16.0F, 0.8D, 1.33D);
  386.         }
  387.  
  388.         this.tasks.removeTask(this.avoidEntity);
  389.  
  390.         if (!this.isTamed())
  391.         {
  392.             this.tasks.addTask(4, this.avoidEntity);
  393.         }
  394.     }
  395.  
  396.     /**
  397.      * Called only once on an entity when first time spawned, via egg, mob spawner, natural spawning etc, but not called
  398.      * when entity is reloaded from nbt. Mainly used for initializing attributes and inventory
  399.      */
  400.     @Nullable
  401.     public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata)
  402.     {
  403.         livingdata = super.onInitialSpawn(difficulty, livingdata);
  404.  
  405.         if (this.getTameSkin() == 0 && this.world.rand.nextInt(7) == 0)
  406.         {
  407.             for (int i = 0; i < 2; ++i)
  408.             {
  409.                 EntityMintyKitty minty_kitty = new EntityMintyKitty(this.world);
  410.                 minty_kitty.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotationYaw, 0.0F);
  411.                 minty_kitty.setGrowingAge(-24000);
  412.                 this.world.spawnEntity(minty_kitty);
  413.             }
  414.         }
  415.  
  416.         return livingdata;
  417.     }
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement