Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and Enhancements: mp_death_drop_gun, Auto-Open Cells, Spawn Management, and !open Command #289

Merged
merged 13 commits into from
Aug 24, 2024
15 changes: 13 additions & 2 deletions lang/Jailbreak.English/Warden/WardenCmdOpenLocale.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Logistics;
using Jailbreak.Formatting.Views.Warden;
Expand All @@ -16,10 +18,19 @@ public IView CannotOpenYet(int seconds) {
}

public IView AlreadyOpened
=> new SimpleView { WardenLocale.PREFIX, "You already opened cells." };
=> new SimpleView { WardenLocale.PREFIX, "Cells are already opened." };

public IView CellsOpenedBy(CCSPlayerController? player) {
return player == null ?
new SimpleView {
WardenLocale.PREFIX,
$"{ChatColors.Blue}The warden {ChatColors.Default}opened the cells."
} :
new SimpleView { WardenLocale.PREFIX, player, "opened the cells." };
}

public IView CellsOpened
=> new SimpleView { WardenLocale.PREFIX, "The warden opened cells." };
=> new SimpleView { WardenLocale.PREFIX, "Cells were auto-opened." };

public IView OpeningFailed
=> new SimpleView { WardenLocale.PREFIX, "Failed to open the cells." };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Cvars.Validators;
using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Extensions;
using Jailbreak.Formatting.Views.Warden;
using Jailbreak.Public.Behaviors;
Expand All @@ -13,19 +14,19 @@

namespace Jailbreak.Warden.Commands;

public class OpenCommandsBehavior(IWardenService warden, IWardenLocale msg,
IWardenCmdOpenLocale wardenCmdOpenMsg, IZoneManager zoneManager)
: IPluginBehavior {
public class WardenOpenCommandsBehavior(IWardenService warden,
IWardenLocale msg, IWardenCmdOpenLocale wardenCmdOpenMsg,
IZoneManager zoneManager) : IPluginBehavior, IWardenOpenCommand {
public static readonly FakeConVar<int> CvOpenCommandCooldown = new(
"css_jb_warden_open_cooldown",
"Minimum seconds warden must wait before being able to open the cells.", 30,
customValidators: new RangeValidator<int>(0, 300));

private bool openedCells;
public bool OpenedCells { get; set; }

[GameEventHandler]
public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info) {
openedCells = false;
OpenedCells = false;
return HookResult.Continue;
}

Expand All @@ -45,15 +46,21 @@ public void Command_Open(CCSPlayerController? executor, CommandInfo info) {
return;
}

if (openedCells) {
if (OpenedCells) {
wardenCmdOpenMsg.AlreadyOpened.ToChat(executor);
return;
}
}

openedCells = true;
var result = MapUtil.OpenCells(zoneManager);
(result ? wardenCmdOpenMsg.CellsOpened : wardenCmdOpenMsg.OpeningFailed)
.ToAllChat();
OpenedCells = true;
var result = MapUtil.OpenCells(zoneManager);
IView message;
if (result) {
if (executor != null && !warden.IsWarden(executor)) {
message = wardenCmdOpenMsg.CellsOpenedBy(executor);
} else { message = wardenCmdOpenMsg.CellsOpenedBy(null); }
} else { message = wardenCmdOpenMsg.OpeningFailed; }

message.ToAllChat();
}
}
34 changes: 30 additions & 4 deletions mod/Jailbreak.Warden/Global/WardenBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
using Jailbreak.Public.Mod.Mute;
using Jailbreak.Public.Mod.Rebel;
using Jailbreak.Public.Mod.Warden;
using Jailbreak.Public.Mod.Zones;
using Jailbreak.Public.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MStatsShared;
using Timer = CounterStrikeSharp.API.Modules.Timers.Timer;

namespace Jailbreak.Warden.Global;

// By making it a struct we ensure values from the CCSPlayerPawn are passed by VALUE.
internal struct PreWardenStats(int armorValue, int health, int maxHealth,
public struct PreWardenStats(int armorValue, int health, int maxHealth,
bool headHealthShot, bool hadHelmetArmor) {
public readonly int ArmorValue = armorValue;
public readonly int Health = health;
Expand All @@ -33,7 +36,8 @@ internal struct PreWardenStats(int armorValue, int health, int maxHealth,
public class WardenBehavior(ILogger<WardenBehavior> logger,
IWardenLocale locale, IRichLogService logs,
ISpecialTreatmentService specialTreatment, IRebelService rebels,
WardenConfig config, IMuteService mute) : IPluginBehavior, IWardenService {
WardenConfig config, IMuteService mute, IServiceProvider provider)
: IPluginBehavior, IWardenService {
private readonly ISet<CCSPlayerController> bluePrisoners =
new HashSet<CCSPlayerController>();

Expand Down Expand Up @@ -61,13 +65,17 @@ public class WardenBehavior(ILogger<WardenBehavior> logger,
"Max HP for the warden", 100, ConVarFlags.FCVAR_NONE,
new RangeValidator<int>(1, 200));

public readonly FakeConVar<int> CvWardenAutoOpenCells =
new("css_jb_warden_opencells_delay",
"Delay in seconds to auto-open cells at, -1 to disable", 60);

private bool firstWarden;
private string? oldTag;
private char? oldTagColor;

private BasePlugin? parent;
private BasePlugin parent = null!;
private PreWardenStats? preWardenStats;
private Timer? unblueTimer;
private Timer? unblueTimer, openCellsTimer;

public void Start(BasePlugin basePlugin) { parent = basePlugin; }

Expand Down Expand Up @@ -386,6 +394,7 @@ private bool playerHasHealthshot(CCSPlayerController player,
public HookResult OnRoundEnd(EventRoundEnd ev, GameEventInfo info) {
TryRemoveWarden();
mute.UnPeaceMute();
openCellsTimer?.Kill();
return HookResult.Continue;
}

Expand All @@ -394,6 +403,23 @@ public HookResult OnRoundStart(EventRoundStart ev, GameEventInfo info) {
firstWarden = true;
preWardenStats = null;

if (CvWardenAutoOpenCells.Value < 0 || RoundUtil.IsWarmup())
return HookResult.Continue;
var openCmd = provider.GetService<IWardenOpenCommand>();
if (openCmd == null) return HookResult.Continue;
var cmdLocale = provider.GetRequiredService<IWardenCmdOpenLocale>();

openCellsTimer?.Kill();
openCellsTimer = parent.AddTimer(CvWardenAutoOpenCells.Value, () => {
if (openCmd.OpenedCells) return;
var zone = provider.GetService<IZoneManager>();
if (zone != null)
MapUtil.OpenCells(zone);
else
MapUtil.OpenCells();
cmdLocale.CellsOpened.ToAllChat();
});

return HookResult.Continue;
}

Expand Down
2 changes: 1 addition & 1 deletion mod/Jailbreak.Warden/WardenServiceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
public static class WardenServiceExtension {
public static void AddJailbreakWarden(
this IServiceCollection serviceCollection) {
serviceCollection.AddConfig<WardenConfig>("warden");

Check warning on line 16 in mod/Jailbreak.Warden/WardenServiceExtension.cs

View workflow job for this annotation

GitHub Actions / build

'ServiceCollectionExtensions.AddConfig<TConfig>(IServiceCollection, string)' is obsolete: 'Conguration is up to each module, and should ideally be done through CVars'

Check warning on line 16 in mod/Jailbreak.Warden/WardenServiceExtension.cs

View workflow job for this annotation

GitHub Actions / build

'ServiceCollectionExtensions.AddConfig<TConfig>(IServiceCollection, string)' is obsolete: 'Conguration is up to each module, and should ideally be done through CVars'

Check warning on line 16 in mod/Jailbreak.Warden/WardenServiceExtension.cs

View workflow job for this annotation

GitHub Actions / build

'ServiceCollectionExtensions.AddConfig<TConfig>(IServiceCollection, string)' is obsolete: 'Conguration is up to each module, and should ideally be done through CVars'

Check warning on line 16 in mod/Jailbreak.Warden/WardenServiceExtension.cs

View workflow job for this annotation

GitHub Actions / build

'ServiceCollectionExtensions.AddConfig<TConfig>(IServiceCollection, string)' is obsolete: 'Conguration is up to each module, and should ideally be done through CVars'
serviceCollection.AddPluginBehavior<IWardenService, WardenBehavior>();
serviceCollection
.AddPluginBehavior<IWardenSelectionService, WardenSelectionBehavior>();
serviceCollection
.AddPluginBehavior<ISpecialTreatmentService, SpecialTreatmentBehavior>();
serviceCollection.AddPluginBehavior<IWardenOpenCommand, WardenOpenCommandsBehavior>();

serviceCollection.AddPluginBehavior<SpecialTreatmentCommandsBehavior>();
serviceCollection.AddPluginBehavior<PeaceCommandsBehavior>();
serviceCollection.AddPluginBehavior<WardenCommandsBehavior>();
serviceCollection.AddPluginBehavior<RollCommandBehavior>();
serviceCollection.AddPluginBehavior<OpenCommandsBehavior>();

serviceCollection.AddPluginBehavior<WardenMarkerBehavior>();
serviceCollection.AddPluginBehavior<WardenPaintBehavior>();
Expand Down
12 changes: 9 additions & 3 deletions mod/Jailbreak.Zones/RandomZoneGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
using CounterStrikeSharp.API.Modules.Timers;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Extensions;
using Jailbreak.Public.Mod.SpecialDay;
using Jailbreak.Public.Mod.Zones;
using Jailbreak.Public.Utils;
using Microsoft.Extensions.DependencyInjection;
using Timer = CounterStrikeSharp.API.Modules.Timers.Timer;

namespace Jailbreak.Zones;

public class RandomZoneGenerator(IZoneManager zoneManager, IZoneFactory factory)
: IPluginBehavior {
public class RandomZoneGenerator(IZoneManager zoneManager, IZoneFactory factory,
IServiceProvider provider) : IPluginBehavior {
private IZone? cells;
private string? currentMap;
private IList<IZone>? manualSpawnPoints;
Expand Down Expand Up @@ -44,13 +46,17 @@ private void reload() {
private void tick() {
if (currentMap != Server.MapName) reload();
currentMap = Server.MapName;

var sdManager = provider.GetService<ISpecialDayManager>();
if (sdManager is { IsSDRunning: true }) return;

foreach (var player in PlayerUtil.GetAlive()) tick(player);
}

private void tick(CCSPlayerController player) {
var pawn = player.PlayerPawn.Value;
if (pawn == null) return;
if (!pawn.OnGroundLastTick) return;
if (!pawn.OnGroundLastTick || !pawn.TakesDamage) return;
var pos = pawn.AbsOrigin;
if (pos == null) return;
pos = pos.Clone();
Expand Down
13 changes: 13 additions & 0 deletions public/Jailbreak.Formatting/Views/Warden/IWardenCmdOpenLocale.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
using CounterStrikeSharp.API.Core;
using Jailbreak.Formatting.Base;

namespace Jailbreak.Formatting.Views.Warden;

public interface IWardenCmdOpenLocale {
/// <summary>
/// The cells were auto-opened.
/// </summary>
public IView CellsOpened { get; }

/// <summary>
/// The cells were opened by the specified player.
/// If the player is null, the cells were opened by the warden.
/// </summary>
/// <param name="player"></param>
/// <returns></returns>
public IView CellsOpenedBy(CCSPlayerController? player);

public IView OpeningFailed { get; }
public IView AlreadyOpened { get; }
public IView CannotOpenYet(int seconds);
Expand Down
9 changes: 6 additions & 3 deletions public/Jailbreak.Public/Mod/SpecialDay/AbstractSpecialDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Jailbreak.Public.Mod.LastRequest;
using Jailbreak.Public.Mod.Rebel;
using Jailbreak.Public.Mod.SpecialDay.Enums;
using Jailbreak.Public.Mod.Warden;
using Jailbreak.Public.Mod.Zones;
using Jailbreak.Public.Utils;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -94,6 +95,8 @@ public virtual void Setup() {
MapUtil.OpenCells();
else
MapUtil.OpenCells(zones);
var openCmd = Provider.GetService<IWardenOpenCommand>();
if (openCmd != null) openCmd.OpenedCells = true;
}

doTeleports();
Expand Down Expand Up @@ -252,6 +255,7 @@ protected object GetConvarValue(ConVar? cvar) {
}

protected void SetConvarValue(ConVar? cvar, object value) {
var previous = GetConvarValue(cvar);
if (cvar == null) return;
try {
switch (cvar.Type) {
Expand Down Expand Up @@ -288,13 +292,12 @@ protected void SetConvarValue(ConVar? cvar, object value) {
or "mp_death_drop_gun") {
// These convars require a frame to take effect, otherwise client-side
// stuff is not properly updated
var opposite = !(bool)value;
Server.ExecuteCommand($"{cvar.Name} {opposite}");
Server.ExecuteCommand($"{cvar.Name} {previous}");
Server.NextFrame(() => Server.ExecuteCommand($"{cvar.Name} {value}"));
} else { Server.ExecuteCommand(cvar.Name + " " + value); }
} catch (Exception e) {
Server.PrintToChatAll(
$"There was an error setting {cvar.Name} ({cvar.Type}) to {value}");
$"There was an error setting {cvar.Name} ({cvar.Type}) to {value} ({value.GetType()}");
Server.PrintToConsole(e.Message);
Server.PrintToConsole(e.StackTrace ?? "");
}
Expand Down
5 changes: 5 additions & 0 deletions public/Jailbreak.Public/Mod/Warden/IWardenOpenCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Jailbreak.Public.Mod.Warden;

public interface IWardenOpenCommand {
bool OpenedCells { get; set; }
}
Loading