Oh, and for those wondering what problems I had with the
engclient_cmd code, test the following script with
amx_forceknife set to 1 and 2. Then set your client-side
cl_lw cvar to 0 and test again.
Method #1 makes the server crash with "New message started when msg 35 has not been sent yet".
Method #2 results in an additional CurWeapon Event call, but no matching CurWeapon Message pair for it (weird, huh?).
Code:
#include <amxmodx>
#include <fakemeta>
new cvar_method, g_msgCurWeapon
enum
{
CVAR_DISABLE = 0, // 0 - disable knife forcing
CVAR_PRE_HOOK, // 1 - force change at pre-hook
CVAR_POST_HOOK // 2 - force change at post-hook
}
public plugin_init()
{
register_plugin("Force Knife", "0.1", "Test")
cvar_method = register_cvar("amx_forceknife", "0")
g_msgCurWeapon = get_user_msgid("CurWeapon")
// CurWeapon Message (pre-hook)
register_message(g_msgCurWeapon, "message_cur_weapon")
// CurWeapon Event (post-hook)
register_event("CurWeapon", "event_cur_weapon", "b")
}
public message_cur_weapon(msg_id, msg_dest, msg_entity)
{
// Player not alive or not an active weapon
if (!is_user_alive(msg_entity) || get_msg_arg_int(1) != 1)
return;
// Get current weapon's id and name
static weapon, wname[32]
weapon = get_msg_arg_int(2)
get_weaponname(weapon, wname, sizeof wname - 1)
// Print some debug info
server_print("[TEST] CurWeapon Message - player %d - weapon %s", msg_entity, wname)
// Check method setting
if (get_pcvar_num(cvar_method) != CVAR_PRE_HOOK)
return;
// Check if holding a knife
if (weapon == CSW_KNIFE)
return;
// If not, force a change
server_print("[TEST] Forcing weapon change...")
engclient_cmd(msg_entity, "weapon_knife")
}
public event_cur_weapon(id)
{
// Player not alive or not an active weapon
if (!is_user_alive(id) || read_data(1) != 1)
return;
// Get current weapon's id and name
static weapon, wname[32]
weapon = read_data(2)
get_weaponname(weapon, wname, sizeof wname - 1)
// Print some debug info
server_print("[TEST] CurWeapon Event - player %d - weapon %s", id, wname)
// Check method setting
if (get_pcvar_num(cvar_method) != CVAR_POST_HOOK)
return;
// Check if holding a knife
if (weapon == CSW_KNIFE)
return;
// If not, force a change
server_print("[TEST] Forcing weapon change...")
engclient_cmd(id, "weapon_knife")
}
__________________