Skip to content

Commit

Permalink
Prevent player loadout from being used if expecting a custom item
Browse files Browse the repository at this point in the history
Now with 100% less crashes, since we're properly using entrefs this time.
... hopefully.

Fixes #9.
  • Loading branch information
nosoop committed Mar 19, 2021
1 parent 19ffe17 commit 56770b6
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion scripting/cwx.sp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ Action OnPlayerLoadoutUpdated(UserMsg msg_id, BfRead msg, const int[] players,
/**
* Item persistence - we return our item's CEconItemView instance when the game looks up our
* inventory item. This prevents our custom item from being invalidated when touch resupply.
*
* The game expects there to be a valid CEconItemView pointer in certain areas of the code, so
* avoid returning a nullptr.
*/
MRESReturn OnGetLoadoutItemPost(int client, Handle hReturn, Handle hParams) {
// TODO: work around invalid class items being invalidated
Expand All @@ -243,7 +246,26 @@ MRESReturn OnGetLoadoutItemPost(int client, Handle hReturn, Handle hParams) {

int storedItem = g_CurrentLoadoutEntity[client][playerClass][loadoutSlot];
if (!IsValidEntity(storedItem) || !HasEntProp(storedItem, Prop_Send, "m_Item")) {
return MRES_Ignored;
// the loadout entity we keep track of isn't valid, so we may need to make one
// we expect to have to equip something new at this point

if (!g_CurrentLoadout[client][playerClass][loadoutSlot][0]) {
// we don't have a custom item; let the game process it
return MRES_Ignored;
}

/**
* we have a custom item we'd like to spawn in, don't return a loadout item, otherwise
* we may equip / unequip a weapon that has side effects (e.g. Gunslinger)
*
* we'll initialize our custom item later in `OnPlayerLoadoutUpdated`
*/
static int s_DefaultItem = INVALID_ENT_REFERENCE;
if (!IsValidEntity(s_DefaultItem)) {
s_DefaultItem = EntIndexToEntRef(TF2_SpawnWearable());
RemoveEntity(s_DefaultItem);
}
storedItem = s_DefaultItem;
}

Address pStoredItemView = GetEntityAddress(storedItem)
Expand Down

0 comments on commit 56770b6

Please sign in to comment.