Get Attack Target

Started by dimondking2016 on Thu, 10/29/2020 - 06:01

Topic category: Help with modding (Java Edition)

Last seen on 08:59, 26. Nov 2020
Joined Jul 2019
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Get Attack Target
Thu, 10/29/2020 - 06:01

The issue I've had is that I have created a wizard using the witch AI base that chases players, villagers and golems and attacks them with potions and special abilties like spawning Evoker fangs and vexes on the target entity.

The problem I've been having is after the target is dead the game crashes.

I'm using MCreator 2020.4

 

Here is the code that is crashing (I didn't make it it's the MCreator generated code)

if (world instanceof World && !world.getWorld().isRemote) {
                        Entity entityToSpawn = new EvokerFangsEntity(EntityType.EVOKER_FANGS, world.getWorld());
                        entityToSpawn.setLocationAndAngles(
                                (((-2) + (4 * Math.random()))
                                        + (((entity instanceof MobEntity) ? ((MobEntity) entity).getAttackTarget() : null).getPosX())),        <--------- This line was where it said was crashing
                                y,
                                (((-2) + (4 * Math.random()))
                                        + (((entity instanceof MobEntity) ? ((MobEntity) entity).getAttackTarget() : null).getPosZ())),
                                world.getRandom().nextFloat() * 360F, 0);
                        if (entityToSpawn instanceof MobEntity)
                            ((MobEntity) entityToSpawn).onInitialSpawn(world, world.getDifficultyForLocation(new BlockPos(entityToSpawn)),
                                    SpawnReason.MOB_SUMMONED, (ILivingEntityData) null, (CompoundNBT) null);
                        world.addEntity(entityToSpawn);
                    }

 

I was wondering if there was a way to check if there is a target entity before spawning something on it as I believe that is why it is returning a crash

 

Last seen on 19:32, 4. Dec 2020
Joined Nov 2018
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
You need to check if there…
Thu, 10/29/2020 - 10:33

You need to check if there actually is an attack target. The game can't find the target so it crashes.

 

Surround

entityToSpawn.setLocationAndAngles(
                                (((-2) + (4 * Math.random()))
                                        + (((entity instanceof MobEntity) ? ((MobEntity) entity).getAttackTarget() : null).getPosX())),        <--------- This line was where it said was crashing
                                y,
                                (((-2) + (4 * Math.random()))
                                        + (((entity instanceof MobEntity) ? ((MobEntity) entity).getAttackTarget() : null).getPosZ())),

all of this with

if(!entity.getAttackTarget == null){

}

Last seen on 19:32, 4. Dec 2020
Joined Nov 2018
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Why do people even bother…
Sun, 11/01/2020 - 17:51

Why do people even bother making forum posts if they don't even check on them?

Last seen on 18:21, 28. Nov 2020
Joined Nov 2019
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
lol you waiting for a reply?…
Sun, 11/01/2020 - 17:53

lol you waiting for a reply? some ppl don't have that habit

Last seen on 19:32, 4. Dec 2020
Joined Nov 2018
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
It's like asking someone for…
Sun, 11/01/2020 - 18:11

It's like asking someone for help on digging a hole and when you come there they are gone.

Nevertheless, you provided a…
Mon, 11/02/2020 - 08:36

Nevertheless, you provided a very useful explanation, it will likely help many others that will just stumble upon this topic ;)

Last seen on 19:32, 4. Dec 2020
Joined Nov 2018
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Thanks.
Mon, 11/02/2020 - 12:06

Thanks.