Gee, if only there were threads with the same exact title requesting help for this exact same issue.
These "legality error codes" aren't cryptic; just google them and you'll find topics discussing them. And if that fails, the source code is public, and you can find where the legality messages are being yielded.
You're playing on an emulator, which doesn't emulate the retail hardware precisely. Minor inaccuracies -> invalid PKM, occasionally.
https://projectpokemon.org/home/forums/topic/47343-problem-invalid-encounter-type-pid-mismtach-3gen/?do=findComment&comment=235731