Skip to content

Commit

Permalink
Add error message when preference sets before player loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
ArKaNeMaN committed Jan 16, 2024
1 parent 0efcbcb commit 48f28ac
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions scripting/player_prefs.sma
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ enum _: IntertKey {
value[256]
};

new g_iPlayerDatabaseId[MAX_PLAYERS + 1];
new g_iPlayerDatabaseId[MAX_PLAYERS + 1] = {0, ...};
new Trie: g_tPlayerPreferences[MAX_PLAYERS + 1];

new Trie: g_tKeys;
Expand Down Expand Up @@ -81,10 +81,7 @@ public bool: native_is_loaded(iPlugin, iArgs) {

new iPlayer = get_param(arg_player_id);

if (!is_user_connected(iPlayer))
return false;

return bool: g_iPlayerDatabaseId[iPlayer];
return IsUserLoaded(iPlayer);
}

public bool: native_get_preference(iPlugin, iArgs) {
Expand Down Expand Up @@ -126,6 +123,11 @@ public bool: native_set_preference(iPlugin, iArgs) {
if (!is_user_connected(iPlayer))
return false;

if (!IsUserLoaded(iPlayer)) {
log_error(0, "Attempt to set preference for not loaded player (%d).", iPlayer);
return false;
}

new szKey[32], szValue[256], szDefaultValue[256];

get_string(arg_key, szKey, charsmax(szKey));
Expand Down Expand Up @@ -167,6 +169,13 @@ public bool: native_set_key_default_value(iPlugin, iArgs) {
return bool: TrieSetString(g_tKeys, szKey, szDefaultValue);
}

bool:IsUserLoaded(const iPlayer) {
if (!is_user_connected(iPlayer))
return false;

return g_iPlayerDatabaseId[iPlayer] > 0;
}

stock LoadPreferences(iPlayer) {
if (g_hSqlTuple == Empty_Handle) {
ExecuteForward(g_iForwards[Forward_PlayerLoaded], _, iPlayer);
Expand Down

0 comments on commit 48f28ac

Please sign in to comment.