Unexplainable bug

Hi, there's a really weird bug with my datapack's custom UI system. I have a cancel button on the second page which resets the ui to the first page, and this works the first time it is pressed. However, after that, if I go back to the second page and click cancel, the ui does reset, but then it thinks every single item has been removed from the first page, and all the buttons are clicked (including the open second page button, so its stuck in a loop). However, if I leave the barrel for any amount of time and then click into it again, it is fixed, and it allows me to click the cancel button. This is shown in the video My pack never checks whether the barrel has been opened (I don't even think this is possible?) so this is really confusing me.
Continue to help post