Jump to content

[1.7.2][Solved]Checking what a thrown entity has collided with


Recommended Posts

Posted

Hey, I'm trying to make a thrown item that does different blocks on however, I can't seem to figure out how to detect the type of block. here is my onImpact method:

 

	protected void onImpact(MovingObjectPosition par1MovingObjectPosition) {
	System.out.println(par1MovingObjectPosition.getClass().isInstance(Blocks.leaves.getClass()));
        if (par1MovingObjectPosition.entityHit != null)
        {
            byte b0 = 12;
            par1MovingObjectPosition.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), (float)b0);
        }
        //System.out.println(par1MovingObjectPosition.typeOfHit.toString());
        if (!this.worldObj.isRemote)
        {
            this.setDead();
            this.dropItem(drop, 1);
        }
}

 

I'm trying to print to the console "true" when it hits leaves, however it always prints false. Any suggestions?

Posted

MovingObjectPosition is not a Block, so of course it's not an instance of Blocks.leaves.getClass().

 

MovingObjectPosition has a MovingObjectType field 'typeOfHit' that tells you if it struck an ENTITY, BLOCK, or was a MISS. If the type is BLOCK, then you can use your moving object position's blockX, blockY, and blockZ fields to get the block that was struck from the world.

 

Thrown items are Entities, after all, so they have a worldObj field you can use:

protected void onImpact(MovingObjectPosition mop) {
Block block = this.worldObj.getBlock(mop.blockX, mop.blockY, mop.blockZ);

// now you have the block, you can check if it is a leaf block with instanceof
if (block instanceof BlockLeaves) {
// it's a leaf block
}

}

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.