Nuparu00

EntityAirplane

Nov 16th, 2018
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.10 KB | None | 0 0
  1.  
  2. public class EntityAirplane extends Entity {
  3.  
  4.     @SideOnly(Side.CLIENT)
  5.     private SoundY sy = new SoundY();
  6.  
  7.     private BlockPos startPoint = null;
  8.     private double distanceToFly = Double.MAX_VALUE;
  9.     private boolean hasDropped = false;
  10.  
  11.     private final float FLY_SPEED = 0.5F;
  12.     private final double Y_HEIGHT = 200;
  13.  
  14.     private ChunkCoordIntPair chunk;
  15.     private Ticket ticket;
  16.  
  17.     public EntityAirplane(World worldIn) {
  18.         super(worldIn);
  19.         this.noClip = true;
  20.         this.height = 1.5F;
  21.         this.width = 7F;
  22.         this.renderDistanceWeight = 40.0D;
  23.         int x = (int) (this.posX / 16);
  24.         int z = (int) (this.posZ / 16);
  25.         if (!worldObj.isRemote && (ticket == null || chunk == null || chunk.chunkXPos != x || chunk.chunkZPos != z)) {
  26.             ticket = ForgeChunkManager.requestTicket(SevenDaysToMine.instance, this.worldObj,
  27.                     ForgeChunkManager.Type.NORMAL);
  28.             chunk = new ChunkCoordIntPair(x, z);
  29.             ForgeChunkManager.forceChunk(ticket, chunk);
  30.         }
  31.  
  32.     }
  33.  
  34.     public EntityAirplane(World worldIn, double x, double y, double z) {
  35.         this(worldIn);
  36.         setPosition(x, y, z);
  37.     }
  38.  
  39.     public EntityAirplane(World worldIn, BlockPos pos) {
  40.         this(worldIn, pos.getX(), pos.getY(), pos.getZ());
  41.     }
  42.  
  43.     public EntityAirplane(World world, BlockPos start, BlockPos center, double distance) {
  44.         this(world);
  45.         this.startPoint = start;
  46.         this.distanceToFly = distance;
  47.  
  48.         Vec3 vec = new Vec3(center.subtract(start));
  49.         vec.normalize();
  50.  
  51.         setPosition(start.getX(), Y_HEIGHT, start.getZ());
  52.         setThrowableHeading((double) vec.xCoord, Y_HEIGHT, (double) vec.zCoord, FLY_SPEED, 0f);
  53.         Utils.getLogger().info(new BlockPos(this));
  54.  
  55.     }
  56.  
  57.     @Override
  58.     protected void entityInit() {
  59.  
  60.     }
  61.  
  62.     @Override
  63.     protected void readEntityFromNBT(NBTTagCompound tagCompund) {
  64.         distanceToFly = tagCompund.getDouble("distance");
  65.         startPoint = BlockPos.fromLong(tagCompund.getLong("start"));
  66.         hasDropped = tagCompund.getBoolean("hasDropped");
  67.     }
  68.  
  69.     @Override
  70.     protected void writeEntityToNBT(NBTTagCompound tagCompound) {
  71.         tagCompound.setDouble("distance", distanceToFly);
  72.         tagCompound.setLong("start", startPoint.toLong());
  73.         tagCompound.setBoolean("hasDropped", hasDropped);
  74.  
  75.     }
  76.  
  77.     @Override
  78.     public void onUpdate() {
  79.         super.onUpdate();
  80.         this.posY = Y_HEIGHT;
  81.         this.posX += this.motionX;
  82.         this.posZ += this.motionZ;
  83.     }
  84.  
  85.     @Override
  86.     public void onEntityUpdate() {
  87.         super.onEntityUpdate();
  88.         Utils.getLogger().info("Update");
  89.         if (!worldObj.isRemote) {
  90.             if (startPoint == null) {
  91.                 this.setDead();
  92.                 Utils.getLogger().info(
  93.                         "EntityAirplane with id " + this.getEntityId() + " has null startPoint. Removing the entity!");
  94.             }
  95.  
  96.             if (!hasDropped && worldObj.rand.nextDouble() * distanceToFly <= 1d) {
  97.                 drop();
  98.             }
  99.  
  100.             if (Utils.getDistance(new BlockPos(posX, posY, posZ), startPoint) >= distanceToFly) {
  101.                 if (!hasDropped) {
  102.                     drop();
  103.                 }
  104.                 this.setDead();
  105.  
  106.             }
  107.         }
  108.         if (worldObj.isRemote) {
  109.             if (worldObj.rand.nextInt(3) == 0) {
  110.                 sy.volume = 15;
  111.                 sy.playSirene(new ResourceLocation(SevenDaysToMine.MODID + ":mod.airplane.engine"), new BlockPos(this));
  112.             }
  113.         }
  114.         int x = (int) (this.posX / 16);
  115.         int z = (int) (this.posZ / 16);
  116.         if (!worldObj.isRemote && (ticket == null || chunk == null || chunk.chunkXPos != x || chunk.chunkZPos != z)) {
  117.             if (ticket != null && chunk != null) {
  118.                 ForgeChunkManager.unforceChunk(ticket, chunk);
  119.             }
  120.             ticket = ForgeChunkManager.requestTicket(SevenDaysToMine.instance, this.worldObj,
  121.                     ForgeChunkManager.Type.NORMAL);
  122.             if (ticket != null) {
  123.                 NBTTagCompound nbt = ticket.getModData();
  124.                 nbt.setLong("UUIDMost", this.getUniqueID().getMostSignificantBits());
  125.                 nbt.setLong("UUIDLeast", this.getUniqueID().getLeastSignificantBits());
  126.                 chunk = new ChunkCoordIntPair(x, z);
  127.                 ForgeChunkManager.forceChunk(ticket, chunk);
  128.             }
  129.         }
  130.  
  131.     }
  132.  
  133.     public void drop() {
  134.         Utils.getLogger().info("Drop!");
  135.         hasDropped = true;
  136.         EntityAirdrop airdrop = new EntityAirdrop(worldObj, new BlockPos(this));
  137.         airdrop.copyLocationAndAnglesFrom(this);
  138.         if (!worldObj.isRemote) {
  139.             worldObj.spawnEntityInWorld(airdrop);
  140.         }
  141.     }
  142.  
  143.     public void setThrowableHeading(double x, double y, double z, float velocity, float inaccuracy) {
  144.         float f = MathHelper.sqrt_double(x * x + y * y + z * z);
  145.         x = x / (double) f;
  146.         y = y / (double) f;
  147.         z = z / (double) f;
  148.         x = x + this.rand.nextGaussian() * 0.007499999832361937D * (double) inaccuracy;
  149.         y = y + this.rand.nextGaussian() * 0.007499999832361937D * (double) inaccuracy;
  150.         z = z + this.rand.nextGaussian() * 0.007499999832361937D * (double) inaccuracy;
  151.         x = x * (double) velocity;
  152.         y = y * (double) velocity;
  153.         z = z * (double) velocity;
  154.  
  155.         double maxMotion = Math.abs(Math.max(Math.abs(x), Math.abs(z)));
  156.  
  157.         this.motionX = (x / maxMotion) * FLY_SPEED;
  158.         // this.motionY = (y / maxMotion) * FLY_SPEED;
  159.         this.motionZ = (z / maxMotion) * FLY_SPEED;
  160.         float f1 = MathHelper.sqrt_double(x * x + z * z);
  161.         this.prevRotationYaw = this.rotationYaw = (float) (MathHelper.atan2(x, z) * 180.0D / Math.PI);
  162.         this.prevRotationPitch = this.rotationPitch = (float) (MathHelper.atan2(y, (double) f1) * 180.0D / Math.PI);
  163.     }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment