Skip to content

Commit

Permalink
Handle commands from the server properly
Browse files Browse the repository at this point in the history
  • Loading branch information
abnerfs committed Mar 16, 2024
1 parent 43cfcb6 commit 5bf6a91
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 45 deletions.
25 changes: 12 additions & 13 deletions Features/NextMapCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@ public NextMapCommand(ChangeMapManager changeMapManager, StringLocalizer stringL

public void CommandHandler(CCSPlayerController? player)
{
if (player is not null)
string text;
if (_changeMapManager.NextMap is not null)
{
string text;
if (_changeMapManager.NextMap is not null)
{
text = _stringLocalizer.LocalizeWithPrefix("nextmap", _changeMapManager.NextMap);
}
else
text = _stringLocalizer.LocalizeWithPrefix("nextmap.decided-by-vote");

if (_config.ShowToAll)
Server.PrintToChatAll(text);
else
player.PrintToChat(text);
text = _stringLocalizer.LocalizeWithPrefix("nextmap", _changeMapManager.NextMap);
}
else
text = _stringLocalizer.LocalizeWithPrefix("nextmap.decided-by-vote");

if (_config.ShowToAll)
Server.PrintToChatAll(text);
else if (player is not null)
player.PrintToChat(text);
else
Server.PrintToConsole(text);
}

public void OnLoad(Plugin plugin)
Expand Down
5 changes: 4 additions & 1 deletion Features/NominationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ public void OnMapsLoaded(object? sender, Map[] maps)
}
}

public void CommandHandler(CCSPlayerController player, string map)
public void CommandHandler(CCSPlayerController? player, string map)
{
if (player is null)
return;

map = map.ToLower().Trim();
if (_pluginState.DisableCommands || !_config.NominationEnabled)
{
Expand Down
5 changes: 4 additions & 1 deletion Features/RockTheVoteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ public void OnMapStart(string map)
_voteManager!.OnMapStart(map);
}

public void CommandHandler(CCSPlayerController player)
public void CommandHandler(CCSPlayerController? player)
{
if (player is null)
return;

if (_pluginState.DisableCommands || !_config.Enabled)
{
player.PrintToChat(_localizer.LocalizeWithPrefix("general.validation.disabled"));
Expand Down
13 changes: 9 additions & 4 deletions Features/TimeLeftCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class Plugin
[ConsoleCommand("timeleft", "Prints in the chat the timeleft in the current map")]
public void OnTimeLeft(CCSPlayerController? player, CommandInfo? command)
{
_timeLeft.CommandHandler(player!);
_timeLeft.CommandHandler(player);
}
}

Expand All @@ -35,13 +35,16 @@ public TimeLeftCommand(TimeLimitManager timeLimitManager, MaxRoundsManager maxRo

}

public void CommandHandler(CCSPlayerController player)
public void CommandHandler(CCSPlayerController? player)
{
string text;

if (_gameRules.WarmupRunning)
{
player.PrintToChat(_localizer.LocalizeWithPrefix("general.validation.warmup"));
if(player is not null)
player.PrintToChat(_localizer.LocalizeWithPrefix("general.validation.warmup"));
else
Server.PrintToConsole(_localizer.LocalizeWithPrefix("general.validation.warmup"));
return;
}

Expand Down Expand Up @@ -82,8 +85,10 @@ public void CommandHandler(CCSPlayerController player)

if (_config.ShowToAll)
Server.PrintToChatAll(text);
else
else if (player is not null)
player.PrintToChat(text);
else
Server.PrintToConsole(text);
}

public void OnConfigParsed(Config config)
Expand Down
5 changes: 4 additions & 1 deletion Features/VotemapCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ public void OnMapsLoaded(object? sender, Map[] maps)
}
}

public void CommandHandler(CCSPlayerController player, string map)
public void CommandHandler(CCSPlayerController? player, string map)
{
if (player is null)
return;

map = map.ToLower().Trim();
if (_pluginState.DisableCommands || !_config.Enabled)
{
Expand Down
52 changes: 27 additions & 25 deletions Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void ConfigureServices(IServiceCollection serviceCollection)
public partial class Plugin : BasePlugin, IPluginConfig<Config>
{
public override string ModuleName => "RockTheVote";
public override string ModuleVersion => "1.7.5";
public override string ModuleVersion => "1.7.6";
public override string ModuleAuthor => "abnerfs";
public override string ModuleDescription => "General purpose map voting plugin";

Expand Down Expand Up @@ -67,31 +67,33 @@ public override void Load(bool hotReload)
public HookResult OnChat(EventPlayerChat @event, GameEventInfo info)
{
var player = Utilities.GetPlayerFromUserid(@event.Userid);

var text = @event.Text.Trim().ToLower();
if (text == "rtv")
{
_rtvManager.CommandHandler(player);
}
else if (text.StartsWith("nominate"))
{
var split = text.Split("nominate");
var map = split.Length > 1 ? split[1].Trim() : "";
_nominationManager.CommandHandler(player, map);
}
else if (text.StartsWith("votemap"))
{
var split = text.Split("votemap");
var map = split.Length > 1 ? split[1].Trim() : "";
_votemapManager.CommandHandler(player, map);
}
else if (text.StartsWith("timeleft"))
{
_timeLeft.CommandHandler(player);
}
else if (text.StartsWith("nextmap"))
if(player is not null)
{
_nextMap.CommandHandler(player);
var text = @event.Text.Trim().ToLower();
if (text == "rtv")
{
_rtvManager.CommandHandler(player);
}
else if (text.StartsWith("nominate"))
{
var split = text.Split("nominate");
var map = split.Length > 1 ? split[1].Trim() : "";
_nominationManager.CommandHandler(player, map);
}
else if (text.StartsWith("votemap"))
{
var split = text.Split("votemap");
var map = split.Length > 1 ? split[1].Trim() : "";
_votemapManager.CommandHandler(player, map);
}
else if (text.StartsWith("timeleft"))
{
_timeLeft.CommandHandler(player);
}
else if (text.StartsWith("nextmap"))
{
_nextMap.CommandHandler(player);
}
}
return HookResult.Continue;
}
Expand Down

0 comments on commit 5bf6a91

Please sign in to comment.