Guest User

Projectile Doesn't Move Client Side

a guest
May 22nd, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.73 KB | None | 0 0
  1. Main Class:
  2. ~~~~~~~~~~~
  3. @Mod(modid = References.MOD_ID, name = References.MOD_NAME, version = References.VERSION)
  4. public class CustomizableGuns
  5. {
  6.     @Instance
  7.     public static CustomizableGuns cguns;
  8.     @SidedProxy(clientSide = "net.cguns.proxy.ClientProxy", serverSide = "net.cguns.proxy.CommonProxy")
  9.     public static CommonProxy proxy;
  10.  
  11.     @EventHandler
  12.     public void preInit(FMLPreInitializationEvent event) {}
  13.  
  14.     @EventHandler
  15.     public void init(FMLInitializationEvent event)
  16.     {
  17.         EntityHandler.INSTANCE.registerEntities();
  18.     }
  19.  
  20.     @EventHandler
  21.     public void postInit(FMLPostInitializationEvent event) {}
  22. }
  23.  
  24. Entity Handler Class:
  25. ~~~~~~~~~~~~~~~~~~~~~
  26. public class EntityHandler
  27. {
  28.     public static final EntityHandler INSTANCE = new EntityHandler();
  29.     private static int entityId = 0;
  30.  
  31.     public void registerEntities()
  32.     {
  33.         EntityRegistry.registerModEntity(EntityBullet.class, "EntityBullet", entityId++, CustomizableGuns.cguns, 64, 10, false);
  34.     }
  35. }
  36.  
  37. Item Gun Class:
  38. ~~~~~~~~~~~~~~~
  39. public class ItemGun extends Item
  40. {
  41.     public ItemGun()
  42.     {
  43.         this.setMaxStackSize(1);
  44.     }
  45.  
  46.     public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt)
  47.     {
  48.         return new ProviderGunParts();
  49.     }
  50.  
  51.     @SideOnly(Side.CLIENT)
  52.     public void addInformation(ItemStack stack, EntityPlayer player, List<String> tooltip, boolean advanced)
  53.     {
  54.         ItemStackHandler stackHandler = CapabilityHelper.getStackHandler(stack);
  55.  
  56.         for (int i = 0; i < stackHandler.getSlots(); ++i)
  57.         {
  58.             ItemStack itemstack = stackHandler.getStackInSlot(i);
  59.  
  60.             if (itemstack != null)
  61.             {
  62.                 tooltip.add(itemstack.getDisplayName());
  63.             }
  64.         }
  65.     }
  66.  
  67.     private ItemStack findAmmunition(EntityPlayer player)
  68.     {
  69.         for (int i = 0; i < player.inventory.getSizeInventory(); ++i)
  70.         {
  71.             ItemStack itemstack = player.inventory.getStackInSlot(i);
  72.  
  73.             if (itemstack != null && itemstack.getItem() == CGItems.AMMO_CLIP)
  74.             {
  75.                 return itemstack;
  76.             }
  77.         }
  78.  
  79.         return null;
  80.     }
  81.  
  82.     public ActionResult<ItemStack> onItemRightClick(ItemStack itemstack, World world, EntityPlayer player, EnumHand hand)
  83.     {
  84.         boolean flag = this.findAmmunition(player) != null;
  85.         ItemStack itemstack1 = this.findAmmunition(player);
  86.  
  87.         if (!player.capabilities.isCreativeMode && !flag)
  88.         {
  89.             return !flag ? new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack) : new ActionResult<ItemStack>
  90.             (EnumActionResult.PASS, itemstack);
  91.         }
  92.         else
  93.         {
  94.             ItemStack tipStack = GunManager.getPart(PartType.TIP, PartPlacement.TOP, itemstack);
  95.  
  96.             if (tipStack != null && tipStack.getItem() != null)
  97.             {
  98.                 player.setActiveHand(hand);
  99.  
  100.                 if (flag || player.capabilities.isCreativeMode)
  101.                 {
  102.                     if (itemstack1 == null)
  103.                     {
  104.                         itemstack1 = new ItemStack(CGItems.AMMO_CLIP);
  105.                     }
  106.  
  107.                     float f = 1.0F;
  108.                     boolean flag1 = player.capabilities.isCreativeMode && itemstack1.getItem() == CGItems.AMMO_CLIP;
  109.  
  110.                     if (!world.isRemote)
  111.                     {
  112.                         EntityBullet entitybullet = ((ItemGunTip) tipStack.getItem()).getBullet(world, player);
  113.                         entitybullet.setAim(player, player.rotationPitch, player.rotationYaw, f * 3.0F, 1.0F);
  114.                         world.spawnEntityInWorld(entitybullet);
  115.                     }
  116.  
  117.                     if (!player.capabilities.isCreativeMode)
  118.                     {
  119.                         --itemstack1.stackSize;
  120.  
  121.                         if (itemstack1.stackSize == 0)
  122.                         {
  123.                             player.inventory.deleteStack(itemstack1);
  124.                         }
  125.  
  126.                         player.addStat(StatList.getObjectUseStats(this));
  127.                     }
  128.                 }
  129.  
  130.                 return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
  131.             }
  132.  
  133.             return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
  134.         }
  135.     }
  136. }
  137.  
  138. Bullet Class:
  139. ~~~~~~~~~~~~~
  140. public class EntityBullet extends Entity implements IProjectile
  141. {
  142.     private static final DataParameter<Byte> BULLET_TYPES = EntityDataManager.createKey(EntityBullet.class, DataSerializers.BYTE);
  143.     public Entity shootingEntity;
  144.     private int ticksInAir;
  145.     private double damage;
  146.     private int knockbackStrength;
  147.  
  148.     public EntityBullet(World world)
  149.     {
  150.         super(world);
  151.         this.damage = 0.5D;
  152.         this.setSize(0.1F, 0.1F);
  153.     }
  154.  
  155.     public EntityBullet(World world, double x, double y, double z)
  156.     {
  157.         this(world);
  158.         this.setPosition(x, y, z);
  159.     }
  160.  
  161.     public EntityBullet(World world, EntityLivingBase shooter)
  162.     {
  163.         this(world, shooter.posX, shooter.posY + (double) shooter.getEyeHeight() - 0.10000000149011612D, shooter.posZ);
  164.         this.shootingEntity = shooter;
  165.     }
  166.  
  167.     @SideOnly(Side.CLIENT)
  168.     public boolean isInRangeToRenderDist(double distance)
  169.     {
  170.         double d0 = this.getEntityBoundingBox().getAverageEdgeLength() * 10.0D;
  171.  
  172.         if (Double.isNaN(d0))
  173.         {
  174.             d0 = 1.0D;
  175.         }
  176.  
  177.         d0 = d0 * 64.0D * getRenderDistanceWeight();
  178.         return distance < d0 * d0;
  179.     }
  180.  
  181.     protected void entityInit()
  182.     {
  183.         this.dataManager.register(BULLET_TYPES, Byte.valueOf((byte) 0));
  184.     }
  185.  
  186.     public void setAim(Entity shooter, float rotationPitch, float rotationYaw, float velocity, float inaccuracy)
  187.     {
  188.         float x = -MathHelper.sin(rotationYaw * 0.017453292F) * MathHelper.cos(rotationPitch * 0.017453292F);
  189.         float y = -MathHelper.sin(rotationPitch * 0.017453292F);
  190.         float z = MathHelper.cos(rotationYaw * 0.017453292F) * MathHelper.cos(rotationPitch * 0.017453292F);
  191.         this.setThrowableHeading((double) x, (double) y, (double) z, velocity, inaccuracy);
  192.         this.motionX += shooter.motionX;
  193.         this.motionZ += shooter.motionZ;
  194.  
  195.         if (!shooter.onGround)
  196.         {
  197.             this.motionY += shooter.motionY;
  198.         }
  199.     }
  200.  
  201.     public void setThrowableHeading(double x, double y, double z, float velocity, float inaccuracy)
  202.     {
  203.         float f = MathHelper.sqrt_double(x * x + y * y + z * z);
  204.         x = x / (double) f;
  205.         y = y / (double) f;
  206.         z = z / (double) f;
  207.         x = x + this.rand.nextGaussian() * 0.007499999832361937D * (double) inaccuracy;
  208.         y = y + this.rand.nextGaussian() * 0.007499999832361937D * (double) inaccuracy;
  209.         z = z + this.rand.nextGaussian() * 0.007499999832361937D * (double) inaccuracy;
  210.         x = x * (double) velocity;
  211.         y = y * (double) velocity;
  212.         z = z * (double) velocity;
  213.         this.motionX = x;
  214.         this.motionY = y;
  215.         this.motionZ = z;
  216.         float f1 = MathHelper.sqrt_double(x * x + z * z);
  217.         this.prevRotationYaw = this.rotationYaw = (float) (MathHelper.atan2(x, z) * (180D / Math.PI));
  218.         this.prevRotationPitch = this.rotationPitch = (float) (MathHelper.atan2(y, (double) f1) * (180D / Math.PI));
  219.         this.ticksInAir = 0;
  220.     }
  221.  
  222.     @SideOnly(Side.CLIENT)
  223.     public void setPositionAndRotationDirect(double x, double y, double z, float yaw, float pitch, int posRotationIncrements, boolean
  224.     teleport)
  225.     {
  226.         this.setPosition(x, y, z);
  227.         this.setRotation(yaw, pitch);
  228.     }
  229.  
  230.     @SideOnly(Side.CLIENT)
  231.     public void setVelocity(double x, double y, double z)
  232.     {
  233.         this.motionX = x;
  234.         this.motionY = y;
  235.         this.motionZ = z;
  236.  
  237.         if (this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F)
  238.         {
  239.             float f = MathHelper.sqrt_double(x * x + z * z);
  240.             this.prevRotationYaw = this.rotationYaw = (float) (MathHelper.atan2(x, z) * (180.0D / Math.PI));
  241.             this.prevRotationPitch = this.rotationPitch = (float) (MathHelper.atan2(y, (double) f) * (180.0D / Math.PI));
  242.             this.prevRotationPitch = this.rotationPitch;
  243.             this.prevRotationYaw = this.rotationYaw;
  244.             this.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotationYaw, this.rotationPitch);
  245.         }
  246.     }
  247.  
  248.     public void onUpdate()
  249.     {
  250.         super.onUpdate();
  251.  
  252.         if (this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F)
  253.         {
  254.             float f = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
  255.             this.prevRotationYaw = this.rotationYaw = (float) (MathHelper.atan2(this.motionX, this.motionZ) * (180D / Math.PI));
  256.             this.prevRotationPitch = this.rotationPitch = (float) (MathHelper.atan2(this.motionY, (double) f) * (180D / Math.PI));
  257.         }
  258.  
  259.         ++this.ticksInAir;
  260.  
  261.         if (this.ticksInAir >= 10)
  262.         {
  263.             this.setDead();
  264.         }
  265.  
  266.         Vec3d vec3d1 = new Vec3d(this.posX, this.posY, this.posZ);
  267.         Vec3d vec3d = new Vec3d(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
  268.         RayTraceResult raytraceresult = this.worldObj.rayTraceBlocks(vec3d1, vec3d, false, true, false);
  269.         vec3d1 = new Vec3d(this.posX, this.posY, this.posZ);
  270.         vec3d = new Vec3d(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
  271.  
  272.         if (raytraceresult != null)
  273.         {
  274.             vec3d = new Vec3d(raytraceresult.hitVec.xCoord, raytraceresult.hitVec.yCoord, raytraceresult.hitVec.zCoord);
  275.         }
  276.  
  277.         Entity entity = this.findEntityOnPath(vec3d1, vec3d);
  278.  
  279.         if (entity != null)
  280.         {
  281.             raytraceresult = new RayTraceResult(entity);
  282.         }
  283.  
  284.         if (raytraceresult != null && raytraceresult.entityHit != null && raytraceresult.entityHit instanceof EntityPlayer)
  285.         {
  286.             EntityPlayer entityplayer = (EntityPlayer) raytraceresult.entityHit;
  287.  
  288.             if (this.shootingEntity instanceof EntityPlayer && !((EntityPlayer) this.shootingEntity).canAttackPlayer(entityplayer))
  289.             {
  290.                 raytraceresult = null;
  291.             }
  292.         }
  293.  
  294.         if (raytraceresult != null)
  295.         {
  296.             this.onHit(raytraceresult);
  297.         }
  298.  
  299.         this.posX += this.motionX;
  300.         this.posY += this.motionY;
  301.         this.posZ += this.motionZ;
  302.         float f4 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
  303.         this.rotationYaw = (float) (MathHelper.atan2(this.motionX, this.motionZ) * (180D / Math.PI));
  304.  
  305.         for (this.rotationPitch = (float) (MathHelper.atan2(this.motionY, (double) f4) * (180D / Math.PI)); this.rotationPitch -
  306.         this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F)
  307.         {
  308.             ;
  309.         }
  310.  
  311.         while (this.rotationPitch - this.prevRotationPitch >= 180.0F)
  312.         {
  313.             this.prevRotationPitch += 360.0F;
  314.         }
  315.  
  316.         while (this.rotationYaw - this.prevRotationYaw < -180.0F)
  317.         {
  318.             this.prevRotationYaw -= 360.0F;
  319.         }
  320.  
  321.         while (this.rotationYaw - this.prevRotationYaw >= 180.0F)
  322.         {
  323.             this.prevRotationYaw += 360.0F;
  324.         }
  325.  
  326.         this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
  327.         this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
  328.         float f1 = 0.99F;
  329.         float f2 = 0.05F;
  330.  
  331.         if (this.isInWater())
  332.         {
  333.             for (int i = 0; i < 4; ++i)
  334.             {
  335.                 float f3 = 0.25F;
  336.                 this.worldObj.spawnParticle(EnumParticleTypes.WATER_BUBBLE, this.posX - this.motionX * (double) f3, this.posY -
  337.             this.motionY * (double) f3, this.posZ - this.motionZ * (double) f3, this.motionX, this.motionY, this.motionZ, new int[0]);
  338.             }
  339.  
  340.             f1 = 0.6F;
  341.         }
  342.         else
  343.         {
  344.             if (this.isIncendiary())
  345.             {
  346.                 float f3 = 0.25F;
  347.                 this.worldObj.spawnParticle(EnumParticleTypes.FLAME, this.posX - this.motionX * (double) f3, this.posY - this.motionY *
  348.                 (double) f3, this.posZ - this.motionZ * (double) f3, this.motionX, this.motionY, this.motionZ, new int[0]);
  349.             }
  350.         }
  351.  
  352.         this.motionX *= (double) f1;
  353.         this.motionY *= (double) f1;
  354.         this.motionZ *= (double) f1;
  355.         this.setPosition(this.posX, this.posY, this.posZ);
  356.         this.doBlockCollisions();
  357.     }
  358.  
  359.     protected void onHit(RayTraceResult raytraceresult)
  360.     {
  361.         Entity entity = raytraceresult.entityHit;
  362.  
  363.         if (entity != null)
  364.         {
  365.             float f = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
  366.             int i = MathHelper.ceiling_double_int((double) f * this.damage);
  367.             DamageSource damagesource;
  368.  
  369.             if (this.shootingEntity == null)
  370.             {
  371.                 damagesource = this.causeBulletDamage(this, this);
  372.             }
  373.             else
  374.             {
  375.                 damagesource = this.causeBulletDamage(this, this.shootingEntity);
  376.             }
  377.  
  378.             if (!(entity instanceof EntityEnderman))
  379.             {
  380.                 if (this.isIncendiary())
  381.                 {
  382.                     entity.setFire(3);
  383.                 }
  384.  
  385.                 if (this.isFrost())
  386.                 {
  387.  
  388.                 }
  389.             }
  390.  
  391.             if (entity.attackEntityFrom(damagesource, (float) i))
  392.             {
  393.                 if (entity instanceof EntityLivingBase)
  394.                 {
  395.                     EntityLivingBase entitylivingbase = (EntityLivingBase) entity;
  396.  
  397.                     if (this.knockbackStrength > 0)
  398.                     {
  399.                         float f1 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
  400.  
  401.                         if (f1 > 0.0F)
  402.                         {
  403.                             entitylivingbase.addVelocity(this.motionX * (double) this.knockbackStrength * 0.6000000238418579D /
  404.                             (double) f1, 0.1D, this.motionZ * (double) this.knockbackStrength * 0.6000000238418579D / (double) f1);
  405.                         }
  406.                     }
  407.  
  408.                     if (this.shootingEntity instanceof EntityLivingBase)
  409.                     {
  410.                         EnchantmentHelper.applyThornEnchantments(entitylivingbase, this.shootingEntity);
  411.                         EnchantmentHelper.applyArthropodEnchantments((EntityLivingBase) this.shootingEntity, entitylivingbase);
  412.                     }
  413.  
  414.                     this.bulletHit(entitylivingbase);
  415.                 }
  416.  
  417.                 if (!(entity instanceof EntityEnderman))
  418.                 {
  419.                     this.setDead();
  420.                 }
  421.             }
  422.             else
  423.             {
  424.                 this.motionX *= -0.10000000149011612D;
  425.                 this.motionY *= -0.10000000149011612D;
  426.                 this.motionZ *= -0.10000000149011612D;
  427.                 this.rotationYaw += 180.0F;
  428.                 this.prevRotationYaw += 180.0F;
  429.                 this.ticksInAir = 0;
  430.  
  431.                 if (!this.worldObj.isRemote && this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ
  432.                 < 0.0010000000474974513D)
  433.                 {
  434.                     this.setDead();
  435.                 }
  436.             }
  437.         }
  438.         else
  439.         {
  440.             BlockPos blockpos = raytraceresult.getBlockPos();
  441.             IBlockState iblockstate = this.worldObj.getBlockState(blockpos);
  442.             this.motionX = (double) ((float) (raytraceresult.hitVec.xCoord - this.posX));
  443.             this.motionY = (double) ((float) (raytraceresult.hitVec.yCoord - this.posY));
  444.             this.motionZ = (double) ((float) (raytraceresult.hitVec.zCoord - this.posZ));
  445.             float f2 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
  446.             this.posX -= this.motionX / (double) f2 * 0.0500000007450586D;
  447.             this.posY -= this.motionY / (double) f2 * 0.0500000007450586D;
  448.             this.posZ -= this.motionZ / (double) f2 * 0.0500000007450586D;
  449.  
  450.             if (iblockstate.getMaterial() != Material.AIR)
  451.             {
  452.                 this.setDead();
  453.             }
  454.         }
  455.     }
  456.  
  457.     protected void bulletHit(EntityLivingBase entitylivingbase) {}
  458.  
  459.     private Entity findEntityOnPath(Vec3d start, Vec3d end)
  460.     {
  461.         Entity entity = null;
  462.         List<Entity> list = this.worldObj.getEntitiesInAABBexcluding(this, this.getEntityBoundingBox().addCoord(this.motionX,
  463.         this.motionY, this.motionZ).expandXyz(1.0D), Predicates.and(new Predicate[] {EntitySelectors.NOT_SPECTATING,
  464.         EntitySelectors.IS_ALIVE, new Predicate<Entity>()
  465.         {
  466.             public boolean apply(Entity entity)
  467.             {
  468.                 return entity.canBeCollidedWith();
  469.             }
  470.         }}));
  471.         double d0 = 0.0D;
  472.  
  473.         for (int i = 0; i < list.size(); ++i)
  474.         {
  475.             Entity entity1 = (Entity) list.get(i);
  476.  
  477.             if (entity1 != this.shootingEntity || this.ticksInAir >= 5)
  478.             {
  479.                 AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().expandXyz(0.30000001192092896D);
  480.                 RayTraceResult raytraceresult = axisalignedbb.calculateIntercept(start, end);
  481.  
  482.                 if (raytraceresult != null)
  483.                 {
  484.                     double d1 = start.squareDistanceTo(raytraceresult.hitVec);
  485.  
  486.                     if (d1 < d0 || d0 == 0.0D)
  487.                     {
  488.                         entity = entity1;
  489.                         d0 = d1;
  490.                     }
  491.                 }
  492.             }
  493.         }
  494.  
  495.         return entity;
  496.     }
  497.  
  498.     protected void writeEntityToNBT(NBTTagCompound compound)
  499.     {
  500.         compound.setShort("life", (short) this.ticksInAir);
  501.         compound.setDouble("damage", this.damage);
  502.         compound.setInteger("knockback", this.knockbackStrength);
  503.     }
  504.  
  505.     protected void readEntityFromNBT(NBTTagCompound compound)
  506.     {
  507.         this.ticksInAir = compound.getShort("life");
  508.         this.damage = compound.getDouble("damage");
  509.         this.knockbackStrength = compound.getInteger("knockback");
  510.     }
  511.  
  512.     protected boolean canTriggerWalking()
  513.     {
  514.         return false;
  515.     }
  516.  
  517.     public void setDamage(double damage)
  518.     {
  519.         this.damage = damage;
  520.     }
  521.  
  522.     public double getDamage()
  523.     {
  524.         return this.damage;
  525.     }
  526.  
  527.     public void setKnockbackStrength(int knockbackStrength)
  528.     {
  529.         this.knockbackStrength = knockbackStrength;
  530.     }
  531.  
  532.     public boolean canBeAttackedWithItem()
  533.     {
  534.         return false;
  535.     }
  536.  
  537.     public float getEyeHeight()
  538.     {
  539.         return 0.0F;
  540.     }
  541.  
  542.     public void setIncendiary(boolean incendiary)
  543.     {
  544.         this.setFlag(0, incendiary);
  545.     }
  546.  
  547.     public boolean isIncendiary()
  548.     {
  549.         return this.getFlag(0);
  550.     }
  551.  
  552.     public void setFrost(boolean frost)
  553.     {
  554.         this.setFlag(1, frost);
  555.     }
  556.  
  557.     public boolean isFrost()
  558.     {
  559.         return this.getFlag(1);
  560.     }
  561.  
  562.     protected boolean getFlag(int flag)
  563.     {
  564.         return (((Byte) this.dataManager.get(BULLET_TYPES)).byteValue() & 1 << flag) != 0;
  565.     }
  566.  
  567.     protected void setFlag(int flag, boolean set)
  568.     {
  569.         byte b0 = ((Byte) this.dataManager.get(BULLET_TYPES)).byteValue();
  570.  
  571.         if (set)
  572.         {
  573.             this.dataManager.set(BULLET_TYPES, Byte.valueOf((byte) (b0 | 1 << flag)));
  574.         }
  575.         else
  576.         {
  577.             this.dataManager.set(BULLET_TYPES, Byte.valueOf((byte) (b0 & ~(1 << flag))));
  578.         }
  579.     }
  580.  
  581.     private DamageSource causeBulletDamage(EntityBullet bullet, Entity indirect)
  582.     {
  583.         return (new EntityDamageSourceIndirect("bullet" + (this.isIncendiary() ? ".incendiary" : (this.isFrost() ? ".frost" : "")),
  584.         bullet, indirect)).setProjectile();
  585.     }
  586. }
Add Comment
Please, Sign In to add comment