Jump to content

Custom EntityItem going random direction when thrown


The_Fireplace

Recommended Posts

When I press Q and throw the Item, the custom EntityItem goes a random direction rather than the direction the player is facing. Here is my code:

 

package the_fireplace.unlogic.entities;

import java.util.ArrayList;
import java.util.List;

import the_fireplace.unlogic.unbase;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public class EntityEnderPearlDust extends EntityItem {
int transformTime = 0;
    /** The health of this EntityItem. (For example, damage for tools) */
    private int health;
public EntityEnderPearlDust(World world){
	super(world);
        this.health = 5;
        this.hoverStart = (float)(Math.random() * Math.PI * 2.0D);
        this.setSize(0.25F, 0.25F);
        this.yOffset = this.height / 2.0F;
        this.delayBeforeCanPickup = 50;
}
public EntityEnderPearlDust(World world, double x,
		double y, double z, ItemStack is) {
	super(world, x, y, z, is);        
        this.setEntityItemStack(is);
        this.lifespan = (is.getItem() == null ? 6000 : is.getItem().getEntityLifespan(is, world));
        this.delayBeforeCanPickup = 50;
}
public EntityEnderPearlDust(World world, double x, double y, double z){
	super(world);
        this.health = 5;
        this.hoverStart = (float)(Math.random() * Math.PI * 2.0D);
        this.setSize(0.25F, 0.25F);
        this.yOffset = this.height / 2.0F;
        this.setPosition(x, y, z);
        this.rotationYaw = (float)(Math.random() * 360.0D);
        this.motionX = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D));
        this.motionY = 0.20000000298023224D;
        this.motionZ = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D));
        this.delayBeforeCanPickup = 50;
}
@Override
public void onUpdate()
{
	super.onUpdate();
	int j = MathHelper.floor_double( this.posX );
	int i = MathHelper.floor_double( this.posY );
	int k = MathHelper.floor_double( this.posZ );
	transformTime++;
	if ( transformTime > 60 )
	{
		if ( !transform() )
			transformTime = 0;
	}
}
public static boolean isSameItem(ItemStack ol, ItemStack op)
{
	return ol != null && op != null && ol.isItemEqual( op );
}
public boolean transform()
{
	World world = this.worldObj;
	if(!world.isRemote){
	ItemStack item = getEntityItem();
		AxisAlignedBB region = AxisAlignedBB.getBoundingBox( posX - 1, posY - 1, posZ - 1, posX + 1, posY + 1, posZ + 1 );
		List<Entity> l = worldObj.getEntitiesWithinAABBExcludingEntity( this, region );

		EntityItem redstone = null;
		EntityItem glowstone = null;
		EntityItem gold = null;

		for (Entity e : l)
		{
			if ( e instanceof EntityItem && !e.isDead )
			{
				ItemStack other = ((EntityItem) e).getEntityItem();
				if ( other != null && other.stackSize > 0 )
				{
					if ( isSameItem( other, new ItemStack( Items.redstone ) ) )
						redstone = (EntityItem) e;

					if ( isSameItem( other, new ItemStack( Items.glowstone_dust ) ) )
						glowstone = (EntityItem) e;

					if ( isSameItem( other, new ItemStack( Items.gold_ingot ) ) )
						gold = (EntityItem) e;
				}
			}
		}

		if ( redstone != null && glowstone != null )
		{
			getEntityItem().stackSize--;
			redstone.getEntityItem().stackSize--;
			glowstone.getEntityItem().stackSize--;
			if( gold != null ){
				gold.getEntityItem().stackSize--;
				if ( gold.getEntityItem().stackSize <= 0 ){
					gold.setDead();
				}
				worldObj.spawnEntityInWorld( new EntityItem( worldObj, posX, posY, posZ, new ItemStack(unbase.AntiGold)));
			}

			if ( getEntityItem().stackSize <= 0 )
				setDead();

			if ( redstone.getEntityItem().stackSize <= 0 )
				redstone.setDead();

			if ( glowstone.getEntityItem().stackSize <= 0 )
				glowstone.setDead();

			ItemStack Output = new ItemStack(unbase.UnlogicGem, 2);
			worldObj.spawnEntityInWorld( new EntityItem( worldObj, posX, posY, posZ, Output ) );

			return true;
		}}

	return false;
}
}

 

If you need to see any more code, please, let me know

If I helped please press the Thank You button.

 

Check out my mods at http://www.curse.com/users/The_Fireplace/projects

Link to comment
Share on other sites

Could it be because you are applying a random velocity to the entity?

 

        this.motionX = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D));
        this.motionY = 0.20000000298023224D;
        this.motionZ = (double)((float)(Math.random() * 0.20000000298023224D - 0.10000000149011612D));

Link to comment
Share on other sites

Hi

 

Item dropping / throwing takes place in EntityPlayer.func_146097_a(), with motion being set in this bit

                f = 0.3F;
                entityitem.motionX = (double)(-MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI) * f);
                entityitem.motionZ = (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI) * f);
                entityitem.motionY = (double)(-MathHelper.sin(this.rotationPitch / 180.0F * (float)Math.PI) * f + 0.1F);

 

You could set a breakpoint in there and follow through what is happening, will probably give you a clue.

 

-TGG

 

 

 

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.



×
×
  • Create New...

Important Information

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