Files
sm-plugins/Couch/scripting/CouchAdmin.sp
T

282 lines
7.3 KiB
SourcePawn

#include <sourcemod>
#include <Couch>
#pragma semicolon 1
#pragma newdecls required
#define PREFIX "\x01[SM] \x04"
// So commands fail with a message instead of an unbound-native error while the core is unloaded.
bool g_bCoreLoaded;
public Plugin myinfo =
{
name = "Couch Admin",
author = "marisakirisame.net",
description = "Admin commands and menu for the ZE couch",
version = "2.0",
url = "https://marisakirisame.net"
};
public void OnPluginStart()
{
RegAdminCmd("sm_couch", Command_Couch, ADMFLAG_CHANGEMAP, "Opens the couch menu");
RegAdminCmd("sm_spawncouch", Command_SpawnCouch, ADMFLAG_CHANGEMAP, "Spawns the couch where you are aiming");
RegAdminCmd("sm_bringcouch", Command_BringCouch, ADMFLAG_CHANGEMAP, "Brings the couch to where you are aiming");
RegAdminCmd("sm_gotocouch", Command_GotoCouch, ADMFLAG_CHANGEMAP, "Teleports you to the couch");
RegAdminCmd("sm_removecouch", Command_RemoveCouch, ADMFLAG_CHANGEMAP, "Removes the couch");
RegAdminCmd("sm_couchpistolonly", Command_PistolOnly, ADMFLAG_CHANGEMAP, "sm_couchpistolonly [0|1] - only pistols and knives move the couch; no argument toggles");
}
public void OnAllPluginsLoaded()
{
g_bCoreLoaded = LibraryExists("Couch");
}
public void OnLibraryAdded(const char[] name)
{
if (StrEqual(name, "Couch"))
g_bCoreLoaded = true;
}
public void OnLibraryRemoved(const char[] name)
{
if (StrEqual(name, "Couch"))
g_bCoreLoaded = false;
}
// ---------------------------------------------------------------------------------------------
// Commands
// ---------------------------------------------------------------------------------------------
public Action Command_Couch(int client, int args)
{
if (!RequireInGame(client) || !RequireCore(client))
return Plugin_Handled;
ShowCouchMenu(client);
return Plugin_Handled;
}
public Action Command_SpawnCouch(int client, int args)
{
if (!RequireInGame(client) || !RequireCore(client))
return Plugin_Handled;
DoSpawn(client);
return Plugin_Handled;
}
public Action Command_BringCouch(int client, int args)
{
if (!RequireInGame(client) || !RequireCore(client))
return Plugin_Handled;
DoBring(client);
return Plugin_Handled;
}
public Action Command_GotoCouch(int client, int args)
{
if (!RequireInGame(client) || !RequireCore(client))
return Plugin_Handled;
DoGoto(client);
return Plugin_Handled;
}
// Needs no position, so it also works from the server console.
public Action Command_RemoveCouch(int client, int args)
{
if (!RequireCore(client))
return Plugin_Handled;
DoRemove(client);
return Plugin_Handled;
}
public Action Command_PistolOnly(int client, int args)
{
if (!RequireCore(client))
return Plugin_Handled;
bool bEnable = !Couch_IsPistolOnly();
if (args >= 1)
bEnable = (GetCmdArgInt(1) != 0);
DoSetPistolOnly(client, bEnable);
return Plugin_Handled;
}
bool RequireInGame(int client)
{
if (client >= 1 && IsClientInGame(client))
return true;
ReplyToCommand(client, "[SM] This command can only be used in-game.");
return false;
}
bool RequireCore(int client)
{
if (g_bCoreLoaded)
return true;
ReplyToCommand(client, "[SM] The Couch core plugin is not loaded.");
return false;
}
// ---------------------------------------------------------------------------------------------
// Actions
// ---------------------------------------------------------------------------------------------
void DoSpawn(int client)
{
if (Couch_Spawn(client) == -1)
{
ReplyToCommand(client, "[SM] Could not create the couch.");
return;
}
ShowActivity2(client, PREFIX, "\x01Spawned the \x04couch\x01, it will respawn here each round until removed.");
LogAction(client, -1, "\"%L\" spawned the couch.", client);
}
void DoBring(int client)
{
if (!Couch_Bring(client))
{
ReplyToCommand(client, "[SM] There is no couch to bring. Use sm_spawncouch first.");
return;
}
ShowActivity2(client, PREFIX, "\x01Brought the \x04couch\x01.");
LogAction(client, -1, "\"%L\" brought the couch.", client);
}
void DoGoto(int client)
{
if (!IsPlayerAlive(client))
{
ReplyToCommand(client, "[SM] You must be alive to go to the couch.");
return;
}
if (!Couch_Goto(client))
{
ReplyToCommand(client, "[SM] There is no couch to go to. Use sm_spawncouch first.");
return;
}
ShowActivity2(client, PREFIX, "\x01Teleported to the \x04couch\x01.");
LogAction(client, -1, "\"%L\" teleported to the couch.", client);
}
void DoRemove(int client)
{
if (!Couch_Remove())
{
ReplyToCommand(client, "[SM] There is no couch to remove.");
return;
}
ShowActivity2(client, PREFIX, "\x01Removed the \x04couch\x01.");
LogAction(client, -1, "\"%L\" removed the couch.", client);
}
void DoToggleEffect(int client)
{
bool bEnable = !Couch_IsEffectEnabled();
Couch_SetEffectEnabled(bEnable);
ShowActivity2(client, PREFIX, "\x01Turned the couch effect \x04%s\x01.", bEnable ? "on" : "off");
LogAction(client, -1, "\"%L\" turned the couch effect %s.", client, bEnable ? "on" : "off");
}
// Announced to everyone so players know why their rifle stopped moving the couch.
void DoSetPistolOnly(int client, bool bEnable)
{
Couch_SetPistolOnly(bEnable);
ShowActivity2(client, PREFIX, "\x01Turned couch pistol-only mode \x04%s\x01.", bEnable ? "on" : "off");
LogAction(client, -1, "\"%L\" turned couch pistol-only mode %s.", client, bEnable ? "on" : "off");
}
// ---------------------------------------------------------------------------------------------
// Menu
// ---------------------------------------------------------------------------------------------
// Rebuilt on every display so the labels always reflect the current state.
void ShowCouchMenu(int client)
{
bool bHasCouch = (Couch_GetCouch() != -1);
bool bPlaced = Couch_IsPlaced();
Menu menu = new Menu(MenuHandler_Couch);
menu.SetTitle("Couch");
// With a couch out, bring is the way to move it; without one there is nothing to bring.
menu.AddItem("spawn", "Spawn couch at crosshair", bHasCouch ? ITEMDRAW_DISABLED : ITEMDRAW_DEFAULT);
menu.AddItem("bring", "Bring couch to crosshair", bHasCouch ? ITEMDRAW_DEFAULT : ITEMDRAW_DISABLED);
menu.AddItem("goto", "Teleport to couch", bHasCouch ? ITEMDRAW_DEFAULT : ITEMDRAW_DISABLED);
if (bHasCouch)
menu.AddItem("remove", "Remove couch");
else if (bPlaced)
menu.AddItem("remove", "Remove couch (respawns next round)");
else
menu.AddItem("remove", "Remove couch", ITEMDRAW_DISABLED);
char sItem[64];
FormatEx(sItem, sizeof(sItem), "Effect: %s", Couch_IsEffectEnabled() ? "ON" : "OFF");
menu.AddItem("effect", sItem);
FormatEx(sItem, sizeof(sItem), "Pistol only: %s", Couch_IsPistolOnly() ? "ON" : "OFF");
menu.AddItem("pistol", sItem);
menu.ExitButton = true;
menu.Display(client, MENU_TIME_FOREVER);
}
public int MenuHandler_Couch(Menu menu, MenuAction action, int param1, int param2)
{
switch (action)
{
case MenuAction_Select:
{
int client = param1;
if (!g_bCoreLoaded)
{
RequireCore(client);
return 0;
}
char sInfo[16];
menu.GetItem(param2, sInfo, sizeof(sInfo));
if (StrEqual(sInfo, "spawn"))
DoSpawn(client);
else if (StrEqual(sInfo, "bring"))
DoBring(client);
else if (StrEqual(sInfo, "goto"))
DoGoto(client);
else if (StrEqual(sInfo, "remove"))
DoRemove(client);
else if (StrEqual(sInfo, "effect"))
DoToggleEffect(client);
else if (StrEqual(sInfo, "pistol"))
DoSetPistolOnly(client, !Couch_IsPistolOnly());
ShowCouchMenu(client);
}
case MenuAction_End:
{
delete menu;
}
}
return 0;
}