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 <sdkhooks>
#include <multicolors>
#include <clientprefs>
#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;
}
}
}