From a605947c17d57d793475934a6e17dae3e5f79cab Mon Sep 17 00:00:00 2001 From: neon <> Date: Mon, 14 Jan 2019 00:30:58 +0100 Subject: [PATCH] enable NoShake by default and add it to !settings and nicer code --- NoShake/scripting/NoShake.sp | 146 +++++++++++++++++++++++++++-------- 1 file changed, 114 insertions(+), 32 deletions(-) diff --git a/NoShake/scripting/NoShake.sp b/NoShake/scripting/NoShake.sp index a021c65a..a62de313 100644 --- a/NoShake/scripting/NoShake.sp +++ b/NoShake/scripting/NoShake.sp @@ -2,6 +2,7 @@ #include #include +#include #include #pragma newdecls required @@ -12,6 +13,9 @@ ConVar g_Cvar_NoShakeGlobal; bool g_bNoShake[MAXPLAYERS + 1] = {false, ...}; bool g_bNoShakeGlobal = false; +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- public Plugin myinfo = { name = "NoShake", @@ -21,6 +25,9 @@ public Plugin myinfo = url = "" }; +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- public void OnPluginStart() { RegConsoleCmd("sm_shake", Command_Shake, "[NoShake] Disables or enables screen shakes."); @@ -33,25 +40,41 @@ public void OnPluginStart() g_Cvar_NoShakeGlobal.AddChangeHook(OnConVarChanged); HookUserMessage(GetUserMessageId("Shake"), MsgHook, true); + + SetCookieMenuItem(MenuHandler_CookieMenu, 0, "NoShake"); } +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- public void OnClientCookiesCached(int client) { - static char sCookieValue[2]; - GetClientCookie(client, g_hNoShakeCookie, sCookieValue, sizeof(sCookieValue)); - g_bNoShake[client] = StringToInt(sCookieValue) != 0; + char sBuffer[4]; + GetClientCookie(client, g_hNoShakeCookie, sBuffer, sizeof(sBuffer)); + + if (sBuffer[0]) + g_bNoShake[client] = true; + else + g_bNoShake[client] = false; } +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- public void OnConVarChanged(ConVar convar, const char[] oldValue, const char[] newValue) { - if(StringToInt(newValue) > StringToInt(oldValue)) - PrintToChatAll("\x03[NoShake]\x01 Enabled NoShake globally!"); - else if(StringToInt(newValue) < StringToInt(oldValue)) - PrintToChatAll("\x03[NoShake]\x01 Disabled NoShake globally!"); + g_bNoShakeGlobal = convar.BoolValue; + + if(g_bNoShakeGlobal) + CPrintToChatAll("{cyan}[NoShake] {white}has been enabled globally."); + else + CPrintToChatAll("{cyan}[NoShake] {white}has been disabled globally."); - g_bNoShakeGlobal = StringToInt(newValue) != 0; } +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- public Action MsgHook(UserMsg msg_id, BfRead msg, const int[] players, int playersNum, bool reliable, bool init) { if(playersNum == 1 && (g_bNoShakeGlobal || g_bNoShake[players[0]])) @@ -60,32 +83,91 @@ public Action MsgHook(UserMsg msg_id, BfRead msg, const int[] players, int playe return Plugin_Continue; } +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- public Action Command_Shake(int client, int args) { - if(g_bNoShakeGlobal) - return Plugin_Handled; - - if(!AreClientCookiesCached(client)) - { - ReplyToCommand(client, "\x03[NoShake]\x01 Please wait. Your settings are still loading."); - return Plugin_Handled; - } - - if(g_bNoShake[client]) - { - g_bNoShake[client] = false; - ReplyToCommand(client, "\x03[NoShake]\x01 has been disabled!"); - } - else - { - g_bNoShake[client] = true; - ReplyToCommand(client, "\x03[NoShake]\x01 has been enabled!"); - } - - static char sCookieValue[2]; - IntToString(g_bNoShake[client], sCookieValue, sizeof(sCookieValue)); - SetClientCookie(client, g_hNoShakeCookie, sCookieValue); - + ToggleShake(client); return Plugin_Handled; } +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void ToggleShake(int client) +{ + if(g_bNoShakeGlobal) + { + CPrintToChat(client, "{cyan}[NoShake] {white}is currently enabled globally."); + return; + } + + g_bNoShake[client] = !g_bNoShake[client]; + SetClientCookie(client, g_hNoShakeCookie, g_bNoShake[client] ? "1" : ""); + CPrintToChat(client, "{cyan}[NoShake] {white}%s", g_bNoShake[client] ? "has been enabled." : "has been disabled."); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void ShowSettingsMenu(int client) +{ + Menu menu = new Menu(MenuHandler_MainMenu); + + menu.SetTitle("NoShake Settings", client); + + char sBuffer[128]; + Format(sBuffer, sizeof(sBuffer), "NoShake: %s", (g_bNoShake[client] || g_bNoShakeGlobal) ? "Enabled" : "Disabled"); + + menu.AddItem("0", sBuffer); + + menu.ExitBackButton = true; + + menu.Display(client, MENU_TIME_FOREVER); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void MenuHandler_CookieMenu(int client, CookieMenuAction action, any info, char[] buffer, int maxlen) +{ + switch(action) + { + case(CookieMenuAction_DisplayOption): + { + Format(buffer, maxlen, "NoShake", client); + } + case(CookieMenuAction_SelectOption): + { + ShowSettingsMenu(client); + } + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public int MenuHandler_MainMenu(Menu menu, MenuAction action, int client, int selection) +{ + switch(action) + { + case(MenuAction_Select): + { + switch(selection) + { + case(0): ToggleShake(client); + } + + ShowSettingsMenu(client); + } + case(MenuAction_Cancel): + { + ShowCookieMenu(client); + } + case(MenuAction_End): + { + delete menu; + } + } +} \ No newline at end of file