@poopoodice Thanks!
So, assuming that I got your comment correctly, would this be a proper way to apply an effect:
@Override
public void doPostAttack(LivingEntity pAttacker, Entity pTarget, int pLevel) {
// IF: Code is executing on the client.
if (pAttacker.level.isClientSide()) {
return;
}
// IF: Entity is not living entity.
if (!(pTarget instanceof LivingEntity)) {
return;
}
// Cast entity to living entity.
LivingEntity mob = (LivingEntity)pTarget;
// IF: Player already has effect.
if (mob.hasEffect(PlayerMobEffects.STUNNED.get())){
return;
}
// Calculate hit chance.
int hitChance = pLevel * 10;
// Create random generator.
Random random = new Random();
// Get number between 0 and 99.
int roll = random.nextInt(100);
// IF: Apply on hit chance was missed.
if (roll >= hitChance) {
return;
}
// Calculate duration of the effect
int duration = 20 * pLevel;
// Create instance of effect.
MobEffectInstance effect = new MobEffectInstance(PlayerMobEffects.STUNNED.get(), duration, pLevel);
// Apply effect to mob.
mob.addEffect(effect);
}
I have tested the code and it works, although I am not sure if there is smarter way to apply effect.
Still need to figure out how to make mob die after it gets stunned.