Jump to content

[SOLVED] Throwable Entity drops twice randomly...


Bedrock_Miner

Recommended Posts

Hi Guys!

 

I've created some Shurikens in minecraft. Everything works well, except the following: I want the shurikens to drop as an Item if they hit the ground. The problem is: Sometimes not one but two Items are dropped. And sometimes an Item is dropped even if I hit an Entity, like a Creeper or something!

 

I found out that the method is called multiple times (I replaced the dropItem with an Output line). But how can this go on? The Entity is killed at the end of the method, so it musnt't be called again. I have no Idea why.  :(

Can you help me?

 

Register Class:

public static void registerEntities() {
	EntityRegistry.registerGlobalEntityID(EntityShuriken.class, "Shuriken", EntityRegistry.findGlobalUniqueEntityId());
	EntityRegistry.registerModEntity(EntityShuriken.class, "Shuriken", 0, MoreTools.instance, 128, 1, true);
	RenderingRegistry.registerEntityRenderingHandler(EntityShuriken.class, new RenderShuriken(MoreToolsItems.shurikenIron));
}

Entity Class:  (copied from entityy Snowball (and edited))

package Bedrockminer.MoreTools.Classes;

import Bedrockminer.MoreTools.Declarations.MoreToolsItems;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.monster.EntityBlaze;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;

public class EntityShuriken extends EntityThrowable
{

    public EntityShuriken(World par1World)
    {
        super(par1World);
    }

    public EntityShuriken(World par1World, EntityLiving par2EntityLiving)
    {
        super(par1World, par2EntityLiving);
    }

    public EntityShuriken(World par1World, double par2, double par4, double par6)
    {
        super(par1World, par2, par4, par6);
    }

    /**
     * Called when this EntityThrowable hits a block or entity.
     */
    protected void onImpact(MovingObjectPosition par1MovingObjectPosition)
    {
    	if (par1MovingObjectPosition.entityHit != null)
    	{
    			byte b0 = 5;
        		par1MovingObjectPosition.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), b0);
        		
    	} else {
    		this.dropItem(MoreToolsItems.shurikenIron.itemID, 1);
    	}
        
    	this.setDead();
    }
}

Item Class:

package Bedrockminer.MoreTools.Classes;

import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class MoreToolsShuriken extends Item {

private boolean shotalready;

public MoreToolsShuriken(int par1) {
	super(par1);
}

public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player){

     EntityShuriken shuriken = new EntityShuriken(world, player);
     world.playSoundAtEntity(player, "random.bow", 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + 0.5F);
     	
     if (!world.isRemote)
     {
    	 world.spawnEntityInWorld(shuriken);
     }
     
     if (!player.capabilities.isCreativeMode){
    	 if (-- stack.stackSize == 0)
    		 return null;
     }
     return stack;
}
}


Render Class: (copied from Snowballs)

package Bedrockminer.MoreTools.Classes;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.entity.Entity;
import net.minecraft.entity.projectile.EntityPotion;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPotion;
import net.minecraft.potion.PotionHelper;
import net.minecraft.util.Icon;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

@SideOnly(Side.CLIENT)
public class RenderShuriken extends Render
{
    private Item field_94151_a;
    private int field_94150_f;

    public RenderShuriken(Item par1, int par2)
    {
        this.field_94151_a = par1;
        this.field_94150_f = par2;
    }

    public RenderShuriken(Item par1)
    {
        this(par1, 0);
    }

    /**
     * Actually renders the given argument. This is a synthetic bridge method, always casting down its argument and then
     * handing it off to a worker function which does the actual work. In all probabilty, the class Render is generic
     * (Render<T extends Entity) and this method has signature public void doRender(T entity, double d, double d1,
     * double d2, float f, float f1). But JAD is pre 1.5 so doesn't do that.
     */
    public void doRender(Entity par1Entity, double par2, double par4, double par6, float par8, float par9)
    {
        Icon icon = this.field_94151_a.getIconFromDamage(this.field_94150_f);

        if (icon != null)
        {
            GL11.glPushMatrix();
            GL11.glTranslatef((float)par2, (float)par4, (float)par6);
            GL11.glEnable(GL12.GL_RESCALE_NORMAL);
            GL11.glScalef(0.5F, 0.5F, 0.5F);
            this.loadTexture("/gui/items.png");
            Tessellator tessellator = Tessellator.instance;

            if (icon == ItemPotion.func_94589_d("potion_splash"))
            {
                int i = PotionHelper.func_77915_a(((EntityPotion)par1Entity).getPotionDamage(), false);
                float f2 = (float)(i >> 16 & 255) / 255.0F;
                float f3 = (float)(i >> 8 & 255) / 255.0F;
                float f4 = (float)(i & 255) / 255.0F;
                GL11.glColor3f(f2, f3, f4);
                GL11.glPushMatrix();
                this.func_77026_a(tessellator, ItemPotion.func_94589_d("potion_contents"));
                GL11.glPopMatrix();
                GL11.glColor3f(1.0F, 1.0F, 1.0F);
            }

            this.func_77026_a(tessellator, icon);
            GL11.glDisable(GL12.GL_RESCALE_NORMAL);
            GL11.glPopMatrix();
        }
    }

    private void func_77026_a(Tessellator par1Tessellator, Icon par2Icon)
    {
        float f = par2Icon.getMinU();
        float f1 = par2Icon.getMaxU();
        float f2 = par2Icon.getMinV();
        float f3 = par2Icon.getMaxV();
        float f4 = 1.0F;
        float f5 = 0.5F;
        float f6 = 0.25F;
        GL11.glRotatef(180.0F - this.renderManager.playerViewY, 0.0F, 1.0F, 0.0F);
        GL11.glRotatef(-this.renderManager.playerViewX, 1.0F, 0.0F, 0.0F);
        par1Tessellator.startDrawingQuads();
        par1Tessellator.setNormal(0.0F, 1.0F, 0.0F);
        par1Tessellator.addVertexWithUV((double)(0.0F - f5), (double)(0.0F - f6), 0.0D, (double)f, (double)f3);
        par1Tessellator.addVertexWithUV((double)(f4 - f5), (double)(0.0F - f6), 0.0D, (double)f1, (double)f3);
        par1Tessellator.addVertexWithUV((double)(f4 - f5), (double)(f4 - f6), 0.0D, (double)f1, (double)f2);
        par1Tessellator.addVertexWithUV((double)(0.0F - f5), (double)(f4 - f6), 0.0D, (double)f, (double)f2);
        par1Tessellator.draw();
    }
}

 

PS: If anybody tried it out with my code and it works, please tell me what exactly you did!

Link to comment
Share on other sites

Look at the arrow entity class.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Not sure if this will work 100%, but try and add:

if (!worldObj.isRemote) {

  //code

}

 

Yeah! That solved it (but I really don't know why ;D)

 

@Draco: I looked in EntityArrow, there its solved quite different (and does'n work either, I'm just too stupid.  ;D)

 

But thanks to both of you!

Link to comment
Share on other sites

If you want to know why that worked it's because, previously, you were spawning the entity on both the client and server side. The "!world.isRemote" makes sure that the code is only executed on the server, which then sends the update to the client. In summary the client was being told to show a new entity twice (once by your code on the client thread and once by the server) so it showed two new entities.

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.