Fix player class validations for transitional state #1186
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When we do such code on the current version of open.mp server:
Player who entered class selection most likely won't be able to select a class and spawn: all buttons won't be clickable like something blocks OnPlayerRequestClass and OnPlayerRequestSpawn callbacks on the server even before they reach your pawn script. This method is actually used to spawn player immediately here, in weapon-config, so it's not something marginal.
The root cause is that
TogglePlayerSpectatingcannot manage to properly update the player's state when toggling on and off quickly, because the client doesn't have enough time to send at least one spectating sync packet back to the server (and thus the server won't update his state to spectating a bit further).The validation checks inside class selection events fully rely on player state being always accurate, and thus reject any attempt to change classes or to spawn with selected class, if the player considered to be spawned (by being in one of such player states: onfoot, driver, passenger or spawned).
Finally, when our code above is executed, player who was in onfoot player state will still be with onfoot state for the server when he enters class selection, because toggling spectator mode was done too fast, without player state change to the spectating state which considered non-spawned one, and thus lets the validation checks inside class selection events to be passed.
Solution
We might be forcefully set player state to some non-spawned when
TogglePlayerSpectatingis called withtoggleparameter set to false, but messing with player states is hacky and it previously lead to even more problems.Instead, this PR implements transitional flag when the player is being left from spectating (which is true until spawn). This period between leaving spectator and actual spawn is now considered a valid case to process class selection events, even if validations inside them see that this player has inappropriate player state.