Skip to content

Commit

Permalink
Add item access restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
nosoop committed Mar 16, 2021
1 parent 268d44e commit ab8ec1c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
18 changes: 14 additions & 4 deletions scripting/cwx.sp
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,24 @@ void OnClientCustomLoadoutItemModified(int client) {
// return LookupAndEquipItem(client, itemuid);
}

/**
* Returns whether or not the player can actually equip this item normally.
* (This does not prevent admins from forcibly applying the item to the player.)
*/
bool CanPlayerEquipItem(int client, const char[] uid) {
// TODO hide based on admin overrides

TFClassType playerClass = TF2_GetPlayerClass(client);

CustomItemDefinition item;
return g_CustomItems.GetArray(uid, item, sizeof(item))
&& item.loadoutPosition[playerClass] != -1;
if (!g_CustomItems.GetArray(uid, item, sizeof(item))) {
// item doesn't exist.
return false;
} else if (item.loadoutPosition[playerClass] == -1) {
return false;
} else if (item.access[0] && !CheckCommandAccess(client, item.access, 0, true)) {
// this item requires access
return false;
}
return true;
}

static bool IsPlayerInRespawnRoom(int client) {
Expand Down
5 changes: 5 additions & 0 deletions scripting/cwx/item_config.sp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ enum struct CustomItemDefinition {
char className[128];
int loadoutPosition[NUM_PLAYER_CLASSES];

char access[64];

KeyValues nativeAttributes;
KeyValues customAttributes;

Expand Down Expand Up @@ -164,6 +166,9 @@ bool CreateItemFromSection(KeyValues config) {

item.bKeepStaticAttributes = !!config.GetNum("keep_static_attrs", true);

// allows restricting access to the item
config.GetString("access", item.access, sizeof(item.access));

if (config.JumpToKey("attributes_game")) {
item.nativeAttributes = new KeyValues("attributes_game");
item.nativeAttributes.Import(config);
Expand Down
2 changes: 1 addition & 1 deletion scripting/cwx/loadout_radio_menu.sp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ static bool ItemVisibleInEquipMenu(int client, const char[] uid) {
return false;
}

// visible for submenu, but player can't equip it
// visible for submenu, but player can't equip it for other reasons
return CanPlayerEquipItem(client, uid);
}

Expand Down

0 comments on commit ab8ec1c

Please sign in to comment.