Advertisement
Guest User

Untitled

a guest
Jul 9th, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.62 KB | None | 0 0
  1. package me.geometrically.prehistoric.entity.entities;
  2.  
  3. import com.dabigjoe.obsidianAPI.animation.wrapper.IEntityAnimated;
  4. import com.google.common.base.Predicate;
  5. import me.geometrically.prehistoric.block.PEBlocks;
  6. import me.geometrically.prehistoric.entity.ai.EntityAIDinoMate;
  7. import me.geometrically.prehistoric.entity.ai.EntityAIGuardNest;
  8. import me.geometrically.prehistoric.entity.ai.animation.EntityAICall;
  9. import me.geometrically.prehistoric.entity.ai.animation.EntityAIEat;
  10. import me.geometrically.prehistoric.entity.ai.animation.EntityAIStartle;
  11. import me.geometrically.prehistoric.entity.entities.dakotaraptor.EntityDakotaraptor;
  12. import net.minecraft.block.Block;
  13. import net.minecraft.entity.*;
  14. import net.minecraft.entity.ai.*;
  15. import net.minecraft.entity.passive.EntityAnimal;
  16. import net.minecraft.entity.passive.EntityTameable;
  17. import net.minecraft.entity.player.EntityPlayer;
  18. import net.minecraft.init.Items;
  19. import net.minecraft.item.ItemFood;
  20. import net.minecraft.item.ItemStack;
  21. import net.minecraft.util.DamageSource;
  22. import net.minecraft.util.EnumHand;
  23. import net.minecraft.util.math.BlockPos;
  24. import net.minecraft.util.math.MathHelper;
  25. import net.minecraft.world.World;
  26.  
  27. import javax.annotation.Nullable;
  28. import java.util.List;
  29. import java.util.Random;
  30.  
  31. public class EntityDinosaurTameable extends EntityTameable implements IEntityAnimated {
  32.  
  33.     public BlockPos nestBlockPos;
  34.     public Block nest;
  35.  
  36.     protected EntityAIEat aiEat;
  37.     protected EntityAIStartle aiStartle;
  38.     protected EntityAIWatchClosest aiWatchClosest;
  39.     protected EntityAICall aiCall;
  40.  
  41.     public boolean running;
  42.     public boolean attacking;
  43.     private int counter = 0;
  44.    
  45.     public EntityDinosaurTameable(World world) {
  46.         super(world);
  47.     }
  48.  
  49.     @Override
  50.     protected void initEntityAI() {
  51.         this.aiSit = new EntityAISit(this);
  52.         this.aiWatchClosest = new EntityAIWatchClosest(this, EntityPlayer.class, 25.0F);
  53.         this.tasks.addTask(0, new EntityAISwimming(this));
  54.         this.tasks.addTask(4, new EntityAILeapAtTarget(this, 0.4F));
  55.         this.tasks.addTask(5, new EntityAIAttackMelee(this, 1.0D, true));
  56.         this.tasks.addTask(6, new EntityDinosaurTameable.EntityAIGuardNest(this, 15.0D, 1.0D));
  57.         this.tasks.addTask(7, new EntityAIFollowOwner(this, 1.0D, 10.0F, 2.0F));
  58.         this.tasks.addTask(9, new EntityAIWanderAvoidWater(this, 1.0D));
  59.         this.tasks.addTask(10, aiWatchClosest);
  60.         this.tasks.addTask(10, new EntityAILookIdle(this));
  61.         this.targetTasks.addTask(1, new EntityAIOwnerHurtByTarget(this));
  62.         this.targetTasks.addTask(2, new EntityAIOwnerHurtTarget(this));
  63.         this.targetTasks.addTask(3, new EntityAIHurtByTarget(this, true, new Class[0]));
  64.     }
  65.  
  66.     @Override
  67.     public void onLivingUpdate() {
  68.         super.onLivingUpdate();
  69.         if (this.aiWatchClosest != null) {
  70.             if (this.aiWatchClosest.shouldExecute()) {
  71.                 this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.3500001192092896D);
  72.                 this.running = true;
  73.             } else {
  74.                 this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.3000001192092896D);
  75.                 this.running = false;
  76.             }
  77.         }
  78.         if (this.attacking) {
  79.             this.counter++;
  80.             if (this.counter >= 20) {
  81.                 this.attacking = false;
  82.             }
  83.         } else {
  84.             this.attacking = false;
  85.             this.counter = 0;
  86.         }
  87.  
  88.         if(!this.isInLove() && this.growingAge == 0){
  89.             if(this.getRNG().nextInt(1) == 0){
  90.                 this.setInLove(null);
  91.             }
  92.         }
  93.  
  94.     }
  95.  
  96.     @Override
  97.     public boolean attackEntityFrom(DamageSource source, float amount) {
  98.         if (this.aiStartle != null) {
  99.             this.aiStartle.execute = true;
  100.         }
  101.         if (this.getHealth() < 20.0F && this.getHealth() + amount > 20.0F && !this.isTamed() && !this.isOwner((EntityLivingBase) source.getTrueSource())) {
  102.             if (this.aiCall != null) {
  103.                 this.aiCall.execute = true;
  104.                 if (source.getTrueSource() != null) {
  105.                     double distance2 = 32.0D;
  106.                     double d4 = -1.0D;
  107.                     for (int i = 0; i < this.world.loadedEntityList.size(); ++i) {
  108.                         Entity currE = (Entity) world.loadedEntityList.get(i);
  109.  
  110.                         if (currE instanceof EntityDakotaraptor && currE != this) {
  111.                             double d5 = currE.getDistanceSq(this.posX, this.posY, this.posZ);
  112.                             if ((distance2 < 0.0D || d5 < distance2 * distance2) && (d4 == -1.0D || d5 < d4)) {
  113.                                 d4 = d5;
  114.                                 if (source.getTrueSource() instanceof EntityLivingBase)
  115.                                     ((EntityDakotaraptor) currE).setAttackTarget((EntityLivingBase) source.getTrueSource());
  116.                             }
  117.                         }
  118.                     }
  119.                 }
  120.             }
  121.         }
  122.         return super.attackEntityFrom(source, amount);
  123.     }
  124.  
  125.     @Override
  126.     public boolean attackEntityAsMob(Entity entityIn) {
  127.         this.attacking = true;
  128.         boolean flag = entityIn.attackEntityFrom(DamageSource.causeMobDamage(this), (float) ((int) this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).getAttributeValue()));
  129.  
  130.         if (flag) {
  131.             this.applyEnchantments(this, entityIn);
  132.         }
  133.         return flag;
  134.     }
  135.  
  136.     @Override
  137.     public boolean processInteract(EntityPlayer player, EnumHand hand) {
  138.         ItemStack itemstack = player.getHeldItem(hand);
  139.         if (this.isTamed()) {
  140.             if (!itemstack.isEmpty()) {
  141.                 if (itemstack.getItem() instanceof ItemFood) {
  142.                     ItemFood itemfood = (ItemFood) itemstack.getItem();
  143.  
  144.                     if (itemfood.isWolfsFavoriteMeat()) {
  145.                         if (this.aiEat != null) {
  146.                             this.aiEat.execute = true;
  147.                         }
  148.                         if (!player.capabilities.isCreativeMode) {
  149.                             itemstack.shrink(1);
  150.                         }
  151.  
  152.                         this.heal((float) itemfood.getHealAmount(itemstack));
  153.                         return true;
  154.                     }
  155.                 }
  156.             }
  157.  
  158.             if (this.isOwner(player) && !this.world.isRemote && !this.isBreedingItem(itemstack)) {
  159.                 this.aiSit.setSitting(!this.isSitting());
  160.                 this.isJumping = false;
  161.                 this.navigator.clearPathEntity();
  162.                 this.setAttackTarget((EntityLivingBase) null);
  163.             }
  164.         } else if (itemstack.getItem() == Items.BONE && this.isBaby())
  165.         {
  166.             if (!player.capabilities.isCreativeMode)
  167.             {
  168.                 itemstack.shrink(1);
  169.             }
  170.  
  171.             if (!this.world.isRemote)
  172.             {
  173.                 if (this.rand.nextInt(3) == 0 && !net.minecraftforge.event.ForgeEventFactory.onAnimalTame(this, player))
  174.                 {
  175.                     this.setTamedBy(player);
  176.                     this.navigator.clearPathEntity();
  177.                     this.setAttackTarget((EntityLivingBase)null);
  178.                     this.aiSit.setSitting(true);
  179.                     this.setHealth(20.0F);
  180.                     this.playTameEffect(true);
  181.                     this.world.setEntityState(this, (byte)7);
  182.                 }
  183.                 else
  184.                 {
  185.                     this.playTameEffect(false);
  186.                     this.world.setEntityState(this, (byte)6);
  187.                 }
  188.             }
  189.  
  190.             return true;
  191.         }
  192.  
  193.         return super.processInteract(player, hand);
  194.     }
  195.  
  196.     @Override
  197.     public EntityDinosaurTameable createChild(EntityAgeable ageable) {
  198.         return new EntityDinosaurTameable(this.world);
  199.     }
  200.  
  201.     @Override
  202.     public boolean isMoving() {
  203.         return this.limbSwingAmount > 0.02F;
  204.     }
  205.  
  206.     public boolean isBaby(){
  207.         return false;
  208.     }
  209.  
  210.     @Override
  211.     public boolean isBreedingItem(ItemStack stack)
  212.     {
  213.         return false;
  214.     }
  215.  
  216.     public class EntityAIDinoMate extends EntityAIBase {
  217.         private final EntityDinosaurTameable animal;
  218.         private final Class <? extends EntityDinosaurTameable> mateClass;
  219.         World world;
  220.         private EntityDinosaurTameable targetMate;
  221.         int spawnBabyDelay;
  222.         double moveSpeed;
  223.  
  224.  
  225.         public EntityAIDinoMate(EntityDinosaurTameable animal, double moveSpeed)
  226.         {
  227.             this.animal = animal;
  228.             this.world = animal.world;
  229.             this.mateClass = animal.getClass();
  230.             this.moveSpeed = moveSpeed;
  231.             this.setMutexBits(3);
  232.         }
  233.  
  234.         /**
  235.          * Returns whether the EntityAIBase should begin execution.
  236.          */
  237.         public boolean shouldExecute()
  238.         {
  239.             if (!this.animal.isInLove())
  240.             {
  241.                 return false;
  242.             }
  243.             else
  244.             {
  245.                 this.targetMate = this.getNearbyMate();
  246.                 return this.targetMate != null && !this.animal.isTamed();
  247.             }
  248.         }
  249.  
  250.         /**
  251.          * Returns whether an in-progress EntityAIBase should continue executing
  252.          */
  253.         public boolean shouldContinueExecuting()
  254.         {
  255.             return this.targetMate.isEntityAlive() && this.targetMate.isInLove() && this.spawnBabyDelay < 60;
  256.         }
  257.  
  258.         /**
  259.          * Reset the task's internal state. Called when this task is interrupted by another one
  260.          */
  261.         public void resetTask()
  262.         {
  263.             this.targetMate = null;
  264.             this.spawnBabyDelay = 0;
  265.         }
  266.  
  267.         /**
  268.          * Keep ticking a continuous task that has already been started
  269.          */
  270.         public void updateTask()
  271.         {
  272.             this.animal.getLookHelper().setLookPositionWithEntity(this.targetMate, 10.0F, (float)this.animal.getVerticalFaceSpeed());
  273.             this.animal.getNavigator().tryMoveToEntityLiving(this.targetMate, this.moveSpeed);
  274.             ++this.spawnBabyDelay;
  275.  
  276.             if (this.spawnBabyDelay >= 60 && this.animal.getDistanceSqToEntity(this.targetMate) < 9.0D)
  277.             {
  278.                 this.spawnNestWithEggs();
  279.             }
  280.         }
  281.  
  282.         /**
  283.          * Loops through nearby animals and finds another animal of the same type that can be mated with. Returns the first
  284.          * valid mate found.
  285.          */
  286.         private EntityDinosaurTameable getNearbyMate()
  287.         {
  288.             List<EntityDinosaurTameable> list = this.world.<EntityDinosaurTameable>getEntitiesWithinAABB(this.mateClass, this.animal.getEntityBoundingBox().grow(8.0D));
  289.             double d0 = Double.MAX_VALUE;
  290.             EntityDinosaurTameable entityanimal = null;
  291.  
  292.             for (EntityDinosaurTameable entityanimal1 : list)
  293.             {
  294.                 if (this.animal.canMateWith(entityanimal1) && this.animal.getDistanceSqToEntity(entityanimal1) < d0)
  295.                 {
  296.                     entityanimal = entityanimal1;
  297.                     d0 = this.animal.getDistanceSqToEntity(entityanimal1);
  298.                 }
  299.             }
  300.  
  301.             return entityanimal;
  302.         }
  303.         private void spawnNestWithEggs(){
  304.             Block nest = PEBlocks.nest;
  305.             this.animal.nest = nest;
  306.             this.targetMate.nest = nest;
  307.  
  308.             BlockPos entityPos = new BlockPos(this.animal.posX, this.animal.posY, this.animal.posZ);
  309.             this.animal.nestBlockPos = entityPos;
  310.             this.targetMate.nestBlockPos = entityPos;
  311.  
  312.             double posX= entityPos.getX() + 0.5D;
  313.             double posY= entityPos.getY() + 0.1D;
  314.             double posZ= entityPos.getZ() + 0.5D;
  315.             world.destroyBlock(entityPos, false);
  316.             world.setBlockState(entityPos, nest.getDefaultState());
  317.             Random random = this.animal.getRNG();
  318.             int eggs = this.animal.getRNG().nextInt(3) + 4;
  319.             EntityEgg egg1 = new EntityEgg(this.world, this.animal.createChild(targetMate));
  320.             EntityEgg egg2 = new EntityEgg(this.world, this.animal.createChild(targetMate));
  321.             EntityEgg egg3 = new EntityEgg(this.world, this.animal.createChild(targetMate));
  322.             EntityEgg egg4 = new EntityEgg(this.world, this.animal.createChild(targetMate));
  323.             egg1.setPositionAndRotation(posX + 0.25D, posY, posZ, random.nextFloat(), 0.125F);
  324.             egg2.setPositionAndRotation(posX - 0.25D, posY, posZ, random.nextFloat(), 0.125F);
  325.             egg3.setPositionAndRotation(posX, posY , posZ - 0.25F, random.nextFloat(), 0.125F);
  326.             egg4.setPositionAndRotation(posX, posY, posZ + 0.25F, random.nextFloat(), 0.125F);
  327.             this.world.spawnEntity(egg1);
  328.             this.world.spawnEntity(egg2);
  329.             this.world.spawnEntity(egg3);
  330.             this.world.spawnEntity(egg4);
  331.  
  332.             if(eggs >= 5){
  333.                 EntityEgg egg5 = new EntityEgg(this.world, this.animal.createChild(targetMate));
  334.                 egg5.setPositionAndRotation(posX + 0.1F, posY, posZ + 0.1F, random.nextFloat(), 0.125F);
  335.                 this.world.spawnEntity(egg5);
  336.             }
  337.             if(eggs == 6){
  338.                 EntityEgg egg6 = new EntityEgg(this.world, this.animal.createChild(targetMate));
  339.                 egg6.setPositionAndRotation(posX - 0.1F, posY, posZ - 0.1F, random.nextFloat(), 0.125F);
  340.                 this.world.spawnEntity(egg6);
  341.             }
  342.  
  343.             this.animal.resetInLove();
  344.             this.targetMate.resetInLove();
  345.             this.animal.setGrowingAge(6000);
  346.             this.targetMate.setGrowingAge(6000);
  347.         }
  348.     }
  349.  
  350.     public class EntityAIGuardNest extends EntityAIBase {
  351.  
  352.         private EntityDinosaurTameable dinosaur;
  353.  
  354.         World world;
  355.  
  356.         private double speed;
  357.         private double distance;
  358.  
  359.         public EntityAIGuardNest(EntityDinosaurTameable dinosaur, double distance ,double speed){
  360.             this.dinosaur = dinosaur;
  361.             this.world = this.dinosaur.world;
  362.  
  363.             this.speed = speed;
  364.             this.distance = distance;
  365.         }
  366.  
  367.         @Override
  368.         public boolean shouldExecute(){
  369.             return this.dinosaur.nest != null && !this.dinosaur.isTamed() && this.dinosaur.nestBlockPos != null;
  370.         }
  371.  
  372.         @Override
  373.         public void updateTask(){
  374.             if(MathHelper.sqrt(this.dinosaur.getDistanceSq(this.dinosaur.nestBlockPos)) > this.distance){
  375.                 System.out.println("test");
  376.                 this.dinosaur.getNavigator().tryMoveToXYZ(this.dinosaur.nestBlockPos.getX(), this.dinosaur.nestBlockPos.getY(), this.dinosaur.nestBlockPos.getZ(), this.speed);
  377.             } else {
  378.                 System.out.println("test2");
  379.                 List<EntityLiving> list = this.dinosaur.world.<EntityLiving>getEntitiesWithinAABB(EntityLiving.class, this.dinosaur.nest.getDefaultState().getCollisionBoundingBox(this.world, this.dinosaur.nestBlockPos).grow(this.distance));
  380.  
  381.                 for (EntityLiving attacker : list)
  382.                 {
  383.                     if (!(attacker instanceof EntityEgg) && !(attacker.getClass() != this.dinosaur.getClass()) && MathHelper.sqrt(attacker.getDistanceSq(this.dinosaur.nestBlockPos)) < this.distance)
  384.                     {
  385.                         this.dinosaur.setAttackTarget(attacker);
  386.                         break;
  387.                     }
  388.                 }
  389.             }
  390.         }
  391.  
  392.         @Override
  393.         public boolean shouldContinueExecuting()
  394.         {
  395.             return this.dinosaur.nest != null;
  396.         }
  397.     }
  398.  
  399. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement