PHP Code:
#include <sourcemod>
#include <sdkhooks>
#include <sdktools>
new g_PVMid[MAXPLAYERS]; // Predicted ViewModel ID's
new g_iSMGModel; // Custom ViewModel index
public OnPluginStart()
{
HookEvent("player_spawn", Event_PlayerSpawn);
}
public OnConfigsExecuted()
{
g_iSMGModel = PrecacheModel("models/weapons/paintball/v_pbgunt.mdl"); // Custom model
}
public OnClientPostAdminCheck(client){
SDKHook(client, SDKHook_WeaponSwitchPost, OnClientWeaponSwitchPost);
}
public OnClientWeaponSwitchPost(client, wpnid)
{
decl String:szWpn[64];
GetEntityClassname(wpnid,szWpn,sizeof(szWpn));
if(StrEqual(szWpn, "weapon_mac10")){
SetEntProp(wpnid, Prop_Send, "m_nModelIndex", 0);
SetEntProp(g_PVMid[client], Prop_Send, "m_nModelIndex", g_iSMGModel);
}
}
public Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
g_PVMid[client] = Weapon_GetViewModelIndex(client, -1);
}
// Thanks to gubka for these 2 functions below.
// Get model index and prevent server from crash
Weapon_GetViewModelIndex(client, sIndex)
{
while ((sIndex = FindEntityByClassname2(sIndex, "predicted_viewmodel")) != -1)
{
new Owner = GetEntPropEnt(sIndex, Prop_Send, "m_hOwner");
if (Owner != client)
continue;
return sIndex;
}
return -1;
}
// Get entity name
FindEntityByClassname2(sStartEnt, String:szClassname[])
{
while (sStartEnt > -1 && !IsValidEntity(sStartEnt)) sStartEnt--;
return FindEntityByClassname(sStartEnt, szClassname);
}
I've tinkered around with cs:go trying to get custom viewmodels working without flickering or turning off prediction and after hours and hours I think i finally got something that seems to work.
It might work with other mods aswell, but i haven't tested it.
The custom model must have animations in the same order as the original model, oherwise it's going to play wrong animations.
EDIT: I added a custom model to attachments to test the code. It's a paintball gun converted from cs 1.6 to cs:go. Lighting of the model might be a bit glicthy
.