Animation Stopping when joining or leaving world
Hasenzahn1 opened this issue ยท 1 comments
Bug
I currently have an animated gif that uses the flags +ANIM,+GLOW
. Sometimes when players enter or leave the world. The gif suddenly stops.
Minecraft Paper version 1.19.2
Newest Version of the yamipa plugin
Why
After investigating your code a bit, I noticed that the FakeImage#nextStep
method that should be called by the scheduler at line 376 FakeImage.java
suddenly stops working. After investigating a bit further, I noticed that the for loop that runs through all the watching players generates a ConcurrentModificationException
that stops the scheduler and thus the animation.
This exception is caused because your listeners in ImageRenderer.java
modify the observingPlayers list with the FakeImage#destroy
and FakeImage#spawn
methods during the processing of the ImageRenderer#onPlayerLocationChange
function.
Although the problem only occurred while players were joining and leaving, the code indicates that this problem can also occur when players respawn, teleport, or change to a different WorldAreaId
.
Idea how to prevent
The easiest way around this problem is probably to wrap the entire FakeImage#nextStep
method in a try-and-catch that catches the ConcurrentModificationException
. This will cause the gif to play even if players exit and re-enter the game. It might be that a frame is skipped for some players, but that should be fine.
Yamipa.Bug.mp4
Good catch! Thanks, @Hasenzahn1!
Indeed, to me this looks like a thread synchronization issue when iterating over the observingPlayers
property from FakeImage
.
All modifications of the observingPlayers
hashset occur on the same thread, and the exception is only thrown on the FakeImage#nextStep
due to the fail-fast mechanism built into HashMap
. So I think it's safe to just catch and ignore the exception.
The other option I'm considering is synchronizing all code blocks that read/write to observingPlayers
, but that will probably affect performance and will be noticeable in large servers or with many animated images. I'll think about it and push a fix.