Change placement of hook CanChangeCode in CodeLockSolved

The CodeLock class contains a method RPC_ChangeCode.
Within this method but before the hook CanChangeCode is called, the lock flag is set to true if there is no code present and the code entered is not the guest code. If the hook CanChangeCode returns a non-null value, the lock is locked without a way for the player to unlock it.

I think moving the hook in front of the mentioned if statement will fix this. If I am wrong, please advise. Thanks!

[BaseEntity.RPC_Server]
[BaseEntity.RPC_Server.MaxDistance(3f)]
private void RPC_ChangeCode(BaseEntity.RPCMessage rpc)
{
    if (!rpc.player.CanInteract())
        return;
    string str = rpc.read.String();
    bool flag = rpc.read.Bit();
    if (this.IsLocked() || str.Length != 4 || (!str.IsNumeric() || !this.hasCode & flag))
        return;
    if (!this.hasCode && !flag)
        this.SetFlag(BaseEntity.Flags.Locked, true);
    if (Interface.CallHook("CanChangeCode", (object) rpc.player, (object) this, (object) str, (object) flag) != null)
        return;
    if (!flag)
    {
        this.code = str;
        this.hasCode = this.code.Length > 0;
        this.whitelistPlayers.Clear();
        this.whitelistPlayers.Add(rpc.player.userID);
    }
    else
    {
        this.guestCode = str;
        this.hasGuestCode = this.guestCode.Length > 0;
        this.guestPlayers.Clear();
        this.guestPlayers.Add(rpc.player.userID);
    }
    this.DoEffect(this.effectCodeChanged.resourcePath);
    this.SendNetworkUpdate();
}

I would really like to get some feedback on this as it is still an issue.

Thanks for the report! We'll be sure to take a look when someone gets a moment.

Thank you very much, I really appreciate it!

@Wulf this is still an issue so I will rephrase the problem:

I have a plugin subscribed to CanChangeCode, it returns null for PlayerA and non-null for PlayerB (ie. PlayerA is allowed, PlayerB is not).

PlayerA places a code lock but does not set a code and so the code lock remains unlocked.

PlayerB tries to set a code but the hook CanChangeCode returns a non-null value causing the code lock to LOCK prior to its code being set.

Result: Nobody can unlock the code lock because it has no code.

Any help is greatly appreciated.

Hey, lost track of this, sorry. So would a solution be to move the hook above the flag setting?

To clarify my original post, I am suggesting swapping the positions of these two if statements so the hook is called first.
BEFORE:

if (!this.hasCode && !flag)
      this.SetFlag(BaseEntity.Flags.Locked, true);
if (Interface.CallHook("CanChangeCode", (object) rpc.player, (object) this, (object) str, (object) flag) != null)
      return;

AFTER:

if (Interface.CallHook("CanChangeCode", (object) rpc.player, (object) this, (object) str, (object) flag) != null)
      return;
if (!this.hasCode && !flag)
      this.SetFlag(BaseEntity.Flags.Locked, true);


Merged post

Yes, that's the solution I recommend. :)

Okay, made the change in the latest develop build. :)

Thank you so much @Wulf!

Locked automatically