Topic category: Help with Minecraft modding (Java Edition)
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
You need to check if there actually is an attack target. The game can't find the target so it crashes.
Surround
all of this with
if(!entity.getAttackTarget == null){
}
Why do people even bother making forum posts if they don't even check on them?
lol you waiting for a reply? some ppl don't have that habit
It's like asking someone for help on digging a hole and when you come there they are gone.
Nevertheless, you provided a very useful explanation, it will likely help many others that will just stumble upon this topic ;)
Thanks.
I apologies for not checking this thank you so much for the answer to this problem.