Advertisement
hassansyyid

EntityWarthogTurret

Jul 5th, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.08 KB | None | 0 0
  1. package halocraft.entities;
  2.  
  3. import halocraft.Main;
  4.  
  5. import java.util.List;
  6.  
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.client.model.ModelBase;
  10. import net.minecraft.entity.Entity;
  11. import net.minecraft.entity.EntityLivingBase;
  12. import net.minecraft.entity.player.EntityPlayer;
  13. import net.minecraft.init.Blocks;
  14. import net.minecraft.init.Items;
  15. import net.minecraft.item.Item;
  16. import net.minecraft.nbt.NBTTagCompound;
  17. import net.minecraft.util.AxisAlignedBB;
  18. import net.minecraft.util.BlockPos;
  19. import net.minecraft.util.DamageSource;
  20. import net.minecraft.util.EntityDamageSourceIndirect;
  21. import net.minecraft.util.EnumParticleTypes;
  22. import net.minecraft.util.MathHelper;
  23. import net.minecraft.world.World;
  24. import net.minecraftforge.fml.relauncher.Side;
  25. import net.minecraftforge.fml.relauncher.SideOnly;
  26.  
  27. public class EntityWarthogTurret extends Entity {
  28.     private boolean isBoatEmpty;
  29.     private double speedMultiplier;
  30.     private int boatPosRotationIncrements;
  31.     private double boatX;
  32.     private double boatY;
  33.     private double boatZ;
  34.     private double boatYaw;
  35.     private double boatPitch;@SideOnly(Side.CLIENT)
  36.     private double velocityX;@SideOnly(Side.CLIENT)
  37.     private double velocityY;@SideOnly(Side.CLIENT)
  38.     private double velocityZ;
  39.     public EntityPlayer secondRider;
  40.     public EntityPlayer thirdRider;
  41.  
  42.     public EntityWarthogTurret(World worldIn) {
  43.         super(worldIn);
  44.         this.isBoatEmpty = true;
  45.         this.speedMultiplier = 0.07D;
  46.         this.preventEntitySpawning = true;
  47.         this.setSize(1.5F, 0.6F);
  48.     }
  49.  
  50.     protected boolean canTriggerWalking() {
  51.         return false;
  52.     }
  53.  
  54.     protected void entityInit() {
  55.         this.dataWatcher.addObject(17, new Integer(0));
  56.         this.dataWatcher.addObject(18, new Integer(1));
  57.         this.dataWatcher.addObject(19, new Float(0.0F));
  58.     }
  59.  
  60.     public AxisAlignedBB getCollisionBox(Entity entityIn) {
  61.         return entityIn.getEntityBoundingBox();
  62.     }
  63.  
  64.     public AxisAlignedBB getBoundingBox() {
  65.         return this.getEntityBoundingBox();
  66.     }
  67.  
  68.     public boolean canBePushed() {
  69.         return true;
  70.     }
  71.  
  72.     public EntityWarthogTurret(World worldIn, double x, double y, double z) {
  73.         this(worldIn);
  74.         this.setPosition(x, y, z);
  75.         this.motionX = 0.0D;
  76.         this.motionY = 0.0D;
  77.         this.motionZ = 0.0D;
  78.         this.prevPosX = x;
  79.         this.prevPosY = y;
  80.         this.prevPosZ = z;
  81.     }
  82.  
  83.     public double getMountedYOffset() {
  84.         return (double) this.height * 0.0D + 0.8D;
  85.     }
  86.  
  87.     public boolean attackEntityFrom(DamageSource source, float amount) {
  88.         if (this.isEntityInvulnerable(source)) {
  89.             return false;
  90.         } else if (!this.worldObj.isRemote && !this.isDead) {
  91.             if (this.riddenByEntity != null && this.riddenByEntity == source.getEntity() && source instanceof EntityDamageSourceIndirect) {
  92.                 return false;
  93.             } else {
  94.                 this.setForwardDirection(-this.getForwardDirection());
  95.                 this.setTimeSinceHit(10);
  96.                 this.setDamageTaken(this.getDamageTaken() + amount * 10.0F);
  97.                 this.setBeenAttacked();
  98.                 boolean flag = source.getEntity() instanceof EntityPlayer && ((EntityPlayer) source.getEntity()).capabilities.isCreativeMode;
  99.  
  100.                 if (flag || this.getDamageTaken() > 40.0F) {
  101.                     if (this.riddenByEntity != null) {
  102.                         this.riddenByEntity.mountEntity(this);
  103.                     }
  104.  
  105.                     if (!flag) {
  106.                         this.dropItemWithOffset(halocraft.Main.itemWarthog, 1, 0.0F);
  107.                     }
  108.  
  109.                     this.setDead();
  110.                 }
  111.  
  112.                 return true;
  113.             }
  114.         } else {
  115.             return true;
  116.         }
  117.     }
  118.  
  119.     @SideOnly(Side.CLIENT)
  120.     public void performHurtAnimation() {
  121.         this.setForwardDirection(-this.getForwardDirection());
  122.         this.setTimeSinceHit(10);
  123.         this.setDamageTaken(this.getDamageTaken() * 11.0F);
  124.     }
  125.  
  126.     public boolean canBeCollidedWith() {
  127.         return !this.isDead;
  128.     }
  129.  
  130.     public World getWorldObj() {
  131.         return this.worldObj;
  132.     }
  133.  
  134.     @SideOnly(Side.CLIENT)
  135.     public void func_180426_a(double p_180426_1_, double p_180426_3_, double p_180426_5_, float p_180426_7_, float p_180426_8_, int p_180426_9_, boolean p_180426_10_) {
  136.         if (p_180426_10_ && this.riddenByEntity != null) {
  137.             this.prevPosX = this.posX = p_180426_1_;
  138.             this.prevPosY = this.posY = p_180426_3_;
  139.             this.prevPosZ = this.posZ = p_180426_5_;
  140.             this.rotationYaw = p_180426_7_;
  141.             this.rotationPitch = p_180426_8_;
  142.             this.boatPosRotationIncrements = 0;
  143.             this.setPosition(p_180426_1_, p_180426_3_, p_180426_5_);
  144.             this.motionX = this.velocityX = 0.0D;
  145.             this.motionY = this.velocityY = 0.0D;
  146.             this.motionZ = this.velocityZ = 0.0D;
  147.         } else {
  148.             if (this.isBoatEmpty) {
  149.                 this.boatPosRotationIncrements = p_180426_9_ + 5;
  150.             } else {
  151.                 double d3 = p_180426_1_ - this.posX;
  152.                 double d4 = p_180426_3_ - this.posY;
  153.                 double d5 = p_180426_5_ - this.posZ;
  154.                 double d6 = d3 * d3 + d4 * d4 + d5 * d5;
  155.  
  156.                 if (d6 <= 1.0D) {
  157.                     return;
  158.                 }
  159.  
  160.                 this.boatPosRotationIncrements = 3;
  161.             }
  162.  
  163.             this.boatX = p_180426_1_;
  164.             this.boatY = p_180426_3_;
  165.             this.boatZ = p_180426_5_;
  166.             this.boatYaw = (double) p_180426_7_;
  167.             this.boatPitch = (double) p_180426_8_;
  168.             this.motionX = this.velocityX;
  169.             this.motionY = this.velocityY;
  170.             this.motionZ = this.velocityZ;
  171.         }
  172.     }
  173.  
  174.     @SideOnly(Side.CLIENT)
  175.     public void setVelocity(double x, double y, double z) {
  176.         this.velocityX = this.motionX = x;
  177.         this.velocityY = this.motionY = y;
  178.         this.velocityZ = this.motionZ = z;
  179.     }
  180.  
  181.     public void onUpdate() {
  182.         super.onUpdate();
  183.  
  184.         if (this.getTimeSinceHit() > 0) {
  185.             this.setTimeSinceHit(this.getTimeSinceHit() - 1);
  186.         }
  187.  
  188.         if (this.getDamageTaken() > 0.0F) {
  189.             this.setDamageTaken(this.getDamageTaken() - 1.0F);
  190.         }
  191.  
  192.         if (this.riddenByEntity == null && this.secondRider != null) {
  193.             if (!this.getWorldObj().isRemote) {
  194.                 this.secondRider.dismountEntity(this);
  195.             }
  196.             this.secondRider = null;
  197.         }
  198.  
  199.         if (this.riddenByEntity == null && this.secondRider == null && this.thirdRider != null) {
  200.             if (!this.getWorldObj().isRemote) {
  201.                 this.thirdRider.dismountEntity(this);
  202.             }
  203.             this.thirdRider = null;
  204.         }
  205.  
  206.         this.prevPosX = this.posX;
  207.         this.prevPosY = this.posY;
  208.         this.prevPosZ = this.posZ;
  209.         byte b0 = 5;
  210.         double d0 = 0.0D;
  211.  
  212.         for (int i = 0; i < b0; ++i) {
  213.             double d1 = this.getEntityBoundingBox().minY + (this.getEntityBoundingBox().maxY - this.getEntityBoundingBox().minY) * (double)(i + 0) / (double) b0 - 0.125D;
  214.             double d3 = this.getEntityBoundingBox().minY + (this.getEntityBoundingBox().maxY - this.getEntityBoundingBox().minY) * (double)(i + 1) / (double) b0 - 0.125D;
  215.             AxisAlignedBB axisalignedbb = new AxisAlignedBB(this.getEntityBoundingBox().minX, d1, this.getEntityBoundingBox().minZ, this.getEntityBoundingBox().maxX, d3, this.getEntityBoundingBox().maxZ);
  216.         }
  217.  
  218.         double d9 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
  219.         double d2;
  220.         double d4;
  221.         int j;
  222.  
  223.         if (d9 > 0.2975D) {
  224.             d2 = Math.cos((double) this.rotationYaw * Math.PI / 180.0D);
  225.             d4 = Math.sin((double) this.rotationYaw * Math.PI / 180.0D);
  226.  
  227.             for (j = 0;
  228.             (double) j < 1.0D + d9 * 60.0D; ++j) {
  229.                 double d5 = (double)(this.rand.nextFloat() * 2.0F - 1.0F);
  230.                 double d6 = (double)(this.rand.nextInt(2) * 2 - 1) * 0.7D;
  231.                 double d7;
  232.                 double d8;
  233.             }
  234.         }
  235.  
  236.         double d10;
  237.         double d11;
  238.  
  239.         if (this.worldObj.isRemote && this.isBoatEmpty) {
  240.             if (this.boatPosRotationIncrements > 0) {
  241.                 d2 = this.posX + (this.boatX - this.posX) / (double) this.boatPosRotationIncrements;
  242.                 d4 = this.posY + (this.boatY - this.posY) / (double) this.boatPosRotationIncrements;
  243.                 d10 = this.posZ + (this.boatZ - this.posZ) / (double) this.boatPosRotationIncrements;
  244.                 d11 = MathHelper.wrapAngleTo180_double(this.boatYaw - (double) this.rotationYaw);
  245.                 this.rotationYaw = (float)((double) this.rotationYaw + d11 / (double) this.boatPosRotationIncrements);
  246.                 this.rotationPitch = (float)((double) this.rotationPitch + (this.boatPitch - (double) this.rotationPitch) / (double) this.boatPosRotationIncrements);
  247.                 --this.boatPosRotationIncrements;
  248.                 this.setPosition(d2, d4, d10);
  249.                 this.setRotation(this.rotationYaw, this.rotationPitch);
  250.             } else {
  251.                 d2 = this.posX + this.motionX;
  252.                 d4 = this.posY + this.motionY;
  253.                 d10 = this.posZ + this.motionZ;
  254.                 this.setPosition(d2, d4, d10);
  255.                 this.motionX *= 0.9900000095367432D;
  256.                 this.motionY *= 0.949999988079071D;
  257.                 this.motionZ *= 0.9900000095367432D;
  258.             }
  259.         } else {
  260.             if (d0 < 1.0D) {
  261.                 d2 = d0 * 2.0D - 1.0D;
  262.                 this.motionY += 0.03999999910593033D * d2;
  263.             } else {
  264.                 if (this.motionY < 0.0D) {
  265.                     this.motionY /= 2.0D;
  266.                 }
  267.  
  268.                 this.motionY += 0.007000000216066837D;
  269.             }
  270.  
  271.             if (this.riddenByEntity instanceof EntityLivingBase) {
  272.                 EntityLivingBase entitylivingbase = (EntityLivingBase) this.riddenByEntity;
  273.                 float f = this.riddenByEntity.rotationYaw + -entitylivingbase.moveStrafing * 90.0F;
  274.                 this.motionX += -Math.sin((double)(f * (float) Math.PI / 180.0F)) * this.speedMultiplier * (double) entitylivingbase.moveForward * 0.05000000074505806D;
  275.                 this.motionZ += Math.cos((double)(f * (float) Math.PI / 180.0F)) * this.speedMultiplier * (double) entitylivingbase.moveForward * 0.05000000074505806D;
  276.             }
  277.  
  278.             d2 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
  279.  
  280.             if (d2 > 0.35D) {
  281.                 d4 = 0.35D / d2;
  282.                 this.motionX *= d4;
  283.                 this.motionZ *= d4;
  284.                 d2 = 0.35D;
  285.             }
  286.  
  287.             if (d2 > d9 && this.speedMultiplier < 0.35D) {
  288.                 this.speedMultiplier += (0.35D - this.speedMultiplier) / 35.0D;
  289.  
  290.                 if (this.speedMultiplier > 0.35D) {
  291.                     this.speedMultiplier = 0.35D;
  292.                 }
  293.             } else {
  294.                 this.speedMultiplier -= (this.speedMultiplier - 0.07D) / 35.0D;
  295.  
  296.                 if (this.speedMultiplier < 0.07D) {
  297.                     this.speedMultiplier = 0.07D;
  298.                 }
  299.             }
  300.  
  301.             int l;
  302.  
  303.             for (l = 0; l < 4; ++l) {
  304.                 int i1 = MathHelper.floor_double(this.posX + ((double)(l % 2) - 0.5D) * 0.8D);
  305.                 j = MathHelper.floor_double(this.posZ + ((double)(l / 2) - 0.5D) * 0.8D);
  306.  
  307.                 for (int j1 = 0; j1 < 2; ++j1) {
  308.                     int k = MathHelper.floor_double(this.posY) + j1;
  309.                     BlockPos blockpos = new BlockPos(i1, k, j);
  310.                     Block block = this.worldObj.getBlockState(blockpos).getBlock();
  311.  
  312.                     if (block == Blocks.snow_layer) {
  313.                         this.worldObj.setBlockToAir(blockpos);
  314.                         this.isCollidedHorizontally = false;
  315.                     } else if (block == Blocks.waterlily) {
  316.                         this.worldObj.destroyBlock(blockpos, true);
  317.                         this.isCollidedHorizontally = false;
  318.                     }
  319.                 }
  320.             }
  321.  
  322.             this.moveEntity(this.motionX, this.motionY, this.motionZ);
  323.  
  324.             if (this.isCollidedHorizontally && d9 > 0.2D) {
  325.                 if (!this.worldObj.isRemote && !this.isDead) {
  326.                     this.setDead();
  327.                     for (l = 0; l < 2; ++l) {
  328.                         this.dropItemWithOffset(halocraft.Main.itemWarthog, 2, 0.0F);
  329.                     }
  330.                 }
  331.             } else {
  332.                 this.motionX *= 0.9900000095367432D;
  333.                 this.motionY *= 0.949999988079071D;
  334.                 this.motionZ *= 0.9900000095367432D;
  335.             }
  336.  
  337.             this.rotationPitch = 0.0F;
  338.             d4 = (double) this.rotationYaw;
  339.             d10 = this.prevPosX - this.posX;
  340.             d11 = this.prevPosZ - this.posZ;
  341.  
  342.             if (d10 * d10 + d11 * d11 > 0.001D) {
  343.                 d4 = (double)((float)(Math.atan2(d11, d10) * 180.0D / Math.PI));
  344.             }
  345.  
  346.             double d12 = MathHelper.wrapAngleTo180_double(d4 - (double) this.rotationYaw);
  347.  
  348.             if (d12 > 20.0D) {
  349.                 d12 = 20.0D;
  350.             }
  351.  
  352.             if (d12 < -20.0D) {
  353.                 d12 = -20.0D;
  354.             }
  355.  
  356.             this.rotationYaw = (float)((double) this.rotationYaw + d12);
  357.             this.setRotation(this.rotationYaw, this.rotationPitch);
  358.  
  359.             if (!this.worldObj.isRemote) {
  360.                 List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().expand(0.20000000298023224D, 0.0D, 0.20000000298023224D));
  361.  
  362.                 if (list != null && !list.isEmpty()) {
  363.                     for (int k1 = 0; k1 < list.size(); ++k1) {
  364.                         Entity entity = (Entity) list.get(k1);
  365.  
  366.                         if (entity != this.riddenByEntity && entity.canBePushed() && entity instanceof EntityWarthog) {
  367.                             entity.applyEntityCollision(this);
  368.                         }
  369.                     }
  370.                 }
  371.  
  372.                 if (this.riddenByEntity != null && this.riddenByEntity.isDead) {
  373.                     this.riddenByEntity = null;
  374.                 }
  375.  
  376.                 if (this.secondRider != null && this.secondRider.isDead) {
  377.                     this.secondRider = null;
  378.                 }
  379.  
  380.                 if (this.thirdRider != null && this.thirdRider.isDead) {
  381.                     this.thirdRider = null;
  382.                 }
  383.             }
  384.         }
  385.     }
  386.  
  387.     public void updateRiderPosition() {
  388.         if (this.riddenByEntity != null) {
  389.             double d0 = Math.cos((double) this.rotationYaw * Math.PI / 180.0D) * 0.4D;
  390.             double d1 = Math.sin((double) this.rotationYaw * Math.PI / 180.0D) * 0.4D;
  391.             this.riddenByEntity.setPosition(this.posX + d0, this.posY + this.getMountedYOffset() + this.riddenByEntity.getYOffset(), this.posZ + d1);
  392.         }
  393.  
  394.         if (this.secondRider != null) {
  395.             double d0 = Math.cos((double) this.rotationYaw * Math.PI / 180.0D) * 0.4D;
  396.             double d1 = Math.sin((double) this.rotationYaw * Math.PI / 180.0D) * 0.4D;
  397.             this.secondRider.setPosition(this.posX + d0 + 1, this.posY + this.getMountedYOffset() + this.secondRider.getYOffset(), this.posZ + d1);
  398.         }
  399.  
  400.         if (this.thirdRider != null) {
  401.             double d0 = Math.cos((double) this.rotationYaw * Math.PI / 180.0D) * 0.4D;
  402.             double d1 = Math.sin((double) this.rotationYaw * Math.PI / 180.0D) * 0.4D;
  403.             this.thirdRider.setPosition(this.posX + d0, this.posY + this.getMountedYOffset() + this.thirdRider.getYOffset(), this.posZ + d1 + 1);
  404.         }
  405.     }
  406.  
  407.     protected void writeEntityToNBT(NBTTagCompound tagCompound) {}
  408.  
  409.     protected void readEntityFromNBT(NBTTagCompound tagCompund) {}
  410.  
  411.     public boolean interactFirst(EntityPlayer playerIn) {
  412.         if (this.riddenByEntity != null && this.riddenByEntity instanceof EntityPlayer && this.riddenByEntity != playerIn) {
  413.             if (this.secondRider != null && this.secondRider instanceof EntityPlayer && this.secondRider != playerIn) {
  414.                 if (this.thirdRider != null && this.thirdRider instanceof EntityPlayer && this.thirdRider != playerIn);
  415.                 else {
  416.                     this.thirdRider = playerIn;
  417.                     if (!this.worldObj.isRemote) {
  418.                         this.mountThirdRider(playerIn);
  419.                     }
  420.                 }
  421.             } else {
  422.                 this.secondRider = playerIn;
  423.                 if (!this.worldObj.isRemote) {
  424.                     this.mountSecondRider(playerIn);
  425.                 }
  426.             }
  427.             return true;
  428.         } else {
  429.             if (!this.worldObj.isRemote) {
  430.                 playerIn.mountEntity(this);
  431.             }
  432.  
  433.             return true;
  434.         }
  435.     }
  436.  
  437.     public void mountThirdRider(EntityPlayer playerIn) {
  438.         if (this.riddenByEntity != null) {
  439.             if (this.riddenByEntity instanceof EntityPlayer) {
  440.                 EntityPlayer primaryRider = (EntityPlayer) this.riddenByEntity;
  441.                 playerIn.setPosition(primaryRider.posX, primaryRider.posY, primaryRider.posZ + 1);
  442.             }
  443.         }
  444.     }
  445.  
  446.     public void mountSecondRider(EntityPlayer playerIn) {
  447.         if (this.riddenByEntity != null) {
  448.             if (this.riddenByEntity instanceof EntityPlayer) {
  449.                 EntityPlayer primaryRider = (EntityPlayer) this.riddenByEntity;
  450.                 playerIn.setPosition(primaryRider.posX + 1, primaryRider.posY, primaryRider.posZ);
  451.             }
  452.         }
  453.     }
  454.  
  455.     protected void func_180433_a(double p_180433_1_, boolean p_180433_3_, Block p_180433_4_, BlockPos p_180433_5_) {
  456.         if (p_180433_3_) {
  457.             if (this.fallDistance > 3.0F) {
  458.                 this.fall(this.fallDistance, 1.0F);
  459.  
  460.                 if (!this.worldObj.isRemote && !this.isDead) {
  461.                     this.setDead();
  462.                     int i;
  463.  
  464.                     for (i = 0; i < 2; ++i) {
  465.                         this.dropItemWithOffset(halocraft.Main.HaloIngot, 2, 0.0F);
  466.                     }
  467.                 }
  468.  
  469.                 this.fallDistance = 0.0F;
  470.             }
  471.         } else if (this.worldObj.getBlockState((new BlockPos(this)).down()).getBlock().getMaterial() != Material.water && p_180433_1_ < 0.0D) {
  472.             this.fallDistance = (float)((double) this.fallDistance - p_180433_1_);
  473.         }
  474.     }
  475.  
  476.     public void setDamageTaken(float p_70266_1_) {
  477.         this.dataWatcher.updateObject(19, Float.valueOf(p_70266_1_));
  478.     }
  479.  
  480.     public float getDamageTaken() {
  481.         return this.dataWatcher.getWatchableObjectFloat(19);
  482.     }
  483.  
  484.     public void setTimeSinceHit(int p_70265_1_) {
  485.         this.dataWatcher.updateObject(17, Integer.valueOf(p_70265_1_));
  486.     }
  487.  
  488.     public int getTimeSinceHit() {
  489.         return this.dataWatcher.getWatchableObjectInt(17);
  490.     }
  491.  
  492.     public void setForwardDirection(int p_70269_1_) {
  493.         this.dataWatcher.updateObject(18, Integer.valueOf(p_70269_1_));
  494.     }
  495.  
  496.     public int getForwardDirection() {
  497.         return this.dataWatcher.getWatchableObjectInt(18);
  498.     }
  499.  
  500.     @SideOnly(Side.CLIENT)
  501.     public void setIsBoatEmpty(boolean isWarthogEmpty) {
  502.         this.isBoatEmpty = isWarthogEmpty;
  503.     }
  504. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement