Jump to content

[Solved]Proper Rendering and offset.


Noah_Beech

Recommended Posts

So as of recent, I have been trying to code in an item that launches fireballs (Firecharges  8) ) And have gotton Very far. My main problem is that when I shoot it, the entity likes to spawn in the player, causing damage and stopping the fireball from going anywhere. I know that one can add offset to stop this, but how would I make it so it spawns say, 1 block in front of the player?

 

Firelauncher

 

 

 

package mod_Rareores;

 

import java.util.Random;

 

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.enchantment.Enchantment;

import net.minecraft.enchantment.EnchantmentHelper;

import net.minecraft.entity.Entity;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.projectile.EntityFireball;

import net.minecraft.entity.projectile.EntitySmallFireball;

import net.minecraft.item.EnumAction;

import net.minecraft.item.Item;

import net.minecraft.item.ItemBow;

import net.minecraft.item.ItemStack;

import net.minecraft.util.MathHelper;

import net.minecraft.world.World;

import net.minecraftforge.common.MinecraftForge;

import net.minecraftforge.event.entity.player.ArrowNockEvent;

 

public class ItemFireLauncher extends Item

{

 

    public ItemFireLauncher(int par1, int par2)

    {

        super(par1);

        maxStackSize = 1;

        setMaxDamage(2468);

        bFull3D = true;

        setCreativeTab(CreativeTabs.tabCombat);

    }

    public String getTextureFile()

    {

    return "/Blocks1.png";

    }

   

 

    /**

    * called when the player releases the use item button. Args: itemstack, world, entityplayer, itemInUseCount

    */

    public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer, int par4)

    {

        boolean flag = par3EntityPlayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantment.infinity.effectId, par1ItemStack) > 0;

 

 

        if (flag || par3EntityPlayer.inventory.hasItem(Item.fireballCharge.itemID))

        {

            int i = getMaxItemUseDuration(par1ItemStack) - par4;

            float f = (float)i /14;

            f = (f * f + f * 2.0F) / 3F;

 

 

            if ((double)f < 0.1D)

            {

                return;

            }

 

 

            if (f > 1.0F)

            {

                f = 1.0F;

            }

           

           

            float N1=par3EntityPlayer.rotationYawHead;

           

   

            EntityFireAmmo entityBall = new EntityFireAmmo(par2World, par3EntityPlayer, N1);

 

           

            par1ItemStack.damageItem(1, par3EntityPlayer);

            par2World.playAuxSFXAtEntity((EntityPlayer)null, 1009, (int)par3EntityPlayer.posX, (int)par3EntityPlayer.posY, (int)par3EntityPlayer.posZ, 0);

 

 

            if (!flag)

            {

                par3EntityPlayer.inventory.consumeInventoryItem(Item.fireballCharge.itemID);

            }

           

            if (!par2World.isRemote)

            {

            par2World.spawnEntityInWorld(entityBall);

             

            }

        }

       

        ItemStack cInv = par3EntityPlayer.inventory.getCurrentItem();

       

        if(cInv.itemID == mod_rareores.ItemFireLauncher.itemID)

        {

        return;

        }

        else

        {

        iconIndex = mod_rareores.ItemFireLauncher.itemID;

        return;

        }

    }

 

 

    public ItemStack onFoodEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)

    {

 

 

        return par1ItemStack;

    }

 

 

    /**

    * How long it takes to use or consume an item

    */

    public int getMaxItemUseDuration(ItemStack par1ItemStack)

    {

        return 72000;

    }

 

    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)

    {

    ArrowNockEvent event = new ArrowNockEvent(par3EntityPlayer, par1ItemStack);

    MinecraftForge.EVENT_BUS.post(event);

    if(event.isCanceled())

    {

    return event.result;

    }

    if(par3EntityPlayer.capabilities.isCreativeMode||par3EntityPlayer.inventory.hasItem(Item.fireballCharge.itemID))

    {

    par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));

    }

    return par1ItemStack;

    }

   

}

 

 

 

 

 

Fireball entity (Yes it is custom, I wanted to change a few things around and control its direction, however it does extend EntityFireball)

 

 

 

package mod_Rareores;

 

import net.minecraft.block.Block;

import net.minecraft.entity.Entity;

import net.minecraft.entity.EntityLiving;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.projectile.EntityFireball;

import net.minecraft.util.DamageSource;

import net.minecraft.util.MathHelper;

import net.minecraft.util.MovingObjectPosition;

import net.minecraft.world.World;

 

public class EntityFireAmmo extends EntityFireball

{

public Entity shootingEntity;

    private int ticksInGround;

    private int ticksInAir = 0;

 

public EntityFireAmmo(World par1World)

    {

        super(par1World);

        this.setSize(0.3125F, 0.3125F);

    }

 

    public EntityFireAmmo(World par1World, EntityLiving par2EntityLiving, double par3, double par5, double par7)

    {

        super(par1World, par2EntityLiving, par3, par5, par7);

        this.setSize(0.3125F, 0.3125F);

    }

 

    public EntityFireAmmo(World par1World, double par2, double par4, double par6, double par8, double par10, double par12)

    {

        super(par1World, par2, par4, par6, par8, par10, par12);

        this.setSize(0.3125F, 0.3125F);

    }

 

    public EntityFireAmmo(World par1World, EntityLiving par2EntityLiving, EntityLiving par3EntityLiving, float par4, float par5)

    {

        super(par1World);

        this.renderDistanceWeight = 10.0D;

        this.shootingEntity = par2EntityLiving;

 

 

        this.posY = par2EntityLiving.posY + (double)par2EntityLiving.getEyeHeight() - 0.10000000149011612D;

        double var6 = par3EntityLiving.posX - par2EntityLiving.posX;

        double var8 = par3EntityLiving.posY + (double)par3EntityLiving.getEyeHeight() - 0.699999988079071D - this.posY;

        double var10 = par3EntityLiving.posZ - par2EntityLiving.posZ;

        double var12 = (double)MathHelper.sqrt_double(var6 * var6 + var10 * var10);

 

        if (var12 >= 1.0E-7D)

        {

            float var14 = (float)(Math.atan2(var10, var6) * 180.0D / Math.PI) - 90.0F;

            float var15 = (float)(-(Math.atan2(var8, var12) * 180.0D / Math.PI));

            double var16 = var6 / var12;

            double var18 = var10 / var12;

            this.setLocationAndAngles(par2EntityLiving.posX + var16, this.posY, par2EntityLiving.posZ + var18, var14, var15);

            this.yOffset = 0.0F;

            float var20 = (float)var12 * 0.2F;

            this.setThrowableHeading(var6, var8 + (double)var20, var10, par4, par5);

        }

    }

   

   

   

    public EntityFireAmmo(World par1World, EntityLiving par2EntityLiving, float par3)

    {

        super(par1World);

        this.renderDistanceWeight = 10.0D;

        this.shootingEntity = par2EntityLiving;

 

       

 

        this.setSize(0.5F, 0.5F);

        this.setLocationAndAngles(par2EntityLiving.posX, par2EntityLiving.posY + (double)par2EntityLiving.getEyeHeight(), par2EntityLiving.posZ, par2EntityLiving.rotationYaw, par2EntityLiving.rotationPitch);

        this.posX -= (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F);

        this.posY -= 0.10000000149011612D;

        this.posZ -= (double)(MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F);

        this.setPosition(this.posX, this.posY, this.posZ);

        this.yOffset = 0.0F;

        this.motionX = (double)(-MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI));

        this.motionZ = (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI));

        this.motionY = (double)(-MathHelper.sin(this.rotationPitch / 180.0F * (float)Math.PI));

        this.setThrowableHeading(this.motionX, this.motionY, this.motionZ, par3 * 1.5F, 1.0F);

    }

 

   

    /**

    * Similar to setArrowHeading, it's point the throwable entity to a x, y, z direction.

    */

    public void setThrowableHeading(double par1, double par3, double par5, float par7, float par8)

    {

        float var9 = MathHelper.sqrt_double(par1 * par1 + par3 * par3 + par5 * par5);

        par1 /= (double)var9;

        par3 /= (double)var9;

        par5 /= (double)var9;

        par1 += this.rand.nextGaussian() * 0.007499999832361937D * (double)par8;

        par3 += this.rand.nextGaussian() * 0.007499999832361937D * (double)par8;

        par5 += this.rand.nextGaussian() * 0.007499999832361937D * (double)par8;

        par1 *= (double)par7;

        par3 *= (double)par7;

        par5 *= (double)par7;

        this.motionX = par1;

        this.motionY = par3;

        this.motionZ = par5;

        float var10 = MathHelper.sqrt_double(par1 * par1 + par5 * par5);

        this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(par1, par5) * 180.0D / Math.PI);

        this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(par3, (double)var10) * 180.0D / Math.PI);

        this.ticksInGround = 0;

    }

   

   

    /**

    * Called when this EntityFireball hits a block or entity.

    */

    protected void onImpact(MovingObjectPosition par1MovingObjectPosition)

    {

        if (!this.worldObj.isRemote)

        {

            if (par1MovingObjectPosition.entityHit != null)

            {

                if (!par1MovingObjectPosition.entityHit.isImmuneToFire() && par1MovingObjectPosition.entityHit.attackEntityFrom(DamageSource.causeFireballDamage(this, this.shootingEntity), 10))

                {

                    par1MovingObjectPosition.entityHit.setFire(5);

                }

            }

            else

            {

                int var2 = par1MovingObjectPosition.blockX;

                int var3 = par1MovingObjectPosition.blockY;

                int var4 = par1MovingObjectPosition.blockZ;

 

                switch (par1MovingObjectPosition.sideHit)

                {

                    case 0:

                        --var3;

                        break;

                    case 1:

                        ++var3;

                        break;

                    case 2:

                        --var4;

                        break;

                    case 3:

                        ++var4;

                        break;

                    case 4:

                        --var2;

                        break;

                    case 5:

                        ++var2;

                }

 

                if (this.worldObj.isAirBlock(var2, var3, var4))

                {

                    this.worldObj.setBlockWithNotify(var2, var3, var4, Block.fire.blockID);

                }

            }

 

            this.setDead();

        }

    }

 

    /**

    * Returns true if other Entities should be prevented from moving through this Entity.

    */

    public boolean canBeCollidedWith()

    {

        return false;

    }

 

    /**

    * Called when the entity is attacked.

    */

    public boolean attackEntityFrom(DamageSource par1DamageSource, int par2)

    {

        return false;

    }

}

 

 

 

 

 

Thanks in advance!

This is the creator of the Rareores mod! Be sure to check it out at

Link to comment
Share on other sites

 public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityplayer)
{ 
itemstack.damageItem(100, entityplayer);
         if (!world.isRemote)
         {                if(par3EntityPlayer.capabilities.isCreativeMode||par3EntityPlayer.inventory.hasItem(Item.fireballCharge.itemID)
{
                 Vec3 look = entityplayer.getLookVec();
                 EntityFireAmmo fireball2 = new EntityFireAmmo(world, entityplayer, 1, 1, 1);
                 fireball2.setPosition(
                                 entityplayer.posX + look.xCoord * 1,
                                 entityplayer.posY + look.yCoord * 1,
                                 entityplayer.posZ + look.zCoord * 1);
                 fireball2.accelerationX = look.xCoord * 0.1;
                 fireball2.accelerationY = look.yCoord * 0.1;
                 fireball2.accelerationZ = look.zCoord * 0.1;
                 world.spawnEntityInWorld(fireball2);
}
         }
         return itemstack;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.