enable NoShake by default and add it to !settings and nicer code

This commit is contained in:
neon 2019-01-14 00:30:58 +01:00
parent 6acd0a59db
commit a605947c17

View File

@ -2,6 +2,7 @@
#include <sourcemod> #include <sourcemod>
#include <sdkhooks> #include <sdkhooks>
#include <multicolors>
#include <clientprefs> #include <clientprefs>
#pragma newdecls required #pragma newdecls required
@ -12,6 +13,9 @@ ConVar g_Cvar_NoShakeGlobal;
bool g_bNoShake[MAXPLAYERS + 1] = {false, ...}; bool g_bNoShake[MAXPLAYERS + 1] = {false, ...};
bool g_bNoShakeGlobal = false; bool g_bNoShakeGlobal = false;
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo = public Plugin myinfo =
{ {
name = "NoShake", name = "NoShake",
@ -21,6 +25,9 @@ public Plugin myinfo =
url = "" url = ""
}; };
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart() public void OnPluginStart()
{ {
RegConsoleCmd("sm_shake", Command_Shake, "[NoShake] Disables or enables screen shakes."); RegConsoleCmd("sm_shake", Command_Shake, "[NoShake] Disables or enables screen shakes.");
@ -33,25 +40,41 @@ public void OnPluginStart()
g_Cvar_NoShakeGlobal.AddChangeHook(OnConVarChanged); g_Cvar_NoShakeGlobal.AddChangeHook(OnConVarChanged);
HookUserMessage(GetUserMessageId("Shake"), MsgHook, true); HookUserMessage(GetUserMessageId("Shake"), MsgHook, true);
SetCookieMenuItem(MenuHandler_CookieMenu, 0, "NoShake");
} }
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientCookiesCached(int client) public void OnClientCookiesCached(int client)
{ {
static char sCookieValue[2]; char sBuffer[4];
GetClientCookie(client, g_hNoShakeCookie, sCookieValue, sizeof(sCookieValue)); GetClientCookie(client, g_hNoShakeCookie, sBuffer, sizeof(sBuffer));
g_bNoShake[client] = StringToInt(sCookieValue) != 0;
if (sBuffer[0])
g_bNoShake[client] = true;
else
g_bNoShake[client] = false;
} }
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnConVarChanged(ConVar convar, const char[] oldValue, const char[] newValue) public void OnConVarChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{ {
if(StringToInt(newValue) > StringToInt(oldValue)) g_bNoShakeGlobal = convar.BoolValue;
PrintToChatAll("\x03[NoShake]\x01 Enabled NoShake globally!");
else if(StringToInt(newValue) < StringToInt(oldValue)) if(g_bNoShakeGlobal)
PrintToChatAll("\x03[NoShake]\x01 Disabled NoShake globally!"); 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) 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]])) 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; return Plugin_Continue;
} }
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_Shake(int client, int args) public Action Command_Shake(int client, int args)
{ {
if(g_bNoShakeGlobal) ToggleShake(client);
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);
return Plugin_Handled; 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;
}
}
}