Jump to content

How to detect when a certain item entity collide a block


madmanu

Recommended Posts

Hello , First sorry if i make english mistakes , I'm french

I would like to know if it's possible to execute something when the entity of item obsidian (when you throw it for example) collide the block and "consume" or delete the entity after .

Thank you in advance :)

 

(I have a mod which create a block but i don't think it will help , there is nothing in it  , also i am a noob in modding ^^' it will be super if you could show me the final code , sorry for the disturbance)

Link to comment
Share on other sites

public void onEntityCollidedWithBlock(World world, int i, int j, int k, Entity entity){


//when mod collides
if(entity instanceof EntityItem){
world.createExplosion((Entity)null, i, j, k, 4F, true);
}

 

I use an explosion to test , but it don't seems to work . And if i succed to test if the entity is an item , i really don't know how to test it's an obsidian

Link to comment
Share on other sites

I think you should take a step back and learn more about Java here.

 

entityItem.getEntityItem() returns an ItemStack, which you are comparing to an Item. You have to compare apples to apples, so to speak:

 

if (entityItem.getEntityItem().getItem() == Item.getItemFromBlock(Blocks.obsidian))

 

Also, you are confusing both me and probably yourself by having 'entity' AND 'entityItem' - are they the same? Where are these entities coming from?

Link to comment
Share on other sites

Method called when entity collides:

public void onEntityCollidedWithBlock(World world, int i, int j, int k, Entity entity){

if entity is an item

if(entity instanceof EntityItem){

if it handles obsidian as item

if(((EntityItem) entity).getEntityItem().getItem() == Item.getItemFromBlock(Blocks.obsidian)){

And now the action. Example:

world.setBlock(i, j, k, Blocks.glass);

And don't forget to remove item:

entity.setDead();

 

Complete code:

public void onEntityCollidedWithBlock(World world, int i, int j, int k, Entity entity){
if(entity instanceof EntityItem){
  if(((EntityItem) entity).getEntityItem().getItem() == Item.getItemFromBlock(Blocks.obsidian)){
   world.setBlock(i, j, k, Blocks.glass);
   entity.setDead();
  }
}
}

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.