Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main Class:
- ~~~~~~~~~~~
- @Mod(modid = References.MOD_ID, name = References.MOD_NAME, version = References.VERSION)
- public class CustomizableGuns
- {
- @Instance
- public static CustomizableGuns cguns;
- @SidedProxy(clientSide = "net.cguns.proxy.ClientProxy", serverSide = "net.cguns.proxy.CommonProxy")
- public static CommonProxy proxy;
- @EventHandler
- public void preInit(FMLPreInitializationEvent event) {}
- @EventHandler
- public void init(FMLInitializationEvent event)
- {
- EntityHandler.INSTANCE.registerEntities();
- }
- @EventHandler
- public void postInit(FMLPostInitializationEvent event) {}
- }
- Entity Handler Class:
- ~~~~~~~~~~~~~~~~~~~~~
- public class EntityHandler
- {
- public static final EntityHandler INSTANCE = new EntityHandler();
- private static int entityId = 0;
- public void registerEntities()
- {
- EntityRegistry.registerModEntity(EntityBullet.class, "EntityBullet", entityId++, CustomizableGuns.cguns, 64, 10, false);
- }
- }
- Item Gun Class:
- ~~~~~~~~~~~~~~~
- public class ItemGun extends Item
- {
- public ItemGun()
- {
- this.setMaxStackSize(1);
- }
- public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt)
- {
- return new ProviderGunParts();
- }
- @SideOnly(Side.CLIENT)
- public void addInformation(ItemStack stack, EntityPlayer player, List<String> tooltip, boolean advanced)
- {
- ItemStackHandler stackHandler = CapabilityHelper.getStackHandler(stack);
- for (int i = 0; i < stackHandler.getSlots(); ++i)
- {
- ItemStack itemstack = stackHandler.getStackInSlot(i);
- if (itemstack != null)
- {
- tooltip.add(itemstack.getDisplayName());
- }
- }
- }
- private ItemStack findAmmunition(EntityPlayer player)
- {
- for (int i = 0; i < player.inventory.getSizeInventory(); ++i)
- {
- ItemStack itemstack = player.inventory.getStackInSlot(i);
- if (itemstack != null && itemstack.getItem() == CGItems.AMMO_CLIP)
- {
- return itemstack;
- }
- }
- return null;
- }
- public ActionResult<ItemStack> onItemRightClick(ItemStack itemstack, World world, EntityPlayer player, EnumHand hand)
- {
- boolean flag = this.findAmmunition(player) != null;
- ItemStack itemstack1 = this.findAmmunition(player);
- if (!player.capabilities.isCreativeMode && !flag)
- {
- return !flag ? new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack) : new ActionResult<ItemStack>
- (EnumActionResult.PASS, itemstack);
- }
- else
- {
- ItemStack tipStack = GunManager.getPart(PartType.TIP, PartPlacement.TOP, itemstack);
- if (tipStack != null && tipStack.getItem() != null)
- {
- player.setActiveHand(hand);
- if (flag || player.capabilities.isCreativeMode)
- {
- if (itemstack1 == null)
- {
- itemstack1 = new ItemStack(CGItems.AMMO_CLIP);
- }
- float f = 1.0F;
- boolean flag1 = player.capabilities.isCreativeMode && itemstack1.getItem() == CGItems.AMMO_CLIP;
- if (!world.isRemote)
- {
- EntityBullet entitybullet = ((ItemGunTip) tipStack.getItem()).getBullet(world, player);
- entitybullet.setAim(player, player.rotationPitch, player.rotationYaw, f * 3.0F, 1.0F);
- world.spawnEntityInWorld(entitybullet);
- }
- if (!player.capabilities.isCreativeMode)
- {
- --itemstack1.stackSize;
- if (itemstack1.stackSize == 0)
- {
- player.inventory.deleteStack(itemstack1);
- }
- player.addStat(StatList.getObjectUseStats(this));
- }
- }
- return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
- }
- return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
- }
- }
- }
- Bullet Class:
- ~~~~~~~~~~~~~
- public class EntityBullet extends Entity implements IProjectile
- {
- private static final DataParameter<Byte> BULLET_TYPES = EntityDataManager.createKey(EntityBullet.class, DataSerializers.BYTE);
- public Entity shootingEntity;
- private int ticksInAir;
- private double damage;
- private int knockbackStrength;
- public EntityBullet(World world)
- {
- super(world);
- this.damage = 0.5D;
- this.setSize(0.1F, 0.1F);
- }
- public EntityBullet(World world, double x, double y, double z)
- {
- this(world);
- this.setPosition(x, y, z);
- }
- public EntityBullet(World world, EntityLivingBase shooter)
- {
- this(world, shooter.posX, shooter.posY + (double) shooter.getEyeHeight() - 0.10000000149011612D, shooter.posZ);
- this.shootingEntity = shooter;
- }
- @SideOnly(Side.CLIENT)
- public boolean isInRangeToRenderDist(double distance)
- {
- double d0 = this.getEntityBoundingBox().getAverageEdgeLength() * 10.0D;
- if (Double.isNaN(d0))
- {
- d0 = 1.0D;
- }
- d0 = d0 * 64.0D * getRenderDistanceWeight();
- return distance < d0 * d0;
- }
- protected void entityInit()
- {
- this.dataManager.register(BULLET_TYPES, Byte.valueOf((byte) 0));
- }
- public void setAim(Entity shooter, float rotationPitch, float rotationYaw, float velocity, float inaccuracy)
- {
- float x = -MathHelper.sin(rotationYaw * 0.017453292F) * MathHelper.cos(rotationPitch * 0.017453292F);
- float y = -MathHelper.sin(rotationPitch * 0.017453292F);
- float z = MathHelper.cos(rotationYaw * 0.017453292F) * MathHelper.cos(rotationPitch * 0.017453292F);
- this.setThrowableHeading((double) x, (double) y, (double) z, velocity, inaccuracy);
- this.motionX += shooter.motionX;
- this.motionZ += shooter.motionZ;
- if (!shooter.onGround)
- {
- this.motionY += shooter.motionY;
- }
- }
- public void setThrowableHeading(double x, double y, double z, float velocity, float inaccuracy)
- {
- float f = MathHelper.sqrt_double(x * x + y * y + z * z);
- x = x / (double) f;
- y = y / (double) f;
- z = z / (double) f;
- x = x + this.rand.nextGaussian() * 0.007499999832361937D * (double) inaccuracy;
- y = y + this.rand.nextGaussian() * 0.007499999832361937D * (double) inaccuracy;
- z = z + this.rand.nextGaussian() * 0.007499999832361937D * (double) inaccuracy;
- x = x * (double) velocity;
- y = y * (double) velocity;
- z = z * (double) velocity;
- this.motionX = x;
- this.motionY = y;
- this.motionZ = z;
- float f1 = MathHelper.sqrt_double(x * x + z * z);
- this.prevRotationYaw = this.rotationYaw = (float) (MathHelper.atan2(x, z) * (180D / Math.PI));
- this.prevRotationPitch = this.rotationPitch = (float) (MathHelper.atan2(y, (double) f1) * (180D / Math.PI));
- this.ticksInAir = 0;
- }
- @SideOnly(Side.CLIENT)
- public void setPositionAndRotationDirect(double x, double y, double z, float yaw, float pitch, int posRotationIncrements, boolean
- teleport)
- {
- this.setPosition(x, y, z);
- this.setRotation(yaw, pitch);
- }
- @SideOnly(Side.CLIENT)
- public void setVelocity(double x, double y, double z)
- {
- this.motionX = x;
- this.motionY = y;
- this.motionZ = z;
- if (this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F)
- {
- float f = MathHelper.sqrt_double(x * x + z * z);
- this.prevRotationYaw = this.rotationYaw = (float) (MathHelper.atan2(x, z) * (180.0D / Math.PI));
- this.prevRotationPitch = this.rotationPitch = (float) (MathHelper.atan2(y, (double) f) * (180.0D / Math.PI));
- this.prevRotationPitch = this.rotationPitch;
- this.prevRotationYaw = this.rotationYaw;
- this.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotationYaw, this.rotationPitch);
- }
- }
- public void onUpdate()
- {
- super.onUpdate();
- if (this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F)
- {
- float f = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
- this.prevRotationYaw = this.rotationYaw = (float) (MathHelper.atan2(this.motionX, this.motionZ) * (180D / Math.PI));
- this.prevRotationPitch = this.rotationPitch = (float) (MathHelper.atan2(this.motionY, (double) f) * (180D / Math.PI));
- }
- ++this.ticksInAir;
- if (this.ticksInAir >= 10)
- {
- this.setDead();
- }
- Vec3d vec3d1 = new Vec3d(this.posX, this.posY, this.posZ);
- Vec3d vec3d = new Vec3d(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
- RayTraceResult raytraceresult = this.worldObj.rayTraceBlocks(vec3d1, vec3d, false, true, false);
- vec3d1 = new Vec3d(this.posX, this.posY, this.posZ);
- vec3d = new Vec3d(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
- if (raytraceresult != null)
- {
- vec3d = new Vec3d(raytraceresult.hitVec.xCoord, raytraceresult.hitVec.yCoord, raytraceresult.hitVec.zCoord);
- }
- Entity entity = this.findEntityOnPath(vec3d1, vec3d);
- if (entity != null)
- {
- raytraceresult = new RayTraceResult(entity);
- }
- if (raytraceresult != null && raytraceresult.entityHit != null && raytraceresult.entityHit instanceof EntityPlayer)
- {
- EntityPlayer entityplayer = (EntityPlayer) raytraceresult.entityHit;
- if (this.shootingEntity instanceof EntityPlayer && !((EntityPlayer) this.shootingEntity).canAttackPlayer(entityplayer))
- {
- raytraceresult = null;
- }
- }
- if (raytraceresult != null)
- {
- this.onHit(raytraceresult);
- }
- this.posX += this.motionX;
- this.posY += this.motionY;
- this.posZ += this.motionZ;
- float f4 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
- this.rotationYaw = (float) (MathHelper.atan2(this.motionX, this.motionZ) * (180D / Math.PI));
- for (this.rotationPitch = (float) (MathHelper.atan2(this.motionY, (double) f4) * (180D / Math.PI)); this.rotationPitch -
- this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F)
- {
- ;
- }
- while (this.rotationPitch - this.prevRotationPitch >= 180.0F)
- {
- this.prevRotationPitch += 360.0F;
- }
- while (this.rotationYaw - this.prevRotationYaw < -180.0F)
- {
- this.prevRotationYaw -= 360.0F;
- }
- while (this.rotationYaw - this.prevRotationYaw >= 180.0F)
- {
- this.prevRotationYaw += 360.0F;
- }
- this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
- this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
- float f1 = 0.99F;
- float f2 = 0.05F;
- if (this.isInWater())
- {
- for (int i = 0; i < 4; ++i)
- {
- float f3 = 0.25F;
- this.worldObj.spawnParticle(EnumParticleTypes.WATER_BUBBLE, this.posX - this.motionX * (double) f3, this.posY -
- this.motionY * (double) f3, this.posZ - this.motionZ * (double) f3, this.motionX, this.motionY, this.motionZ, new int[0]);
- }
- f1 = 0.6F;
- }
- else
- {
- if (this.isIncendiary())
- {
- float f3 = 0.25F;
- this.worldObj.spawnParticle(EnumParticleTypes.FLAME, this.posX - this.motionX * (double) f3, this.posY - this.motionY *
- (double) f3, this.posZ - this.motionZ * (double) f3, this.motionX, this.motionY, this.motionZ, new int[0]);
- }
- }
- this.motionX *= (double) f1;
- this.motionY *= (double) f1;
- this.motionZ *= (double) f1;
- this.setPosition(this.posX, this.posY, this.posZ);
- this.doBlockCollisions();
- }
- protected void onHit(RayTraceResult raytraceresult)
- {
- Entity entity = raytraceresult.entityHit;
- if (entity != null)
- {
- float f = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
- int i = MathHelper.ceiling_double_int((double) f * this.damage);
- DamageSource damagesource;
- if (this.shootingEntity == null)
- {
- damagesource = this.causeBulletDamage(this, this);
- }
- else
- {
- damagesource = this.causeBulletDamage(this, this.shootingEntity);
- }
- if (!(entity instanceof EntityEnderman))
- {
- if (this.isIncendiary())
- {
- entity.setFire(3);
- }
- if (this.isFrost())
- {
- }
- }
- if (entity.attackEntityFrom(damagesource, (float) i))
- {
- if (entity instanceof EntityLivingBase)
- {
- EntityLivingBase entitylivingbase = (EntityLivingBase) entity;
- if (this.knockbackStrength > 0)
- {
- float f1 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
- if (f1 > 0.0F)
- {
- entitylivingbase.addVelocity(this.motionX * (double) this.knockbackStrength * 0.6000000238418579D /
- (double) f1, 0.1D, this.motionZ * (double) this.knockbackStrength * 0.6000000238418579D / (double) f1);
- }
- }
- if (this.shootingEntity instanceof EntityLivingBase)
- {
- EnchantmentHelper.applyThornEnchantments(entitylivingbase, this.shootingEntity);
- EnchantmentHelper.applyArthropodEnchantments((EntityLivingBase) this.shootingEntity, entitylivingbase);
- }
- this.bulletHit(entitylivingbase);
- }
- if (!(entity instanceof EntityEnderman))
- {
- this.setDead();
- }
- }
- else
- {
- this.motionX *= -0.10000000149011612D;
- this.motionY *= -0.10000000149011612D;
- this.motionZ *= -0.10000000149011612D;
- this.rotationYaw += 180.0F;
- this.prevRotationYaw += 180.0F;
- this.ticksInAir = 0;
- if (!this.worldObj.isRemote && this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ
- < 0.0010000000474974513D)
- {
- this.setDead();
- }
- }
- }
- else
- {
- BlockPos blockpos = raytraceresult.getBlockPos();
- IBlockState iblockstate = this.worldObj.getBlockState(blockpos);
- this.motionX = (double) ((float) (raytraceresult.hitVec.xCoord - this.posX));
- this.motionY = (double) ((float) (raytraceresult.hitVec.yCoord - this.posY));
- this.motionZ = (double) ((float) (raytraceresult.hitVec.zCoord - this.posZ));
- float f2 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
- this.posX -= this.motionX / (double) f2 * 0.0500000007450586D;
- this.posY -= this.motionY / (double) f2 * 0.0500000007450586D;
- this.posZ -= this.motionZ / (double) f2 * 0.0500000007450586D;
- if (iblockstate.getMaterial() != Material.AIR)
- {
- this.setDead();
- }
- }
- }
- protected void bulletHit(EntityLivingBase entitylivingbase) {}
- private Entity findEntityOnPath(Vec3d start, Vec3d end)
- {
- Entity entity = null;
- List<Entity> list = this.worldObj.getEntitiesInAABBexcluding(this, this.getEntityBoundingBox().addCoord(this.motionX,
- this.motionY, this.motionZ).expandXyz(1.0D), Predicates.and(new Predicate[] {EntitySelectors.NOT_SPECTATING,
- EntitySelectors.IS_ALIVE, new Predicate<Entity>()
- {
- public boolean apply(Entity entity)
- {
- return entity.canBeCollidedWith();
- }
- }}));
- double d0 = 0.0D;
- for (int i = 0; i < list.size(); ++i)
- {
- Entity entity1 = (Entity) list.get(i);
- if (entity1 != this.shootingEntity || this.ticksInAir >= 5)
- {
- AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().expandXyz(0.30000001192092896D);
- RayTraceResult raytraceresult = axisalignedbb.calculateIntercept(start, end);
- if (raytraceresult != null)
- {
- double d1 = start.squareDistanceTo(raytraceresult.hitVec);
- if (d1 < d0 || d0 == 0.0D)
- {
- entity = entity1;
- d0 = d1;
- }
- }
- }
- }
- return entity;
- }
- protected void writeEntityToNBT(NBTTagCompound compound)
- {
- compound.setShort("life", (short) this.ticksInAir);
- compound.setDouble("damage", this.damage);
- compound.setInteger("knockback", this.knockbackStrength);
- }
- protected void readEntityFromNBT(NBTTagCompound compound)
- {
- this.ticksInAir = compound.getShort("life");
- this.damage = compound.getDouble("damage");
- this.knockbackStrength = compound.getInteger("knockback");
- }
- protected boolean canTriggerWalking()
- {
- return false;
- }
- public void setDamage(double damage)
- {
- this.damage = damage;
- }
- public double getDamage()
- {
- return this.damage;
- }
- public void setKnockbackStrength(int knockbackStrength)
- {
- this.knockbackStrength = knockbackStrength;
- }
- public boolean canBeAttackedWithItem()
- {
- return false;
- }
- public float getEyeHeight()
- {
- return 0.0F;
- }
- public void setIncendiary(boolean incendiary)
- {
- this.setFlag(0, incendiary);
- }
- public boolean isIncendiary()
- {
- return this.getFlag(0);
- }
- public void setFrost(boolean frost)
- {
- this.setFlag(1, frost);
- }
- public boolean isFrost()
- {
- return this.getFlag(1);
- }
- protected boolean getFlag(int flag)
- {
- return (((Byte) this.dataManager.get(BULLET_TYPES)).byteValue() & 1 << flag) != 0;
- }
- protected void setFlag(int flag, boolean set)
- {
- byte b0 = ((Byte) this.dataManager.get(BULLET_TYPES)).byteValue();
- if (set)
- {
- this.dataManager.set(BULLET_TYPES, Byte.valueOf((byte) (b0 | 1 << flag)));
- }
- else
- {
- this.dataManager.set(BULLET_TYPES, Byte.valueOf((byte) (b0 & ~(1 << flag))));
- }
- }
- private DamageSource causeBulletDamage(EntityBullet bullet, Entity indirect)
- {
- return (new EntityDamageSourceIndirect("bullet" + (this.isIncendiary() ? ".incendiary" : (this.isFrost() ? ".frost" : "")),
- bullet, indirect)).setProjectile();
- }
- }
Add Comment
Please, Sign In to add comment