[SOLVED] On Collided With Block BUG

Started by Kane on Wed, 08/23/2017 - 10:12

Topic category: Bugs and solutions

Last seen on 21:22, 22. Jun 2019
Joined Aug 2014
User points:

Statistics:

  • Comments:
  • Forum topics:
  • Modifications:
  • Wiki pages:
[SOLVED] On Collided With Block BUG
Wed, 08/23/2017 - 15:17 (edited)

I'm trying to make that when a player collides with a certain block and has in hand a certain item, something happens.

But it doesn't work, I get these errors 

Have the method changed?

Please repair it

Edited by Kane on Wed, 08/23/2017 - 15:17
Last seen on 00:21, 11. Aug 2019
Joined Aug 2013
User points:

Statistics:

  • Comments:
  • Forum topics:
  • Modifications:
  • Wiki pages:
  • wiki contributor
  • moderator
Field inventory is only in
Wed, 08/23/2017 - 10:29

Field inventory is only in type EntityPlayer (and its children). You have to check if the entity is an instance of EntityPlayer and if so, you need to cast the entity to EntityPlayer.
Also, you can check EntityLivingBase instead of EntityPlayer and then use getActiveItemStack() for getting the held ItemStack ( or getHeldItem() in older versions)

Last seen on 21:22, 22. Jun 2019
Joined Aug 2014
User points:

Statistics:

  • Comments:
  • Forum topics:
  • Modifications:
  • Wiki pages:
RE:Field inventory is only in
Wed, 08/23/2017 - 11:19

@#1 Yeah I've already tried to check if the entity is istanceof EntityPlayer, but I'm writing "wrong" or in the wrong place

Last seen on 21:22, 22. Jun 2019
Joined Aug 2014
User points:

Statistics:

  • Comments:
  • Forum topics:
  • Modifications:
  • Wiki pages:
RE:Field inventory is only in
Wed, 08/23/2017 - 13:10

@#1 I tried this, but I get same errors
[spoiler]

if (entity instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) entity;

[/spoiler]

Last seen on 00:21, 11. Aug 2019
Joined Aug 2013
User points:

Statistics:

  • Comments:
  • Forum topics:
  • Modifications:
  • Wiki pages:
  • wiki contributor
  • moderator
As always - telling something
Wed, 08/23/2017 - 13:48

As always - telling something without seeing the whole code is actually quite hard. However, because you say that you have got the very same error I personally guess that you are still using the entity variable instead of the player variable when getting the inventory.

Last seen on 21:22, 22. Jun 2019
Joined Aug 2014
User points:

Statistics:

  • Comments:
  • Forum topics:
  • Modifications:
  • Wiki pages:
RE:As always - telling something
Wed, 08/23/2017 - 15:16

@#2 Yeah sorry, but thanks to your comment I solved it just checking for the player as I wrote above and replacing entity in [spoiler]if (entity.inventory.getCurrentItem() != null && entity.inventory.getCurrentItem().getItem() == mcreator_animaLuciferiSwordTool.block)[/spoiler] with player

Thanks a lot