Posted May 27, 201411 yr 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?
May 28, 201411 yr 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 } } http://i.imgur.com/NdrFdld.png[/img]
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.